Skip to content

Commit a80007e

Browse files
dpopp07germanattanasio
authored andcommitted
update stt example
1 parent a62e245 commit a80007e

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

examples/speech_to_text.v1.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
32
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
43
var fs = require('fs');
54

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

11+
/*
12+
This code will print the entire response to the console when it
13+
receives the 'data' event. Some applications will want to write
14+
out only the transcribed text, to the console or to a file.
15+
To this, remove `objectMode: true` from the `params` object.
16+
Then, uncomment the block of code at Line 30.
17+
*/
18+
1219
var params = {
13-
content_type: 'audio/wav'
20+
content_type: 'audio/wav',
21+
objectMode: false
1422
};
1523

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

22-
// and pipe out the transcription
30+
/*
31+
// pipe out the transcription to a file
2332
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
2433
25-
// listen for 'data' events for just the final text
26-
// listen for 'results' events to get the raw JSON with interim results, timings, etc.
34+
// get strings instead of Buffers from `data` events
35+
// only do this when objectMode is `false`
36+
recognizeStream.setEncoding('utf8');
37+
*/
2738

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

30-
['data', 'results', 'speaker_labels', 'error', 'close'].forEach(function(
31-
eventName
32-
) {
33-
recognizeStream.on(
34-
eventName,
35-
console.log.bind(console, eventName + ' event: ')
36-
);
37-
});
43+
// Displays events on the console.
44+
function onEvent(name, event) {
45+
console.log(name, JSON.stringify(event, null, 2));
46+
};

0 commit comments

Comments
 (0)