Skip to content

Commit baeafe3

Browse files
committed
ensure playback-error events are emitted on correct stream
1 parent b8bab2c commit baeafe3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

speech-to-text/recognize-blob.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var FilePlayer = require('./file-player.js');
2121
var FormatStream = require('./format-stream.js');
2222
var TimingStream = require('./timing-stream.js');
2323
var assign = require('object.assign/polyfill')();
24+
var WritableElementStream = require('./writable-element-stream');
2425

2526
/**
2627
* @module watson-speech/speech-to-text/recognize-blob
@@ -36,6 +37,7 @@ var assign = require('object.assign/polyfill')();
3637
* @param {Boolean} [options.play=false] - If a file is set, play it locally as it's being uploaded
3738
* @param {Boolena} [options.format=true] - pipe the text through a {FormatStream} which performs light formatting
3839
* @param {Boolena} [options.realtime=options.play] - pipe the text through a {TimingStream} which slows the output down to real-time to match the audio playback.
40+
* @param {String|DOMElement} [options.outputElement] pipe the text to a WriteableElementStream targeting the specified element. Also defaults objectMode to true to enable interim results.
3941
*
4042
* @returns {RecognizeStream}
4143
*/
@@ -44,6 +46,11 @@ module.exports = function recognizeBlob(options) {
4446
throw new Error("WatsonSpeechToText: missing required parameter: opts.token");
4547
}
4648

49+
// the WritableElementStream works best in objectMode
50+
if (options.outputElement && options.objectMode !== false) {
51+
options.objectMode = true;
52+
}
53+
4754
var realtime = options.realtime || typeof options.realtime === 'undefined' && options.play;
4855

4956
// we don't want the readable stream to have objectMode on the input even if we're setting it for the output
@@ -68,10 +75,14 @@ module.exports = function recognizeBlob(options) {
6875
FilePlayer.playFile(options.data).then(function (player) {
6976
recognizeStream.on('stop', player.stop.bind(player));
7077
}).catch(function (err) {
71-
recognizeStream.emit('playback-error', err);
78+
stream.emit('playback-error', err);
7279
});
7380
}
7481

82+
if (options.outputElement) {
83+
stream.pipe(new WritableElementStream(options))
84+
}
85+
7586
return stream;
7687
};
7788

0 commit comments

Comments
 (0)