csukuangfj commited on
Commit
a2f4e13
1 Parent(s): 93c7594

update model

Browse files
app-asr.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 = createOnlineRecognizer(Module);
49
  console.log('recognizer is created!', recognizer);
50
  };
51
 
sherpa-onnx-asr.js CHANGED
@@ -1,161 +1,181 @@
1
- function freeConfig(config) {
2
  if ('buffer' in config) {
3
- _free(config.buffer);
4
  }
5
 
6
  if ('config' in config) {
7
- freeConfig(config.config)
8
  }
9
 
10
  if ('transducer' in config) {
11
- freeConfig(config.transducer)
12
  }
13
 
14
  if ('paraformer' in config) {
15
- freeConfig(config.paraformer)
16
  }
17
 
18
  if ('ctc' in config) {
19
- freeConfig(config.ctc)
20
  }
21
 
22
  if ('feat' in config) {
23
- freeConfig(config.feat)
24
  }
25
 
26
  if ('model' in config) {
27
- freeConfig(config.model)
28
  }
29
 
30
- _free(config.ptr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
32
 
33
  // The user should free the returned pointers
34
- function initSherpaOnnxOnlineTransducerModelConfig(config) {
35
- let encoderLen = lengthBytesUTF8(config.encoder) + 1;
36
- let decoderLen = lengthBytesUTF8(config.decoder) + 1;
37
- let joinerLen = lengthBytesUTF8(config.joiner) + 1;
38
 
39
- let n = encoderLen + decoderLen + joinerLen;
40
 
41
- let buffer = _malloc(n);
42
 
43
- let len = 3 * 4; // 3 pointers
44
- let ptr = _malloc(len);
45
 
46
  let offset = 0;
47
- stringToUTF8(config.encoder, buffer + offset, encoderLen);
48
  offset += encoderLen;
49
 
50
- stringToUTF8(config.decoder, buffer + offset, decoderLen);
51
  offset += decoderLen;
52
 
53
- stringToUTF8(config.joiner, buffer + offset, joinerLen);
54
 
55
  offset = 0;
56
- setValue(ptr, buffer + offset, 'i8*');
57
  offset += encoderLen;
58
 
59
- setValue(ptr + 4, buffer + offset, 'i8*');
60
  offset += decoderLen;
61
 
62
- setValue(ptr + 8, buffer + offset, 'i8*');
63
 
64
  return {
65
  buffer: buffer, ptr: ptr, len: len,
66
  }
67
  }
68
 
69
- function initSherpaOnnxOnlineParaformerModelConfig(config) {
70
- let encoderLen = lengthBytesUTF8(config.encoder) + 1;
71
- let decoderLen = lengthBytesUTF8(config.decoder) + 1;
72
 
73
- let n = encoderLen + decoderLen;
74
- let buffer = _malloc(n);
75
 
76
- let len = 2 * 4; // 2 pointers
77
- let ptr = _malloc(len);
78
 
79
  let offset = 0;
80
- stringToUTF8(config.encoder, buffer + offset, encoderLen);
81
  offset += encoderLen;
82
 
83
- stringToUTF8(config.decoder, buffer + offset, decoderLen);
84
 
85
  offset = 0;
86
- setValue(ptr, buffer + offset, 'i8*');
87
  offset += encoderLen;
88
 
89
- setValue(ptr + 4, buffer + offset, 'i8*');
90
 
91
  return {
92
  buffer: buffer, ptr: ptr, len: len,
93
  }
94
  }
95
 
96
- function initSherpaOnnxOnlineZipformer2CtcModelConfig(config) {
97
- let n = lengthBytesUTF8(config.model) + 1;
98
- let buffer = _malloc(n);
99
 
100
- let len = 1 * 4; // 1 pointer
101
- let ptr = _malloc(len);
102
 
103
- stringToUTF8(config.model, buffer, n);
104
 
105
- setValue(ptr, buffer, 'i8*');
106
 
107
  return {
108
  buffer: buffer, ptr: ptr, len: len,
109
  }
110
  }
111
 
112
- function initSherpaOnnxOnlineModelConfig(config) {
113
- let transducer = initSherpaOnnxOnlineTransducerModelConfig(config.transducer);
114
- let paraformer = initSherpaOnnxOnlineParaformerModelConfig(config.paraformer);
115
- let ctc = initSherpaOnnxOnlineZipformer2CtcModelConfig(config.zipformer2Ctc);
 
 
 
116
 
117
- let len = transducer.len + paraformer.len + ctc.len + 5 * 4;
118
- let ptr = _malloc(len);
119
 
120
  let offset = 0;
121
- _CopyHeap(transducer.ptr, transducer.len, ptr + offset);
122
  offset += transducer.len;
123
 
124
- _CopyHeap(paraformer.ptr, paraformer.len, ptr + offset);
125
  offset += paraformer.len;
126
 
127
- _CopyHeap(ctc.ptr, ctc.len, ptr + offset);
128
  offset += ctc.len;
129
 
130
- let tokensLen = lengthBytesUTF8(config.tokens) + 1;
131
- let providerLen = lengthBytesUTF8(config.provider) + 1;
132
- let modelTypeLen = lengthBytesUTF8(config.modelType) + 1;
133
- let bufferLen = tokensLen + providerLen + modelTypeLen;
134
- let buffer = _malloc(bufferLen);
135
 
136
  offset = 0;
137
- stringToUTF8(config.tokens, buffer, tokensLen);
138
  offset += tokensLen;
139
 
140
- stringToUTF8(config.provider, buffer + offset, providerLen);
141
  offset += providerLen;
142
 
143
- stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
144
 
145
  offset = transducer.len + paraformer.len + ctc.len;
146
- setValue(ptr + offset, buffer, 'i8*'); // tokens
147
  offset += 4;
148
 
149
- setValue(ptr + offset, config.numThreads, 'i32');
150
  offset += 4;
151
 
152
- setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
153
  offset += 4;
154
 
155
- setValue(ptr + offset, config.debug, 'i32');
156
  offset += 4;
157
 
158
- setValue(ptr + offset, buffer + tokensLen + providerLen, 'i8*'); // modelType
 
159
  offset += 4;
160
 
161
  return {
@@ -164,63 +184,63 @@ function initSherpaOnnxOnlineModelConfig(config) {
164
  }
165
  }
166
 
167
- function initSherpaOnnxFeatureConfig(config) {
168
- let len = 2 * 4; // 2 pointers
169
- let ptr = _malloc(len);
170
 
171
- setValue(ptr, config.sampleRate, 'i32');
172
- setValue(ptr + 4, config.featureDim, 'i32');
173
  return {ptr: ptr, len: len};
174
  }
175
 
176
- function initSherpaOnnxOnlineRecognizerConfig(config) {
177
- let feat = initSherpaOnnxFeatureConfig(config.featConfig);
178
- let model = initSherpaOnnxOnlineModelConfig(config.modelConfig);
179
 
180
- let len = feat.len + model.len + 8 * 4;
181
- let ptr = _malloc(len);
182
 
183
  let offset = 0;
184
- _CopyHeap(feat.ptr, feat.len, ptr + offset);
185
  offset += feat.len;
186
 
187
- _CopyHeap(model.ptr, model.len, ptr + offset);
188
  offset += model.len;
189
 
190
- let decodingMethodLen = lengthBytesUTF8(config.decodingMethod) + 1;
191
- let hotwordsFileLen = lengthBytesUTF8(config.hotwordsFile) + 1;
192
- let bufferLen = decodingMethodLen + hotwordsFileLen;
193
- let buffer = _malloc(bufferLen);
194
 
195
  offset = 0;
196
- stringToUTF8(config.decodingMethod, buffer, decodingMethodLen);
197
  offset += decodingMethodLen;
198
 
199
- stringToUTF8(config.hotwordsFile, buffer + offset, hotwordsFileLen);
200
 
201
  offset = feat.len + model.len;
202
- setValue(ptr + offset, buffer, 'i8*'); // decoding method
203
  offset += 4;
204
 
205
- setValue(ptr + offset, config.maxActivePaths, 'i32');
206
  offset += 4;
207
 
208
- setValue(ptr + offset, config.enableEndpoint, 'i32');
209
  offset += 4;
210
 
211
- setValue(ptr + offset, config.rule1MinTrailingSilence, 'float');
212
  offset += 4;
213
 
214
- setValue(ptr + offset, config.rule2MinTrailingSilence, 'float');
215
  offset += 4;
216
 
217
- setValue(ptr + offset, config.rule3MinUtteranceLength, 'float');
218
  offset += 4;
219
 
220
- setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
221
  offset += 4;
222
 
223
- setValue(ptr + offset, config.hotwordsScore, 'float');
224
  offset += 4;
225
 
226
  return {
@@ -229,21 +249,21 @@ function initSherpaOnnxOnlineRecognizerConfig(config) {
229
  }
230
 
231
 
232
- function createRecognizer() {
233
- let onlineTransducerModelConfig = {
234
  encoder: '',
235
  decoder: '',
236
  joiner: '',
237
- }
238
 
239
- let onlineParaformerModelConfig = {
240
  encoder: '',
241
  decoder: '',
242
- }
243
 
244
- let onlineZipformer2CtcModelConfig = {
245
  model: '',
246
- }
247
 
248
  let type = 0;
249
 
@@ -266,7 +286,7 @@ function createRecognizer() {
266
  }
267
 
268
 
269
- let onlineModelConfig = {
270
  transducer: onlineTransducerModelConfig,
271
  paraformer: onlineParaformerModelConfig,
272
  zipformer2Ctc: onlineZipformer2CtcModelConfig,
@@ -275,12 +295,12 @@ function createRecognizer() {
275
  provider: 'cpu',
276
  debug: 1,
277
  modelType: '',
278
- }
279
 
280
- let featureConfig = {
281
  sampleRate: 16000,
282
  featureDim: 80,
283
- }
284
 
285
  let recognizerConfig = {
286
  featConfig: featureConfig,
@@ -293,23 +313,336 @@ function createRecognizer() {
293
  rule3MinUtteranceLength: 20,
294
  hotwordsFile: '',
295
  hotwordsScore: 1.5,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
- return new OnlineRecognizer(recognizerConfig);
 
 
 
 
 
 
 
 
299
  }
300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  class OnlineStream {
302
- constructor(handle) {
303
  this.handle = handle;
304
  this.pointer = null; // buffer
305
  this.n = 0; // buffer size
 
306
  }
307
 
308
  free() {
309
  if (this.handle) {
310
- _DestroyOnlineStream(this.handle);
311
  this.handle = null;
312
- _free(this.pointer);
313
  this.pointer = null;
314
  this.n = 0;
315
  }
@@ -321,61 +654,73 @@ class OnlineStream {
321
  */
322
  acceptWaveform(sampleRate, samples) {
323
  if (this.n < samples.length) {
324
- _free(this.pointer);
325
- this.pointer = _malloc(samples.length * samples.BYTES_PER_ELEMENT);
 
326
  this.n = samples.length
327
  }
328
 
329
- Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
330
- _AcceptWaveform(this.handle, sampleRate, this.pointer, samples.length);
 
331
  }
332
 
333
  inputFinished() {
334
- _InputFinished(this.handle);
335
  }
336
  };
337
 
338
  class OnlineRecognizer {
339
- constructor(configObj) {
340
- let config = initSherpaOnnxOnlineRecognizerConfig(configObj)
341
- let handle = _CreateOnlineRecognizer(config.ptr);
 
342
 
343
- freeConfig(config);
344
 
345
  this.handle = handle;
 
346
  }
347
 
348
  free() {
349
- _DestroyOnlineRecognizer(this.handle);
350
  this.handle = 0
351
  }
352
 
353
  createStream() {
354
- let handle = _CreateOnlineStream(this.handle);
355
- return new OnlineStream(handle);
356
  }
357
 
358
  isReady(stream) {
359
- return _IsOnlineStreamReady(this.handle, stream.handle) == 1;
360
  }
361
 
362
  decode(stream) {
363
- return _DecodeOnlineStream(this.handle, stream.handle);
364
  }
365
 
366
  isEndpoint(stream) {
367
- return _IsEndpoint(this.handle, stream.handle) == 1;
368
  }
369
 
370
  reset(stream) {
371
- _Reset(this.handle, stream.handle);
372
  }
373
 
374
  getResult(stream) {
375
- let r = _GetOnlineStreamResult(this.handle, stream.handle);
376
- let textPtr = getValue(r, 'i8*');
377
- let text = UTF8ToString(textPtr);
378
- _DestroyOnlineRecognizerResult(r);
379
  return text;
380
  }
381
  }
 
 
 
 
 
 
 
 
 
1
+ function freeConfig(config, Module) {
2
  if ('buffer' in config) {
3
+ Module._free(config.buffer);
4
  }
5
 
6
  if ('config' in config) {
7
+ freeConfig(config.config, Module)
8
  }
9
 
10
  if ('transducer' in config) {
11
+ freeConfig(config.transducer, Module)
12
  }
13
 
14
  if ('paraformer' in config) {
15
+ freeConfig(config.paraformer, Module)
16
  }
17
 
18
  if ('ctc' in config) {
19
+ freeConfig(config.ctc, Module)
20
  }
21
 
22
  if ('feat' in config) {
23
+ freeConfig(config.feat, Module)
24
  }
25
 
26
  if ('model' in config) {
27
+ freeConfig(config.model, Module)
28
  }
29
 
30
+ if ('nemoCtc' in config) {
31
+ freeConfig(config.nemoCtc, Module)
32
+ }
33
+
34
+ if ('whisper' in config) {
35
+ freeConfig(config.whisper, Module)
36
+ }
37
+
38
+ if ('tdnn' in config) {
39
+ freeConfig(config.tdnn, Module)
40
+ }
41
+
42
+ if ('lm' in config) {
43
+ freeConfig(config.lm, Module)
44
+ }
45
+
46
+ Module._free(config.ptr);
47
  }
48
 
49
  // The user should free the returned pointers
50
+ function initSherpaOnnxOnlineTransducerModelConfig(config, Module) {
51
+ const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
52
+ const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
53
+ const joinerLen = Module.lengthBytesUTF8(config.joiner) + 1;
54
 
55
+ const n = encoderLen + decoderLen + joinerLen;
56
 
57
+ const buffer = Module._malloc(n);
58
 
59
+ const len = 3 * 4; // 3 pointers
60
+ const ptr = Module._malloc(len);
61
 
62
  let offset = 0;
63
+ Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
64
  offset += encoderLen;
65
 
66
+ Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
67
  offset += decoderLen;
68
 
69
+ Module.stringToUTF8(config.joiner, buffer + offset, joinerLen);
70
 
71
  offset = 0;
72
+ Module.setValue(ptr, buffer + offset, 'i8*');
73
  offset += encoderLen;
74
 
75
+ Module.setValue(ptr + 4, buffer + offset, 'i8*');
76
  offset += decoderLen;
77
 
78
+ Module.setValue(ptr + 8, buffer + offset, 'i8*');
79
 
80
  return {
81
  buffer: buffer, ptr: ptr, len: len,
82
  }
83
  }
84
 
85
+ function initSherpaOnnxOnlineParaformerModelConfig(config, Module) {
86
+ const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
87
+ const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
88
 
89
+ const n = encoderLen + decoderLen;
90
+ const buffer = Module._malloc(n);
91
 
92
+ const len = 2 * 4; // 2 pointers
93
+ const ptr = Module._malloc(len);
94
 
95
  let offset = 0;
96
+ Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
97
  offset += encoderLen;
98
 
99
+ Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
100
 
101
  offset = 0;
102
+ Module.setValue(ptr, buffer + offset, 'i8*');
103
  offset += encoderLen;
104
 
105
+ Module.setValue(ptr + 4, buffer + offset, 'i8*');
106
 
107
  return {
108
  buffer: buffer, ptr: ptr, len: len,
109
  }
110
  }
111
 
112
+ function initSherpaOnnxOnlineZipformer2CtcModelConfig(config, Module) {
113
+ const n = Module.lengthBytesUTF8(config.model) + 1;
114
+ const buffer = Module._malloc(n);
115
 
116
+ const len = 1 * 4; // 1 pointer
117
+ const ptr = Module._malloc(len);
118
 
119
+ Module.stringToUTF8(config.model, buffer, n);
120
 
121
+ Module.setValue(ptr, buffer, 'i8*');
122
 
123
  return {
124
  buffer: buffer, ptr: ptr, len: len,
125
  }
126
  }
127
 
128
+ function initSherpaOnnxOnlineModelConfig(config, Module) {
129
+ const transducer =
130
+ initSherpaOnnxOnlineTransducerModelConfig(config.transducer, Module);
131
+ const paraformer =
132
+ initSherpaOnnxOnlineParaformerModelConfig(config.paraformer, Module);
133
+ const ctc = initSherpaOnnxOnlineZipformer2CtcModelConfig(
134
+ config.zipformer2Ctc, Module);
135
 
136
+ const len = transducer.len + paraformer.len + ctc.len + 5 * 4;
137
+ const ptr = Module._malloc(len);
138
 
139
  let offset = 0;
140
+ Module._CopyHeap(transducer.ptr, transducer.len, ptr + offset);
141
  offset += transducer.len;
142
 
143
+ Module._CopyHeap(paraformer.ptr, paraformer.len, ptr + offset);
144
  offset += paraformer.len;
145
 
146
+ Module._CopyHeap(ctc.ptr, ctc.len, ptr + offset);
147
  offset += ctc.len;
148
 
149
+ const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
150
+ const providerLen = Module.lengthBytesUTF8(config.provider) + 1;
151
+ const modelTypeLen = Module.lengthBytesUTF8(config.modelType) + 1;
152
+ const bufferLen = tokensLen + providerLen + modelTypeLen;
153
+ const buffer = Module._malloc(bufferLen);
154
 
155
  offset = 0;
156
+ Module.stringToUTF8(config.tokens, buffer, tokensLen);
157
  offset += tokensLen;
158
 
159
+ Module.stringToUTF8(config.provider, buffer + offset, providerLen);
160
  offset += providerLen;
161
 
162
+ Module.stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
163
 
164
  offset = transducer.len + paraformer.len + ctc.len;
165
+ Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
166
  offset += 4;
167
 
168
+ Module.setValue(ptr + offset, config.numThreads, 'i32');
169
  offset += 4;
170
 
171
+ Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
172
  offset += 4;
173
 
174
+ Module.setValue(ptr + offset, config.debug, 'i32');
175
  offset += 4;
176
 
177
+ Module.setValue(
178
+ ptr + offset, buffer + tokensLen + providerLen, 'i8*'); // modelType
179
  offset += 4;
180
 
181
  return {
 
184
  }
185
  }
186
 
187
+ function initSherpaOnnxFeatureConfig(config, Module) {
188
+ const len = 2 * 4; // 2 pointers
189
+ const ptr = Module._malloc(len);
190
 
191
+ Module.setValue(ptr, config.sampleRate, 'i32');
192
+ Module.setValue(ptr + 4, config.featureDim, 'i32');
193
  return {ptr: ptr, len: len};
194
  }
195
 
196
+ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
197
+ const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
198
+ const model = initSherpaOnnxOnlineModelConfig(config.modelConfig, Module);
199
 
200
+ const len = feat.len + model.len + 8 * 4;
201
+ const ptr = Module._malloc(len);
202
 
203
  let offset = 0;
204
+ Module._CopyHeap(feat.ptr, feat.len, ptr + offset);
205
  offset += feat.len;
206
 
207
+ Module._CopyHeap(model.ptr, model.len, ptr + offset);
208
  offset += model.len;
209
 
210
+ const decodingMethodLen = Module.lengthBytesUTF8(config.decodingMethod) + 1;
211
+ const hotwordsFileLen = Module.lengthBytesUTF8(config.hotwordsFile) + 1;
212
+ const bufferLen = decodingMethodLen + hotwordsFileLen;
213
+ const buffer = Module._malloc(bufferLen);
214
 
215
  offset = 0;
216
+ Module.stringToUTF8(config.decodingMethod, buffer, decodingMethodLen);
217
  offset += decodingMethodLen;
218
 
219
+ Module.stringToUTF8(config.hotwordsFile, buffer + offset, hotwordsFileLen);
220
 
221
  offset = feat.len + model.len;
222
+ Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
223
  offset += 4;
224
 
225
+ Module.setValue(ptr + offset, config.maxActivePaths, 'i32');
226
  offset += 4;
227
 
228
+ Module.setValue(ptr + offset, config.enableEndpoint, 'i32');
229
  offset += 4;
230
 
231
+ Module.setValue(ptr + offset, config.rule1MinTrailingSilence, 'float');
232
  offset += 4;
233
 
234
+ Module.setValue(ptr + offset, config.rule2MinTrailingSilence, 'float');
235
  offset += 4;
236
 
237
+ Module.setValue(ptr + offset, config.rule3MinUtteranceLength, 'float');
238
  offset += 4;
239
 
240
+ Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
241
  offset += 4;
242
 
243
+ Module.setValue(ptr + offset, config.hotwordsScore, 'float');
244
  offset += 4;
245
 
246
  return {
 
249
  }
250
 
251
 
252
+ function createOnlineRecognizer(Module, myConfig) {
253
+ const onlineTransducerModelConfig = {
254
  encoder: '',
255
  decoder: '',
256
  joiner: '',
257
+ };
258
 
259
+ const onlineParaformerModelConfig = {
260
  encoder: '',
261
  decoder: '',
262
+ };
263
 
264
+ const onlineZipformer2CtcModelConfig = {
265
  model: '',
266
+ };
267
 
268
  let type = 0;
269
 
 
286
  }
287
 
288
 
289
+ const onlineModelConfig = {
290
  transducer: onlineTransducerModelConfig,
291
  paraformer: onlineParaformerModelConfig,
292
  zipformer2Ctc: onlineZipformer2CtcModelConfig,
 
295
  provider: 'cpu',
296
  debug: 1,
297
  modelType: '',
298
+ };
299
 
300
+ const featureConfig = {
301
  sampleRate: 16000,
302
  featureDim: 80,
303
+ };
304
 
305
  let recognizerConfig = {
306
  featConfig: featureConfig,
 
313
  rule3MinUtteranceLength: 20,
314
  hotwordsFile: '',
315
  hotwordsScore: 1.5,
316
+ };
317
+ if (myConfig) {
318
+ recognizerConfig = myConfig;
319
+ }
320
+
321
+ return new OnlineRecognizer(recognizerConfig, Module);
322
+ }
323
+
324
+ function initSherpaOnnxOfflineTransducerModelConfig(config, Module) {
325
+ const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
326
+ const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
327
+ const joinerLen = Module.lengthBytesUTF8(config.joiner) + 1;
328
+
329
+ const n = encoderLen + decoderLen + joinerLen;
330
+
331
+ const buffer = Module._malloc(n);
332
+
333
+ const len = 3 * 4; // 3 pointers
334
+ const ptr = Module._malloc(len);
335
+
336
+ let offset = 0;
337
+ Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
338
+ offset += encoderLen;
339
+
340
+ Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
341
+ offset += decoderLen;
342
+
343
+ Module.stringToUTF8(config.joiner, buffer + offset, joinerLen);
344
+
345
+ offset = 0;
346
+ Module.setValue(ptr, buffer + offset, 'i8*');
347
+ offset += encoderLen;
348
+
349
+ Module.setValue(ptr + 4, buffer + offset, 'i8*');
350
+ offset += decoderLen;
351
+
352
+ Module.setValue(ptr + 8, buffer + offset, 'i8*');
353
+
354
+ return {
355
+ buffer: buffer, ptr: ptr, len: len,
356
+ }
357
+ }
358
+
359
+ function initSherpaOnnxOfflineParaformerModelConfig(config, Module) {
360
+ const n = Module.lengthBytesUTF8(config.model) + 1;
361
+
362
+ const buffer = Module._malloc(n);
363
+
364
+ const len = 1 * 4; // 1 pointer
365
+ const ptr = Module._malloc(len);
366
+
367
+ Module.stringToUTF8(config.model, buffer, n);
368
+
369
+ Module.setValue(ptr, buffer, 'i8*');
370
+
371
+ return {
372
+ buffer: buffer, ptr: ptr, len: len,
373
+ }
374
+ }
375
+
376
+ function initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config, Module) {
377
+ const n = Module.lengthBytesUTF8(config.model) + 1;
378
+
379
+ const buffer = Module._malloc(n);
380
+
381
+ const len = 1 * 4; // 1 pointer
382
+ const ptr = Module._malloc(len);
383
+
384
+ Module.stringToUTF8(config.model, buffer, n);
385
+
386
+ Module.setValue(ptr, buffer, 'i8*');
387
+
388
+ return {
389
+ buffer: buffer, ptr: ptr, len: len,
390
+ }
391
+ }
392
+
393
+ function initSherpaOnnxOfflineWhisperModelConfig(config, Module) {
394
+ const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
395
+ const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
396
+
397
+ const n = encoderLen + decoderLen;
398
+ const buffer = Module._malloc(n);
399
+
400
+ const len = 2 * 4; // 2 pointers
401
+ const ptr = Module._malloc(len);
402
+
403
+ let offset = 0;
404
+ Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
405
+ offset += encoderLen;
406
+
407
+ Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
408
+
409
+ offset = 0;
410
+ Module.setValue(ptr, buffer + offset, 'i8*');
411
+ offset += encoderLen;
412
+
413
+ Module.setValue(ptr + 4, buffer + offset, 'i8*');
414
+
415
+ return {
416
+ buffer: buffer, ptr: ptr, len: len,
417
+ }
418
+ }
419
+
420
+ function initSherpaOnnxOfflineTdnnModelConfig(config, Module) {
421
+ const n = Module.lengthBytesUTF8(config.model) + 1;
422
+ const buffer = Module._malloc(n);
423
+
424
+ const len = 1 * 4; // 1 pointer
425
+ const ptr = Module._malloc(len);
426
+
427
+ Module.stringToUTF8(config.model, buffer, n);
428
+
429
+ Module.setValue(ptr, buffer, 'i8*');
430
+
431
+ return {
432
+ buffer: buffer, ptr: ptr, len: len,
433
+ }
434
+ }
435
+
436
+ function initSherpaOnnxOfflineLMConfig(config, Module) {
437
+ const n = Module.lengthBytesUTF8(config.model) + 1;
438
+ const buffer = Module._malloc(n);
439
+
440
+ const len = 2 * 4;
441
+ const ptr = Module._malloc(len);
442
+
443
+ Module.stringToUTF8(config.model, buffer, n);
444
+ Module.setValue(ptr, buffer, 'i8*');
445
+ Module.setValue(ptr + 4, config.scale, 'float');
446
+
447
+ return {
448
+ buffer: buffer, ptr: ptr, len: len,
449
+ }
450
+ }
451
+
452
+ function initSherpaOnnxOfflineModelConfig(config, Module) {
453
+ const transducer =
454
+ initSherpaOnnxOfflineTransducerModelConfig(config.transducer, Module);
455
+ const paraformer =
456
+ initSherpaOnnxOfflineParaformerModelConfig(config.paraformer, Module);
457
+ const nemoCtc =
458
+ initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config.nemoCtc, Module);
459
+ const whisper =
460
+ initSherpaOnnxOfflineWhisperModelConfig(config.whisper, Module);
461
+ const tdnn = initSherpaOnnxOfflineTdnnModelConfig(config.tdnn, Module);
462
+
463
+ const len = transducer.len + paraformer.len + nemoCtc.len + whisper.len +
464
+ tdnn.len + 5 * 4;
465
+ const ptr = Module._malloc(len);
466
+
467
+ let offset = 0;
468
+ Module._CopyHeap(transducer.ptr, transducer.len, ptr + offset);
469
+ offset += transducer.len;
470
+
471
+ Module._CopyHeap(paraformer.ptr, paraformer.len, ptr + offset);
472
+ offset += paraformer.len;
473
+
474
+ Module._CopyHeap(nemoCtc.ptr, nemoCtc.len, ptr + offset);
475
+ offset += nemoCtc.len;
476
+
477
+ Module._CopyHeap(whisper.ptr, whisper.len, ptr + offset);
478
+ offset += whisper.len;
479
+
480
+ Module._CopyHeap(tdnn.ptr, tdnn.len, ptr + offset);
481
+ offset += tdnn.len;
482
+
483
+ const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
484
+ const providerLen = Module.lengthBytesUTF8(config.provider) + 1;
485
+ const modelTypeLen = Module.lengthBytesUTF8(config.modelType) + 1;
486
+ const bufferLen = tokensLen + providerLen + modelTypeLen;
487
+ const buffer = Module._malloc(bufferLen);
488
+
489
+ offset = 0;
490
+ Module.stringToUTF8(config.tokens, buffer, tokensLen);
491
+ offset += tokensLen;
492
+
493
+ Module.stringToUTF8(config.provider, buffer + offset, providerLen);
494
+ offset += providerLen;
495
+
496
+ Module.stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
497
+
498
+ offset =
499
+ transducer.len + paraformer.len + nemoCtc.len + whisper.len + tdnn.len;
500
+ Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
501
+ offset += 4;
502
+
503
+ Module.setValue(ptr + offset, config.numThreads, 'i32');
504
+ offset += 4;
505
+
506
+ Module.setValue(ptr + offset, config.debug, 'i32');
507
+ offset += 4;
508
+
509
+ Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
510
+ offset += 4;
511
+
512
+ Module.setValue(
513
+ ptr + offset, buffer + tokensLen + providerLen, 'i8*'); // modelType
514
+ offset += 4;
515
+
516
+ return {
517
+ buffer: buffer, ptr: ptr, len: len, transducer: transducer,
518
+ paraformer: paraformer, nemoCtc: nemoCtc, whisper: whisper, tdnn: tdnn
519
  }
520
+ }
521
+
522
+ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
523
+ const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
524
+ const model = initSherpaOnnxOfflineModelConfig(config.modelConfig, Module);
525
+ const lm = initSherpaOnnxOfflineLMConfig(config.lmConfig, Module);
526
+
527
+ const len = feat.len + model.len + lm.len + 4 * 4;
528
+ const ptr = Module._malloc(len);
529
+
530
+ let offset = 0;
531
+ Module._CopyHeap(feat.ptr, feat.len, ptr + offset);
532
+ offset += feat.len;
533
+
534
+ Module._CopyHeap(model.ptr, model.len, ptr + offset);
535
+ offset += model.len;
536
+
537
+ Module._CopyHeap(lm.ptr, lm.len, ptr + offset);
538
+ offset += lm.len;
539
+
540
+ const decodingMethodLen = Module.lengthBytesUTF8(config.decodingMethod) + 1;
541
+ const hotwordsFileLen = Module.lengthBytesUTF8(config.hotwordsFile) + 1;
542
+ const bufferLen = decodingMethodLen + hotwordsFileLen;
543
+ const buffer = Module._malloc(bufferLen);
544
+
545
+ offset = 0;
546
+ Module.stringToUTF8(config.decodingMethod, buffer, decodingMethodLen);
547
+ offset += decodingMethodLen;
548
+
549
+ Module.stringToUTF8(config.hotwordsFile, buffer + offset, hotwordsFileLen);
550
+
551
+ offset = feat.len + model.len + lm.len;
552
+
553
+ Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
554
+ offset += 4;
555
+
556
+ Module.setValue(ptr + offset, config.maxActivePaths, 'i32');
557
+ offset += 4;
558
 
559
+ Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
560
+ offset += 4;
561
+
562
+ Module.setValue(ptr + offset, config.hotwordsScore, 'float');
563
+ offset += 4;
564
+
565
+ return {
566
+ buffer: buffer, ptr: ptr, len: len, feat: feat, model: model, lm: lm
567
+ }
568
  }
569
 
570
+ class OfflineStream {
571
+ constructor(handle, Module) {
572
+ this.handle = handle;
573
+ this.Module = Module;
574
+ }
575
+
576
+ free() {
577
+ if (this.handle) {
578
+ this.Module._DestroyOfflineStream(this.handle);
579
+ this.handle = null;
580
+ }
581
+ }
582
+
583
+ /**
584
+ * @param sampleRate {Number}
585
+ * @param samples {Float32Array} Containing samples in the range [-1, 1]
586
+ */
587
+ acceptWaveform(sampleRate, samples) {
588
+ const pointer =
589
+ this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
590
+ this.Module.HEAPF32.set(samples, pointer / samples.BYTES_PER_ELEMENT);
591
+ this.Module._AcceptWaveformOffline(
592
+ this.handle, sampleRate, pointer, samples.length);
593
+ this.Module._free(pointer);
594
+ }
595
+ };
596
+
597
+ class OfflineRecognizer {
598
+ constructor(configObj, Module) {
599
+ this.config = configObj;
600
+ const config = initSherpaOnnxOfflineRecognizerConfig(configObj, Module);
601
+ const handle = Module._CreateOfflineRecognizer(config.ptr);
602
+ freeConfig(config, Module);
603
+
604
+ this.handle = handle;
605
+ this.Module = Module;
606
+ }
607
+
608
+ free() {
609
+ this.Module._DestroyOfflineRecognizer(this.handle);
610
+ this.handle = 0
611
+ }
612
+
613
+ createStream() {
614
+ const handle = this.Module._CreateOfflineStream(this.handle);
615
+ return new OfflineStream(handle, this.Module);
616
+ }
617
+
618
+ decode(stream) {
619
+ this.Module._DecodeOfflineStream(this.handle, stream.handle);
620
+ }
621
+
622
+ getResult(stream) {
623
+ const r = this.Module._GetOfflineStreamResult(stream.handle);
624
+
625
+ const textPtr = this.Module.getValue(r, 'i8*');
626
+ const text = this.Module.UTF8ToString(textPtr);
627
+
628
+ this.Module._DestroyOfflineRecognizerResult(r);
629
+ return text;
630
+ }
631
+ };
632
+
633
  class OnlineStream {
634
+ constructor(handle, Module) {
635
  this.handle = handle;
636
  this.pointer = null; // buffer
637
  this.n = 0; // buffer size
638
+ this.Module = Module;
639
  }
640
 
641
  free() {
642
  if (this.handle) {
643
+ this.Module._DestroyOnlineStream(this.handle);
644
  this.handle = null;
645
+ this.Module._free(this.pointer);
646
  this.pointer = null;
647
  this.n = 0;
648
  }
 
654
  */
655
  acceptWaveform(sampleRate, samples) {
656
  if (this.n < samples.length) {
657
+ this.Module._free(this.pointer);
658
+ this.pointer =
659
+ this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
660
  this.n = samples.length
661
  }
662
 
663
+ this.Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
664
+ this.Module._AcceptWaveform(
665
+ this.handle, sampleRate, this.pointer, samples.length);
666
  }
667
 
668
  inputFinished() {
669
+ this.Module._InputFinished(this.handle);
670
  }
671
  };
672
 
673
  class OnlineRecognizer {
674
+ constructor(configObj, Module) {
675
+ this.config = configObj;
676
+ const config = initSherpaOnnxOnlineRecognizerConfig(configObj, Module)
677
+ const handle = Module._CreateOnlineRecognizer(config.ptr);
678
 
679
+ freeConfig(config, Module);
680
 
681
  this.handle = handle;
682
+ this.Module = Module;
683
  }
684
 
685
  free() {
686
+ this.Module._DestroyOnlineRecognizer(this.handle);
687
  this.handle = 0
688
  }
689
 
690
  createStream() {
691
+ const handle = this.Module._CreateOnlineStream(this.handle);
692
+ return new OnlineStream(handle, this.Module);
693
  }
694
 
695
  isReady(stream) {
696
+ return this.Module._IsOnlineStreamReady(this.handle, stream.handle) == 1;
697
  }
698
 
699
  decode(stream) {
700
+ this.Module._DecodeOnlineStream(this.handle, stream.handle);
701
  }
702
 
703
  isEndpoint(stream) {
704
+ return this.Module._IsEndpoint(this.handle, stream.handle) == 1;
705
  }
706
 
707
  reset(stream) {
708
+ this.Module._Reset(this.handle, stream.handle);
709
  }
710
 
711
  getResult(stream) {
712
+ const r = this.Module._GetOnlineStreamResult(this.handle, stream.handle);
713
+ const textPtr = this.Module.getValue(r, 'i8*');
714
+ const text = this.Module.UTF8ToString(textPtr);
715
+ this.Module._DestroyOnlineRecognizerResult(r);
716
  return text;
717
  }
718
  }
719
+
720
+ if (typeof process == 'object' && typeof process.versions == 'object' &&
721
+ typeof process.versions.node == 'string') {
722
+ module.exports = {
723
+ createOnlineRecognizer,
724
+ OfflineRecognizer,
725
+ };
726
+ }
sherpa-onnx-wasm-main-asr.js CHANGED
The diff for this file is too large to render. See raw diff
 
sherpa-onnx-wasm-main-asr.wasm CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:4b63023e34e23e2b57373419f6a27a430a910da1ca2c71dcc7627eba750b3d01
3
- size 10603197
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1a0203bb9a961f60296accef1575081f71ebead089bcb86953a55597fb6c364
3
+ size 10606252