Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return timestamps for WebAssembly #737

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(sherpa-onnx)

set(SHERPA_ONNX_VERSION "1.9.15")
set(SHERPA_ONNX_VERSION "1.9.16")

# Disable warning about
#
Expand Down
2 changes: 1 addition & 1 deletion build-aarch64-linux-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cmake \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \
..
Expand Down
2 changes: 1 addition & 1 deletion build-arm-linux-gnueabihf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ cmake \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake \
..
Expand Down
2 changes: 1 addition & 1 deletion build-wasm-simd-nodejs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cmake \
-DSHERPA_ONNX_ENABLE_BINARY=OFF \
-DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \
..
make -j10
make -j3
make install

ls -lh install/bin/wasm/nodejs
2 changes: 1 addition & 1 deletion nodejs-examples/test-offline-nemo-ctc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {highWaterMark: 4096})

stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened);
recognizer.decode(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);

stream.free();
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-offline-paraformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096})

stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened);
recognizer.decode(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);

stream.free();
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-offline-transducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096})

stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened);
recognizer.decode(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);

stream.free();
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-offline-whisper.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096})

stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened);
recognizer.decode(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);

stream.free();
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-paraformer-microphone.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ai.on('data', data => {
}

const isEndpoint = recognizer.isEndpoint(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;

if (text.length > 0 && lastText != text) {
lastText = text;
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-paraformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function decode(samples) {
while (recognizer.isReady(stream)) {
recognizer.decode(stream);
}
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);
}

Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-transducer-microphone.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ai.on('data', data => {
}

const isEndpoint = recognizer.isEndpoint(stream);
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;

if (text.length > 0 && lastText != text) {
lastText = text;
Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-transducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function decode(samples) {
while (recognizer.isReady(stream)) {
recognizer.decode(stream);
}
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);
}

Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-zipformer2-ctc-hlg.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function decode(samples) {
while (recognizer.isReady(stream)) {
recognizer.decode(stream);
}
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);
}

Expand Down
2 changes: 1 addition & 1 deletion nodejs-examples/test-online-zipformer2-ctc.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function decode(samples) {
while (recognizer.isReady(stream)) {
recognizer.decode(stream);
}
const text = recognizer.getResult(stream);
const text = recognizer.getResult(stream).text;
console.log(text);
}

Expand Down
29 changes: 28 additions & 1 deletion sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ void DestroyOnlineRecognizerResult(const SherpaOnnxOnlineRecognizerResult *r) {
}
}

const char *GetOnlineStreamResultAsJson(
const SherpaOnnxOnlineRecognizer *recognizer,
const SherpaOnnxOnlineStream *stream) {
sherpa_onnx::OnlineRecognizerResult result =
recognizer->impl->GetResult(stream->impl.get());
std::string json = result.AsJsonString();
char *pJson = new char[json.size() + 1];
std::copy(json.begin(), json.end(), pJson);
pJson[json.size()] = 0;
return pJson;
}

void DestroyOnlineStreamResultJson(const char *s) { delete[] s; }

void Reset(const SherpaOnnxOnlineRecognizer *recognizer,
const SherpaOnnxOnlineStream *stream) {
recognizer->impl->Reset(stream->impl.get());
Expand Down Expand Up @@ -409,7 +423,7 @@ void DecodeMultipleOfflineStreams(SherpaOnnxOfflineRecognizer *recognizer,
}

const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
SherpaOnnxOfflineStream *stream) {
const SherpaOnnxOfflineStream *stream) {
const sherpa_onnx::OfflineRecognitionResult &result =
stream->impl->GetResult();
const auto &text = result.text;
Expand Down Expand Up @@ -444,6 +458,19 @@ void DestroyOfflineRecognizerResult(
}
}

const char *GetOfflineStreamResultAsJson(
const SherpaOnnxOfflineStream *stream) {
const sherpa_onnx::OfflineRecognitionResult &result =
stream->impl->GetResult();
std::string json = result.AsJsonString();
char *pJson = new char[json.size() + 1];
std::copy(json.begin(), json.end(), pJson);
pJson[json.size()] = 0;
return pJson;
}

void DestroyOfflineStreamResultJson(const char *s) { delete[] s; }

// ============================================================
// For Keyword Spot
// ============================================================
Expand Down
20 changes: 19 additions & 1 deletion sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
SHERPA_ONNX_API void DestroyOnlineRecognizerResult(
const SherpaOnnxOnlineRecognizerResult *r);

/// Return the result as a json string.
/// The user has to invoke
/// DestroyOnlineStreamResultJson()
/// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const char *GetOnlineStreamResultAsJson(
const SherpaOnnxOnlineRecognizer *recognizer,
const SherpaOnnxOnlineStream *stream);

SHERPA_ONNX_API void DestroyOnlineStreamResultJson(const char *s);

/// Reset an OnlineStream , which clears the neural network model state
/// and the state for decoding.
///
Expand Down Expand Up @@ -482,14 +492,22 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerResult {
/// DestroyOnlineRecognizerResult() to free the returned pointer to
/// avoid memory leak.
SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
SherpaOnnxOfflineStream *stream);
const SherpaOnnxOfflineStream *stream);

/// Destroy the pointer returned by GetOfflineStreamResult().
///
/// @param r A pointer returned by GetOfflineStreamResult()
SHERPA_ONNX_API void DestroyOfflineRecognizerResult(
const SherpaOnnxOfflineRecognizerResult *r);

/// Return the result as a json string.
/// The user has to use DestroyOfflineStreamResultJson()
/// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const char *GetOfflineStreamResultAsJson(
const SherpaOnnxOfflineStream *stream);

SHERPA_ONNX_API void DestroyOfflineStreamResultJson(const char *s);

// ============================================================
// For Keyword Spot
// ============================================================
Expand Down
4 changes: 4 additions & 0 deletions wasm/asr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ set(exported_functions
CreateOnlineRecognizer
CreateOnlineStream
DecodeOnlineStream
DestroyOfflineStreamResultJson
DestroyOnlineRecognizer
DestroyOnlineRecognizerResult
DestroyOnlineStream
DestroyOnlineStreamResultJson
GetOfflineStreamResultAsJson
GetOnlineStreamResult
GetOnlineStreamResultAsJson
InputFinished
IsEndpoint
IsOnlineStreamReady
Expand Down
2 changes: 1 addition & 1 deletion wasm/asr/app-asr.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ if (navigator.mediaDevices.getUserMedia) {
}

let isEndpoint = recognizer.isEndpoint(recognizer_stream);
let result = recognizer.getResult(recognizer_stream);
let result = recognizer.getResult(recognizer_stream).text;


if (result.length > 0 && lastResult != result) {
Expand Down
23 changes: 12 additions & 11 deletions wasm/asr/sherpa-onnx-asr.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,12 @@ class OfflineRecognizer {
}

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

const textPtr = this.Module.getValue(r, 'i8*');
const text = this.Module.UTF8ToString(textPtr);

this.Module._DestroyOfflineRecognizerResult(r);
return text;
return ans;
}
};

Expand Down Expand Up @@ -750,11 +749,13 @@ class OnlineRecognizer {
}

getResult(stream) {
const r = this.Module._GetOnlineStreamResult(this.handle, stream.handle);
const textPtr = this.Module.getValue(r, 'i8*');
const text = this.Module.UTF8ToString(textPtr);
this.Module._DestroyOnlineRecognizerResult(r);
return text;
const r =
this.Module._GetOnlineStreamResultAsJson(this.handle, stream.handle);
const jsonStr = this.Module.UTF8ToString(r);
const ans = JSON.parse(jsonStr);
this.Module._DestroyOnlineStreamResultJson(r);

return ans;
}
}

Expand Down
16 changes: 10 additions & 6 deletions wasm/nodejs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ set(exported_functions
DestroyOnlineRecognizer
DestroyOnlineRecognizerResult
DestroyOnlineStream
DestroyOnlineStreamResultJson
GetOnlineStreamResult
GetOnlineStreamResultAsJson
InputFinished
IsEndpoint
IsOnlineStreamReady
Reset
# non-streaming ASR
PrintOfflineRecognizerConfig
AcceptWaveformOffline
CreateOfflineRecognizer
DestroyOfflineRecognizer
CreateOfflineStream
DestroyOfflineStream
AcceptWaveformOffline
DecodeOfflineStream
DecodeMultipleOfflineStreams
GetOfflineStreamResult
DecodeOfflineStream
DestroyOfflineRecognizer
DestroyOfflineRecognizerResult
DestroyOfflineStream
DestroyOfflineStreamResultJson
GetOfflineStreamResult
GetOfflineStreamResultAsJson
PrintOfflineRecognizerConfig
# online kws
CreateKeywordSpotter
DestroyKeywordSpotter
Expand Down
Loading