1
1
'use strict' ;
2
-
3
2
var SpeechToTextV1 = require ( 'watson-developer-cloud/speech-to-text/v1' ) ;
4
3
var fs = require ( 'fs' ) ;
5
4
@@ -9,8 +8,17 @@ var speechToText = new SpeechToTextV1({
9
8
url : 'https://stream.watsonplatform.net/speech-to-text/api/'
10
9
} ) ;
11
10
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
+
12
19
var params = {
13
- content_type : 'audio/wav'
20
+ content_type : 'audio/wav' ,
21
+ objectMode : false
14
22
} ;
15
23
16
24
// create the stream
@@ -19,19 +27,20 @@ var recognizeStream = speechToText.createRecognizeStream(params);
19
27
// pipe in some audio
20
28
fs . createReadStream ( __dirname + '/resources/speech.wav' ) . pipe ( recognizeStream ) ;
21
29
22
- // and pipe out the transcription
30
+ /*
31
+ // pipe out the transcription to a file
23
32
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
24
33
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
+ */
27
38
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 ) ; } ) ;
29
42
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