Skip to content

Commit

Permalink
fix wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Jul 26, 2024
1 parent 141e4b5 commit a57e5e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
45 changes: 24 additions & 21 deletions wasm/asr/sherpa-onnx-asr.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ class OfflineStream {

free() {
if (this.handle) {
this.Module._DestroyOfflineStream(this.handle);
this.Module._SherpaOnnxDestroyOfflineStream(this.handle);
this.handle = null;
}
}
Expand All @@ -882,7 +882,7 @@ class OfflineStream {
const pointer =
this.Module._malloc(samples.length * samples.BYTES_PER_ELEMENT);
this.Module.HEAPF32.set(samples, pointer / samples.BYTES_PER_ELEMENT);
this.Module._AcceptWaveformOffline(
this.Module._SherpaOnnxAcceptWaveformOffline(
this.handle, sampleRate, pointer, samples.length);
this.Module._free(pointer);
}
Expand All @@ -892,32 +892,33 @@ class OfflineRecognizer {
constructor(configObj, Module) {
this.config = configObj;
const config = initSherpaOnnxOfflineRecognizerConfig(configObj, Module);
const handle = Module._CreateOfflineRecognizer(config.ptr);
const handle = Module._SherpaOnnxCreateOfflineRecognizer(config.ptr);
freeConfig(config, Module);

this.handle = handle;
this.Module = Module;
}

free() {
this.Module._DestroyOfflineRecognizer(this.handle);
this.Module._SherpaOnnxDestroyOfflineRecognizer(this.handle);
this.handle = 0
}

createStream() {
const handle = this.Module._CreateOfflineStream(this.handle);
const handle = this.Module._SherpaOnnxCreateOfflineStream(this.handle);
return new OfflineStream(handle, this.Module);
}

decode(stream) {
this.Module._DecodeOfflineStream(this.handle, stream.handle);
this.Module._SherpaOnnxDecodeOfflineStream(this.handle, stream.handle);
}

getResult(stream) {
const r = this.Module._GetOfflineStreamResultAsJson(stream.handle);
const r =
this.Module._SherpaOnnxGetOfflineStreamResultAsJson(stream.handle);
const jsonStr = this.Module.UTF8ToString(r);
const ans = JSON.parse(jsonStr);
this.Module._DestroyOfflineStreamResultJson(r);
this.Module._SherpaOnnxDestroyOfflineStreamResultJson(r);

return ans;
}
Expand All @@ -933,7 +934,7 @@ class OnlineStream {

free() {
if (this.handle) {
this.Module._DestroyOnlineStream(this.handle);
this.Module._SherpaOnnxDestroyOnlineStream(this.handle);
this.handle = null;
this.Module._free(this.pointer);
this.pointer = null;
Expand All @@ -954,20 +955,20 @@ class OnlineStream {
}

this.Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
this.Module._AcceptWaveform(
this.Module._SherpaOnnxOnlineStreamAcceptWaveform(
this.handle, sampleRate, this.pointer, samples.length);
}

inputFinished() {
this.Module._InputFinished(this.handle);
this.Module._SherpaOnnxOnlineStreamInputFinished(this.handle);
}
};

class OnlineRecognizer {
constructor(configObj, Module) {
this.config = configObj;
const config = initSherpaOnnxOnlineRecognizerConfig(configObj, Module)
const handle = Module._CreateOnlineRecognizer(config.ptr);
const handle = Module._SherpaOnnxCreateOnlineRecognizer(config.ptr);

freeConfig(config, Module);

Expand All @@ -976,37 +977,39 @@ class OnlineRecognizer {
}

free() {
this.Module._DestroyOnlineRecognizer(this.handle);
this.Module._SherpaOnnxDestroyOnlineRecognizer(this.handle);
this.handle = 0
}

createStream() {
const handle = this.Module._CreateOnlineStream(this.handle);
const handle = this.Module._SherpaOnnxCreateOnlineStream(this.handle);
return new OnlineStream(handle, this.Module);
}

isReady(stream) {
return this.Module._IsOnlineStreamReady(this.handle, stream.handle) == 1;
return this.Module._SherpaOnnxIsOnlineStreamReady(
this.handle, stream.handle) == 1;
}

decode(stream) {
this.Module._DecodeOnlineStream(this.handle, stream.handle);
this.Module._SherpaOnnxDecodeOnlineStream(this.handle, stream.handle);
}

isEndpoint(stream) {
return this.Module._IsEndpoint(this.handle, stream.handle) == 1;
return this.Module._SherpaOnnxOnlineStreamIsEndpoint(
this.handle, stream.handle) == 1;
}

reset(stream) {
this.Module._Reset(this.handle, stream.handle);
this.Module._SherpaOnnxOnlineStreamReset(this.handle, stream.handle);
}

getResult(stream) {
const r =
this.Module._GetOnlineStreamResultAsJson(this.handle, stream.handle);
const r = this.Module._SherpaOnnxGetOnlineStreamResultAsJson(
this.handle, stream.handle);
const jsonStr = this.Module.UTF8ToString(r);
const ans = JSON.parse(jsonStr);
this.Module._DestroyOnlineStreamResultJson(r);
this.Module._SherpaOnnxDestroyOnlineStreamResultJson(r);

return ans;
}
Expand Down
22 changes: 12 additions & 10 deletions wasm/kws/sherpa-onnx-kws.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Stream {

free() {
if (this.handle) {
this.Module._DestroyOnlineKwsStream(this.handle);
this.Module._SherpaOnnxDestroyOnlineStream(this.handle);
this.handle = null;
this.Module._free(this.pointer);
this.pointer = null;
Expand All @@ -210,20 +210,20 @@ class Stream {
}

this.Module.HEAPF32.set(samples, this.pointer / samples.BYTES_PER_ELEMENT);
this.Module._AcceptWaveform(
this.Module._SherpaOnnxOnlineStreamAcceptWaveform(
this.handle, sampleRate, this.pointer, samples.length);
}

inputFinished() {
this.Module._InputFinished(this.handle);
this.Module._SherpaOnnxOnlineStreamInputFinished(this.handle);
}
};

class Kws {
constructor(configObj, Module) {
this.config = configObj;
let config = initKwsConfig(configObj, Module)
let handle = Module._CreateKeywordSpotter(config.ptr);
let handle = Module._SherpaOnnxCreateKeywordSpotter(config.ptr);

freeConfig(config, Module);

Expand All @@ -232,28 +232,30 @@ class Kws {
}

free() {
this.Module._DestroyKeywordSpotter(this.handle);
this.Module._SherpaOnnxDestroyKeywordSpotter(this.handle);
this.handle = 0
}

createStream() {
let handle = this.Module._CreateKeywordStream(this.handle);
let handle = this.Module._SherpaOnnxCreateKeywordStream(this.handle);
return new Stream(handle, this.Module);
}

isReady(stream) {
return this.Module._IsKeywordStreamReady(this.handle, stream.handle) === 1;
return this.Module._SherpaOnnxIsKeywordStreamReady(
this.handle, stream.handle) == 1;
}

decode(stream) {
return this.Module._DecodeKeywordStream(this.handle, stream.handle);
return this.Module._SherpaOnnxDecodeKeywordStream(
this.handle, stream.handle);
}

getResult(stream) {
let r = this.Module._GetKeywordResult(this.handle, stream.handle);
let r = this.Module._SherpaOnnxGetKeywordResult(this.handle, stream.handle);
let jsonPtr = this.Module.getValue(r + 24, 'i8*');
let json = this.Module.UTF8ToString(jsonPtr);
this.Module._DestroyKeywordResult(r);
this.Module._SherpaOnnxDestroyKeywordResult(r);
return JSON.parse(json);
}
}
Expand Down

0 comments on commit a57e5e3

Please sign in to comment.