streaming-asr.js
1.4 KB
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
const addon = require('./addon.js');
class Display {
constructor(maxWordPerline) {
this.handle = addon.createDisplay(maxWordPerline);
}
print(idx, text) {
addon.print(this.handle, idx, text)
}
}
class OnlineStream {
constructor(handle) {
this.handle = handle;
}
// obj is {samples: samples, sampleRate: sampleRate}
// samples is a float32 array containing samples in the range [-1, 1]
// sampleRate is a number
acceptWaveform(obj) {
addon.acceptWaveformOnline(this.handle, obj)
}
inputFinished() {
addon.inputFinished(this.handle)
}
}
class OnlineRecognizer {
constructor(config) {
this.handle = addon.createOnlineRecognizer(config);
this.config = config
}
createStream() {
const handle = addon.createOnlineStream(this.handle);
return new OnlineStream(handle);
}
isReady(stream) {
return addon.isOnlineStreamReady(this.handle, stream.handle);
}
decode(stream) {
addon.decodeOnlineStream(this.handle, stream.handle);
}
isEndpoint(stream) {
return addon.isEndpoint(this.handle, stream.handle);
}
reset(stream) {
addon.reset(this.handle, stream.handle);
}
getResult(stream) {
const jsonStr =
addon.getOnlineStreamResultAsJson(this.handle, stream.handle);
return JSON.parse(jsonStr);
}
}
module.exports = {
OnlineRecognizer,
OnlineStream,
Display
}