Skip to content

Commit 4ae2224

Browse files
committed
removing unused recognizeWs method, improving STT jsdoc
1 parent 37744fb commit 4ae2224

File tree

1 file changed

+38
-50
lines changed
  • services/speech_to_text

1 file changed

+38
-50
lines changed

services/speech_to_text/v1.js

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ function formatChunk(chunk) {
5353
return result;
5454
}
5555

56-
///**
57-
// * Speech Recognition API Wrapper
58-
// * @lends speech_to_text
59-
// */
56+
/**
57+
* Speech Recognition API Wrapper
58+
* @constructor
59+
* @param options
60+
*/
6061
function SpeechToText(options) {
6162
// Default URL
6263
var serviceDefaults = {
@@ -66,48 +67,6 @@ function SpeechToText(options) {
6667
// Replace default options with user provided
6768
this._options = extend(serviceDefaults, options);
6869
}
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-
};
11170

11271
/**
11372
* Speech recognition for given audio using default model.
@@ -159,6 +118,7 @@ SpeechToText.prototype.recognize = function(params, callback) {
159118
*
160119
* @param {String} [content_type] The Content-type e.g. audio/l16; rate=48000
161120
* @param {String} [session_id] The session id
121+
* @deprecated use createRecognizeStream instead
162122
*/
163123
SpeechToText.prototype.recognizeLive = function(params, callback) {
164124
var missingParams = helper.getMissingParams(params,
@@ -216,8 +176,8 @@ SpeechToText.prototype.recognizeLive = function(params, callback) {
216176
* otherwise it waits for the next recognition.
217177
*
218178
* @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
221181
*/
222182
SpeechToText.prototype.observeResult = function(params, callback) {
223183
var missingParams = helper.getMissingParams(params, ['session_id', 'cookie_session']);
@@ -269,6 +229,7 @@ SpeechToText.prototype.observeResult = function(params, callback) {
269229
* The returned state has to be 'initialized' to be able to do recognize POST.
270230
*
271231
* @param {String} [params.session_id] Session used in the recognition.
232+
* @deprecated use createRecognizeStream instead
272233
*/
273234
SpeechToText.prototype.getRecognizeStatus = function(params, callback) {
274235
var missingParams = helper.getMissingParams(params, ['session_id']);
@@ -385,7 +346,16 @@ SpeechToText.prototype.deleteSession = function(params, callback) {
385346
return requestFactory(parameters, callback);
386347
};
387348

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+
*/
389359
function RecognizeStream(options){
390360
Duplex.call(this, options);
391361

@@ -419,6 +389,9 @@ function RecognizeStream(options){
419389
}
420390
});
421391

392+
/**
393+
* @event RecognizeStream#error
394+
*/
422395
function emitError(msg, frame, err) {
423396
if (err) {
424397
err.message = msg + ' ' + err.message;
@@ -444,6 +417,11 @@ function RecognizeStream(options){
444417
connection.on('close', function(reasonCode, description) {
445418
self.listening = false;
446419
self.push(null);
420+
/**
421+
* @event RecognizeStream#connection-close
422+
* @param {Number} reasonCode
423+
* @param {String} description
424+
*/
447425
self.emit('connection-close', reasonCode, description);
448426
});
449427

@@ -470,9 +448,19 @@ function RecognizeStream(options){
470448
connection.close();
471449
}
472450
} 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+
*/
473456
self.emit('results', data);
474457
// note: currently there is always either no entries or exactly 1 entry in the results array. However, this may change in the future.
475458
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+
*/
476464
self.push(data.results[0].alternatives[0].transcript, 'utf8'); // this is the "data" event that can be easily piped to other streams
477465
}
478466
} else {
@@ -510,7 +498,7 @@ RecognizeStream.prototype._write = function(chunk, encoding, callback) {
510498
/**
511499
* Replaces recognizeLive & friends with a single 2-way stream over websockets
512500
* @param params
513-
* @returns {*}
501+
* @returns {RecognizeStream}
514502
*/
515503
SpeechToText.prototype.createRecognizeStream = function(params) {
516504
params = params || {};

0 commit comments

Comments
 (0)