Skip to content

Commit

Permalink
update stt example
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 authored and germanattanasio committed Jun 27, 2018
1 parent a62e245 commit a80007e
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions examples/speech_to_text.v1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');

Expand All @@ -9,8 +8,17 @@ var speechToText = new SpeechToTextV1({
url: 'https://stream.watsonplatform.net/speech-to-text/api/'
});

/*
This code will print the entire response to the console when it
receives the 'data' event. Some applications will want to write
out only the transcribed text, to the console or to a file.
To this, remove `objectMode: true` from the `params` object.
Then, uncomment the block of code at Line 30.
*/

var params = {
content_type: 'audio/wav'
content_type: 'audio/wav',
objectMode: false
};

// create the stream
Expand All @@ -19,19 +27,20 @@ var recognizeStream = speechToText.createRecognizeStream(params);
// pipe in some audio
fs.createReadStream(__dirname + '/resources/speech.wav').pipe(recognizeStream);

// and pipe out the transcription
/*
// pipe out the transcription to a file
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
// listen for 'data' events for just the final text
// listen for 'results' events to get the raw JSON with interim results, timings, etc.
// get strings instead of Buffers from `data` events
// only do this when objectMode is `false`
recognizeStream.setEncoding('utf8');
*/

recognizeStream.setEncoding('utf8'); // to get strings instead of Buffers from `data` events
recognizeStream.on('data', function(event) { onEvent('Data:', event); });
recognizeStream.on('error', function(event) { onEvent('Error:', event); });
recognizeStream.on('close', function(event) { onEvent('Close:', event); });

['data', 'results', 'speaker_labels', 'error', 'close'].forEach(function(
eventName
) {
recognizeStream.on(
eventName,
console.log.bind(console, eventName + ' event: ')
);
});
// Displays events on the console.
function onEvent(name, event) {
console.log(name, JSON.stringify(event, null, 2));
};

0 comments on commit a80007e

Please sign in to comment.