@@ -53,10 +53,11 @@ function formatChunk(chunk) {
53
53
return result ;
54
54
}
55
55
56
- ///**
57
- // * Speech Recognition API Wrapper
58
- // * @lends speech_to_text
59
- // */
56
+ /**
57
+ * Speech Recognition API Wrapper
58
+ * @constructor
59
+ * @param options
60
+ */
60
61
function SpeechToText ( options ) {
61
62
// Default URL
62
63
var serviceDefaults = {
@@ -66,48 +67,6 @@ function SpeechToText(options) {
66
67
// Replace default options with user provided
67
68
this . _options = extend ( serviceDefaults , options ) ;
68
69
}
69
- /**
70
- * Replaces recognizeLive & friends with a single 2-way stream over websockets
71
- * @param params
72
- * @param callback
73
- * @returns {* }
74
- */
75
- SpeechToText . prototype . recognizeWs = function ( params , callback ) {
76
-
77
- var missingParams = helper . getMissingParams ( params , [ 'audio' , 'content_type' ] ) ;
78
- if ( missingParams ) {
79
- callback ( new Error ( 'Missing required parameters: ' + missingParams . join ( ', ' ) ) ) ;
80
- return ;
81
- }
82
- if ( ! isStream ( params . audio ) ) {
83
- callback ( new Error ( 'audio is not a standard Node.js Stream' ) ) ;
84
- return ;
85
- }
86
-
87
- var queryParams = pick ( params , [ 'continuous' , 'max_alternatives' , 'timestamps' ,
88
- 'word_confidence' , 'inactivity_timeout' , 'model' ] ) ;
89
-
90
- var _url = '/v1' ;
91
- _url += ( params . session_id ) ? ( '/sessions/' + params . session_id ) : '' ;
92
- _url += '/recognize' ;
93
-
94
- var parameters = {
95
- options : {
96
- method : 'POST' ,
97
- url : _url ,
98
- headers : {
99
- 'Content-Type' : params . content_type
100
- } ,
101
- json : true ,
102
- qs : queryParams ,
103
- } ,
104
- defaultOptions : this . _options
105
- } ;
106
- return params . audio . on ( 'response' , function ( response ) {
107
- // Replace content-type
108
- response . headers [ 'content-type' ] = params . content_type ;
109
- } ) . pipe ( requestFactory ( parameters , callback ) ) ;
110
- } ;
111
70
112
71
/**
113
72
* Speech recognition for given audio using default model.
@@ -159,6 +118,7 @@ SpeechToText.prototype.recognize = function(params, callback) {
159
118
*
160
119
* @param {String } [content_type] The Content-type e.g. audio/l16; rate=48000
161
120
* @param {String } [session_id] The session id
121
+ * @deprecated use createRecognizeStream instead
162
122
*/
163
123
SpeechToText . prototype . recognizeLive = function ( params , callback ) {
164
124
var missingParams = helper . getMissingParams ( params ,
@@ -216,8 +176,8 @@ SpeechToText.prototype.recognizeLive = function(params, callback) {
216
176
* otherwise it waits for the next recognition.
217
177
*
218
178
* @param {String } [params.session_id] Session used in the recognition.
219
- * @param {boolean } [params.interim_results] If true,
220
- * interim results will be returned. Default: false.
179
+ * @param {boolean } [params.interim_results] If true, interim results will be returned. Default: false.
180
+ * @deprecated use createRecognizeStream instead
221
181
*/
222
182
SpeechToText . prototype . observeResult = function ( params , callback ) {
223
183
var missingParams = helper . getMissingParams ( params , [ 'session_id' , 'cookie_session' ] ) ;
@@ -269,6 +229,7 @@ SpeechToText.prototype.observeResult = function(params, callback) {
269
229
* The returned state has to be 'initialized' to be able to do recognize POST.
270
230
*
271
231
* @param {String } [params.session_id] Session used in the recognition.
232
+ * @deprecated use createRecognizeStream instead
272
233
*/
273
234
SpeechToText . prototype . getRecognizeStatus = function ( params , callback ) {
274
235
var missingParams = helper . getMissingParams ( params , [ 'session_id' ] ) ;
@@ -385,7 +346,16 @@ SpeechToText.prototype.deleteSession = function(params, callback) {
385
346
return requestFactory ( parameters , callback ) ;
386
347
} ;
387
348
388
-
349
+ /**
350
+ * pipe()-able Node.js Readable/Writeable stream - accepts binary audio and emits text in it's `data` events.
351
+ * Also emits `results` events with interim results and other data.
352
+ *
353
+ * Cannot be instantiated directly, instead reated by calling #createRecognizeStream()
354
+ *
355
+ * Uses WebSockets under the hood. For audio with no recognizable speech, no `data` events are emitted.
356
+ * @param options
357
+ * @constructor
358
+ */
389
359
function RecognizeStream ( options ) {
390
360
Duplex . call ( this , options ) ;
391
361
@@ -419,6 +389,9 @@ function RecognizeStream(options){
419
389
}
420
390
} ) ;
421
391
392
+ /**
393
+ * @event RecognizeStream#error
394
+ */
422
395
function emitError ( msg , frame , err ) {
423
396
if ( err ) {
424
397
err . message = msg + ' ' + err . message ;
@@ -444,6 +417,11 @@ function RecognizeStream(options){
444
417
connection . on ( 'close' , function ( reasonCode , description ) {
445
418
self . listening = false ;
446
419
self . push ( null ) ;
420
+ /**
421
+ * @event RecognizeStream#connection-close
422
+ * @param {Number } reasonCode
423
+ * @param {String } description
424
+ */
447
425
self . emit ( 'connection-close' , reasonCode , description ) ;
448
426
} ) ;
449
427
@@ -470,9 +448,19 @@ function RecognizeStream(options){
470
448
connection . close ( ) ;
471
449
}
472
450
} else if ( data . results ) {
451
+ /**
452
+ * Object with interim or final results, including possible alternatives. May have no results at all for empty audio files.
453
+ * @event RecognizeStream#results
454
+ * @param {Object } results
455
+ */
473
456
self . emit ( 'results' , data ) ;
474
457
// note: currently there is always either no entries or exactly 1 entry in the results array. However, this may change in the future.
475
458
if ( data . results [ 0 ] && data . results [ 0 ] . final && data . results [ 0 ] . alternatives ) {
459
+ /**
460
+ * Finalized text
461
+ * @event RecognizeStream#data
462
+ * @param {String } transcript
463
+ */
476
464
self . push ( data . results [ 0 ] . alternatives [ 0 ] . transcript , 'utf8' ) ; // this is the "data" event that can be easily piped to other streams
477
465
}
478
466
} else {
@@ -510,7 +498,7 @@ RecognizeStream.prototype._write = function(chunk, encoding, callback) {
510
498
/**
511
499
* Replaces recognizeLive & friends with a single 2-way stream over websockets
512
500
* @param params
513
- * @returns {* }
501
+ * @returns {RecognizeStream }
514
502
*/
515
503
SpeechToText . prototype . createRecognizeStream = function ( params ) {
516
504
params = params || { } ;
0 commit comments