正在显示
20 个修改的文件
包含
88 行增加
和
34 行删除
| @@ -57,7 +57,7 @@ cmake \ | @@ -57,7 +57,7 @@ cmake \ | ||
| 57 | -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | 57 | -DSHERPA_ONNX_ENABLE_CHECK=OFF \ |
| 58 | -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | 58 | -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ |
| 59 | -DSHERPA_ONNX_ENABLE_JNI=OFF \ | 59 | -DSHERPA_ONNX_ENABLE_JNI=OFF \ |
| 60 | - -DSHERPA_ONNX_ENABLE_C_API=OFF \ | 60 | + -DSHERPA_ONNX_ENABLE_C_API=ON \ |
| 61 | -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ | 61 | -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ |
| 62 | -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \ | 62 | -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \ |
| 63 | .. | 63 | .. |
| @@ -52,7 +52,7 @@ cmake \ | @@ -52,7 +52,7 @@ cmake \ | ||
| 52 | -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | 52 | -DSHERPA_ONNX_ENABLE_CHECK=OFF \ |
| 53 | -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | 53 | -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ |
| 54 | -DSHERPA_ONNX_ENABLE_JNI=OFF \ | 54 | -DSHERPA_ONNX_ENABLE_JNI=OFF \ |
| 55 | - -DSHERPA_ONNX_ENABLE_C_API=OFF \ | 55 | + -DSHERPA_ONNX_ENABLE_C_API=ON \ |
| 56 | -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ | 56 | -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ |
| 57 | -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake \ | 57 | -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake \ |
| 58 | .. | 58 | .. |
| @@ -57,7 +57,7 @@ cmake \ | @@ -57,7 +57,7 @@ cmake \ | ||
| 57 | -DSHERPA_ONNX_ENABLE_BINARY=OFF \ | 57 | -DSHERPA_ONNX_ENABLE_BINARY=OFF \ |
| 58 | -DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \ | 58 | -DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \ |
| 59 | .. | 59 | .. |
| 60 | -make -j10 | 60 | +make -j3 |
| 61 | make install | 61 | make install |
| 62 | 62 | ||
| 63 | ls -lh install/bin/wasm/nodejs | 63 | ls -lh install/bin/wasm/nodejs |
| @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {highWaterMark: 4096}) | @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {highWaterMark: 4096}) | ||
| 100 | 100 | ||
| 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); | 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); |
| 102 | recognizer.decode(stream); | 102 | recognizer.decode(stream); |
| 103 | - const text = recognizer.getResult(stream); | 103 | + const text = recognizer.getResult(stream).text; |
| 104 | console.log(text); | 104 | console.log(text); |
| 105 | 105 | ||
| 106 | stream.free(); | 106 | stream.free(); |
| @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | ||
| 100 | 100 | ||
| 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); | 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); |
| 102 | recognizer.decode(stream); | 102 | recognizer.decode(stream); |
| 103 | - const text = recognizer.getResult(stream); | 103 | + const text = recognizer.getResult(stream).text; |
| 104 | console.log(text); | 104 | console.log(text); |
| 105 | 105 | ||
| 106 | stream.free(); | 106 | stream.free(); |
| @@ -101,7 +101,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | @@ -101,7 +101,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | ||
| 101 | 101 | ||
| 102 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); | 102 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); |
| 103 | recognizer.decode(stream); | 103 | recognizer.decode(stream); |
| 104 | - const text = recognizer.getResult(stream); | 104 | + const text = recognizer.getResult(stream).text; |
| 105 | console.log(text); | 105 | console.log(text); |
| 106 | 106 | ||
| 107 | stream.free(); | 107 | stream.free(); |
| @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | @@ -100,7 +100,7 @@ fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | ||
| 100 | 100 | ||
| 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); | 101 | stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); |
| 102 | recognizer.decode(stream); | 102 | recognizer.decode(stream); |
| 103 | - const text = recognizer.getResult(stream); | 103 | + const text = recognizer.getResult(stream).text; |
| 104 | console.log(text); | 104 | console.log(text); |
| 105 | 105 | ||
| 106 | stream.free(); | 106 | stream.free(); |
| @@ -86,7 +86,7 @@ ai.on('data', data => { | @@ -86,7 +86,7 @@ ai.on('data', data => { | ||
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | const isEndpoint = recognizer.isEndpoint(stream); | 88 | const isEndpoint = recognizer.isEndpoint(stream); |
| 89 | - const text = recognizer.getResult(stream); | 89 | + const text = recognizer.getResult(stream).text; |
| 90 | 90 | ||
| 91 | if (text.length > 0 && lastText != text) { | 91 | if (text.length > 0 && lastText != text) { |
| 92 | lastText = text; | 92 | lastText = text; |
| @@ -75,7 +75,7 @@ function decode(samples) { | @@ -75,7 +75,7 @@ function decode(samples) { | ||
| 75 | while (recognizer.isReady(stream)) { | 75 | while (recognizer.isReady(stream)) { |
| 76 | recognizer.decode(stream); | 76 | recognizer.decode(stream); |
| 77 | } | 77 | } |
| 78 | - const text = recognizer.getResult(stream); | 78 | + const text = recognizer.getResult(stream).text; |
| 79 | console.log(text); | 79 | console.log(text); |
| 80 | } | 80 | } |
| 81 | 81 |
| @@ -88,7 +88,7 @@ ai.on('data', data => { | @@ -88,7 +88,7 @@ ai.on('data', data => { | ||
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | const isEndpoint = recognizer.isEndpoint(stream); | 90 | const isEndpoint = recognizer.isEndpoint(stream); |
| 91 | - const text = recognizer.getResult(stream); | 91 | + const text = recognizer.getResult(stream).text; |
| 92 | 92 | ||
| 93 | if (text.length > 0 && lastText != text) { | 93 | if (text.length > 0 && lastText != text) { |
| 94 | lastText = text; | 94 | lastText = text; |
| @@ -77,7 +77,7 @@ function decode(samples) { | @@ -77,7 +77,7 @@ function decode(samples) { | ||
| 77 | while (recognizer.isReady(stream)) { | 77 | while (recognizer.isReady(stream)) { |
| 78 | recognizer.decode(stream); | 78 | recognizer.decode(stream); |
| 79 | } | 79 | } |
| 80 | - const text = recognizer.getResult(stream); | 80 | + const text = recognizer.getResult(stream).text; |
| 81 | console.log(text); | 81 | console.log(text); |
| 82 | } | 82 | } |
| 83 | 83 |
| @@ -74,7 +74,7 @@ function decode(samples) { | @@ -74,7 +74,7 @@ function decode(samples) { | ||
| 74 | while (recognizer.isReady(stream)) { | 74 | while (recognizer.isReady(stream)) { |
| 75 | recognizer.decode(stream); | 75 | recognizer.decode(stream); |
| 76 | } | 76 | } |
| 77 | - const text = recognizer.getResult(stream); | 77 | + const text = recognizer.getResult(stream).text; |
| 78 | console.log(text); | 78 | console.log(text); |
| 79 | } | 79 | } |
| 80 | 80 |
| @@ -75,7 +75,7 @@ function decode(samples) { | @@ -75,7 +75,7 @@ function decode(samples) { | ||
| 75 | while (recognizer.isReady(stream)) { | 75 | while (recognizer.isReady(stream)) { |
| 76 | recognizer.decode(stream); | 76 | recognizer.decode(stream); |
| 77 | } | 77 | } |
| 78 | - const text = recognizer.getResult(stream); | 78 | + const text = recognizer.getResult(stream).text; |
| 79 | console.log(text); | 79 | console.log(text); |
| 80 | } | 80 | } |
| 81 | 81 |
| @@ -243,6 +243,20 @@ void DestroyOnlineRecognizerResult(const SherpaOnnxOnlineRecognizerResult *r) { | @@ -243,6 +243,20 @@ void DestroyOnlineRecognizerResult(const SherpaOnnxOnlineRecognizerResult *r) { | ||
| 243 | } | 243 | } |
| 244 | } | 244 | } |
| 245 | 245 | ||
| 246 | +const char *GetOnlineStreamResultAsJson( | ||
| 247 | + const SherpaOnnxOnlineRecognizer *recognizer, | ||
| 248 | + const SherpaOnnxOnlineStream *stream) { | ||
| 249 | + sherpa_onnx::OnlineRecognizerResult result = | ||
| 250 | + recognizer->impl->GetResult(stream->impl.get()); | ||
| 251 | + std::string json = result.AsJsonString(); | ||
| 252 | + char *pJson = new char[json.size() + 1]; | ||
| 253 | + std::copy(json.begin(), json.end(), pJson); | ||
| 254 | + pJson[json.size()] = 0; | ||
| 255 | + return pJson; | ||
| 256 | +} | ||
| 257 | + | ||
| 258 | +void DestroyOnlineStreamResultJson(const char *s) { delete[] s; } | ||
| 259 | + | ||
| 246 | void Reset(const SherpaOnnxOnlineRecognizer *recognizer, | 260 | void Reset(const SherpaOnnxOnlineRecognizer *recognizer, |
| 247 | const SherpaOnnxOnlineStream *stream) { | 261 | const SherpaOnnxOnlineStream *stream) { |
| 248 | recognizer->impl->Reset(stream->impl.get()); | 262 | recognizer->impl->Reset(stream->impl.get()); |
| @@ -409,7 +423,7 @@ void DecodeMultipleOfflineStreams(SherpaOnnxOfflineRecognizer *recognizer, | @@ -409,7 +423,7 @@ void DecodeMultipleOfflineStreams(SherpaOnnxOfflineRecognizer *recognizer, | ||
| 409 | } | 423 | } |
| 410 | 424 | ||
| 411 | const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( | 425 | const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( |
| 412 | - SherpaOnnxOfflineStream *stream) { | 426 | + const SherpaOnnxOfflineStream *stream) { |
| 413 | const sherpa_onnx::OfflineRecognitionResult &result = | 427 | const sherpa_onnx::OfflineRecognitionResult &result = |
| 414 | stream->impl->GetResult(); | 428 | stream->impl->GetResult(); |
| 415 | const auto &text = result.text; | 429 | const auto &text = result.text; |
| @@ -444,6 +458,19 @@ void DestroyOfflineRecognizerResult( | @@ -444,6 +458,19 @@ void DestroyOfflineRecognizerResult( | ||
| 444 | } | 458 | } |
| 445 | } | 459 | } |
| 446 | 460 | ||
| 461 | +const char *GetOfflineStreamResultAsJson( | ||
| 462 | + const SherpaOnnxOfflineStream *stream) { | ||
| 463 | + const sherpa_onnx::OfflineRecognitionResult &result = | ||
| 464 | + stream->impl->GetResult(); | ||
| 465 | + std::string json = result.AsJsonString(); | ||
| 466 | + char *pJson = new char[json.size() + 1]; | ||
| 467 | + std::copy(json.begin(), json.end(), pJson); | ||
| 468 | + pJson[json.size()] = 0; | ||
| 469 | + return pJson; | ||
| 470 | +} | ||
| 471 | + | ||
| 472 | +void DestroyOfflineStreamResultJson(const char *s) { delete[] s; } | ||
| 473 | + | ||
| 447 | // ============================================================ | 474 | // ============================================================ |
| 448 | // For Keyword Spot | 475 | // For Keyword Spot |
| 449 | // ============================================================ | 476 | // ============================================================ |
| @@ -286,6 +286,16 @@ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult( | @@ -286,6 +286,16 @@ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult( | ||
| 286 | SHERPA_ONNX_API void DestroyOnlineRecognizerResult( | 286 | SHERPA_ONNX_API void DestroyOnlineRecognizerResult( |
| 287 | const SherpaOnnxOnlineRecognizerResult *r); | 287 | const SherpaOnnxOnlineRecognizerResult *r); |
| 288 | 288 | ||
| 289 | +/// Return the result as a json string. | ||
| 290 | +/// The user has to invoke | ||
| 291 | +/// DestroyOnlineStreamResultJson() | ||
| 292 | +/// to free the returned pointer to avoid memory leak | ||
| 293 | +SHERPA_ONNX_API const char *GetOnlineStreamResultAsJson( | ||
| 294 | + const SherpaOnnxOnlineRecognizer *recognizer, | ||
| 295 | + const SherpaOnnxOnlineStream *stream); | ||
| 296 | + | ||
| 297 | +SHERPA_ONNX_API void DestroyOnlineStreamResultJson(const char *s); | ||
| 298 | + | ||
| 289 | /// Reset an OnlineStream , which clears the neural network model state | 299 | /// Reset an OnlineStream , which clears the neural network model state |
| 290 | /// and the state for decoding. | 300 | /// and the state for decoding. |
| 291 | /// | 301 | /// |
| @@ -482,7 +492,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerResult { | @@ -482,7 +492,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerResult { | ||
| 482 | /// DestroyOnlineRecognizerResult() to free the returned pointer to | 492 | /// DestroyOnlineRecognizerResult() to free the returned pointer to |
| 483 | /// avoid memory leak. | 493 | /// avoid memory leak. |
| 484 | SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( | 494 | SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( |
| 485 | - SherpaOnnxOfflineStream *stream); | 495 | + const SherpaOnnxOfflineStream *stream); |
| 486 | 496 | ||
| 487 | /// Destroy the pointer returned by GetOfflineStreamResult(). | 497 | /// Destroy the pointer returned by GetOfflineStreamResult(). |
| 488 | /// | 498 | /// |
| @@ -490,6 +500,14 @@ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( | @@ -490,6 +500,14 @@ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( | ||
| 490 | SHERPA_ONNX_API void DestroyOfflineRecognizerResult( | 500 | SHERPA_ONNX_API void DestroyOfflineRecognizerResult( |
| 491 | const SherpaOnnxOfflineRecognizerResult *r); | 501 | const SherpaOnnxOfflineRecognizerResult *r); |
| 492 | 502 | ||
| 503 | +/// Return the result as a json string. | ||
| 504 | +/// The user has to use DestroyOfflineStreamResultJson() | ||
| 505 | +/// to free the returned pointer to avoid memory leak | ||
| 506 | +SHERPA_ONNX_API const char *GetOfflineStreamResultAsJson( | ||
| 507 | + const SherpaOnnxOfflineStream *stream); | ||
| 508 | + | ||
| 509 | +SHERPA_ONNX_API void DestroyOfflineStreamResultJson(const char *s); | ||
| 510 | + | ||
| 493 | // ============================================================ | 511 | // ============================================================ |
| 494 | // For Keyword Spot | 512 | // For Keyword Spot |
| 495 | // ============================================================ | 513 | // ============================================================ |
| @@ -13,10 +13,14 @@ set(exported_functions | @@ -13,10 +13,14 @@ set(exported_functions | ||
| 13 | CreateOnlineRecognizer | 13 | CreateOnlineRecognizer |
| 14 | CreateOnlineStream | 14 | CreateOnlineStream |
| 15 | DecodeOnlineStream | 15 | DecodeOnlineStream |
| 16 | + DestroyOfflineStreamResultJson | ||
| 16 | DestroyOnlineRecognizer | 17 | DestroyOnlineRecognizer |
| 17 | DestroyOnlineRecognizerResult | 18 | DestroyOnlineRecognizerResult |
| 18 | DestroyOnlineStream | 19 | DestroyOnlineStream |
| 20 | + DestroyOnlineStreamResultJson | ||
| 21 | + GetOfflineStreamResultAsJson | ||
| 19 | GetOnlineStreamResult | 22 | GetOnlineStreamResult |
| 23 | + GetOnlineStreamResultAsJson | ||
| 20 | InputFinished | 24 | InputFinished |
| 21 | IsEndpoint | 25 | IsEndpoint |
| 22 | IsOnlineStreamReady | 26 | IsOnlineStreamReady |
| @@ -108,7 +108,7 @@ if (navigator.mediaDevices.getUserMedia) { | @@ -108,7 +108,7 @@ if (navigator.mediaDevices.getUserMedia) { | ||
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | let isEndpoint = recognizer.isEndpoint(recognizer_stream); | 110 | let isEndpoint = recognizer.isEndpoint(recognizer_stream); |
| 111 | - let result = recognizer.getResult(recognizer_stream); | 111 | + let result = recognizer.getResult(recognizer_stream).text; |
| 112 | 112 | ||
| 113 | 113 | ||
| 114 | if (result.length > 0 && lastResult != result) { | 114 | if (result.length > 0 && lastResult != result) { |
| @@ -661,13 +661,12 @@ class OfflineRecognizer { | @@ -661,13 +661,12 @@ class OfflineRecognizer { | ||
| 661 | } | 661 | } |
| 662 | 662 | ||
| 663 | getResult(stream) { | 663 | getResult(stream) { |
| 664 | - const r = this.Module._GetOfflineStreamResult(stream.handle); | 664 | + const r = this.Module._GetOfflineStreamResultAsJson(stream.handle); |
| 665 | + const jsonStr = this.Module.UTF8ToString(r); | ||
| 666 | + const ans = JSON.parse(jsonStr); | ||
| 667 | + this.Module._DestroyOfflineStreamResultJson(r); | ||
| 665 | 668 | ||
| 666 | - const textPtr = this.Module.getValue(r, 'i8*'); | ||
| 667 | - const text = this.Module.UTF8ToString(textPtr); | ||
| 668 | - | ||
| 669 | - this.Module._DestroyOfflineRecognizerResult(r); | ||
| 670 | - return text; | 669 | + return ans; |
| 671 | } | 670 | } |
| 672 | }; | 671 | }; |
| 673 | 672 | ||
| @@ -750,11 +749,13 @@ class OnlineRecognizer { | @@ -750,11 +749,13 @@ class OnlineRecognizer { | ||
| 750 | } | 749 | } |
| 751 | 750 | ||
| 752 | getResult(stream) { | 751 | getResult(stream) { |
| 753 | - const r = this.Module._GetOnlineStreamResult(this.handle, stream.handle); | ||
| 754 | - const textPtr = this.Module.getValue(r, 'i8*'); | ||
| 755 | - const text = this.Module.UTF8ToString(textPtr); | ||
| 756 | - this.Module._DestroyOnlineRecognizerResult(r); | ||
| 757 | - return text; | 752 | + const r = |
| 753 | + this.Module._GetOnlineStreamResultAsJson(this.handle, stream.handle); | ||
| 754 | + const jsonStr = this.Module.UTF8ToString(r); | ||
| 755 | + const ans = JSON.parse(jsonStr); | ||
| 756 | + this.Module._DestroyOnlineStreamResultJson(r); | ||
| 757 | + | ||
| 758 | + return ans; | ||
| 758 | } | 759 | } |
| 759 | } | 760 | } |
| 760 | 761 |
| @@ -21,22 +21,26 @@ set(exported_functions | @@ -21,22 +21,26 @@ set(exported_functions | ||
| 21 | DestroyOnlineRecognizer | 21 | DestroyOnlineRecognizer |
| 22 | DestroyOnlineRecognizerResult | 22 | DestroyOnlineRecognizerResult |
| 23 | DestroyOnlineStream | 23 | DestroyOnlineStream |
| 24 | + DestroyOnlineStreamResultJson | ||
| 24 | GetOnlineStreamResult | 25 | GetOnlineStreamResult |
| 26 | + GetOnlineStreamResultAsJson | ||
| 25 | InputFinished | 27 | InputFinished |
| 26 | IsEndpoint | 28 | IsEndpoint |
| 27 | IsOnlineStreamReady | 29 | IsOnlineStreamReady |
| 28 | Reset | 30 | Reset |
| 29 | # non-streaming ASR | 31 | # non-streaming ASR |
| 30 | - PrintOfflineRecognizerConfig | 32 | + AcceptWaveformOffline |
| 31 | CreateOfflineRecognizer | 33 | CreateOfflineRecognizer |
| 32 | - DestroyOfflineRecognizer | ||
| 33 | CreateOfflineStream | 34 | CreateOfflineStream |
| 34 | - DestroyOfflineStream | ||
| 35 | - AcceptWaveformOffline | ||
| 36 | - DecodeOfflineStream | ||
| 37 | DecodeMultipleOfflineStreams | 35 | DecodeMultipleOfflineStreams |
| 38 | - GetOfflineStreamResult | 36 | + DecodeOfflineStream |
| 37 | + DestroyOfflineRecognizer | ||
| 39 | DestroyOfflineRecognizerResult | 38 | DestroyOfflineRecognizerResult |
| 39 | + DestroyOfflineStream | ||
| 40 | + DestroyOfflineStreamResultJson | ||
| 41 | + GetOfflineStreamResult | ||
| 42 | + GetOfflineStreamResultAsJson | ||
| 43 | + PrintOfflineRecognizerConfig | ||
| 40 | # online kws | 44 | # online kws |
| 41 | CreateKeywordSpotter | 45 | CreateKeywordSpotter |
| 42 | DestroyKeywordSpotter | 46 | DestroyKeywordSpotter |
-
请 注册 或 登录 后发表评论