Skip to content

Commit

Permalink
fix kws for WebAssembly (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jun 14, 2024
1 parent 155f22d commit c214d8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions wasm/kws/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ if (navigator.mediaDevices.getUserMedia) {


let result = recognizer.getResult(recognizer_stream);
console.log(result)

if (result.keyword.length > 0) {
console.log(result)
lastResult = result;
resultList.push(JSON.stringify(result));
}
Expand Down Expand Up @@ -287,4 +287,4 @@ function downsampleBuffer(buffer, exportSampleRate) {
offsetBuffer = nextOffsetBuffer;
}
return result;
};
};
30 changes: 27 additions & 3 deletions wasm/kws/sherpa-onnx-kws.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function initModelConfig(config, Module) {
const paraformer_len = 2 * 4
const ctc_len = 1 * 4

const len = transducer.len + paraformer_len + ctc_len + 5 * 4;
const len = transducer.len + paraformer_len + ctc_len + 7 * 4;
const ptr = Module._malloc(len);

let offset = 0;
Expand All @@ -76,7 +76,10 @@ function initModelConfig(config, Module) {
const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
const providerLen = Module.lengthBytesUTF8(config.provider) + 1;
const modelTypeLen = Module.lengthBytesUTF8(config.modelType) + 1;
const bufferLen = tokensLen + providerLen + modelTypeLen;
const modelingUnitLen = Module.lengthBytesUTF8(config.modelingUnit || '') + 1;
const bpeVocabLen = Module.lengthBytesUTF8(config.bpeVocab || '') + 1;
const bufferLen =
tokensLen + providerLen + modelTypeLen + modelingUnitLen + bpeVocabLen;
const buffer = Module._malloc(bufferLen);

offset = 0;
Expand All @@ -87,6 +90,14 @@ function initModelConfig(config, Module) {
offset += providerLen;

Module.stringToUTF8(config.modelType, buffer + offset, modelTypeLen);
offset += modelTypeLen;

Module.stringToUTF8(
config.modelingUnit || '', buffer + offset, modelingUnitLen);
offset += modelingUnitLen;

Module.stringToUTF8(config.bpeVocab || '', buffer + offset, bpeVocabLen);
offset += bpeVocabLen;

offset = transducer.len + paraformer_len + ctc_len;
Module.setValue(ptr + offset, buffer, 'i8*'); // tokens
Expand All @@ -105,6 +116,17 @@ function initModelConfig(config, Module) {
ptr + offset, buffer + tokensLen + providerLen, 'i8*'); // modelType
offset += 4;

Module.setValue(
ptr + offset, buffer + tokensLen + providerLen + modelTypeLen,
'i8*'); // modelingUnit
offset += 4;

Module.setValue(
ptr + offset,
buffer + tokensLen + providerLen + modelTypeLen + modelingUnitLen,
'i8*'); // bpeVocab
offset += 4;

return {
buffer: buffer, ptr: ptr, len: len, transducer: transducer
}
Expand Down Expand Up @@ -248,7 +270,9 @@ function createKws(Module, myConfig) {
provider: 'cpu',
modelType: '',
numThreads: 1,
debug: 1
debug: 1,
modelingUnit: 'cjkchar',
bpeVocab: '',
};

let featConfig = {
Expand Down
2 changes: 1 addition & 1 deletion wasm/kws/sherpa-onnx-wasm-main-kws.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static_assert(sizeof(SherpaOnnxOnlineZipformer2CtcModelConfig) == 1 * 4, "");
static_assert(sizeof(SherpaOnnxOnlineModelConfig) ==
sizeof(SherpaOnnxOnlineTransducerModelConfig) +
sizeof(SherpaOnnxOnlineParaformerModelConfig) +
sizeof(SherpaOnnxOnlineZipformer2CtcModelConfig) + 5 * 4,
sizeof(SherpaOnnxOnlineZipformer2CtcModelConfig) + 7 * 4,
"");
static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, "");
static_assert(sizeof(SherpaOnnxKeywordSpotterConfig) ==
Expand Down

0 comments on commit c214d8f

Please sign in to comment.