Skip to content

Commit 0438a3e

Browse files
committed
ensure STT RecognizeStream always emits speaker_labels events when a message includes speaker_labels
1 parent eb86921 commit 0438a3e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

speech-to-text/recognize_stream.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ RecognizeStream.prototype.initialize = function() {
148148
return emitError('Invalid JSON received from service:', frame, jsonEx);
149149
}
150150

151+
var recognized = false;
151152
if (data.error) {
152153
emitError(data.error, frame);
153-
} else if (data.state === 'listening') {
154+
recognized = true;
155+
}
156+
157+
if (data.state === 'listening') {
154158
// this is emitted both when the server is ready for audio, and after we send the close message to indicate that it's done processing
155159
if (!self.listening) {
156160
self.listening = true;
@@ -159,7 +163,10 @@ RecognizeStream.prototype.initialize = function() {
159163
self.listening = false;
160164
socket.close();
161165
}
162-
} else if (data.results) {
166+
recognized = true;
167+
}
168+
169+
if (data.results) {
163170
/**
164171
* Object with interim or final results, including possible alternatives. May have no results at all for empty audio files.
165172
* @event RecognizeStream#results
@@ -175,14 +182,22 @@ RecognizeStream.prototype.initialize = function() {
175182
*/
176183
self.push(data.results[0].alternatives[0].transcript, 'utf8'); // this is the "data" event that can be easily piped to other streams
177184
}
178-
} else if (data.speaker_labels) {
185+
recognized = true;
186+
}
187+
188+
// note: some messages will have both results and speaker_labels
189+
// this will cause them to be emitted twice - once for each event
190+
if (data.speaker_labels) {
179191
/**
180192
* Speaker labels
181193
* @event RecognizeStream#speaker_labels
182194
* @param {Object} speaker_labels
183195
*/
184196
self.emit('speaker_labels', data);
185-
} else {
197+
recognized = true;
198+
}
199+
200+
if (!recognized) {
186201
emitError('Unrecognised message from server', frame);
187202
}
188203
};

0 commit comments

Comments
 (0)