-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
434 lines (378 loc) · 11.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const Concat = require('concat-stream');
const ChildProcess = require('child_process');
const Fs = require('fs');
const Mic = require('mic');
const Path = require('path');
const PocketSphinx = require('pocketsphinx').ps;
const Which = require('which');
const stateEnum = {
STOPPED: 'stopped',
LOADING: 'loading',
PRELISTEN: 'pre-listening',
LISTENING: 'listening',
STREAMING: 'streaming',
PAUSED: 'paused'
};
/**
* Wakeword module.
* @module wakeword
*/
module.exports = {
/**
* The file name to output PocketSphinx logs to.
* null will output to stdout.
*/
logFile: null,
/**
* The file name to use for passing wake words to PocketSphinx.
*/
keywordFile: 'wakewords.txt',
/**
* The microphone device name, when applicable.
*/
deviceName: 'default',
/**
* The time between voice processing calls, in milliseconds.
*/
sampleTime: 100,
/**
* The PocketSphinx decoder object being used. Setting this to null when
* in the stopped state will cause it to be recreated.
*/
decoder: null,
/**
* The current state. Can be <code>stopped</code>, <code>loading</code>,
* <code>listening</code> or <code>paused</code>.
* @readonly
*/
state: stateEnum.STOPPED,
/**
* The default keyword search threshold to specify to Pocketsphinx when
* none is specified with the keyword.
*/
defaultKwsThreshold: '1e-20',
/**
* Optional cd-metrics object to log information to.
*/
metrics: null,
// Private properties.
sphinxConfig: null,
lastWords: [],
detected: null,
pendingState: null,
/**
* Retrieves the PocketSphinx configuration. This configuration will be used
* whenever the next PocketSphinx decoder is created.
*
* @returns {Promise.Object} A Promise that resolves a PocketSphinx
* configuration object.
*/
getPsConfig: function() {
return new Promise((resolve, reject) => {
if (this.sphinxConfig) {
resolve(this.sphinxConfig);
return;
}
Which('pocketsphinx_continuous', (e, path) => {
if (e) {
reject(e);
return;
}
path = Path.join(Path.dirname(path), '..', 'share',
'pocketsphinx', 'model', 'en-us');
if (!Fs.statSync(path).isDirectory()) {
reject(`Pocketsphinx en-us model not found at '${path}'`);
return;
}
var config = this.sphinxConfig = PocketSphinx.Decoder.defaultConfig();
config.setString("-hmm", Path.join(path, 'en-us'));
config.setString("-dict", Path.join(path, 'cmudict-en-us.dict'));
config.setString("-lm", Path.join(path, 'en-us.lm.bin'));
if (this.logFile) {
config.setString('-logfn', this.logFile);
}
resolve(this.sphinxConfig);
});
});
},
/**
* Retrieves the PocketSphinx decoder. If one hasn't been created yet, one
* will be created.
*
* @returns {Promise.Object} A Promise that resolves a PocketSphinx decoder
* object.
*/
getDecoder: function() {
return new Promise((resolve, reject) => {
this.getPsConfig().then(() => {
if (!this.decoder) {
this.decoder = new PocketSphinx.Decoder(this.sphinxConfig);
}
resolve(this.decoder);
});
});
},
/**
* Retrieves a microphone object. If one hasn't been created yet, one will
* be created.
*
* @returns {Object} A microphone object.
*/
getMic: function() {
if (!this.mic) {
this.mic = Mic(
{ rate: '16000',
channels: '1',
encoding: 'signed-integer',
device: this.deviceName });
this.mic.getAudioStream().on('error', e => {
console.error('Error streaming from microphone', e);
});
}
return this.mic;
},
/**
* This callback handles microphone data after a successful wake word
* recognition.
*
* @callback onwakeCallback
* @param {Object} data Data from the microphone.
* @param {string} word The word that was recognised.
*/
/**
* This callback is notified when microphone recording has started.
*
* @callback onreadyCallback
*/
/**
* Listens for the specified wake words. Once a word has been recognised,
* microphone data will be continuously streamed to the {@link onwake}
* callback until it is cancelled.
*
* @example
* var wakeTime;
* var rawStream = null;
* Wakeword.listen(['record'], 0.87, (data, word) => {
* if (!rawStream) {
* wakeTime = Date.now();
* rawStream = Fs.createWriteStream('recording.raw', {
* defaultEncoding: 'binary'
* });
* }
*
* rawStream.write(data);
*
* if (Date.now() - wakeTime > 5000) {
* Wakeword.stop();
* rawStream.end();
* }
* });
*
* @param {Array.<string>} words An array of wake words
* @param {number} scoreThreshold The recognition score threshold (e.g. 0.87)
* @param {onwakeCallback} onwake The callback to handle microphone data
* @param {onreadyCallback} [onready] The callback to be notified when
* listening has started
*/
listen: function(words, scoreThreshold, onwake, onready) {
switch (this.state) {
case stateEnum.LOADING:
this.pendingState = {
state: stateEnum.LISTENING,
words: words,
scoreThreshold: scoreThreshold,
onwake: onwake };
return;
case stateEnum.STREAMING:
case stateEnum.LISTENING:
case stateEnum.PAUSED:
this.stop();
break;
}
this.state = stateEnum.LOADING;
this.getDecoder().then(() => {
var startListening = () => {
if (this.pendingState) {
var pendingState = this.pendingState;
this.pendingState = null;
switch (pendingState.state) {
case stateEnum.LISTENING:
this.state = stateEnum.STOPPED;
this.listen(pendingState.words, pendingState.scoreThreshold,
pendingState.onwake);
return;
case stateEnum.STOPPED:
this.state = stateEnum.STOPPED;
return;
default:
console.error('Stopping, invalid pending state',
pendingState.state);
this.state = stateEnum.STOPPED;
return;
}
}
this.decoder.setKws('wakeword', this.keywordFile);
this.decoder.setSearch('wakeword');
this.decoder.startUtt();
this.state = stateEnum.PRELISTEN;
this.record(data => {
if (!this.detected) {
this.decoder.processRaw(data, false, false);
var now = Date.now();
var hyp = this.decoder.hyp();
if (hyp && hyp.hypstr) {
this.decoder.endUtt();
var score = this._getScore();
if (score >= scoreThreshold) {
this.detected = hyp.hypstr;
this.state = stateEnum.STREAMING;
if (this.metrics) {
this.metrics.recordFloatingPointEventAsync(
'wakeword', 'spot', 'success', score);
}
} else {
this.decoder.startUtt();
if (this.metrics) {
this.metrics.recordFloatingPointEventAsync(
'wakeword', 'spot', 'fail', score);
}
}
}
return;
}
onwake(data, this.detected);
});
if (onready) {
onready();
}
};
if (JSON.stringify(this.lastWords) === JSON.stringify(words)) {
startListening();
} else {
// pocketsphinx doesn't allow for multiple keywords or configurable
// threshold via any public API except for the keyword file.
Fs.open(this.keywordFile, 'w', (e, file) => {
if (e) {
console.error('Error opening keyword file', e);
this.state = stateEnum.STOPPED;
return;
}
for (var word of words) {
if (word.match(/[^\/]*\/[^\/]*\/$/)) {
Fs.write(file, `${word}\n`);
} else {
Fs.write(file, `${word}/${this.defaultKwsThreshold}/\n`);
}
}
Fs.close(file, e => {
if (e) {
console.error('Error closing keyword file', e);
this.state = stateEnum.STOPPED;
return;
}
startListening();
});
});
}
});
},
_getScore: function() {
var seg = this.decoder.seg().iter().next();
if (!seg) {
return 0;
}
return this.decoder.getLogmath().exp(seg.prob);
},
/**
* Pauses microphone recording.
*/
pause: function() {
switch (this.state) {
case stateEnum.STREAMING:
case stateEnum.LISTENING:
this.mic.pause();
this.state = stateEnum.PAUSED;
break;
case stateEnum.PAUSED:
break;
default:
console.warn('Attempted to pause from invalid state: ', this.state);
}
},
/**
* Resumes microphone recording. If called after recognising a wake word,
* this will stop streaming to the <code>onwake</code> callback and resume
* listening for the last requested wake words.
*/
resume: function() {
switch (this.state) {
case stateEnum.LISTENING:
break;
case stateEnum.STREAMING:
if (this.detected && this.detected.length > 0) {
this.decoder.startUtt();
this.detected = null;
}
break;
case stateEnum.PAUSED:
this.mic.resume();
this.state = this.detected ? stateEnum.STREAMING : stateEnum.LISTENING;
break;
default:
console.warn('Attempted to resume from invalid state: ', this.state);
}
},
/**
* This callback handles microphone data when recording.
*
* @callback recordCallback
* @param {Object} data Data from the microphone.
*/
/**
* Streams microphone data to the given {@link callback}.
*
* @param {recordCallback} callback The callback to handle microphone data
*/
record: function(callback) {
if (this.state !== stateEnum.STOPPED &&
this.state !== stateEnum.PRELISTEN) {
console.warn('Attempted to record from invalid state: ' + this.state);
return;
}
var buffer = Concat(callback);
var speechSampleTime = Date.now();
var stream = this.getMic().getAudioStream();
stream.on('data', data => {
var now = Date.now();
buffer.write(data);
if (now - speechSampleTime > this.sampleTime) {
buffer.end();
buffer = Concat(callback);
speechSampleTime = now;
}
});
this.state = (this.state === stateEnum.STOPPED) ?
stateEnum.STREAMING : stateEnum.LISTENING;
this.mic.start();
},
/**
* Stops microphone recording and wake word recognition.
*/
stop: function() {
switch (this.state) {
case stateEnum.STOPPED:
return;
case stateEnum.LOADING:
this.pendingState = { state: stateEnum.STOPPED };
return;
}
this.mic.getAudioStream().removeAllListeners();
this.mic.stop();
this.mic = null; // XXX Doesn't seem like mic is reusable after stopping
if (!this.detected) {
this.decoder.endUtt();
}
this.detected = null;
this.state = stateEnum.STOPPED;
}
};