-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
128 lines (128 loc) · 4.28 KB
/
models.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stream = require("stream-browserify");
const path = require("path");
const fs = require("fs");
const SnowboyDetectNative = require('./snowboy.node').SnowboyDetect;
var DetectionResult;
(function (DetectionResult) {
DetectionResult[DetectionResult["SILENCE"] = -2] = "SILENCE";
DetectionResult[DetectionResult["ERROR"] = -1] = "ERROR";
DetectionResult[DetectionResult["SOUND"] = 0] = "SOUND";
})(DetectionResult || (DetectionResult = {}));
var ModelType;
(function (ModelType) {
ModelType[ModelType["PMDL"] = 0] = "PMDL";
ModelType[ModelType["UMDL"] = 1] = "UMDL";
})(ModelType || (ModelType = {}));
class HotwordModels {
constructor() {
this.models = [];
}
add(model) {
model.hotwords = [].concat(model.hotwords);
model.sensitivity = model.sensitivity || "0.5";
if (fs.existsSync(model.file) === false) {
throw new Error(`Model ${model.file} does not exists.`);
}
const type = path.extname(model.file).toUpperCase();
if (ModelType[type] === ModelType.PMDL && model.hotwords.length > 1) {
throw new Error('Personal models can define only one hotword.');
}
this.models.push(model);
this.lookupTable = this.generateHotwordsLookupTable();
}
get modelString() {
return this.models.map((model) => model.file).join();
}
get sensitivityString() {
return this.models.map((model) => model.sensitivity).join();
}
lookup(index) {
const lookupIndex = index - 1;
if (lookupIndex < 0 || lookupIndex >= this.lookupTable.length) {
throw new Error('Index out of bounds.');
}
return this.lookupTable[lookupIndex];
}
numHotwords() {
return this.lookupTable.length;
}
generateHotwordsLookupTable() {
return this.models.reduce((hotwords, model) => {
return hotwords.concat(model.hotwords);
}, new Array());
}
}
exports.HotwordModels = HotwordModels;
class SnowboyDetect extends stream.Writable {
constructor(options) {
super();
this.models = options.models;
this.nativeInstance = new SnowboyDetectNative(options.resource, options.models.modelString);
if (this.nativeInstance.NumHotwords() !== options.models.numHotwords()) {
throw new Error('Loaded hotwords count does not match number of hotwords defined.');
}
this.nativeInstance.SetSensitivity(options.models.sensitivityString);
if (options.audioGain) {
this.nativeInstance.SetAudioGain(options.audioGain);
}
}
reset() {
return this.nativeInstance.Reset();
}
runDetection(buffer) {
const index = this.nativeInstance.RunDetection(buffer);
this.processDetectionResult(index);
return index;
}
setSensitivity(sensitivity) {
this.nativeInstance.SetSensitivity(sensitivity);
}
getSensitivity() {
return this.nativeInstance.GetSensitivity();
}
setAudioGain(gain) {
this.nativeInstance.SetAudioGain(gain);
}
updateModel() {
this.nativeInstance.UpdateModel();
}
numHotwords() {
return this.nativeInstance.NumHotwords();
}
sampleRate() {
return this.nativeInstance.SampleRate();
}
numChannels() {
return this.nativeInstance.NumChannels();
}
bitsPerSample() {
return this.nativeInstance.BitsPerSample();
}
_write(chunk, encoding, callback) {
const index = this.nativeInstance.RunDetection(chunk);
this.processDetectionResult(index);
return callback();
}
processDetectionResult(index) {
switch (index) {
case DetectionResult.ERROR:
this.emit('error');
break;
case DetectionResult.SILENCE:
this.emit('silence');
break;
case DetectionResult.SOUND:
this.emit('sound');
break;
default:
const hotword = this.models.lookup(index);
this.emit('hotword', index, hotword);
break;
}
}
}
exports.SnowboyDetect = SnowboyDetect;
exports.Detector = SnowboyDetect;
exports.Models = HotwordModels;