@@ -26,6 +26,11 @@ export const UNKNOWN_TAG = '_unknown_';
26
26
27
27
let streaming = false ;
28
28
29
+ export function getMajorAndMinorVersion ( version : string ) {
30
+ const versionItems = version . split ( '.' ) ;
31
+ return versionItems . slice ( 0 , 2 ) . join ( '.' ) ;
32
+ }
33
+
29
34
/**
30
35
* Speech-Command Recognizer using browser-native (WebAudio) spectral featutres.
31
36
*/
@@ -35,8 +40,8 @@ export class BrowserFftSpeechCommandRecognizer implements
35
40
static readonly DEFAULT_VOCABULARY_NAME = '18w' ;
36
41
37
42
readonly MODEL_URL_PREFIX =
38
- `https://storage.googleapis.com/tfjs-speech-commands-models /v${
39
- version } /browser_fft`;
43
+ `https://storage.googleapis.com/tfjs-models/tfjs/ speech-commands/v${
44
+ getMajorAndMinorVersion ( version ) } /browser_fft`;
40
45
41
46
private readonly SAMPLE_RATE_HZ = 44100 ;
42
47
private readonly FFT_SIZE = 1024 ;
@@ -107,7 +112,7 @@ export class BrowserFftSpeechCommandRecognizer implements
107
112
/**
108
113
* Start streaming recognition.
109
114
*
110
- * To stop the recognition, use `stopStreaming ()`.
115
+ * To stop the recognition, use `stopListening ()`.
111
116
*
112
117
* Example: TODO(cais): Add exapmle code snippet.
113
118
*
@@ -132,9 +137,8 @@ export class BrowserFftSpeechCommandRecognizer implements
132
137
* @throws Error, if streaming recognition is already started or
133
138
* if `config` contains invalid values.
134
139
*/
135
- async startStreaming (
136
- callback : RecognizerCallback ,
137
- config ?: StreamingRecognitionConfig ) : Promise < void > {
140
+ async listen ( callback : RecognizerCallback ,
141
+ config ?: StreamingRecognitionConfig ) : Promise < void > {
138
142
if ( streaming ) {
139
143
throw new Error (
140
144
'Cannot start streaming again when streaming is ongoing.' ) ;
@@ -355,7 +359,7 @@ export class BrowserFftSpeechCommandRecognizer implements
355
359
*
356
360
* @throws Error if there is not ongoing streaming recognition.
357
361
*/
358
- async stopStreaming ( ) : Promise < void > {
362
+ async stopListening ( ) : Promise < void > {
359
363
if ( ! streaming ) {
360
364
throw new Error ( 'Cannot stop streaming when streaming is not ongoing.' ) ;
361
365
}
@@ -366,7 +370,7 @@ export class BrowserFftSpeechCommandRecognizer implements
366
370
/**
367
371
* Check if streaming recognition is ongoing.
368
372
*/
369
- isStreaming ( ) : boolean {
373
+ isListening ( ) : boolean {
370
374
return streaming ;
371
375
}
372
376
@@ -397,7 +401,7 @@ export class BrowserFftSpeechCommandRecognizer implements
397
401
if ( this . model == null ) {
398
402
throw new Error (
399
403
'Model has not been loaded yet. Load model by calling ' +
400
- 'ensureModelLoaded(), recognizer (), or startStreaming ().' ) ;
404
+ 'ensureModelLoaded(), recognize (), or listen ().' ) ;
401
405
}
402
406
return this . model . inputs [ 0 ] . shape ;
403
407
}
@@ -479,6 +483,15 @@ export class BrowserFftSpeechCommandRecognizer implements
479
483
output . scores = await Promise . all ( scorePromises ) as Float32Array [ ] ;
480
484
tf . dispose ( unstacked ) ;
481
485
}
486
+
487
+ if ( config . includeSpectrogram ) {
488
+ output . spectrogram = {
489
+ data : ( input instanceof tf . Tensor ?
490
+ await input . data ( ) : input ) as Float32Array ,
491
+ frameSize : this . nonBatchInputShape [ 1 ] ,
492
+ } ;
493
+ }
494
+
482
495
return output ;
483
496
}
484
497
@@ -507,7 +520,7 @@ export class BrowserFftSpeechCommandRecognizer implements
507
520
if ( this . model == null ) {
508
521
throw new Error (
509
522
'Model has not been loaded yet. Load model by calling ' +
510
- 'ensureModelLoaded(), recognizer(), or startStreaming ().' ) ;
523
+ 'ensureModelLoaded(), recognizer(), or listen ().' ) ;
511
524
}
512
525
tf . util . assert (
513
526
name != null && typeof name === 'string' && name . length > 1 ,
0 commit comments