return timestamps for WebAssembly (#737)
This commit is contained in:
@@ -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
|
||||
#
|
||||
|
||||
@@ -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 \
|
||||
..
|
||||
|
||||
@@ -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 \
|
||||
..
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
@@ -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;
|
||||
@@ -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
|
||||
// ============================================================
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
@@ -482,7 +492,7 @@ 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().
|
||||
///
|
||||
@@ -490,6 +500,14 @@ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *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
|
||||
// ============================================================
|
||||
|
||||
@@ -13,10 +13,14 @@ set(exported_functions
|
||||
CreateOnlineRecognizer
|
||||
CreateOnlineStream
|
||||
DecodeOnlineStream
|
||||
DestroyOfflineStreamResultJson
|
||||
DestroyOnlineRecognizer
|
||||
DestroyOnlineRecognizerResult
|
||||
DestroyOnlineStream
|
||||
DestroyOnlineStreamResultJson
|
||||
GetOfflineStreamResultAsJson
|
||||
GetOnlineStreamResult
|
||||
GetOnlineStreamResultAsJson
|
||||
InputFinished
|
||||
IsEndpoint
|
||||
IsOnlineStreamReady
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,22 +21,26 @@ set(exported_functions
|
||||
DestroyOnlineRecognizer
|
||||
DestroyOnlineRecognizerResult
|
||||
DestroyOnlineStream
|
||||
DestroyOnlineStreamResultJson
|
||||
GetOnlineStreamResult
|
||||
GetOnlineStreamResultAsJson
|
||||
InputFinished
|
||||
IsEndpoint
|
||||
IsOnlineStreamReady
|
||||
Reset
|
||||
# non-streaming ASR
|
||||
PrintOfflineRecognizerConfig
|
||||
CreateOfflineRecognizer
|
||||
DestroyOfflineRecognizer
|
||||
CreateOfflineStream
|
||||
DestroyOfflineStream
|
||||
AcceptWaveformOffline
|
||||
DecodeOfflineStream
|
||||
CreateOfflineRecognizer
|
||||
CreateOfflineStream
|
||||
DecodeMultipleOfflineStreams
|
||||
GetOfflineStreamResult
|
||||
DecodeOfflineStream
|
||||
DestroyOfflineRecognizer
|
||||
DestroyOfflineRecognizerResult
|
||||
DestroyOfflineStream
|
||||
DestroyOfflineStreamResultJson
|
||||
GetOfflineStreamResult
|
||||
GetOfflineStreamResultAsJson
|
||||
PrintOfflineRecognizerConfig
|
||||
# online kws
|
||||
CreateKeywordSpotter
|
||||
DestroyKeywordSpotter
|
||||
|
||||
Reference in New Issue
Block a user