@@ -148,9 +148,13 @@ RecognizeStream.prototype.initialize = function() {
148
148
return emitError ( 'Invalid JSON received from service:' , frame , jsonEx ) ;
149
149
}
150
150
151
+ var recognized = false ;
151
152
if ( data . error ) {
152
153
emitError ( data . error , frame ) ;
153
- } else if ( data . state === 'listening' ) {
154
+ recognized = true ;
155
+ }
156
+
157
+ if ( data . state === 'listening' ) {
154
158
// 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
155
159
if ( ! self . listening ) {
156
160
self . listening = true ;
@@ -159,7 +163,10 @@ RecognizeStream.prototype.initialize = function() {
159
163
self . listening = false ;
160
164
socket . close ( ) ;
161
165
}
162
- } else if ( data . results ) {
166
+ recognized = true ;
167
+ }
168
+
169
+ if ( data . results ) {
163
170
/**
164
171
* Object with interim or final results, including possible alternatives. May have no results at all for empty audio files.
165
172
* @event RecognizeStream#results
@@ -175,14 +182,22 @@ RecognizeStream.prototype.initialize = function() {
175
182
*/
176
183
self . push ( data . results [ 0 ] . alternatives [ 0 ] . transcript , 'utf8' ) ; // this is the "data" event that can be easily piped to other streams
177
184
}
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 ) {
179
191
/**
180
192
* Speaker labels
181
193
* @event RecognizeStream#speaker_labels
182
194
* @param {Object } speaker_labels
183
195
*/
184
196
self . emit ( 'speaker_labels' , data ) ;
185
- } else {
197
+ recognized = true ;
198
+ }
199
+
200
+ if ( ! recognized ) {
186
201
emitError ( 'Unrecognised message from server' , frame ) ;
187
202
}
188
203
} ;
0 commit comments