-
Notifications
You must be signed in to change notification settings - Fork 207
/
audio-recognizer.js
142 lines (134 loc) · 4.59 KB
/
audio-recognizer.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
// import {makePromise} from './util.js';
class CallbackManager {
constructor() {
this.currentId = 0;
this.callbackPool = {};
}
add(clb) {
var id = this.currentId;
this.callbackPool[id] = clb;
this.currentId++;
return id;
}
get(id) {
const clb = this.callbackPool[id];
if (clb) {
delete this.callbackPool[id];
return clb;
} else {
return null;
}
}
}
class AudioRecognizer extends EventTarget {
constructor({sampleRate}) {
super();
this.callbackManager = new CallbackManager();
// this.loadPromise = makePromise();
this.loaded = false;
const workerURL = '/pocketsphinx.js/webapp/js/recognizer.js';
this.worker = new Worker(workerURL);
this.worker.onmessage = async e => {
this.worker.onmessage = e => {
// This is the case when we have a callback id to be called
if (e.data.hasOwnProperty('id')) {
var clb = this.callbackManager.get(e.data['id']);
var data = {};
if (e.data.hasOwnProperty('data')) data = e.data.data;
if (clb) clb(data);
}
/* // This is a case when the recognizer has a new hypothesis
if (e.data.hasOwnProperty('hyp')) {
var newHyp = e.data.hyp;
if (e.data.hasOwnProperty('final') && e.data.final) newHyp = "Final: " + newHyp;
updateHyp(newHyp);
} */
// This is a case when the recognizer has a new result
if (e.data.hasOwnProperty('result')) {
// console.log('got result', e.data.result);
this.dispatchEvent(new MessageEvent('result', {
data: e.data.result,
}));
}
// This is the case when we have an error
if (e.data.hasOwnProperty('status') && (e.data.status == "error")) {
// updateStatus("Error in " + e.data.command + " with code " + e.data.code);
this.dispatchEvent(new MessageEvent('error', {
data: e.data,
}));
}
};
this.worker.postMessage({
command: 'configure',
data: {
sampleRate,
},
});
await this.postRecognizerJob({
command: 'lazyLoad',
data: {
// pocketsphinx/model/en-us/en-us-phone.lm.bin
// folders: [["/", "zh_broadcastnews_ptm256_8000"]],
folders: [],
files: [
["/", "en-us-phone.lm.bin", "../../model/en-us/en-us-phone.lm.bin"],
/* ["/zh_broadcastnews_ptm256_8000", "means", "../zh_broadcastnews_ptm256_8000/means"],
["/zh_broadcastnews_ptm256_8000", "variances", "../zh_broadcastnews_ptm256_8000/variances"],
["/zh_broadcastnews_ptm256_8000", "transition_matrices", "../zh_broadcastnews_ptm256_8000/transition_matrices"],
["/zh_broadcastnews_ptm256_8000", "sendump", "../zh_broadcastnews_ptm256_8000/sendump"],
["/zh_broadcastnews_ptm256_8000", "mdef", "../zh_broadcastnews_ptm256_8000/mdef"],
["/zh_broadcastnews_ptm256_8000", "feat.params", "../zh_broadcastnews_ptm256_8000/feat.params"],
["/zh_broadcastnews_ptm256_8000", "mixture_weights", "../zh_broadcastnews_ptm256_8000/mixture_weights"],
["/zh_broadcastnews_ptm256_8000", "noisedict", "../zh_broadcastnews_ptm256_8000/noisedict"] */
// ["/", "kws.txt", "../kws.txt"],
// ["/", "kws.dict", "../kws.dict"],
],
},
});
await this.postRecognizerJob(
{
command: 'initialize',
data: [/*["-kws", "kws.txt"], ["-dict","kws.dict"], */ ['-allphone', 'en-us-phone.lm.bin'], ['-logfn', '/dev/null']],
}
);
this.worker.postMessage({
command: 'start',
data: '',
});
// this.loadPromise.accept();
this.loaded = true;
};
this.worker.postMessage({
'pocketsphinx.js': 'pocketsphinx.js',
'pocketsphinx.wasm': 'pocketsphinx.wasm',
});
}
/* waitForLoad() {
return this.loadPromise;
} */
postRecognizerJob(message) {
return new Promise((accept, reject) => {
var msg = message || {};
msg.callbackId = this.callbackManager.add(accept);
this.worker.postMessage(msg);
});
}
send(result) {
if (this.loaded) {
this.worker.postMessage({
command: 'process',
data: result,
}, [result.buffer]);
}
}
destroy() {
this.worker.terminate();
this.worker = null;
/* this.worker.postMessage({
command: 'stop',
}); */
}
}
export {
AudioRecognizer,
};