Skip to content

Commit fb29367

Browse files
committed
bug fix, defaults improvement
* recognize stream now uses service defaults * moved non-default settings to recognizeFile/Microphone() helper methods * fixed bug with recognizeStream autodetecting file content-type
1 parent bc15df2 commit fb29367

File tree

5 files changed

+25
-47
lines changed

5 files changed

+25
-47
lines changed

speech-to-text/recognize-file.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,10 @@ module.exports = function recognizeFile(options) { // eslint-disable-line comple
7878
options.timestamps = true;
7979
}
8080

81-
// we don't want the readable stream to have objectMode on the input even if we're setting it for the output
82-
// unless were in realtime mode - in which case the timing stream requires objectMode input.
83-
var rsOpts = assign({}, options);
84-
rsOpts.readableObjectMode = options.objectMode || realtime;
85-
delete rsOpts.objectMode;
86-
87-
81+
var rsOpts = assign({
82+
continuous: true,
83+
interim_results: true,
84+
}, options);
8885

8986
var stream = new BlobStream(options.data);
9087
var recognizeStream = new RecognizeStream(rsOpts);

speech-to-text/recognize-microphone.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ module.exports = function recognizeMicrophone(options) {
7979
options.smart_formatting = options.format;
8080
}
8181

82-
// we don't want the readable stream to have objectMode on the input even if we're setting it for the output
83-
var rsOpts = assign({}, options);
84-
rsOpts.readableObjectMode = options.objectMode;
85-
rsOpts['content-type'] = 'audio/l16;rate=16000';
86-
delete rsOpts.objectMode;
82+
var rsOpts = assign({
83+
continuous: true,
84+
'content-type': 'audio/l16;rate=16000',
85+
interim_results: true,
86+
}, options);
8787

8888
var recognizeStream = new RecognizeStream(rsOpts);
8989
var streams = [recognizeStream]; // collect all of the streams so that we can bundle up errors and send them to the last one

speech-to-text/recognize-stream.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ var util = require('util');
2222
var pick = require('object.pick');
2323
var W3CWebSocket = require('websocket').w3cwebsocket;
2424
var contentType = require('./content-type');
25-
var defaults = require('defaults');
2625
var qs = require('../util/querystring.js');
2726

2827
var OPENING_MESSAGE_PARAMS_ALLOWED = [
@@ -140,14 +139,21 @@ var QUERY_PARAMS_ALLOWED = [
140139
* @param {Number} [options.word_alternatives_threshold] - Number between 0 and 1 representing the minimum confidence before including an alternative word in the results. Must be set to enable word alternatives,
141140
* @param {Boolean} [options.profanity_filter=false] - set to true to filter out profanity and replace the words with *'s
142141
* @param {Number} [options.inactivity_timeout=30] - how many seconds of silence before automatically closing the stream (even if continuous is true). use -1 for infinity
143-
* @param {Boolean} [options.readableObjectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Changes several other defaults.
142+
* @param {Boolean} [options.readableObjectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Does not affect input (which must be binary)
143+
* @param {Boolean} [options.objectMode=false] - alias for options.readableObjectMode
144144
* @param {Number} [options.X-Watson-Learning-Opt-Out=false] - set to true to opt-out of allowing Watson to use this request to improve it's services
145145
* @param {Boolean} [options.smart_formatting=false] - formats numeric values such as dates, times, currency, etc.
146146
* @param {String} [options.customization_id] - not yet supported on the public STT service
147147
*
148148
* @constructor
149149
*/
150150
function RecognizeStream(options) {
151+
// this stream only supports objectMode on the output side.
152+
// It must receive binary data input.
153+
if (options.objectMode) {
154+
options.readableObjectMode = true;
155+
delete options.objectMode;
156+
}
151157
Duplex.call(this, options);
152158
this.options = options;
153159
this.listening = false;
@@ -161,7 +167,7 @@ function RecognizeStream(options) {
161167
* @param {String} event
162168
*/
163169
function flowForResults(event) {
164-
if (event === 'results' || event === 'result') {
170+
if (event === 'results' || event === 'result' || event === 'speaker_labels') {
165171
self.removeListener('newListener', flowForResults);
166172
process.nextTick(function() {
167173
self.resume(); // put this stream in flowing mode
@@ -199,34 +205,9 @@ RecognizeStream.prototype.initialize = function() {
199205
var queryString = qs.stringify(queryParams);
200206
var url = (options.url || 'wss://stream.watsonplatform.net/speech-to-text/api').replace(/^http/, 'ws') + '/v1/recognize?' + queryString;
201207

202-
// turn off all the extras if we're just outputting text
203-
var textModeDefaults = {
204-
action: 'start',
205-
'content-type': 'audio/wav',
206-
continuous: true,
207-
inactivity_timeout: 30,
208-
interim_results: true,
209-
word_confidence: false,
210-
timestamps: false,
211-
max_alternatives: 1
212-
};
213-
214-
// but turn everything on if we're in objectMode and the end user can consume it
215-
var objectModeDefaults = {
216-
action: 'start',
217-
'content-type': 'audio/wav',
218-
continuous: true,
219-
inactivity_timeout: 30,
220-
interim_results: true,
221-
word_confidence: false,
222-
timestamps: false,
223-
max_alternatives: 1
224-
};
225208

226-
var openingMessage = defaults(
227-
pick(options, OPENING_MESSAGE_PARAMS_ALLOWED),
228-
(options.objectMode || options.readableObjectMode) ? objectModeDefaults : textModeDefaults
229-
);
209+
var openingMessage = pick(options, OPENING_MESSAGE_PARAMS_ALLOWED);
210+
openingMessage.action = 'start';
230211

231212
var self = this;
232213

@@ -291,7 +272,7 @@ RecognizeStream.prototype.initialize = function() {
291272
* @event RecognizeStream#message
292273
* @param {Object} message - frame object with a data attribute that's either a string or a Buffer/TypedArray
293274
*/
294-
this.emit('message', frame);
275+
self.emit('message', frame);
295276

296277
if (typeof frame.data !== 'string') {
297278
return emitError('Unexpected binary data received from server', frame);
@@ -317,7 +298,7 @@ RecognizeStream.prototype.initialize = function() {
317298
self.emit('listening');
318299
}
319300
} else {
320-
if (options.objectMode || options.readableObjectMode) {
301+
if (options.readableObjectMode) {
321302
/**
322303
* Object with interim or final results, possibly including confidence scores, alternatives, and word timing.
323304
* @event RecognizeStream#data

test/speaker-stream-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ describe('SpeakerStream', function() {
216216
});
217217
stream.on('end', function() {
218218
assert(called);
219-
done()
219+
done();
220220
});
221221
var messageStream = require('./resources/car_loan_stream.json');
222222
messageStream.forEach(function(msg) {
@@ -229,7 +229,7 @@ describe('SpeakerStream', function() {
229229
var stream = new SpeakerStream();
230230
stream.on('error', done);
231231
var lastMsg;
232-
stream.on('data', function (msg) {
232+
stream.on('data', function(msg) {
233233
assert(msg);
234234
assert.notDeepEqual(msg, lastMsg);
235235
lastMsg = msg;

test/timing-stream-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('TimingStream', function() {
183183
nextTick(function() { // write is always async (?)
184184
assert(actual.length);
185185
actual.reduce(function(lastIndex, msg) {
186-
assert.equal(msg.result_index, lastIndex, "wrong index on result, expecting " + lastIndex + " got " + JSON.stringify(msg, null, 2));
186+
assert.equal(msg.result_index, lastIndex, 'wrong index on result, expecting ' + lastIndex + ' got ' + JSON.stringify(msg, null, 2));
187187
// index should always increment after a final message
188188
return (msg.results[0].final) ? lastIndex + 1 : lastIndex;
189189
}, 0);

0 commit comments

Comments
 (0)