2016年7月11日 星期一

testtw

var watson = require('watson-developer-cloud'); var fs = require('fs'); var cp = require('child_process'); var Sound = require('node-aplay'); console.log('start tw watson regonition'); opencmd(); function opencmd() { var speech_to_text = watson.speech_to_text({ username: 'c8090707-bcda-49e0-98f9-172c40420c1a', password: 'cZGWayUpvsSJ', version: 'v1' }); var params = { content_type: 'audio/wav', model: 'zh-CN_BroadbandModel', continuous: true }; // create the stream var recognizeStream = speech_to_text.createRecognizeStream(params); // pipe in some audio //fs.createReadStream('/home/pi/voice/interlude/help.wav').pipe(recognizeStream); // start the recording var mic = cp.spawn('arecord', ['--device=plughw:0,0', '--format=S16_LE', '--rate=44100', '--channels=1']); //, '--duration=10' mic.stderr.pipe(process.stderr); // save a local copy of your audio (in addition to streaming it) by uncommenting this //mic.stdout.pipe(require('fs').createWriteStream('test.wav')); // optionally compress, and then pipe the audio to the STT service mic.stdout.pipe(recognizeStream); // alternate option for testing: comment out all of the mic/flac stuff and pipe from a file //require('fs').createReadStream('test.wav').pipe(transcriptInput); //new Sound('/home/pi/voice/interlude/pleasesay.wav').play(); // end the recording setTimeout(function() { mic.kill(); }, 45* 5000); // and pipe out the transcription //recognizeStream.pipe(fs.createWriteStream('transcription.txt')); //new Sound('/home/pi/voice/interlude/pleasesay.wav').play(); // listen for 'data' events for just the final text // listen for 'results' events to get the raw JSON with interim results, timings, etc. recognizeStream.setEncoding('utf8'); // to get strings instead of Buffers from `data` events /* // listen for 'data' events for just the final text recognizeStream.on('results', function(data) { if(data.results[0] && data.results[0].final && data.results[0].alternatives) { console.log('xxxxx'+data.results[0].alternatives[0].transcript); } //console.log.bind(console, 'data event: '); //var transcription = converter.toBuffer(); //console.log(transcription); }); */ // listen for 'data' events for just the final text recognizeStream.on('error', function() { console.log.bind(console, 'error event: '); //var transcription = converter.toBuffer(); // console.log(transcription); }); // listen for 'data' events for just the final text recognizeStream.on('data', function(data) { console.log('xxxxx'+data); console.log.bind(console, 'data event: '); // var transcription = converter.toBuffer(); // console.log(transcription); }); //['data', 'error', 'connection-close'].forEach(function(eventName) { // recognizeStream.on(eventName, console.log.bind(console, eventName + ' event: ')); //}); }

沒有留言:

張貼留言