Skip to content

Commit 37744fb

Browse files
committed
STT: do not choke on empty files
Fixes watson-developer-cloud#198
1 parent 5e22fa8 commit 37744fb

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

services/speech_to_text/v1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ function RecognizeStream(options){
471471
}
472472
} else if (data.results) {
473473
self.emit('results', data);
474-
// note: currently there is always exactly 1 entry in the results array. However, this may change in the future.
475-
if(data.results[0].final && data.results[0].alternatives) {
474+
// note: currently there is always either no entries or exactly 1 entry in the results array. However, this may change in the future.
475+
if(data.results[0] && data.results[0].final && data.results[0].alternatives) {
476476
self.push(data.results[0].alternatives[0].transcript, 'utf8'); // this is the "data" event that can be easily piped to other streams
477477
}
478478
} else {

test/resources/blank.wav

111 KB
Binary file not shown.

test/test.integration-all-services.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,21 @@ describe('integration-all-services', function() {
315315
assert.equal(transcription.trim(), 'thunderstorms could produce large hail isolated tornadoes and heavy rain');
316316
done();
317317
}));
318-
});
319318
});
320319

320+
it('createRecognizeStream() - no words', function (done) {
321+
var recognizeStream = speech_to_text.createRecognizeStream({content_type: 'audio/l16; rate=44100'});
322+
recognizeStream.setEncoding('utf8');
323+
fs.createReadStream(__dirname + '/resources/blank.wav')
324+
.pipe(recognizeStream)
325+
.on('error', done)
326+
.on('data', function(text) {
327+
assert(!text, 'no text expected for an audio file with no words')
328+
})
329+
.on('end', done);
330+
});
331+
});
332+
321333
describe('functional_dialog', function() {
322334
this.timeout(THIRTY_SECONDS);
323335
var dialog = watson.dialog(auth.dialog);

0 commit comments

Comments
 (0)