Refactor node-addon-api to remove duplicate. (#873)
This commit is contained in:
@@ -77,4 +77,19 @@ jobs:
|
|||||||
|
|
||||||
cd /shared
|
cd /shared
|
||||||
|
|
||||||
|
d=nodejs-addon-examples
|
||||||
|
echo "dir: $d"
|
||||||
|
cd $d
|
||||||
|
npm install --verbose
|
||||||
|
git status
|
||||||
|
ls -lh
|
||||||
|
ls -lh node_modules
|
||||||
|
|
||||||
|
export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH
|
||||||
|
export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH
|
||||||
|
export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH
|
||||||
|
export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-arm64:$LD_LIBRARY_PATH
|
||||||
|
|
||||||
|
cd ../
|
||||||
|
|
||||||
.github/scripts/test-nodejs-addon-npm.sh
|
.github/scripts/test-nodejs-addon-npm.sh
|
||||||
|
|||||||
37
scripts/node-addon-api/src/macros.h
Normal file
37
scripts/node-addon-api/src/macros.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// scripts/node-addon-api/src/macros.h
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
#ifndef SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_
|
||||||
|
#define SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_
|
||||||
|
|
||||||
|
#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; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \
|
||||||
|
do { \
|
||||||
|
if (o.Has(#js_name) && o.Get(#js_name).IsNumber()) { \
|
||||||
|
c.c_name = o.Get(#js_name).As<Napi::Number>().Int32Value(); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SHERPA_ONNX_ASSIGN_ATTR_FLOAT(c_name, js_name) \
|
||||||
|
do { \
|
||||||
|
if (o.Has(#js_name) && o.Get(#js_name).IsNumber()) { \
|
||||||
|
c.c_name = o.Get(#js_name).As<Napi::Number>().FloatValue(); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#endif // SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_
|
||||||
@@ -3,7 +3,8 @@
|
|||||||
// Copyright (c) 2024 Xiaomi Corporation
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "napi.h" // NOLINT
|
#include "macros.h" // NOLINT
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
#include "sherpa-onnx/c-api/c-api.h"
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
|
||||||
// defined in ./streaming-asr.cc
|
// defined in ./streaming-asr.cc
|
||||||
@@ -11,172 +12,87 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj);
|
|||||||
|
|
||||||
static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig(
|
static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOfflineTransducerModelConfig config;
|
SherpaOnnxOfflineTransducerModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) {
|
if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("transducer").As<Napi::Object>();
|
Napi::Object o = obj.Get("transducer").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder);
|
||||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder);
|
||||||
std::string s = encoder.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(joiner, joiner);
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.encoder = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
|
||||||
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
|
||||||
std::string s = decoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.decoder = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("joiner") && o.Get("joiner").IsString()) {
|
|
||||||
Napi::String joiner = o.Get("joiner").As<Napi::String>();
|
|
||||||
std::string s = joiner.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.joiner = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig(
|
static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOfflineParaformerModelConfig config;
|
SherpaOnnxOfflineParaformerModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) {
|
if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("paraformer").As<Napi::Object>();
|
Napi::Object o = obj.Get("paraformer").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
|
||||||
std::string s = model.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.model = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig(
|
static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOfflineNemoEncDecCtcModelConfig config;
|
SherpaOnnxOfflineNemoEncDecCtcModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("nemoCtc") || !obj.Get("nemoCtc").IsObject()) {
|
if (!obj.Has("nemoCtc") || !obj.Get("nemoCtc").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("nemoCtc").As<Napi::Object>();
|
Napi::Object o = obj.Get("nemoCtc").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
|
||||||
std::string s = model.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.model = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig(
|
static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOfflineWhisperModelConfig config;
|
SherpaOnnxOfflineWhisperModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("whisper") || !obj.Get("whisper").IsObject()) {
|
if (!obj.Has("whisper") || !obj.Get("whisper").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("whisper").As<Napi::Object>();
|
Napi::Object o = obj.Get("whisper").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder);
|
||||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder);
|
||||||
std::string s = encoder.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(language, language);
|
||||||
char *p = new char[s.size() + 1];
|
SHERPA_ONNX_ASSIGN_ATTR_STR(task, languagek);
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.encoder = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
|
||||||
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
|
||||||
std::string s = decoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.decoder = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("language") && o.Get("language").IsString()) {
|
|
||||||
Napi::String language = o.Get("language").As<Napi::String>();
|
|
||||||
std::string s = language.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.language = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("task") && o.Get("task").IsString()) {
|
|
||||||
Napi::String task = o.Get("task").As<Napi::String>();
|
|
||||||
std::string s = task.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.task = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig(
|
static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOfflineTdnnModelConfig config;
|
SherpaOnnxOfflineTdnnModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("tdnn") || !obj.Get("tdnn").IsObject()) {
|
if (!obj.Has("tdnn") || !obj.Get("tdnn").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("tdnn").As<Napi::Object>();
|
Napi::Object o = obj.Get("tdnn").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
|
||||||
std::string s = model.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.model = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) {
|
static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) {
|
||||||
@@ -195,19 +111,8 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) {
|
|||||||
c.whisper = GetOfflineWhisperModelConfig(o);
|
c.whisper = GetOfflineWhisperModelConfig(o);
|
||||||
c.tdnn = GetOfflineTdnnModelConfig(o);
|
c.tdnn = GetOfflineTdnnModelConfig(o);
|
||||||
|
|
||||||
if (o.Has("tokens") && o.Get("tokens").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens);
|
||||||
Napi::String tokens = o.Get("tokens").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads);
|
||||||
std::string s = tokens.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.tokens = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
|
||||||
c.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("debug") &&
|
if (o.Has("debug") &&
|
||||||
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
||||||
@@ -218,25 +123,8 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.Has("provider") && o.Get("provider").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
|
||||||
Napi::String provider = o.Get("provider").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
|
||||||
std::string s = provider.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.provider = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("modelType") && o.Get("modelType").IsString()) {
|
|
||||||
Napi::String model_type = o.Get("modelType").As<Napi::String>();
|
|
||||||
std::string s = model_type.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.model_type = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -251,19 +139,8 @@ static SherpaOnnxOfflineLMConfig GetOfflineLMConfig(Napi::Object obj) {
|
|||||||
|
|
||||||
Napi::Object o = obj.Get("lmConfig").As<Napi::Object>();
|
Napi::Object o = obj.Get("lmConfig").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(scale, scale);
|
||||||
std::string s = model.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.model = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("scale") && o.Get("scale").IsNumber()) {
|
|
||||||
c.scale = o.Get("scale").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -295,34 +172,10 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) {
|
|||||||
c.model_config = GetOfflineModelConfig(o);
|
c.model_config = GetOfflineModelConfig(o);
|
||||||
c.lm_config = GetOfflineLMConfig(o);
|
c.lm_config = GetOfflineLMConfig(o);
|
||||||
|
|
||||||
if (o.Has("decodingMethod") && o.Get("decodingMethod").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod);
|
||||||
Napi::String decoding_method = o.Get("decodingMethod").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths);
|
||||||
std::string s = decoding_method.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(hotwords_file, hotwordsFile);
|
||||||
char *p = new char[s.size() + 1];
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore);
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.decoding_method = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("maxActivePaths") && o.Get("maxActivePaths").IsNumber()) {
|
|
||||||
c.max_active_paths =
|
|
||||||
o.Get("maxActivePaths").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("hotwordsFile") && o.Get("hotwordsFile").IsString()) {
|
|
||||||
Napi::String hotwords_file = o.Get("hotwordsFile").As<Napi::String>();
|
|
||||||
std::string s = hotwords_file.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.hotwords_file = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("hotwordsScore") && o.Get("hotwordsScore").IsNumber()) {
|
|
||||||
c.hotwords_score = o.Get("hotwordsScore").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
SherpaOnnxOfflineRecognizer *recognizer = CreateOfflineRecognizer(&c);
|
SherpaOnnxOfflineRecognizer *recognizer = CreateOfflineRecognizer(&c);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "napi.h" // NOLINT
|
#include "macros.h" // NOLINT
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
#include "sherpa-onnx/c-api/c-api.h"
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
|
||||||
static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig(
|
static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig(
|
||||||
@@ -17,68 +18,14 @@ static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("vits").As<Napi::Object>();
|
Napi::Object o = obj.Get("vits").As<Napi::Object>();
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(lexicon, lexicon);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens);
|
||||||
std::string s = model.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(data_dir, dataDir);
|
||||||
char *p = new char[s.size() + 1];
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(noise_scale, noiseScale);
|
||||||
std::copy(s.begin(), s.end(), p);
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(noise_scale_w, noiseScaleW);
|
||||||
p[s.size()] = 0;
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(length_scale, lengthScale);
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_STR(dict_dir, dictDir);
|
||||||
c.model = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("lexicon") && o.Get("lexicon").IsString()) {
|
|
||||||
Napi::String lexicon = o.Get("lexicon").As<Napi::String>();
|
|
||||||
std::string s = lexicon.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.lexicon = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("tokens") && o.Get("tokens").IsString()) {
|
|
||||||
Napi::String tokens = o.Get("tokens").As<Napi::String>();
|
|
||||||
std::string s = tokens.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.tokens = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("dataDir") && o.Get("dataDir").IsString()) {
|
|
||||||
Napi::String data_dir = o.Get("dataDir").As<Napi::String>();
|
|
||||||
std::string s = data_dir.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.data_dir = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("noiseScale") && o.Get("noiseScale").IsNumber()) {
|
|
||||||
c.noise_scale = o.Get("noiseScale").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("noiseScaleW") && o.Get("noiseScaleW").IsNumber()) {
|
|
||||||
c.noise_scale_w = o.Get("noiseScaleW").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("lengthScale") && o.Get("lengthScale").IsNumber()) {
|
|
||||||
c.length_scale = o.Get("lengthScale").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("dictDir") && o.Get("dictDir").IsString()) {
|
|
||||||
Napi::String dict_dir = o.Get("dictDir").As<Napi::String>();
|
|
||||||
std::string s = dict_dir.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.dict_dir = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -96,9 +43,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig(
|
|||||||
|
|
||||||
c.vits = GetOfflineTtsVitsModelConfig(o);
|
c.vits = GetOfflineTtsVitsModelConfig(o);
|
||||||
|
|
||||||
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, num_threads);
|
||||||
c.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("debug") &&
|
if (o.Has("debug") &&
|
||||||
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
||||||
@@ -109,15 +54,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.Has("provider") && o.Get("provider").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
|
||||||
Napi::String provider = o.Get("provider").As<Napi::String>();
|
|
||||||
std::string s = provider.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.provider = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -148,30 +85,9 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
|
|||||||
|
|
||||||
c.model = GetOfflineTtsModelConfig(o);
|
c.model = GetOfflineTtsModelConfig(o);
|
||||||
|
|
||||||
if (o.Has("ruleFsts") && o.Get("ruleFsts").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fsts, ruleFsts);
|
||||||
Napi::String rule_fsts = o.Get("ruleFsts").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_num_sentences, maxNumSentences);
|
||||||
std::string s = rule_fsts.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fars, ruleFars);
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.rule_fsts = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("maxNumSentences") && o.Get("maxNumSentences").IsNumber()) {
|
|
||||||
c.max_num_sentences =
|
|
||||||
o.Get("maxNumSentences").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("ruleFars") && o.Get("ruleFars").IsString()) {
|
|
||||||
Napi::String rule_fars = o.Get("ruleFars").As<Napi::String>();
|
|
||||||
std::string s = rule_fars.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.rule_fars = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c);
|
SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "napi.h" // NOLINT
|
#include "macros.h" // NOLINT
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
#include "sherpa-onnx/c-api/c-api.h"
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
|
||||||
static SherpaOnnxSpokenLanguageIdentificationWhisperConfig
|
static SherpaOnnxSpokenLanguageIdentificationWhisperConfig
|
||||||
@@ -18,29 +19,9 @@ GetSpokenLanguageIdentificationWhisperConfig(Napi::Object obj) {
|
|||||||
|
|
||||||
Napi::Object o = obj.Get("whisper").As<Napi::Object>();
|
Napi::Object o = obj.Get("whisper").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder);
|
||||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder);
|
||||||
std::string s = encoder.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(tail_paddings, tailPaddings);
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.encoder = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
|
||||||
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
|
||||||
std::string s = decoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.decoder = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("tailPaddings") && o.Get("tailPaddings").IsNumber()) {
|
|
||||||
c.tail_paddings = o.Get("tailPaddings").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -70,9 +51,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) {
|
|||||||
memset(&c, 0, sizeof(c));
|
memset(&c, 0, sizeof(c));
|
||||||
c.whisper = GetSpokenLanguageIdentificationWhisperConfig(o);
|
c.whisper = GetSpokenLanguageIdentificationWhisperConfig(o);
|
||||||
|
|
||||||
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads);
|
||||||
c.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("debug") &&
|
if (o.Has("debug") &&
|
||||||
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
||||||
@@ -82,16 +61,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) {
|
|||||||
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
|
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
|
||||||
if (o.Has("provider") && o.Get("provider").IsString()) {
|
|
||||||
Napi::String provider = o.Get("provider").As<Napi::String>();
|
|
||||||
std::string s = provider.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.provider = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SherpaOnnxSpokenLanguageIdentification *slid =
|
const SherpaOnnxSpokenLanguageIdentification *slid =
|
||||||
SherpaOnnxCreateSpokenLanguageIdentification(&c);
|
SherpaOnnxCreateSpokenLanguageIdentification(&c);
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
// Copyright (c) 2024 Xiaomi Corporation
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "napi.h" // NOLINT
|
#include "macros.h" // NOLINT
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
#include "sherpa-onnx/c-api/c-api.h"
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
/*
|
/*
|
||||||
{
|
{
|
||||||
@@ -14,26 +15,19 @@
|
|||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
||||||
SherpaOnnxFeatureConfig config;
|
SherpaOnnxFeatureConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("featConfig") || !obj.Get("featConfig").IsObject()) {
|
if (!obj.Has("featConfig") || !obj.Get("featConfig").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object featConfig = obj.Get("featConfig").As<Napi::Object>();
|
Napi::Object o = obj.Get("featConfig").As<Napi::Object>();
|
||||||
|
|
||||||
if (featConfig.Has("sampleRate") && featConfig.Get("sampleRate").IsNumber()) {
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(sample_rate, sampleRate);
|
||||||
config.sample_rate =
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(feature_dim, featureDim);
|
||||||
featConfig.Get("sampleRate").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (featConfig.Has("featureDim") && featConfig.Get("featureDim").IsNumber()) {
|
return c;
|
||||||
config.feature_dim =
|
|
||||||
featConfig.Get("featureDim").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
{
|
{
|
||||||
@@ -47,192 +41,103 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
|||||||
|
|
||||||
static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig(
|
static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOnlineTransducerModelConfig config;
|
SherpaOnnxOnlineTransducerModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) {
|
if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("transducer").As<Napi::Object>();
|
Napi::Object o = obj.Get("transducer").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder);
|
||||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder);
|
||||||
std::string s = encoder.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(joiner, joiner);
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.encoder = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
|
||||||
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
|
||||||
std::string s = decoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.decoder = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("joiner") && o.Get("joiner").IsString()) {
|
|
||||||
Napi::String joiner = o.Get("joiner").As<Napi::String>();
|
|
||||||
std::string s = joiner.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.joiner = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOnlineZipformer2CtcModelConfig
|
static SherpaOnnxOnlineZipformer2CtcModelConfig
|
||||||
GetOnlineZipformer2CtcModelConfig(Napi::Object obj) {
|
GetOnlineZipformer2CtcModelConfig(Napi::Object obj) {
|
||||||
SherpaOnnxOnlineZipformer2CtcModelConfig config;
|
SherpaOnnxOnlineZipformer2CtcModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("zipformer2Ctc") || !obj.Get("zipformer2Ctc").IsObject()) {
|
if (!obj.Has("zipformer2Ctc") || !obj.Get("zipformer2Ctc").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("zipformer2Ctc").As<Napi::Object>();
|
Napi::Object o = obj.Get("zipformer2Ctc").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
|
||||||
std::string s = model.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.model = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig(
|
static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOnlineParaformerModelConfig config;
|
SherpaOnnxOnlineParaformerModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) {
|
if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("paraformer").As<Napi::Object>();
|
Napi::Object o = obj.Get("paraformer").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder);
|
||||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder);
|
||||||
std::string s = encoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.encoder = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
|
||||||
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
|
||||||
std::string s = decoder.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.decoder = p;
|
|
||||||
}
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
|
static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
|
||||||
SherpaOnnxOnlineModelConfig config;
|
SherpaOnnxOnlineModelConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("modelConfig") || !obj.Get("modelConfig").IsObject()) {
|
if (!obj.Has("modelConfig") || !obj.Get("modelConfig").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("modelConfig").As<Napi::Object>();
|
Napi::Object o = obj.Get("modelConfig").As<Napi::Object>();
|
||||||
|
|
||||||
config.transducer = GetOnlineTransducerModelConfig(o);
|
c.transducer = GetOnlineTransducerModelConfig(o);
|
||||||
config.paraformer = GetOnlineParaformerModelConfig(o);
|
c.paraformer = GetOnlineParaformerModelConfig(o);
|
||||||
config.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o);
|
c.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o);
|
||||||
|
|
||||||
if (o.Has("tokens") && o.Get("tokens").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens);
|
||||||
Napi::String tokens = o.Get("tokens").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads);
|
||||||
std::string s = tokens.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.tokens = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
|
||||||
config.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("provider") && o.Get("provider").IsString()) {
|
|
||||||
Napi::String provider = o.Get("provider").As<Napi::String>();
|
|
||||||
std::string s = provider.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.provider = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("debug") &&
|
if (o.Has("debug") &&
|
||||||
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
||||||
if (o.Get("debug").IsBoolean()) {
|
if (o.Get("debug").IsBoolean()) {
|
||||||
config.debug = o.Get("debug").As<Napi::Boolean>().Value();
|
c.debug = o.Get("debug").As<Napi::Boolean>().Value();
|
||||||
} else {
|
} else {
|
||||||
config.debug = o.Get("debug").As<Napi::Number>().Int32Value();
|
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.Has("modelType") && o.Get("modelType").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
|
||||||
Napi::String model_type = o.Get("modelType").As<Napi::String>();
|
|
||||||
std::string s = model_type.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.model_type = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig(
|
static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig(
|
||||||
Napi::Object obj) {
|
Napi::Object obj) {
|
||||||
SherpaOnnxOnlineCtcFstDecoderConfig config;
|
SherpaOnnxOnlineCtcFstDecoderConfig c;
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&c, 0, sizeof(c));
|
||||||
|
|
||||||
if (!obj.Has("ctcFstDecoderConfig") ||
|
if (!obj.Has("ctcFstDecoderConfig") ||
|
||||||
!obj.Get("ctcFstDecoderConfig").IsObject()) {
|
!obj.Get("ctcFstDecoderConfig").IsObject()) {
|
||||||
return config;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("ctcFstDecoderConfig").As<Napi::Object>();
|
Napi::Object o = obj.Get("ctcFstDecoderConfig").As<Napi::Object>();
|
||||||
|
|
||||||
if (o.Has("graph") && o.Get("graph").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(graph, graph);
|
||||||
Napi::String graph = o.Get("graph").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active, maxActive);
|
||||||
std::string s = graph.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
config.graph = p;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("maxActive") && o.Get("maxActive").IsNumber()) {
|
|
||||||
config.max_active = o.Get("maxActive").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
||||||
@@ -254,75 +159,36 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object config = info[0].As<Napi::Object>();
|
Napi::Object o = info[0].As<Napi::Object>();
|
||||||
SherpaOnnxOnlineRecognizerConfig c;
|
SherpaOnnxOnlineRecognizerConfig c;
|
||||||
memset(&c, 0, sizeof(c));
|
memset(&c, 0, sizeof(c));
|
||||||
c.feat_config = GetFeatureConfig(config);
|
c.feat_config = GetFeatureConfig(o);
|
||||||
c.model_config = GetOnlineModelConfig(config);
|
c.model_config = GetOnlineModelConfig(o);
|
||||||
|
|
||||||
if (config.Has("decodingMethod") && config.Get("decodingMethod").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod);
|
||||||
Napi::String decoding_method =
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths);
|
||||||
config.Get("decodingMethod").As<Napi::String>();
|
|
||||||
std::string s = decoding_method.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.decoding_method = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.Has("maxActivePaths") && config.Get("maxActivePaths").IsNumber()) {
|
|
||||||
c.max_active_paths =
|
|
||||||
config.Get("maxActivePaths").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
// enableEndpoint can be either a boolean or an integer
|
// enableEndpoint can be either a boolean or an integer
|
||||||
if (config.Has("enableEndpoint") &&
|
if (o.Has("enableEndpoint") && (o.Get("enableEndpoint").IsNumber() ||
|
||||||
(config.Get("enableEndpoint").IsNumber() ||
|
o.Get("enableEndpoint").IsBoolean())) {
|
||||||
config.Get("enableEndpoint").IsBoolean())) {
|
if (o.Get("enableEndpoint").IsNumber()) {
|
||||||
if (config.Get("enableEndpoint").IsNumber()) {
|
|
||||||
c.enable_endpoint =
|
c.enable_endpoint =
|
||||||
config.Get("enableEndpoint").As<Napi::Number>().Int32Value();
|
o.Get("enableEndpoint").As<Napi::Number>().Int32Value();
|
||||||
} else {
|
} else {
|
||||||
c.enable_endpoint =
|
c.enable_endpoint = o.Get("enableEndpoint").As<Napi::Boolean>().Value();
|
||||||
config.Get("enableEndpoint").As<Napi::Boolean>().Value();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.Has("rule1MinTrailingSilence") &&
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(rule1_min_trailing_silence,
|
||||||
config.Get("rule1MinTrailingSilence").IsNumber()) {
|
rule1MinTrailingSilence);
|
||||||
c.rule1_min_trailing_silence =
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(rule2_min_trailing_silence,
|
||||||
config.Get("rule1MinTrailingSilence").As<Napi::Number>().FloatValue();
|
rule2MinTrailingSilence);
|
||||||
}
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(rule3_min_utterance_length,
|
||||||
|
rule3MinUtteranceLength);
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_STR(hotwords_file, hotwordsFile);
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore);
|
||||||
|
|
||||||
if (config.Has("rule2MinTrailingSilence") &&
|
c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o);
|
||||||
config.Get("rule2MinTrailingSilence").IsNumber()) {
|
|
||||||
c.rule2_min_trailing_silence =
|
|
||||||
config.Get("rule2MinTrailingSilence").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.Has("rule3MinUtteranceLength") &&
|
|
||||||
config.Get("rule3MinUtteranceLength").IsNumber()) {
|
|
||||||
c.rule3_min_utterance_length =
|
|
||||||
config.Get("rule3MinUtteranceLength").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.Has("hotwordsFile") && config.Get("hotwordsFile").IsString()) {
|
|
||||||
Napi::String hotwords_file = config.Get("hotwordsFile").As<Napi::String>();
|
|
||||||
std::string s = hotwords_file.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.hotwords_file = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.Has("hotwordsScore") && config.Get("hotwordsScore").IsNumber()) {
|
|
||||||
c.hotwords_score =
|
|
||||||
config.Get("hotwordsScore").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(config);
|
|
||||||
|
|
||||||
SherpaOnnxOnlineRecognizer *recognizer = CreateOnlineRecognizer(&c);
|
SherpaOnnxOnlineRecognizer *recognizer = CreateOnlineRecognizer(&c);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "napi.h" // NOLINT
|
#include "macros.h" // NOLINT
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
#include "sherpa-onnx/c-api/c-api.h"
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
|
||||||
static Napi::External<SherpaOnnxCircularBuffer> CreateCircularBufferWrapper(
|
static Napi::External<SherpaOnnxCircularBuffer> CreateCircularBufferWrapper(
|
||||||
@@ -247,34 +248,11 @@ static SherpaOnnxSileroVadModelConfig GetSileroVadConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Napi::Object o = obj.Get("sileroVad").As<Napi::Object>();
|
Napi::Object o = obj.Get("sileroVad").As<Napi::Object>();
|
||||||
|
SHERPA_ONNX_ASSIGN_ATTR_STR(model, model);
|
||||||
if (o.Has("model") && o.Get("model").IsString()) {
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(threshold, threshold);
|
||||||
Napi::String model = o.Get("model").As<Napi::String>();
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(min_silence_duration, minSilenceDuration);
|
||||||
std::string s = model.Utf8Value();
|
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(min_speech_duration, minSpeechDuration);
|
||||||
char *p = new char[s.size() + 1];
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(window_size, windowSize);
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.model = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("threshold") && o.Get("threshold").IsNumber()) {
|
|
||||||
c.threshold = o.Get("threshold").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("minSilenceDuration") && o.Get("minSilenceDuration").IsNumber()) {
|
|
||||||
c.min_silence_duration =
|
|
||||||
o.Get("minSilenceDuration").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("minSpeechDuration") && o.Get("minSpeechDuration").IsNumber()) {
|
|
||||||
c.min_speech_duration =
|
|
||||||
o.Get("minSpeechDuration").As<Napi::Number>().FloatValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("windowSize") && o.Get("windowSize").IsNumber()) {
|
|
||||||
c.window_size = o.Get("windowSize").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -313,23 +291,9 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) {
|
|||||||
memset(&c, 0, sizeof(c));
|
memset(&c, 0, sizeof(c));
|
||||||
c.silero_vad = GetSileroVadConfig(o);
|
c.silero_vad = GetSileroVadConfig(o);
|
||||||
|
|
||||||
if (o.Has("sampleRate") && o.Get("sampleRate").IsNumber()) {
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(sample_rate, sampleRate);
|
||||||
c.sample_rate = o.Get("sampleRate").As<Napi::Number>().Int32Value();
|
SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads);
|
||||||
}
|
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
|
||||||
|
|
||||||
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
|
||||||
c.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("provider") && o.Get("provider").IsString()) {
|
|
||||||
Napi::String provider = o.Get("provider").As<Napi::String>();
|
|
||||||
std::string s = provider.Utf8Value();
|
|
||||||
char *p = new char[s.size() + 1];
|
|
||||||
std::copy(s.begin(), s.end(), p);
|
|
||||||
p[s.size()] = 0;
|
|
||||||
|
|
||||||
c.provider = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o.Has("debug") &&
|
if (o.Has("debug") &&
|
||||||
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
(o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {
|
||||||
|
|||||||
Reference in New Issue
Block a user