csukuangfj
commited on
Commit
•
d88250a
1
Parent(s):
fdb440a
update model
Browse files- app.js +1 -1
- sherpa-ncnn-wasm-main.js +0 -0
- sherpa-ncnn.js +80 -68
app.js
CHANGED
@@ -45,7 +45,7 @@ Module.onRuntimeInitialized = function() {
|
|
45 |
|
46 |
startBtn.disabled = false;
|
47 |
|
48 |
-
recognizer = createRecognizer();
|
49 |
console.log('recognizer is created!', recognizer);
|
50 |
};
|
51 |
|
|
|
45 |
|
46 |
startBtn.disabled = false;
|
47 |
|
48 |
+
recognizer = createRecognizer(Module);
|
49 |
console.log('recognizer is created!', recognizer);
|
50 |
};
|
51 |
|
sherpa-ncnn-wasm-main.js
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
sherpa-ncnn.js
CHANGED
@@ -1,51 +1,51 @@
|
|
1 |
|
2 |
|
3 |
-
function freeConfig(config) {
|
4 |
if ('buffer' in config) {
|
5 |
-
_free(config.buffer);
|
6 |
}
|
7 |
-
_free(config.ptr);
|
8 |
}
|
9 |
|
10 |
// The user should free the returned pointers
|
11 |
-
function initSherpaNcnnModelConfig(config) {
|
12 |
-
let encoderParamLen = lengthBytesUTF8(config.encoderParam) + 1;
|
13 |
-
let decoderParamLen = lengthBytesUTF8(config.decoderParam) + 1;
|
14 |
-
let joinerParamLen = lengthBytesUTF8(config.joinerParam) + 1;
|
15 |
|
16 |
-
let encoderBinLen = lengthBytesUTF8(config.encoderBin) + 1;
|
17 |
-
let decoderBinLen = lengthBytesUTF8(config.decoderBin) + 1;
|
18 |
-
let joinerBinLen = lengthBytesUTF8(config.joinerBin) + 1;
|
19 |
|
20 |
-
let tokensLen = lengthBytesUTF8(config.tokens) + 1;
|
21 |
|
22 |
let n = encoderParamLen + decoderParamLen + joinerParamLen;
|
23 |
n += encoderBinLen + decoderBinLen + joinerBinLen;
|
24 |
n += tokensLen;
|
25 |
|
26 |
-
let buffer = _malloc(n);
|
27 |
-
let ptr = _malloc(4 * 9);
|
28 |
|
29 |
let offset = 0;
|
30 |
-
stringToUTF8(config.encoderParam, buffer + offset, encoderParamLen);
|
31 |
offset += encoderParamLen;
|
32 |
|
33 |
-
stringToUTF8(config.encoderBin, buffer + offset, encoderBinLen);
|
34 |
offset += encoderBinLen;
|
35 |
|
36 |
-
stringToUTF8(config.decoderParam, buffer + offset, decoderParamLen);
|
37 |
offset += decoderParamLen;
|
38 |
|
39 |
-
stringToUTF8(config.decoderBin, buffer + offset, decoderBinLen);
|
40 |
offset += decoderBinLen;
|
41 |
|
42 |
-
stringToUTF8(config.joinerParam, buffer + offset, joinerParamLen);
|
43 |
offset += joinerParamLen;
|
44 |
|
45 |
-
stringToUTF8(config.joinerBin, buffer + offset, joinerBinLen);
|
46 |
offset += joinerBinLen;
|
47 |
|
48 |
-
stringToUTF8(config.tokens, buffer + offset, tokensLen);
|
49 |
offset += tokensLen;
|
50 |
|
51 |
offset = 0;
|
@@ -78,12 +78,12 @@ function initSherpaNcnnModelConfig(config) {
|
|
78 |
}
|
79 |
}
|
80 |
|
81 |
-
function initSherpaNcnnDecoderConfig(config) {
|
82 |
-
let n = lengthBytesUTF8(config.decodingMethod) + 1;
|
83 |
-
let buffer = _malloc(n);
|
84 |
-
let ptr = _malloc(4 * 2);
|
85 |
|
86 |
-
stringToUTF8(config.decodingMethod, buffer, n);
|
87 |
|
88 |
Module.setValue(ptr, buffer, 'i8*');
|
89 |
Module.setValue(ptr + 4, config.numActivePaths, 'i32');
|
@@ -93,8 +93,8 @@ function initSherpaNcnnDecoderConfig(config) {
|
|
93 |
}
|
94 |
}
|
95 |
|
96 |
-
function initSherpaNcnnFeatureExtractorConfig(config) {
|
97 |
-
let ptr = _malloc(4 * 2);
|
98 |
Module.setValue(ptr, config.samplingRate, 'float');
|
99 |
Module.setValue(ptr + 4, config.featureDim, 'i32');
|
100 |
return {
|
@@ -102,23 +102,24 @@ function initSherpaNcnnFeatureExtractorConfig(config) {
|
|
102 |
}
|
103 |
}
|
104 |
|
105 |
-
function initSherpaNcnnRecognizerConfig(config) {
|
106 |
-
let featConfig =
|
107 |
-
|
108 |
-
let
|
|
|
109 |
|
110 |
let numBytes =
|
111 |
featConfig.len + modelConfig.len + decoderConfig.len + 4 * 4 + 4 * 2;
|
112 |
|
113 |
-
let ptr = _malloc(numBytes);
|
114 |
let offset = 0;
|
115 |
-
_CopyHeap(featConfig.ptr, featConfig.len, ptr + offset);
|
116 |
offset += featConfig.len;
|
117 |
|
118 |
-
_CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset)
|
119 |
offset += modelConfig.len;
|
120 |
|
121 |
-
_CopyHeap(decoderConfig.ptr, decoderConfig.len, ptr + offset)
|
122 |
offset += decoderConfig.len;
|
123 |
|
124 |
Module.setValue(ptr + offset, config.enableEndpoint, 'i32');
|
@@ -146,17 +147,18 @@ function initSherpaNcnnRecognizerConfig(config) {
|
|
146 |
}
|
147 |
|
148 |
class Stream {
|
149 |
-
constructor(handle) {
|
150 |
this.handle = handle;
|
151 |
this.pointer = null;
|
152 |
-
this.n = 0
|
|
|
153 |
}
|
154 |
|
155 |
free() {
|
156 |
if (this.handle) {
|
157 |
-
_DestroyStream(this.handle);
|
158 |
this.handle = null;
|
159 |
-
_free(this.pointer);
|
160 |
this.pointer = null;
|
161 |
this.n = 0;
|
162 |
}
|
@@ -168,13 +170,15 @@ class Stream {
|
|
168 |
*/
|
169 |
acceptWaveform(sampleRate, samples) {
|
170 |
if (this.n < samples.length) {
|
171 |
-
_free(this.pointer);
|
172 |
-
this.pointer =
|
|
|
173 |
this.n = samples.length
|
174 |
}
|
175 |
|
176 |
-
Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
|
177 |
-
|
|
|
178 |
}
|
179 |
|
180 |
inputFinished() {
|
@@ -183,59 +187,56 @@ class Stream {
|
|
183 |
};
|
184 |
|
185 |
class Recognizer {
|
186 |
-
constructor(configObj,
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
}
|
191 |
-
|
192 |
-
let config = initSherpaNcnnRecognizerConfig(configObj)
|
193 |
-
let handle = _CreateRecognizer(config.ptr);
|
194 |
|
195 |
-
freeConfig(config.featConfig);
|
196 |
-
freeConfig(config.modelConfig);
|
197 |
-
freeConfig(config.decoderConfig);
|
198 |
-
freeConfig(config);
|
199 |
|
200 |
this.handle = handle;
|
|
|
201 |
}
|
202 |
|
203 |
free() {
|
204 |
-
_DestroyRecognizer(this.handle);
|
205 |
this.handle = 0
|
206 |
}
|
207 |
|
208 |
createStream() {
|
209 |
-
let handle = _CreateStream(this.handle);
|
210 |
-
return new Stream(handle);
|
211 |
}
|
212 |
|
213 |
isReady(stream) {
|
214 |
-
return _IsReady(this.handle, stream.handle) == 1;
|
215 |
}
|
216 |
|
217 |
isEndpoint(stream) {
|
218 |
-
return _IsEndpoint(this.handle, stream.handle) == 1;
|
219 |
}
|
220 |
|
221 |
decode(stream) {
|
222 |
-
return _Decode(this.handle, stream.handle);
|
223 |
}
|
224 |
|
225 |
reset(stream) {
|
226 |
-
_Reset(this.handle, stream.handle);
|
227 |
}
|
228 |
|
229 |
getResult(stream) {
|
230 |
-
let r = _GetResult(this.handle, stream.handle);
|
231 |
-
let textPtr = getValue(r, 'i8*');
|
232 |
-
let text = UTF8ToString(textPtr);
|
233 |
-
_DestroyResult(r);
|
234 |
return text;
|
235 |
}
|
236 |
}
|
237 |
|
238 |
-
function createRecognizer() {
|
239 |
let modelConfig = {
|
240 |
encoderParam: './encoder_jit_trace-pnnx.ncnn.param',
|
241 |
encoderBin: './encoder_jit_trace-pnnx.ncnn.bin',
|
@@ -268,5 +269,16 @@ function createRecognizer() {
|
|
268 |
rule3MinUtternceLength: 20,
|
269 |
};
|
270 |
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
}
|
|
|
1 |
|
2 |
|
3 |
+
function freeConfig(config, Module) {
|
4 |
if ('buffer' in config) {
|
5 |
+
Module._free(config.buffer);
|
6 |
}
|
7 |
+
Module._free(config.ptr);
|
8 |
}
|
9 |
|
10 |
// The user should free the returned pointers
|
11 |
+
function initSherpaNcnnModelConfig(config, Module) {
|
12 |
+
let encoderParamLen = Module.lengthBytesUTF8(config.encoderParam) + 1;
|
13 |
+
let decoderParamLen = Module.lengthBytesUTF8(config.decoderParam) + 1;
|
14 |
+
let joinerParamLen = Module.lengthBytesUTF8(config.joinerParam) + 1;
|
15 |
|
16 |
+
let encoderBinLen = Module.lengthBytesUTF8(config.encoderBin) + 1;
|
17 |
+
let decoderBinLen = Module.lengthBytesUTF8(config.decoderBin) + 1;
|
18 |
+
let joinerBinLen = Module.lengthBytesUTF8(config.joinerBin) + 1;
|
19 |
|
20 |
+
let tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
|
21 |
|
22 |
let n = encoderParamLen + decoderParamLen + joinerParamLen;
|
23 |
n += encoderBinLen + decoderBinLen + joinerBinLen;
|
24 |
n += tokensLen;
|
25 |
|
26 |
+
let buffer = Module._malloc(n);
|
27 |
+
let ptr = Module._malloc(4 * 9);
|
28 |
|
29 |
let offset = 0;
|
30 |
+
Module.stringToUTF8(config.encoderParam, buffer + offset, encoderParamLen);
|
31 |
offset += encoderParamLen;
|
32 |
|
33 |
+
Module.stringToUTF8(config.encoderBin, buffer + offset, encoderBinLen);
|
34 |
offset += encoderBinLen;
|
35 |
|
36 |
+
Module.stringToUTF8(config.decoderParam, buffer + offset, decoderParamLen);
|
37 |
offset += decoderParamLen;
|
38 |
|
39 |
+
Module.stringToUTF8(config.decoderBin, buffer + offset, decoderBinLen);
|
40 |
offset += decoderBinLen;
|
41 |
|
42 |
+
Module.stringToUTF8(config.joinerParam, buffer + offset, joinerParamLen);
|
43 |
offset += joinerParamLen;
|
44 |
|
45 |
+
Module.stringToUTF8(config.joinerBin, buffer + offset, joinerBinLen);
|
46 |
offset += joinerBinLen;
|
47 |
|
48 |
+
Module.stringToUTF8(config.tokens, buffer + offset, tokensLen);
|
49 |
offset += tokensLen;
|
50 |
|
51 |
offset = 0;
|
|
|
78 |
}
|
79 |
}
|
80 |
|
81 |
+
function initSherpaNcnnDecoderConfig(config, Module) {
|
82 |
+
let n = Module.lengthBytesUTF8(config.decodingMethod) + 1;
|
83 |
+
let buffer = Module._malloc(n);
|
84 |
+
let ptr = Module._malloc(4 * 2);
|
85 |
|
86 |
+
Module.stringToUTF8(config.decodingMethod, buffer, n);
|
87 |
|
88 |
Module.setValue(ptr, buffer, 'i8*');
|
89 |
Module.setValue(ptr + 4, config.numActivePaths, 'i32');
|
|
|
93 |
}
|
94 |
}
|
95 |
|
96 |
+
function initSherpaNcnnFeatureExtractorConfig(config, Module) {
|
97 |
+
let ptr = Module._malloc(4 * 2);
|
98 |
Module.setValue(ptr, config.samplingRate, 'float');
|
99 |
Module.setValue(ptr + 4, config.featureDim, 'i32');
|
100 |
return {
|
|
|
102 |
}
|
103 |
}
|
104 |
|
105 |
+
function initSherpaNcnnRecognizerConfig(config, Module) {
|
106 |
+
let featConfig =
|
107 |
+
initSherpaNcnnFeatureExtractorConfig(config.featConfig, Module);
|
108 |
+
let modelConfig = initSherpaNcnnModelConfig(config.modelConfig, Module);
|
109 |
+
let decoderConfig = initSherpaNcnnDecoderConfig(config.decoderConfig, Module);
|
110 |
|
111 |
let numBytes =
|
112 |
featConfig.len + modelConfig.len + decoderConfig.len + 4 * 4 + 4 * 2;
|
113 |
|
114 |
+
let ptr = Module._malloc(numBytes);
|
115 |
let offset = 0;
|
116 |
+
Module._CopyHeap(featConfig.ptr, featConfig.len, ptr + offset);
|
117 |
offset += featConfig.len;
|
118 |
|
119 |
+
Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset)
|
120 |
offset += modelConfig.len;
|
121 |
|
122 |
+
Module._CopyHeap(decoderConfig.ptr, decoderConfig.len, ptr + offset)
|
123 |
offset += decoderConfig.len;
|
124 |
|
125 |
Module.setValue(ptr + offset, config.enableEndpoint, 'i32');
|
|
|
147 |
}
|
148 |
|
149 |
class Stream {
|
150 |
+
constructor(handle, Module) {
|
151 |
this.handle = handle;
|
152 |
this.pointer = null;
|
153 |
+
this.n = 0;
|
154 |
+
this.Module = Module;
|
155 |
}
|
156 |
|
157 |
free() {
|
158 |
if (this.handle) {
|
159 |
+
this.Module._DestroyStream(this.handle);
|
160 |
this.handle = null;
|
161 |
+
this.Module._free(this.pointer);
|
162 |
this.pointer = null;
|
163 |
this.n = 0;
|
164 |
}
|
|
|
170 |
*/
|
171 |
acceptWaveform(sampleRate, samples) {
|
172 |
if (this.n < samples.length) {
|
173 |
+
this.Module._free(this.pointer);
|
174 |
+
this.pointer =
|
175 |
+
this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
|
176 |
this.n = samples.length
|
177 |
}
|
178 |
|
179 |
+
this.Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
|
180 |
+
this.Module._AcceptWaveform(
|
181 |
+
this.handle, sampleRate, this.pointer, samples.length);
|
182 |
}
|
183 |
|
184 |
inputFinished() {
|
|
|
187 |
};
|
188 |
|
189 |
class Recognizer {
|
190 |
+
constructor(configObj, Module) {
|
191 |
+
this.config = configObj;
|
192 |
+
let config = initSherpaNcnnRecognizerConfig(configObj, Module)
|
193 |
+
let handle = Module._CreateRecognizer(config.ptr);
|
|
|
|
|
|
|
|
|
194 |
|
195 |
+
freeConfig(config.featConfig, Module);
|
196 |
+
freeConfig(config.modelConfig, Module);
|
197 |
+
freeConfig(config.decoderConfig, Module);
|
198 |
+
freeConfig(config, Module);
|
199 |
|
200 |
this.handle = handle;
|
201 |
+
this.Module = Module;
|
202 |
}
|
203 |
|
204 |
free() {
|
205 |
+
this.Module._DestroyRecognizer(this.handle);
|
206 |
this.handle = 0
|
207 |
}
|
208 |
|
209 |
createStream() {
|
210 |
+
let handle = this.Module._CreateStream(this.handle);
|
211 |
+
return new Stream(handle, this.Module);
|
212 |
}
|
213 |
|
214 |
isReady(stream) {
|
215 |
+
return this.Module._IsReady(this.handle, stream.handle) == 1;
|
216 |
}
|
217 |
|
218 |
isEndpoint(stream) {
|
219 |
+
return this.Module._IsEndpoint(this.handle, stream.handle) == 1;
|
220 |
}
|
221 |
|
222 |
decode(stream) {
|
223 |
+
return this.Module._Decode(this.handle, stream.handle);
|
224 |
}
|
225 |
|
226 |
reset(stream) {
|
227 |
+
this.Module._Reset(this.handle, stream.handle);
|
228 |
}
|
229 |
|
230 |
getResult(stream) {
|
231 |
+
let r = this.Module._GetResult(this.handle, stream.handle);
|
232 |
+
let textPtr = this.Module.getValue(r, 'i8*');
|
233 |
+
let text = this.Module.UTF8ToString(textPtr);
|
234 |
+
this.Module._DestroyResult(r);
|
235 |
return text;
|
236 |
}
|
237 |
}
|
238 |
|
239 |
+
function createRecognizer(Module, myConfig) {
|
240 |
let modelConfig = {
|
241 |
encoderParam: './encoder_jit_trace-pnnx.ncnn.param',
|
242 |
encoderBin: './encoder_jit_trace-pnnx.ncnn.bin',
|
|
|
269 |
rule3MinUtternceLength: 20,
|
270 |
};
|
271 |
|
272 |
+
if (myConfig) {
|
273 |
+
configObj = myConfig;
|
274 |
+
}
|
275 |
+
|
276 |
+
return new Recognizer(configObj, Module);
|
277 |
+
}
|
278 |
+
|
279 |
+
if (typeof process == 'object' && typeof process.versions == 'object' &&
|
280 |
+
typeof process.versions.node == 'string') {
|
281 |
+
module.exports = {
|
282 |
+
createRecognizer,
|
283 |
+
};
|
284 |
}
|