Skip to content

Commit

Permalink
Support passing utf-8 strings from JavaScript to C++.
Browse files Browse the repository at this point in the history
We first convert utf-16 strings to Uint8Array and then we
pass the array to C++.
  • Loading branch information
csukuangfj committed Sep 18, 2024
1 parent bf06b26 commit 2be8d58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion nodejs-addon-examples/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"sherpa-onnx-node": "^1.10.26"
"sherpa-onnx-node": "^1.10.27"
}
}
15 changes: 15 additions & 0 deletions nodejs-addon-examples/test_asr_non_streaming_sense_voice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ const sherpa_onnx = require('sherpa-onnx-node');

// Please download test files from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models


// If your path contains non-ascii characters, e.g., Chinese, you can use
// the following code
//

// let encoder = new TextEncoder();
// let tokens = encoder.encode(
// './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/测试.txt');
// let model = encoder.encode(
// './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/测试.int8.onnx');


const config = {
'featConfig': {
'sampleRate': 16000,
Expand All @@ -12,9 +25,11 @@ const config = {
'senseVoice': {
'model':
'./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx',
// 'model': model,
'useInverseTextNormalization': 1,
},
'tokens': './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt',
// 'tokens': tokens,
'numThreads': 2,
'provider': 'cpu',
'debug': 1,
Expand Down
29 changes: 18 additions & 11 deletions scripts/node-addon-api/src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
#include <algorithm>
#include <string>

#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \
do { \
if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \
Napi::String _str = o.Get(#js_name).As<Napi::String>(); \
std::string s = _str.Utf8Value(); \
char *p = new char[s.size() + 1]; \
std::copy(s.begin(), s.end(), p); \
p[s.size()] = 0; \
\
c.c_name = p; \
} \
#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \
do { \
if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \
Napi::String _str = o.Get(#js_name).As<Napi::String>(); \
std::string s = _str.Utf8Value(); \
char *p = new char[s.size() + 1]; \
std::copy(s.begin(), s.end(), p); \
p[s.size()] = 0; \
\
c.c_name = p; \
} else if (o.Has(#js_name) && o.Get(#js_name).IsTypedArray()) { \
Napi::Uint8Array _array = o.Get(#js_name).As<Napi::Uint8Array>(); \
char *p = new char[_array.ElementLength() + 1]; \
std::copy(_array.Data(), _array.Data() + _array.ElementLength(), p); \
p[_array.ElementLength()] = '\0'; \
\
c.c_name = p; \
} \
} while (0)

#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \
Expand Down

0 comments on commit 2be8d58

Please sign in to comment.