Commit
·
41111d1
1
Parent(s):
3fb0f88
update model
Browse files- sherpa-onnx-asr.js +222 -73
- sherpa-onnx-wasm-main-asr.js +0 -0
sherpa-onnx-asr.js
CHANGED
|
@@ -39,6 +39,10 @@ function freeConfig(config, Module) {
|
|
| 39 |
freeConfig(config.tdnn, Module)
|
| 40 |
}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if ('lm' in config) {
|
| 43 |
freeConfig(config.lm, Module)
|
| 44 |
}
|
|
@@ -52,9 +56,9 @@ function freeConfig(config, Module) {
|
|
| 52 |
|
| 53 |
// The user should free the returned pointers
|
| 54 |
function initSherpaOnnxOnlineTransducerModelConfig(config, Module) {
|
| 55 |
-
const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
|
| 56 |
-
const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
|
| 57 |
-
const joinerLen = Module.lengthBytesUTF8(config.joiner) + 1;
|
| 58 |
|
| 59 |
const n = encoderLen + decoderLen + joinerLen;
|
| 60 |
|
|
@@ -64,13 +68,13 @@ function initSherpaOnnxOnlineTransducerModelConfig(config, Module) {
|
|
| 64 |
const ptr = Module._malloc(len);
|
| 65 |
|
| 66 |
let offset = 0;
|
| 67 |
-
Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
|
| 68 |
offset += encoderLen;
|
| 69 |
|
| 70 |
-
Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
|
| 71 |
offset += decoderLen;
|
| 72 |
|
| 73 |
-
Module.stringToUTF8(config.joiner, buffer + offset, joinerLen);
|
| 74 |
|
| 75 |
offset = 0;
|
| 76 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
@@ -87,8 +91,8 @@ function initSherpaOnnxOnlineTransducerModelConfig(config, Module) {
|
|
| 87 |
}
|
| 88 |
|
| 89 |
function initSherpaOnnxOnlineParaformerModelConfig(config, Module) {
|
| 90 |
-
const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
|
| 91 |
-
const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
|
| 92 |
|
| 93 |
const n = encoderLen + decoderLen;
|
| 94 |
const buffer = Module._malloc(n);
|
|
@@ -97,10 +101,10 @@ function initSherpaOnnxOnlineParaformerModelConfig(config, Module) {
|
|
| 97 |
const ptr = Module._malloc(len);
|
| 98 |
|
| 99 |
let offset = 0;
|
| 100 |
-
Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
|
| 101 |
offset += encoderLen;
|
| 102 |
|
| 103 |
-
Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
|
| 104 |
|
| 105 |
offset = 0;
|
| 106 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
@@ -114,13 +118,13 @@ function initSherpaOnnxOnlineParaformerModelConfig(config, Module) {
|
|
| 114 |
}
|
| 115 |
|
| 116 |
function initSherpaOnnxOnlineZipformer2CtcModelConfig(config, Module) {
|
| 117 |
-
const n = Module.lengthBytesUTF8(config.model) + 1;
|
| 118 |
const buffer = Module._malloc(n);
|
| 119 |
|
| 120 |
const len = 1 * 4; // 1 pointer
|
| 121 |
const ptr = Module._malloc(len);
|
| 122 |
|
| 123 |
-
Module.stringToUTF8(config.model, buffer, n);
|
| 124 |
|
| 125 |
Module.setValue(ptr, buffer, 'i8*');
|
| 126 |
|
|
@@ -130,10 +134,33 @@ function initSherpaOnnxOnlineZipformer2CtcModelConfig(config, Module) {
|
|
| 130 |
}
|
| 131 |
|
| 132 |
function initSherpaOnnxOnlineModelConfig(config, Module) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
const transducer =
|
| 134 |
initSherpaOnnxOnlineTransducerModelConfig(config.transducer, Module);
|
|
|
|
| 135 |
const paraformer =
|
| 136 |
initSherpaOnnxOnlineParaformerModelConfig(config.paraformer, Module);
|
|
|
|
| 137 |
const ctc = initSherpaOnnxOnlineZipformer2CtcModelConfig(
|
| 138 |
config.zipformer2Ctc, Module);
|
| 139 |
|
|
@@ -150,9 +177,9 @@ function initSherpaOnnxOnlineModelConfig(config, Module) {
|
|
| 150 |
Module._CopyHeap(ctc.ptr, ctc.len, ptr + offset);
|
| 151 |
offset += ctc.len;
|
| 152 |
|
| 153 |
-
const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
|
| 154 |
-
const providerLen = Module.lengthBytesUTF8(config.provider) + 1;
|
| 155 |
-
const modelTypeLen = Module.lengthBytesUTF8(config.modelType) + 1;
|
| 156 |
const modelingUnitLen = Module.lengthBytesUTF8(config.modelingUnit || '') + 1;
|
| 157 |
const bpeVocabLen = Module.lengthBytesUTF8(config.bpeVocab || '') + 1;
|
| 158 |
|
|
@@ -161,13 +188,13 @@ function initSherpaOnnxOnlineModelConfig(config, Module) {
|
|
| 161 |
const buffer = Module._malloc(bufferLen);
|
| 162 |
|
| 163 |
offset = 0;
|
| 164 |
-
Module.stringToUTF8(config.tokens, buffer, tokensLen);
|
| 165 |
offset += tokensLen;
|
| 166 |
|
| 167 |
-
Module.stringToUTF8(config.provider, buffer + offset, providerLen);
|
| 168 |
offset += providerLen;
|
| 169 |
|
| 170 |
-
Module.stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
|
| 171 |
offset += modelTypeLen;
|
| 172 |
|
| 173 |
Module.stringToUTF8(
|
|
@@ -181,13 +208,13 @@ function initSherpaOnnxOnlineModelConfig(config, Module) {
|
|
| 181 |
Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
|
| 182 |
offset += 4;
|
| 183 |
|
| 184 |
-
Module.setValue(ptr + offset, config.numThreads, 'i32');
|
| 185 |
offset += 4;
|
| 186 |
|
| 187 |
Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
|
| 188 |
offset += 4;
|
| 189 |
|
| 190 |
-
Module.setValue(ptr + offset, config.debug, 'i32');
|
| 191 |
offset += 4;
|
| 192 |
|
| 193 |
Module.setValue(
|
|
@@ -215,8 +242,8 @@ function initSherpaOnnxFeatureConfig(config, Module) {
|
|
| 215 |
const len = 2 * 4; // 2 pointers
|
| 216 |
const ptr = Module._malloc(len);
|
| 217 |
|
| 218 |
-
Module.setValue(ptr, config.sampleRate, 'i32');
|
| 219 |
-
Module.setValue(ptr + 4, config.featureDim, 'i32');
|
| 220 |
return {ptr: ptr, len: len};
|
| 221 |
}
|
| 222 |
|
|
@@ -224,16 +251,30 @@ function initSherpaOnnxOnlineCtcFstDecoderConfig(config, Module) {
|
|
| 224 |
const len = 2 * 4;
|
| 225 |
const ptr = Module._malloc(len);
|
| 226 |
|
| 227 |
-
const graphLen = Module.lengthBytesUTF8(config.graph) + 1;
|
| 228 |
const buffer = Module._malloc(graphLen);
|
| 229 |
Module.stringToUTF8(config.graph, buffer, graphLen);
|
| 230 |
|
| 231 |
Module.setValue(ptr, buffer, 'i8*');
|
| 232 |
-
Module.setValue(ptr + 4, config.maxActive, 'i32');
|
| 233 |
return {ptr: ptr, len: len, buffer: buffer};
|
| 234 |
}
|
| 235 |
|
| 236 |
function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
| 238 |
const model = initSherpaOnnxOnlineModelConfig(config.modelConfig, Module);
|
| 239 |
const ctcFstDecoder = initSherpaOnnxOnlineCtcFstDecoderConfig(
|
|
@@ -249,8 +290,9 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
|
| 249 |
Module._CopyHeap(model.ptr, model.len, ptr + offset);
|
| 250 |
offset += model.len;
|
| 251 |
|
| 252 |
-
const decodingMethodLen =
|
| 253 |
-
|
|
|
|
| 254 |
const ruleFstsFileLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
|
| 255 |
const ruleFarsFileLen = Module.lengthBytesUTF8(config.ruleFars || '') + 1;
|
| 256 |
const bufferLen =
|
|
@@ -258,10 +300,12 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
|
| 258 |
const buffer = Module._malloc(bufferLen);
|
| 259 |
|
| 260 |
offset = 0;
|
| 261 |
-
Module.stringToUTF8(
|
|
|
|
| 262 |
offset += decodingMethodLen;
|
| 263 |
|
| 264 |
-
Module.stringToUTF8(
|
|
|
|
| 265 |
offset += hotwordsFileLen;
|
| 266 |
|
| 267 |
Module.stringToUTF8(config.ruleFsts || '', buffer + offset, ruleFstsFileLen);
|
|
@@ -274,25 +318,25 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
|
| 274 |
Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
|
| 275 |
offset += 4;
|
| 276 |
|
| 277 |
-
Module.setValue(ptr + offset, config.maxActivePaths, 'i32');
|
| 278 |
offset += 4;
|
| 279 |
|
| 280 |
-
Module.setValue(ptr + offset, config.enableEndpoint, 'i32');
|
| 281 |
offset += 4;
|
| 282 |
|
| 283 |
-
Module.setValue(ptr + offset, config.rule1MinTrailingSilence, 'float');
|
| 284 |
offset += 4;
|
| 285 |
|
| 286 |
-
Module.setValue(ptr + offset, config.rule2MinTrailingSilence, 'float');
|
| 287 |
offset += 4;
|
| 288 |
|
| 289 |
-
Module.setValue(ptr + offset, config.rule3MinUtteranceLength, 'float');
|
| 290 |
offset += 4;
|
| 291 |
|
| 292 |
Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
|
| 293 |
offset += 4;
|
| 294 |
|
| 295 |
-
Module.setValue(ptr + offset, config.hotwordsScore, 'float');
|
| 296 |
offset += 4;
|
| 297 |
|
| 298 |
Module._CopyHeap(ctcFstDecoder.ptr, ctcFstDecoder.len, ptr + offset);
|
|
@@ -313,7 +357,6 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
|
| 313 |
}
|
| 314 |
}
|
| 315 |
|
| 316 |
-
|
| 317 |
function createOnlineRecognizer(Module, myConfig) {
|
| 318 |
const onlineTransducerModelConfig = {
|
| 319 |
encoder: '',
|
|
@@ -395,9 +438,9 @@ function createOnlineRecognizer(Module, myConfig) {
|
|
| 395 |
}
|
| 396 |
|
| 397 |
function initSherpaOnnxOfflineTransducerModelConfig(config, Module) {
|
| 398 |
-
const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
|
| 399 |
-
const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
|
| 400 |
-
const joinerLen = Module.lengthBytesUTF8(config.joiner) + 1;
|
| 401 |
|
| 402 |
const n = encoderLen + decoderLen + joinerLen;
|
| 403 |
|
|
@@ -407,13 +450,13 @@ function initSherpaOnnxOfflineTransducerModelConfig(config, Module) {
|
|
| 407 |
const ptr = Module._malloc(len);
|
| 408 |
|
| 409 |
let offset = 0;
|
| 410 |
-
Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
|
| 411 |
offset += encoderLen;
|
| 412 |
|
| 413 |
-
Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
|
| 414 |
offset += decoderLen;
|
| 415 |
|
| 416 |
-
Module.stringToUTF8(config.joiner, buffer + offset, joinerLen);
|
| 417 |
|
| 418 |
offset = 0;
|
| 419 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
@@ -430,14 +473,14 @@ function initSherpaOnnxOfflineTransducerModelConfig(config, Module) {
|
|
| 430 |
}
|
| 431 |
|
| 432 |
function initSherpaOnnxOfflineParaformerModelConfig(config, Module) {
|
| 433 |
-
const n = Module.lengthBytesUTF8(config.model) + 1;
|
| 434 |
|
| 435 |
const buffer = Module._malloc(n);
|
| 436 |
|
| 437 |
const len = 1 * 4; // 1 pointer
|
| 438 |
const ptr = Module._malloc(len);
|
| 439 |
|
| 440 |
-
Module.stringToUTF8(config.model, buffer, n);
|
| 441 |
|
| 442 |
Module.setValue(ptr, buffer, 'i8*');
|
| 443 |
|
|
@@ -447,14 +490,14 @@ function initSherpaOnnxOfflineParaformerModelConfig(config, Module) {
|
|
| 447 |
}
|
| 448 |
|
| 449 |
function initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config, Module) {
|
| 450 |
-
const n = Module.lengthBytesUTF8(config.model) + 1;
|
| 451 |
|
| 452 |
const buffer = Module._malloc(n);
|
| 453 |
|
| 454 |
const len = 1 * 4; // 1 pointer
|
| 455 |
const ptr = Module._malloc(len);
|
| 456 |
|
| 457 |
-
Module.stringToUTF8(config.model, buffer, n);
|
| 458 |
|
| 459 |
Module.setValue(ptr, buffer, 'i8*');
|
| 460 |
|
|
@@ -464,10 +507,10 @@ function initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config, Module) {
|
|
| 464 |
}
|
| 465 |
|
| 466 |
function initSherpaOnnxOfflineWhisperModelConfig(config, Module) {
|
| 467 |
-
const encoderLen = Module.lengthBytesUTF8(config.encoder) + 1;
|
| 468 |
-
const decoderLen = Module.lengthBytesUTF8(config.decoder) + 1;
|
| 469 |
-
const languageLen = Module.lengthBytesUTF8(config.language) + 1;
|
| 470 |
-
const taskLen = Module.lengthBytesUTF8(config.task) + 1;
|
| 471 |
|
| 472 |
const n = encoderLen + decoderLen + languageLen + taskLen;
|
| 473 |
const buffer = Module._malloc(n);
|
|
@@ -476,16 +519,16 @@ function initSherpaOnnxOfflineWhisperModelConfig(config, Module) {
|
|
| 476 |
const ptr = Module._malloc(len);
|
| 477 |
|
| 478 |
let offset = 0;
|
| 479 |
-
Module.stringToUTF8(config.encoder, buffer + offset, encoderLen);
|
| 480 |
offset += encoderLen;
|
| 481 |
|
| 482 |
-
Module.stringToUTF8(config.decoder, buffer + offset, decoderLen);
|
| 483 |
offset += decoderLen;
|
| 484 |
|
| 485 |
-
Module.stringToUTF8(config.language, buffer + offset, languageLen);
|
| 486 |
offset += languageLen;
|
| 487 |
|
| 488 |
-
Module.stringToUTF8(config.task, buffer + offset, taskLen);
|
| 489 |
|
| 490 |
offset = 0;
|
| 491 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
@@ -508,13 +551,13 @@ function initSherpaOnnxOfflineWhisperModelConfig(config, Module) {
|
|
| 508 |
}
|
| 509 |
|
| 510 |
function initSherpaOnnxOfflineTdnnModelConfig(config, Module) {
|
| 511 |
-
const n = Module.lengthBytesUTF8(config.model) + 1;
|
| 512 |
const buffer = Module._malloc(n);
|
| 513 |
|
| 514 |
const len = 1 * 4; // 1 pointer
|
| 515 |
const ptr = Module._malloc(len);
|
| 516 |
|
| 517 |
-
Module.stringToUTF8(config.model, buffer, n);
|
| 518 |
|
| 519 |
Module.setValue(ptr, buffer, 'i8*');
|
| 520 |
|
|
@@ -523,16 +566,48 @@ function initSherpaOnnxOfflineTdnnModelConfig(config, Module) {
|
|
| 523 |
}
|
| 524 |
}
|
| 525 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
function initSherpaOnnxOfflineLMConfig(config, Module) {
|
| 527 |
-
const n = Module.lengthBytesUTF8(config.model) + 1;
|
| 528 |
const buffer = Module._malloc(n);
|
| 529 |
|
| 530 |
const len = 2 * 4;
|
| 531 |
const ptr = Module._malloc(len);
|
| 532 |
|
| 533 |
-
Module.stringToUTF8(config.model, buffer, n);
|
| 534 |
Module.setValue(ptr, buffer, 'i8*');
|
| 535 |
-
Module.setValue(ptr + 4, config.scale, 'float');
|
| 536 |
|
| 537 |
return {
|
| 538 |
buffer: buffer, ptr: ptr, len: len,
|
|
@@ -540,18 +615,70 @@ function initSherpaOnnxOfflineLMConfig(config, Module) {
|
|
| 540 |
}
|
| 541 |
|
| 542 |
function initSherpaOnnxOfflineModelConfig(config, Module) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
const transducer =
|
| 544 |
initSherpaOnnxOfflineTransducerModelConfig(config.transducer, Module);
|
|
|
|
| 545 |
const paraformer =
|
| 546 |
initSherpaOnnxOfflineParaformerModelConfig(config.paraformer, Module);
|
|
|
|
| 547 |
const nemoCtc =
|
| 548 |
initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config.nemoCtc, Module);
|
|
|
|
| 549 |
const whisper =
|
| 550 |
initSherpaOnnxOfflineWhisperModelConfig(config.whisper, Module);
|
|
|
|
| 551 |
const tdnn = initSherpaOnnxOfflineTdnnModelConfig(config.tdnn, Module);
|
| 552 |
|
|
|
|
|
|
|
|
|
|
| 553 |
const len = transducer.len + paraformer.len + nemoCtc.len + whisper.len +
|
| 554 |
-
tdnn.len + 8 * 4;
|
|
|
|
| 555 |
const ptr = Module._malloc(len);
|
| 556 |
|
| 557 |
let offset = 0;
|
|
@@ -570,9 +697,10 @@ function initSherpaOnnxOfflineModelConfig(config, Module) {
|
|
| 570 |
Module._CopyHeap(tdnn.ptr, tdnn.len, ptr + offset);
|
| 571 |
offset += tdnn.len;
|
| 572 |
|
| 573 |
-
|
| 574 |
-
const
|
| 575 |
-
const
|
|
|
|
| 576 |
const modelingUnitLen = Module.lengthBytesUTF8(config.modelingUnit || '') + 1;
|
| 577 |
const bpeVocabLen = Module.lengthBytesUTF8(config.bpeVocab || '') + 1;
|
| 578 |
const teleSpeechCtcLen =
|
|
@@ -580,16 +708,17 @@ function initSherpaOnnxOfflineModelConfig(config, Module) {
|
|
| 580 |
|
| 581 |
const bufferLen = tokensLen + providerLen + modelTypeLen + modelingUnitLen +
|
| 582 |
bpeVocabLen + teleSpeechCtcLen;
|
|
|
|
| 583 |
const buffer = Module._malloc(bufferLen);
|
| 584 |
|
| 585 |
offset = 0;
|
| 586 |
Module.stringToUTF8(config.tokens, buffer, tokensLen);
|
| 587 |
offset += tokensLen;
|
| 588 |
|
| 589 |
-
Module.stringToUTF8(config.provider, buffer + offset, providerLen);
|
| 590 |
offset += providerLen;
|
| 591 |
|
| 592 |
-
Module.stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
|
| 593 |
offset += modelTypeLen;
|
| 594 |
|
| 595 |
Module.stringToUTF8(
|
|
@@ -608,10 +737,10 @@ function initSherpaOnnxOfflineModelConfig(config, Module) {
|
|
| 608 |
Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
|
| 609 |
offset += 4;
|
| 610 |
|
| 611 |
-
Module.setValue(ptr + offset, config.numThreads, 'i32');
|
| 612 |
offset += 4;
|
| 613 |
|
| 614 |
-
Module.setValue(ptr + offset, config.debug, 'i32');
|
| 615 |
offset += 4;
|
| 616 |
|
| 617 |
Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
|
|
@@ -639,13 +768,30 @@ function initSherpaOnnxOfflineModelConfig(config, Module) {
|
|
| 639 |
'i8*'); // teleSpeechCtc
|
| 640 |
offset += 4;
|
| 641 |
|
|
|
|
|
|
|
| 642 |
return {
|
| 643 |
buffer: buffer, ptr: ptr, len: len, transducer: transducer,
|
| 644 |
-
paraformer: paraformer, nemoCtc: nemoCtc, whisper: whisper, tdnn: tdnn
|
|
|
|
| 645 |
}
|
| 646 |
}
|
| 647 |
|
| 648 |
function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
| 650 |
const model = initSherpaOnnxOfflineModelConfig(config.modelConfig, Module);
|
| 651 |
const lm = initSherpaOnnxOfflineLMConfig(config.lmConfig, Module);
|
|
@@ -663,8 +809,9 @@ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
|
| 663 |
Module._CopyHeap(lm.ptr, lm.len, ptr + offset);
|
| 664 |
offset += lm.len;
|
| 665 |
|
| 666 |
-
const decodingMethodLen =
|
| 667 |
-
|
|
|
|
| 668 |
const ruleFstsLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
|
| 669 |
const ruleFarsLen = Module.lengthBytesUTF8(config.ruleFars || '') + 1;
|
| 670 |
const bufferLen =
|
|
@@ -672,10 +819,12 @@ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
|
| 672 |
const buffer = Module._malloc(bufferLen);
|
| 673 |
|
| 674 |
offset = 0;
|
| 675 |
-
Module.stringToUTF8(
|
|
|
|
| 676 |
offset += decodingMethodLen;
|
| 677 |
|
| 678 |
-
Module.stringToUTF8(
|
|
|
|
| 679 |
offset += hotwordsFileLen;
|
| 680 |
|
| 681 |
Module.stringToUTF8(config.ruleFsts || '', buffer + offset, ruleFstsLen);
|
|
@@ -689,13 +838,13 @@ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
|
| 689 |
Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
|
| 690 |
offset += 4;
|
| 691 |
|
| 692 |
-
Module.setValue(ptr + offset, config.maxActivePaths, 'i32');
|
| 693 |
offset += 4;
|
| 694 |
|
| 695 |
Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
|
| 696 |
offset += 4;
|
| 697 |
|
| 698 |
-
Module.setValue(ptr + offset, config.hotwordsScore, 'float');
|
| 699 |
offset += 4;
|
| 700 |
|
| 701 |
Module.setValue(
|
|
|
|
| 39 |
freeConfig(config.tdnn, Module)
|
| 40 |
}
|
| 41 |
|
| 42 |
+
if ('senseVoice' in config) {
|
| 43 |
+
freeConfig(config.senseVoice, Module)
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
if ('lm' in config) {
|
| 47 |
freeConfig(config.lm, Module)
|
| 48 |
}
|
|
|
|
| 56 |
|
| 57 |
// The user should free the returned pointers
|
| 58 |
function initSherpaOnnxOnlineTransducerModelConfig(config, Module) {
|
| 59 |
+
const encoderLen = Module.lengthBytesUTF8(config.encoder || '') + 1;
|
| 60 |
+
const decoderLen = Module.lengthBytesUTF8(config.decoder || '') + 1;
|
| 61 |
+
const joinerLen = Module.lengthBytesUTF8(config.joiner || '') + 1;
|
| 62 |
|
| 63 |
const n = encoderLen + decoderLen + joinerLen;
|
| 64 |
|
|
|
|
| 68 |
const ptr = Module._malloc(len);
|
| 69 |
|
| 70 |
let offset = 0;
|
| 71 |
+
Module.stringToUTF8(config.encoder || '', buffer + offset, encoderLen);
|
| 72 |
offset += encoderLen;
|
| 73 |
|
| 74 |
+
Module.stringToUTF8(config.decoder || '', buffer + offset, decoderLen);
|
| 75 |
offset += decoderLen;
|
| 76 |
|
| 77 |
+
Module.stringToUTF8(config.joiner || '', buffer + offset, joinerLen);
|
| 78 |
|
| 79 |
offset = 0;
|
| 80 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
function initSherpaOnnxOnlineParaformerModelConfig(config, Module) {
|
| 94 |
+
const encoderLen = Module.lengthBytesUTF8(config.encoder || '') + 1;
|
| 95 |
+
const decoderLen = Module.lengthBytesUTF8(config.decoder || '') + 1;
|
| 96 |
|
| 97 |
const n = encoderLen + decoderLen;
|
| 98 |
const buffer = Module._malloc(n);
|
|
|
|
| 101 |
const ptr = Module._malloc(len);
|
| 102 |
|
| 103 |
let offset = 0;
|
| 104 |
+
Module.stringToUTF8(config.encoder || '', buffer + offset, encoderLen);
|
| 105 |
offset += encoderLen;
|
| 106 |
|
| 107 |
+
Module.stringToUTF8(config.decoder || '', buffer + offset, decoderLen);
|
| 108 |
|
| 109 |
offset = 0;
|
| 110 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
function initSherpaOnnxOnlineZipformer2CtcModelConfig(config, Module) {
|
| 121 |
+
const n = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 122 |
const buffer = Module._malloc(n);
|
| 123 |
|
| 124 |
const len = 1 * 4; // 1 pointer
|
| 125 |
const ptr = Module._malloc(len);
|
| 126 |
|
| 127 |
+
Module.stringToUTF8(config.model || '', buffer, n);
|
| 128 |
|
| 129 |
Module.setValue(ptr, buffer, 'i8*');
|
| 130 |
|
|
|
|
| 134 |
}
|
| 135 |
|
| 136 |
function initSherpaOnnxOnlineModelConfig(config, Module) {
|
| 137 |
+
if (!('transducer' in config)) {
|
| 138 |
+
config.transducer = {
|
| 139 |
+
encoder: '',
|
| 140 |
+
decoder: '',
|
| 141 |
+
joiner: '',
|
| 142 |
+
};
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
if (!('paraformer' in config)) {
|
| 146 |
+
config.paraformer = {
|
| 147 |
+
encoder: '',
|
| 148 |
+
decoder: '',
|
| 149 |
+
};
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
if (!('zipformer2Ctc' in config)) {
|
| 153 |
+
config.zipformer2Ctc = {
|
| 154 |
+
model: '',
|
| 155 |
+
};
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
const transducer =
|
| 159 |
initSherpaOnnxOnlineTransducerModelConfig(config.transducer, Module);
|
| 160 |
+
|
| 161 |
const paraformer =
|
| 162 |
initSherpaOnnxOnlineParaformerModelConfig(config.paraformer, Module);
|
| 163 |
+
|
| 164 |
const ctc = initSherpaOnnxOnlineZipformer2CtcModelConfig(
|
| 165 |
config.zipformer2Ctc, Module);
|
| 166 |
|
|
|
|
| 177 |
Module._CopyHeap(ctc.ptr, ctc.len, ptr + offset);
|
| 178 |
offset += ctc.len;
|
| 179 |
|
| 180 |
+
const tokensLen = Module.lengthBytesUTF8(config.tokens || '') + 1;
|
| 181 |
+
const providerLen = Module.lengthBytesUTF8(config.provider || 'cpu') + 1;
|
| 182 |
+
const modelTypeLen = Module.lengthBytesUTF8(config.modelType || '') + 1;
|
| 183 |
const modelingUnitLen = Module.lengthBytesUTF8(config.modelingUnit || '') + 1;
|
| 184 |
const bpeVocabLen = Module.lengthBytesUTF8(config.bpeVocab || '') + 1;
|
| 185 |
|
|
|
|
| 188 |
const buffer = Module._malloc(bufferLen);
|
| 189 |
|
| 190 |
offset = 0;
|
| 191 |
+
Module.stringToUTF8(config.tokens || '', buffer, tokensLen);
|
| 192 |
offset += tokensLen;
|
| 193 |
|
| 194 |
+
Module.stringToUTF8(config.provider || 'cpu', buffer + offset, providerLen);
|
| 195 |
offset += providerLen;
|
| 196 |
|
| 197 |
+
Module.stringToUTF8(config.modelType || '', buffer + offset, modelTypeLen);
|
| 198 |
offset += modelTypeLen;
|
| 199 |
|
| 200 |
Module.stringToUTF8(
|
|
|
|
| 208 |
Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
|
| 209 |
offset += 4;
|
| 210 |
|
| 211 |
+
Module.setValue(ptr + offset, config.numThreads || 1, 'i32');
|
| 212 |
offset += 4;
|
| 213 |
|
| 214 |
Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
|
| 215 |
offset += 4;
|
| 216 |
|
| 217 |
+
Module.setValue(ptr + offset, config.debug || 0, 'i32');
|
| 218 |
offset += 4;
|
| 219 |
|
| 220 |
Module.setValue(
|
|
|
|
| 242 |
const len = 2 * 4; // 2 pointers
|
| 243 |
const ptr = Module._malloc(len);
|
| 244 |
|
| 245 |
+
Module.setValue(ptr, config.sampleRate || 16000, 'i32');
|
| 246 |
+
Module.setValue(ptr + 4, config.featureDim || 80, 'i32');
|
| 247 |
return {ptr: ptr, len: len};
|
| 248 |
}
|
| 249 |
|
|
|
|
| 251 |
const len = 2 * 4;
|
| 252 |
const ptr = Module._malloc(len);
|
| 253 |
|
| 254 |
+
const graphLen = Module.lengthBytesUTF8(config.graph || '') + 1;
|
| 255 |
const buffer = Module._malloc(graphLen);
|
| 256 |
Module.stringToUTF8(config.graph, buffer, graphLen);
|
| 257 |
|
| 258 |
Module.setValue(ptr, buffer, 'i8*');
|
| 259 |
+
Module.setValue(ptr + 4, config.maxActive || 3000, 'i32');
|
| 260 |
return {ptr: ptr, len: len, buffer: buffer};
|
| 261 |
}
|
| 262 |
|
| 263 |
function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
| 264 |
+
if (!('featConfig' in config)) {
|
| 265 |
+
config.featConfig = {
|
| 266 |
+
sampleRate: 16000,
|
| 267 |
+
featureDim: 80,
|
| 268 |
+
};
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
if (!('ctcFstDecoderConfig' in config)) {
|
| 272 |
+
config.ctcFstDecoderConfig = {
|
| 273 |
+
graph: '',
|
| 274 |
+
maxActive: 3000,
|
| 275 |
+
};
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
| 279 |
const model = initSherpaOnnxOnlineModelConfig(config.modelConfig, Module);
|
| 280 |
const ctcFstDecoder = initSherpaOnnxOnlineCtcFstDecoderConfig(
|
|
|
|
| 290 |
Module._CopyHeap(model.ptr, model.len, ptr + offset);
|
| 291 |
offset += model.len;
|
| 292 |
|
| 293 |
+
const decodingMethodLen =
|
| 294 |
+
Module.lengthBytesUTF8(config.decodingMethod || 'greedy_search') + 1;
|
| 295 |
+
const hotwordsFileLen = Module.lengthBytesUTF8(config.hotwordsFile || '') + 1;
|
| 296 |
const ruleFstsFileLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
|
| 297 |
const ruleFarsFileLen = Module.lengthBytesUTF8(config.ruleFars || '') + 1;
|
| 298 |
const bufferLen =
|
|
|
|
| 300 |
const buffer = Module._malloc(bufferLen);
|
| 301 |
|
| 302 |
offset = 0;
|
| 303 |
+
Module.stringToUTF8(
|
| 304 |
+
config.decodingMethod || 'greedy_search', buffer, decodingMethodLen);
|
| 305 |
offset += decodingMethodLen;
|
| 306 |
|
| 307 |
+
Module.stringToUTF8(
|
| 308 |
+
config.hotwordsFile || '', buffer + offset, hotwordsFileLen);
|
| 309 |
offset += hotwordsFileLen;
|
| 310 |
|
| 311 |
Module.stringToUTF8(config.ruleFsts || '', buffer + offset, ruleFstsFileLen);
|
|
|
|
| 318 |
Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
|
| 319 |
offset += 4;
|
| 320 |
|
| 321 |
+
Module.setValue(ptr + offset, config.maxActivePaths || 4, 'i32');
|
| 322 |
offset += 4;
|
| 323 |
|
| 324 |
+
Module.setValue(ptr + offset, config.enableEndpoint || 0, 'i32');
|
| 325 |
offset += 4;
|
| 326 |
|
| 327 |
+
Module.setValue(ptr + offset, config.rule1MinTrailingSilence || 2.4, 'float');
|
| 328 |
offset += 4;
|
| 329 |
|
| 330 |
+
Module.setValue(ptr + offset, config.rule2MinTrailingSilence || 1.2, 'float');
|
| 331 |
offset += 4;
|
| 332 |
|
| 333 |
+
Module.setValue(ptr + offset, config.rule3MinUtteranceLength || 20, 'float');
|
| 334 |
offset += 4;
|
| 335 |
|
| 336 |
Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
|
| 337 |
offset += 4;
|
| 338 |
|
| 339 |
+
Module.setValue(ptr + offset, config.hotwordsScore || 1.5, 'float');
|
| 340 |
offset += 4;
|
| 341 |
|
| 342 |
Module._CopyHeap(ctcFstDecoder.ptr, ctcFstDecoder.len, ptr + offset);
|
|
|
|
| 357 |
}
|
| 358 |
}
|
| 359 |
|
|
|
|
| 360 |
function createOnlineRecognizer(Module, myConfig) {
|
| 361 |
const onlineTransducerModelConfig = {
|
| 362 |
encoder: '',
|
|
|
|
| 438 |
}
|
| 439 |
|
| 440 |
function initSherpaOnnxOfflineTransducerModelConfig(config, Module) {
|
| 441 |
+
const encoderLen = Module.lengthBytesUTF8(config.encoder || '') + 1;
|
| 442 |
+
const decoderLen = Module.lengthBytesUTF8(config.decoder || '') + 1;
|
| 443 |
+
const joinerLen = Module.lengthBytesUTF8(config.joiner || '') + 1;
|
| 444 |
|
| 445 |
const n = encoderLen + decoderLen + joinerLen;
|
| 446 |
|
|
|
|
| 450 |
const ptr = Module._malloc(len);
|
| 451 |
|
| 452 |
let offset = 0;
|
| 453 |
+
Module.stringToUTF8(config.encoder || '', buffer + offset, encoderLen);
|
| 454 |
offset += encoderLen;
|
| 455 |
|
| 456 |
+
Module.stringToUTF8(config.decoder || '', buffer + offset, decoderLen);
|
| 457 |
offset += decoderLen;
|
| 458 |
|
| 459 |
+
Module.stringToUTF8(config.joiner || '', buffer + offset, joinerLen);
|
| 460 |
|
| 461 |
offset = 0;
|
| 462 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
|
|
| 473 |
}
|
| 474 |
|
| 475 |
function initSherpaOnnxOfflineParaformerModelConfig(config, Module) {
|
| 476 |
+
const n = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 477 |
|
| 478 |
const buffer = Module._malloc(n);
|
| 479 |
|
| 480 |
const len = 1 * 4; // 1 pointer
|
| 481 |
const ptr = Module._malloc(len);
|
| 482 |
|
| 483 |
+
Module.stringToUTF8(config.model || '', buffer, n);
|
| 484 |
|
| 485 |
Module.setValue(ptr, buffer, 'i8*');
|
| 486 |
|
|
|
|
| 490 |
}
|
| 491 |
|
| 492 |
function initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config, Module) {
|
| 493 |
+
const n = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 494 |
|
| 495 |
const buffer = Module._malloc(n);
|
| 496 |
|
| 497 |
const len = 1 * 4; // 1 pointer
|
| 498 |
const ptr = Module._malloc(len);
|
| 499 |
|
| 500 |
+
Module.stringToUTF8(config.model || '', buffer, n);
|
| 501 |
|
| 502 |
Module.setValue(ptr, buffer, 'i8*');
|
| 503 |
|
|
|
|
| 507 |
}
|
| 508 |
|
| 509 |
function initSherpaOnnxOfflineWhisperModelConfig(config, Module) {
|
| 510 |
+
const encoderLen = Module.lengthBytesUTF8(config.encoder || '') + 1;
|
| 511 |
+
const decoderLen = Module.lengthBytesUTF8(config.decoder || '') + 1;
|
| 512 |
+
const languageLen = Module.lengthBytesUTF8(config.language || '') + 1;
|
| 513 |
+
const taskLen = Module.lengthBytesUTF8(config.task || '') + 1;
|
| 514 |
|
| 515 |
const n = encoderLen + decoderLen + languageLen + taskLen;
|
| 516 |
const buffer = Module._malloc(n);
|
|
|
|
| 519 |
const ptr = Module._malloc(len);
|
| 520 |
|
| 521 |
let offset = 0;
|
| 522 |
+
Module.stringToUTF8(config.encoder || '', buffer + offset, encoderLen);
|
| 523 |
offset += encoderLen;
|
| 524 |
|
| 525 |
+
Module.stringToUTF8(config.decoder || '', buffer + offset, decoderLen);
|
| 526 |
offset += decoderLen;
|
| 527 |
|
| 528 |
+
Module.stringToUTF8(config.language || '', buffer + offset, languageLen);
|
| 529 |
offset += languageLen;
|
| 530 |
|
| 531 |
+
Module.stringToUTF8(config.task || '', buffer + offset, taskLen);
|
| 532 |
|
| 533 |
offset = 0;
|
| 534 |
Module.setValue(ptr, buffer + offset, 'i8*');
|
|
|
|
| 551 |
}
|
| 552 |
|
| 553 |
function initSherpaOnnxOfflineTdnnModelConfig(config, Module) {
|
| 554 |
+
const n = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 555 |
const buffer = Module._malloc(n);
|
| 556 |
|
| 557 |
const len = 1 * 4; // 1 pointer
|
| 558 |
const ptr = Module._malloc(len);
|
| 559 |
|
| 560 |
+
Module.stringToUTF8(config.model || '', buffer, n);
|
| 561 |
|
| 562 |
Module.setValue(ptr, buffer, 'i8*');
|
| 563 |
|
|
|
|
| 566 |
}
|
| 567 |
}
|
| 568 |
|
| 569 |
+
function initSherpaOnnxOfflineSenseVoiceModelConfig(config, Module) {
|
| 570 |
+
const modelLen = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 571 |
+
const languageLen = Module.lengthBytesUTF8(config.language || '') + 1;
|
| 572 |
+
|
| 573 |
+
// useItn is a integer with 4 bytes
|
| 574 |
+
const n = modelLen + languageLen;
|
| 575 |
+
const buffer = Module._malloc(n);
|
| 576 |
+
|
| 577 |
+
const len = 3 * 4; // 2 pointers + 1 int
|
| 578 |
+
const ptr = Module._malloc(len);
|
| 579 |
+
|
| 580 |
+
let offset = 0;
|
| 581 |
+
Module.stringToUTF8(config.model || '', buffer + offset, modelLen);
|
| 582 |
+
offset += modelLen;
|
| 583 |
+
|
| 584 |
+
Module.stringToUTF8(config.language || '', buffer + offset, languageLen);
|
| 585 |
+
offset += languageLen;
|
| 586 |
+
|
| 587 |
+
offset = 0;
|
| 588 |
+
Module.setValue(ptr, buffer + offset, 'i8*');
|
| 589 |
+
offset += modelLen;
|
| 590 |
+
|
| 591 |
+
Module.setValue(ptr + 4, buffer + offset, 'i8*');
|
| 592 |
+
offset += languageLen;
|
| 593 |
+
|
| 594 |
+
Module.setValue(ptr + 8, config.useInverseTextNormalization || 0, 'i32');
|
| 595 |
+
|
| 596 |
+
return {
|
| 597 |
+
buffer: buffer, ptr: ptr, len: len,
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
function initSherpaOnnxOfflineLMConfig(config, Module) {
|
| 602 |
+
const n = Module.lengthBytesUTF8(config.model || '') + 1;
|
| 603 |
const buffer = Module._malloc(n);
|
| 604 |
|
| 605 |
const len = 2 * 4;
|
| 606 |
const ptr = Module._malloc(len);
|
| 607 |
|
| 608 |
+
Module.stringToUTF8(config.model || '', buffer, n);
|
| 609 |
Module.setValue(ptr, buffer, 'i8*');
|
| 610 |
+
Module.setValue(ptr + 4, config.scale || 1, 'float');
|
| 611 |
|
| 612 |
return {
|
| 613 |
buffer: buffer, ptr: ptr, len: len,
|
|
|
|
| 615 |
}
|
| 616 |
|
| 617 |
function initSherpaOnnxOfflineModelConfig(config, Module) {
|
| 618 |
+
if (!('transducer' in config)) {
|
| 619 |
+
config.transducer = {
|
| 620 |
+
encoder: '',
|
| 621 |
+
decoder: '',
|
| 622 |
+
joiner: '',
|
| 623 |
+
};
|
| 624 |
+
}
|
| 625 |
+
|
| 626 |
+
if (!('paraformer' in config)) {
|
| 627 |
+
config.paraformer = {
|
| 628 |
+
model: '',
|
| 629 |
+
};
|
| 630 |
+
}
|
| 631 |
+
|
| 632 |
+
if (!('nemoCtc' in config)) {
|
| 633 |
+
config.nemoCtc = {
|
| 634 |
+
model: '',
|
| 635 |
+
};
|
| 636 |
+
}
|
| 637 |
+
|
| 638 |
+
if (!('whisper' in config)) {
|
| 639 |
+
config.whisper = {
|
| 640 |
+
encoder: '',
|
| 641 |
+
decoder: '',
|
| 642 |
+
language: '',
|
| 643 |
+
task: '',
|
| 644 |
+
tailPaddings: -1,
|
| 645 |
+
};
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
if (!('tdnn' in config)) {
|
| 649 |
+
config.tdnn = {
|
| 650 |
+
model: '',
|
| 651 |
+
};
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
if (!('senseVoice' in config)) {
|
| 655 |
+
config.senseVoice = {
|
| 656 |
+
model: '',
|
| 657 |
+
language: '',
|
| 658 |
+
useInverseTextNormalization: 0,
|
| 659 |
+
};
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
const transducer =
|
| 663 |
initSherpaOnnxOfflineTransducerModelConfig(config.transducer, Module);
|
| 664 |
+
|
| 665 |
const paraformer =
|
| 666 |
initSherpaOnnxOfflineParaformerModelConfig(config.paraformer, Module);
|
| 667 |
+
|
| 668 |
const nemoCtc =
|
| 669 |
initSherpaOnnxOfflineNemoEncDecCtcModelConfig(config.nemoCtc, Module);
|
| 670 |
+
|
| 671 |
const whisper =
|
| 672 |
initSherpaOnnxOfflineWhisperModelConfig(config.whisper, Module);
|
| 673 |
+
|
| 674 |
const tdnn = initSherpaOnnxOfflineTdnnModelConfig(config.tdnn, Module);
|
| 675 |
|
| 676 |
+
const senseVoice =
|
| 677 |
+
initSherpaOnnxOfflineSenseVoiceModelConfig(config.senseVoice, Module);
|
| 678 |
+
|
| 679 |
const len = transducer.len + paraformer.len + nemoCtc.len + whisper.len +
|
| 680 |
+
tdnn.len + 8 * 4 + senseVoice.len;
|
| 681 |
+
|
| 682 |
const ptr = Module._malloc(len);
|
| 683 |
|
| 684 |
let offset = 0;
|
|
|
|
| 697 |
Module._CopyHeap(tdnn.ptr, tdnn.len, ptr + offset);
|
| 698 |
offset += tdnn.len;
|
| 699 |
|
| 700 |
+
|
| 701 |
+
const tokensLen = Module.lengthBytesUTF8(config.tokens || '') + 1;
|
| 702 |
+
const providerLen = Module.lengthBytesUTF8(config.provider || 'cpu') + 1;
|
| 703 |
+
const modelTypeLen = Module.lengthBytesUTF8(config.modelType || '') + 1;
|
| 704 |
const modelingUnitLen = Module.lengthBytesUTF8(config.modelingUnit || '') + 1;
|
| 705 |
const bpeVocabLen = Module.lengthBytesUTF8(config.bpeVocab || '') + 1;
|
| 706 |
const teleSpeechCtcLen =
|
|
|
|
| 708 |
|
| 709 |
const bufferLen = tokensLen + providerLen + modelTypeLen + modelingUnitLen +
|
| 710 |
bpeVocabLen + teleSpeechCtcLen;
|
| 711 |
+
|
| 712 |
const buffer = Module._malloc(bufferLen);
|
| 713 |
|
| 714 |
offset = 0;
|
| 715 |
Module.stringToUTF8(config.tokens, buffer, tokensLen);
|
| 716 |
offset += tokensLen;
|
| 717 |
|
| 718 |
+
Module.stringToUTF8(config.provider || 'cpu', buffer + offset, providerLen);
|
| 719 |
offset += providerLen;
|
| 720 |
|
| 721 |
+
Module.stringToUTF8(config.modelType || '', buffer + offset, modelTypeLen);
|
| 722 |
offset += modelTypeLen;
|
| 723 |
|
| 724 |
Module.stringToUTF8(
|
|
|
|
| 737 |
Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
|
| 738 |
offset += 4;
|
| 739 |
|
| 740 |
+
Module.setValue(ptr + offset, config.numThreads || 1, 'i32');
|
| 741 |
offset += 4;
|
| 742 |
|
| 743 |
+
Module.setValue(ptr + offset, config.debug || 0, 'i32');
|
| 744 |
offset += 4;
|
| 745 |
|
| 746 |
Module.setValue(ptr + offset, buffer + tokensLen, 'i8*'); // provider
|
|
|
|
| 768 |
'i8*'); // teleSpeechCtc
|
| 769 |
offset += 4;
|
| 770 |
|
| 771 |
+
Module._CopyHeap(senseVoice.ptr, senseVoice.len, ptr + offset);
|
| 772 |
+
|
| 773 |
return {
|
| 774 |
buffer: buffer, ptr: ptr, len: len, transducer: transducer,
|
| 775 |
+
paraformer: paraformer, nemoCtc: nemoCtc, whisper: whisper, tdnn: tdnn,
|
| 776 |
+
senseVoice: senseVoice,
|
| 777 |
}
|
| 778 |
}
|
| 779 |
|
| 780 |
function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
| 781 |
+
if (!('featConfig' in config)) {
|
| 782 |
+
config.featConfig = {
|
| 783 |
+
sampleRate: 16000,
|
| 784 |
+
featureDim: 80,
|
| 785 |
+
};
|
| 786 |
+
}
|
| 787 |
+
|
| 788 |
+
if (!('lmConfig' in config)) {
|
| 789 |
+
config.lmConfig = {
|
| 790 |
+
model: '',
|
| 791 |
+
scale: 1.0,
|
| 792 |
+
};
|
| 793 |
+
}
|
| 794 |
+
|
| 795 |
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
| 796 |
const model = initSherpaOnnxOfflineModelConfig(config.modelConfig, Module);
|
| 797 |
const lm = initSherpaOnnxOfflineLMConfig(config.lmConfig, Module);
|
|
|
|
| 809 |
Module._CopyHeap(lm.ptr, lm.len, ptr + offset);
|
| 810 |
offset += lm.len;
|
| 811 |
|
| 812 |
+
const decodingMethodLen =
|
| 813 |
+
Module.lengthBytesUTF8(config.decodingMethod || 'greedy_search') + 1;
|
| 814 |
+
const hotwordsFileLen = Module.lengthBytesUTF8(config.hotwordsFile || '') + 1;
|
| 815 |
const ruleFstsLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
|
| 816 |
const ruleFarsLen = Module.lengthBytesUTF8(config.ruleFars || '') + 1;
|
| 817 |
const bufferLen =
|
|
|
|
| 819 |
const buffer = Module._malloc(bufferLen);
|
| 820 |
|
| 821 |
offset = 0;
|
| 822 |
+
Module.stringToUTF8(
|
| 823 |
+
config.decodingMethod || 'greedy_search', buffer, decodingMethodLen);
|
| 824 |
offset += decodingMethodLen;
|
| 825 |
|
| 826 |
+
Module.stringToUTF8(
|
| 827 |
+
config.hotwordsFile || '', buffer + offset, hotwordsFileLen);
|
| 828 |
offset += hotwordsFileLen;
|
| 829 |
|
| 830 |
Module.stringToUTF8(config.ruleFsts || '', buffer + offset, ruleFstsLen);
|
|
|
|
| 838 |
Module.setValue(ptr + offset, buffer, 'i8*'); // decoding method
|
| 839 |
offset += 4;
|
| 840 |
|
| 841 |
+
Module.setValue(ptr + offset, config.maxActivePaths || 4, 'i32');
|
| 842 |
offset += 4;
|
| 843 |
|
| 844 |
Module.setValue(ptr + offset, buffer + decodingMethodLen, 'i8*');
|
| 845 |
offset += 4;
|
| 846 |
|
| 847 |
+
Module.setValue(ptr + offset, config.hotwordsScore || 1.5, 'float');
|
| 848 |
offset += 4;
|
| 849 |
|
| 850 |
Module.setValue(
|
sherpa-onnx-wasm-main-asr.js
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|