Fangjun Kuang
Committed by GitHub

Support passing utf-8 strings from JavaScript to C++. (#1355)

We first convert utf-16 strings to Uint8Array and then we
pass the array to C++.
{
"dependencies": {
"sherpa-onnx-node": "^1.10.26"
"sherpa-onnx-node": "^1.10.27"
}
}
... ...
... ... @@ -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,
... ... @@ -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,
... ...
... ... @@ -17,6 +17,13 @@
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)
... ...