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++.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
"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
|
// Please download test files from
|
||||||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
// 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 = {
|
const config = {
|
||||||
'featConfig': {
|
'featConfig': {
|
||||||
'sampleRate': 16000,
|
'sampleRate': 16000,
|
||||||
@@ -12,9 +25,11 @@ const config = {
|
|||||||
'senseVoice': {
|
'senseVoice': {
|
||||||
'model':
|
'model':
|
||||||
'./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx',
|
'./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx',
|
||||||
|
// 'model': model,
|
||||||
'useInverseTextNormalization': 1,
|
'useInverseTextNormalization': 1,
|
||||||
},
|
},
|
||||||
'tokens': './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt',
|
'tokens': './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt',
|
||||||
|
// 'tokens': tokens,
|
||||||
'numThreads': 2,
|
'numThreads': 2,
|
||||||
'provider': 'cpu',
|
'provider': 'cpu',
|
||||||
'debug': 1,
|
'debug': 1,
|
||||||
|
|||||||
@@ -7,17 +7,24 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \
|
#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \
|
||||||
do { \
|
do { \
|
||||||
if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \
|
if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \
|
||||||
Napi::String _str = o.Get(#js_name).As<Napi::String>(); \
|
Napi::String _str = o.Get(#js_name).As<Napi::String>(); \
|
||||||
std::string s = _str.Utf8Value(); \
|
std::string s = _str.Utf8Value(); \
|
||||||
char *p = new char[s.size() + 1]; \
|
char *p = new char[s.size() + 1]; \
|
||||||
std::copy(s.begin(), s.end(), p); \
|
std::copy(s.begin(), s.end(), p); \
|
||||||
p[s.size()] = 0; \
|
p[s.size()] = 0; \
|
||||||
\
|
\
|
||||||
c.c_name = p; \
|
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)
|
} while (0)
|
||||||
|
|
||||||
#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \
|
#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \
|
||||||
|
|||||||
Reference in New Issue
Block a user