From dfd78744832eec38b68c448925822491440dd93a Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 14 May 2024 09:20:06 +0800 Subject: [PATCH 1/5] refactor names --- .../node-addon-api/src/non-streaming-asr.cc | 60 +++---- scripts/node-addon-api/src/streaming-asr.cc | 150 +++++++++--------- 2 files changed, 102 insertions(+), 108 deletions(-) diff --git a/scripts/node-addon-api/src/non-streaming-asr.cc b/scripts/node-addon-api/src/non-streaming-asr.cc index a1fc2f37c..177d54c58 100644 --- a/scripts/node-addon-api/src/non-streaming-asr.cc +++ b/scripts/node-addon-api/src/non-streaming-asr.cc @@ -11,11 +11,11 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj); static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( Napi::Object obj) { - SherpaOnnxOfflineTransducerModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOfflineTransducerModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("transducer").As(); @@ -27,7 +27,7 @@ static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.encoder = p; + c.encoder = p; } if (o.Has("decoder") && o.Get("decoder").IsString()) { @@ -37,7 +37,7 @@ static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.decoder = p; + c.decoder = p; } if (o.Has("joiner") && o.Get("joiner").IsString()) { @@ -47,19 +47,19 @@ static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.joiner = p; + c.joiner = p; } - return config; + return c; } static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig( Napi::Object obj) { - SherpaOnnxOfflineParaformerModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOfflineParaformerModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("paraformer").As(); @@ -71,19 +71,19 @@ static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.model = p; + c.model = p; } - return config; + return c; } static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig( Napi::Object obj) { - SherpaOnnxOfflineNemoEncDecCtcModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOfflineNemoEncDecCtcModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("nemoCtc") || !obj.Get("nemoCtc").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("nemoCtc").As(); @@ -95,19 +95,19 @@ static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.model = p; + c.model = p; } - return config; + return c; } static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( Napi::Object obj) { - SherpaOnnxOfflineWhisperModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOfflineWhisperModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("whisper") || !obj.Get("whisper").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("whisper").As(); @@ -119,7 +119,7 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.encoder = p; + c.encoder = p; } if (o.Has("decoder") && o.Get("decoder").IsString()) { @@ -129,7 +129,7 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.decoder = p; + c.decoder = p; } if (o.Has("language") && o.Get("language").IsString()) { @@ -139,7 +139,7 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.language = p; + c.language = p; } if (o.Has("task") && o.Get("task").IsString()) { @@ -149,19 +149,19 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.task = p; + c.task = p; } - return config; + return c; } static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig( Napi::Object obj) { - SherpaOnnxOfflineTdnnModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOfflineTdnnModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("tdnn") || !obj.Get("tdnn").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("tdnn").As(); @@ -173,10 +173,10 @@ static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.model = p; + c.model = p; } - return config; + return c; } static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) { diff --git a/scripts/node-addon-api/src/streaming-asr.cc b/scripts/node-addon-api/src/streaming-asr.cc index 7412427cc..4cd2187eb 100644 --- a/scripts/node-addon-api/src/streaming-asr.cc +++ b/scripts/node-addon-api/src/streaming-asr.cc @@ -14,26 +14,24 @@ }; */ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) { - SherpaOnnxFeatureConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxFeatureConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("featConfig") || !obj.Get("featConfig").IsObject()) { - return config; + return c; } - Napi::Object featConfig = obj.Get("featConfig").As(); + Napi::Object o = obj.Get("featConfig").As(); - if (featConfig.Has("sampleRate") && featConfig.Get("sampleRate").IsNumber()) { - config.sample_rate = - featConfig.Get("sampleRate").As().Int32Value(); + if (o.Has("sampleRate") && o.Get("sampleRate").IsNumber()) { + c.sample_rate = o.Get("sampleRate").As().Int32Value(); } - if (featConfig.Has("featureDim") && featConfig.Get("featureDim").IsNumber()) { - config.feature_dim = - featConfig.Get("featureDim").As().Int32Value(); + if (o.Has("featureDim") && o.Get("featureDim").IsNumber()) { + c.feature_dim = o.Get("featureDim").As().Int32Value(); } - return config; + return c; } /* { @@ -47,11 +45,11 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) { static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( Napi::Object obj) { - SherpaOnnxOnlineTransducerModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOnlineTransducerModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("transducer").As(); @@ -63,7 +61,7 @@ static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.encoder = p; + c.encoder = p; } if (o.Has("decoder") && o.Get("decoder").IsString()) { @@ -73,7 +71,7 @@ static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.decoder = p; + c.decoder = p; } if (o.Has("joiner") && o.Get("joiner").IsString()) { @@ -83,19 +81,19 @@ static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.joiner = p; + c.joiner = p; } - return config; + return c; } static SherpaOnnxOnlineZipformer2CtcModelConfig GetOnlineZipformer2CtcModelConfig(Napi::Object obj) { - SherpaOnnxOnlineZipformer2CtcModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOnlineZipformer2CtcModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("zipformer2Ctc") || !obj.Get("zipformer2Ctc").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("zipformer2Ctc").As(); @@ -107,19 +105,19 @@ GetOnlineZipformer2CtcModelConfig(Napi::Object obj) { std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.model = p; + c.model = p; } - return config; + return c; } static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig( Napi::Object obj) { - SherpaOnnxOnlineParaformerModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOnlineParaformerModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("paraformer").As(); @@ -131,7 +129,7 @@ static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.encoder = p; + c.encoder = p; } if (o.Has("decoder") && o.Get("decoder").IsString()) { @@ -141,24 +139,24 @@ static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.decoder = p; + c.decoder = p; } - return config; + return c; } static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { - SherpaOnnxOnlineModelConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOnlineModelConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("modelConfig") || !obj.Get("modelConfig").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("modelConfig").As(); - config.transducer = GetOnlineTransducerModelConfig(o); - config.paraformer = GetOnlineParaformerModelConfig(o); - config.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o); + c.transducer = GetOnlineTransducerModelConfig(o); + c.paraformer = GetOnlineParaformerModelConfig(o); + c.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o); if (o.Has("tokens") && o.Get("tokens").IsString()) { Napi::String tokens = o.Get("tokens").As(); @@ -167,11 +165,11 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.tokens = p; + c.tokens = p; } if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) { - config.num_threads = o.Get("numThreads").As().Int32Value(); + c.num_threads = o.Get("numThreads").As().Int32Value(); } if (o.Has("provider") && o.Get("provider").IsString()) { @@ -181,15 +179,15 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.provider = p; + c.provider = p; } if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { if (o.Get("debug").IsBoolean()) { - config.debug = o.Get("debug").As().Value(); + c.debug = o.Get("debug").As().Value(); } else { - config.debug = o.Get("debug").As().Int32Value(); + c.debug = o.Get("debug").As().Int32Value(); } } @@ -200,20 +198,20 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.model_type = p; + c.model_type = p; } - return config; + return c; } static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig( Napi::Object obj) { - SherpaOnnxOnlineCtcFstDecoderConfig config; - memset(&config, 0, sizeof(config)); + SherpaOnnxOnlineCtcFstDecoderConfig c; + memset(&c, 0, sizeof(c)); if (!obj.Has("ctcFstDecoderConfig") || !obj.Get("ctcFstDecoderConfig").IsObject()) { - return config; + return c; } Napi::Object o = obj.Get("ctcFstDecoderConfig").As(); @@ -225,14 +223,14 @@ static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig( std::copy(s.begin(), s.end(), p); p[s.size()] = 0; - config.graph = p; + c.graph = p; } if (o.Has("maxActive") && o.Get("maxActive").IsNumber()) { - config.max_active = o.Get("maxActive").As().Int32Value(); + c.max_active = o.Get("maxActive").As().Int32Value(); } - return config; + return c; } static Napi::External CreateOnlineRecognizerWrapper( @@ -254,15 +252,14 @@ static Napi::External CreateOnlineRecognizerWrapper( return {}; } - Napi::Object config = info[0].As(); + Napi::Object o = info[0].As(); SherpaOnnxOnlineRecognizerConfig c; memset(&c, 0, sizeof(c)); - c.feat_config = GetFeatureConfig(config); - c.model_config = GetOnlineModelConfig(config); + c.feat_config = GetFeatureConfig(o); + c.model_config = GetOnlineModelConfig(o); - if (config.Has("decodingMethod") && config.Get("decodingMethod").IsString()) { - Napi::String decoding_method = - config.Get("decodingMethod").As(); + if (o.Has("decodingMethod") && o.Get("decodingMethod").IsString()) { + Napi::String decoding_method = o.Get("decodingMethod").As(); std::string s = decoding_method.Utf8Value(); char *p = new char[s.size() + 1]; std::copy(s.begin(), s.end(), p); @@ -271,44 +268,42 @@ static Napi::External CreateOnlineRecognizerWrapper( c.decoding_method = p; } - if (config.Has("maxActivePaths") && config.Get("maxActivePaths").IsNumber()) { + if (o.Has("maxActivePaths") && o.Get("maxActivePaths").IsNumber()) { c.max_active_paths = - config.Get("maxActivePaths").As().Int32Value(); + o.Get("maxActivePaths").As().Int32Value(); } // enableEndpoint can be either a boolean or an integer - if (config.Has("enableEndpoint") && - (config.Get("enableEndpoint").IsNumber() || - config.Get("enableEndpoint").IsBoolean())) { - if (config.Get("enableEndpoint").IsNumber()) { + if (o.Has("enableEndpoint") && (o.Get("enableEndpoint").IsNumber() || + o.Get("enableEndpoint").IsBoolean())) { + if (o.Get("enableEndpoint").IsNumber()) { c.enable_endpoint = - config.Get("enableEndpoint").As().Int32Value(); + o.Get("enableEndpoint").As().Int32Value(); } else { - c.enable_endpoint = - config.Get("enableEndpoint").As().Value(); + c.enable_endpoint = o.Get("enableEndpoint").As().Value(); } } - if (config.Has("rule1MinTrailingSilence") && - config.Get("rule1MinTrailingSilence").IsNumber()) { + if (o.Has("rule1MinTrailingSilence") && + o.Get("rule1MinTrailingSilence").IsNumber()) { c.rule1_min_trailing_silence = - config.Get("rule1MinTrailingSilence").As().FloatValue(); + o.Get("rule1MinTrailingSilence").As().FloatValue(); } - if (config.Has("rule2MinTrailingSilence") && - config.Get("rule2MinTrailingSilence").IsNumber()) { + if (o.Has("rule2MinTrailingSilence") && + o.Get("rule2MinTrailingSilence").IsNumber()) { c.rule2_min_trailing_silence = - config.Get("rule2MinTrailingSilence").As().FloatValue(); + o.Get("rule2MinTrailingSilence").As().FloatValue(); } - if (config.Has("rule3MinUtteranceLength") && - config.Get("rule3MinUtteranceLength").IsNumber()) { + if (o.Has("rule3MinUtteranceLength") && + o.Get("rule3MinUtteranceLength").IsNumber()) { c.rule3_min_utterance_length = - config.Get("rule3MinUtteranceLength").As().FloatValue(); + o.Get("rule3MinUtteranceLength").As().FloatValue(); } - if (config.Has("hotwordsFile") && config.Get("hotwordsFile").IsString()) { - Napi::String hotwords_file = config.Get("hotwordsFile").As(); + if (o.Has("hotwordsFile") && o.Get("hotwordsFile").IsString()) { + Napi::String hotwords_file = o.Get("hotwordsFile").As(); std::string s = hotwords_file.Utf8Value(); char *p = new char[s.size() + 1]; std::copy(s.begin(), s.end(), p); @@ -317,12 +312,11 @@ static Napi::External CreateOnlineRecognizerWrapper( c.hotwords_file = p; } - if (config.Has("hotwordsScore") && config.Get("hotwordsScore").IsNumber()) { - c.hotwords_score = - config.Get("hotwordsScore").As().FloatValue(); + if (o.Has("hotwordsScore") && o.Get("hotwordsScore").IsNumber()) { + c.hotwords_score = o.Get("hotwordsScore").As().FloatValue(); } - c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(config); + c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o); SherpaOnnxOnlineRecognizer *recognizer = CreateOnlineRecognizer(&c); From 102b965dafd77d258c288a54e5e409c37155a192 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 14 May 2024 09:47:21 +0800 Subject: [PATCH 2/5] Refactor node-addon-api to remove duplicate. --- scripts/node-addon-api/src/macros.h | 34 ++++ .../node-addon-api/src/non-streaming-asr.cc | 191 ++---------------- .../node-addon-api/src/non-streaming-tts.cc | 114 ++--------- .../src/spoken-language-identification.cc | 44 +--- scripts/node-addon-api/src/streaming-asr.cc | 177 +++------------- scripts/node-addon-api/src/vad.cc | 56 +---- 6 files changed, 111 insertions(+), 505 deletions(-) create mode 100644 scripts/node-addon-api/src/macros.h diff --git a/scripts/node-addon-api/src/macros.h b/scripts/node-addon-api/src/macros.h new file mode 100644 index 000000000..cf5cb0033 --- /dev/null +++ b/scripts/node-addon-api/src/macros.h @@ -0,0 +1,34 @@ +// scripts/node-addon-api/src/macros.h +// +// Copyright (c) 2023 Xiaomi Corporation +#ifndef SCRIPTS_NODE_ADDONE_API_SRC_MACROS_H_ +#define SCRIPTS_NODE_ADDONE_API_SRC_MACROS_H_ + +#define 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(); \ + 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 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().Int32Value(); \ + } \ + } while (0) + +#define 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().FloatValue(); \ + } \ + } while (0) + +#endif // SCRIPTS_NODE_ADDONE_API_SRC_MACROS_H_ diff --git a/scripts/node-addon-api/src/non-streaming-asr.cc b/scripts/node-addon-api/src/non-streaming-asr.cc index 177d54c58..32b4d7480 100644 --- a/scripts/node-addon-api/src/non-streaming-asr.cc +++ b/scripts/node-addon-api/src/non-streaming-asr.cc @@ -3,7 +3,8 @@ // Copyright (c) 2024 Xiaomi Corporation #include -#include "napi.h" // NOLINT +#include "macros.h" // NOLINT +#include "napi.h" // NOLINT #include "sherpa-onnx/c-api/c-api.h" // defined in ./streaming-asr.cc @@ -20,35 +21,9 @@ static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( Napi::Object o = obj.Get("transducer").As(); - if (o.Has("encoder") && o.Get("encoder").IsString()) { - Napi::String encoder = o.Get("encoder").As(); - std::string s = encoder.Utf8Value(); - 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(); - 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("joiner") && o.Get("joiner").IsString()) { - Napi::String joiner = o.Get("joiner").As(); - std::string s = joiner.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; - - c.joiner = p; - } + ASSIGN_ATTR_STR(encoder, encoder); + ASSIGN_ATTR_STR(decoder, decoder); + ASSIGN_ATTR_STR(joiner, joiner); return c; } @@ -64,15 +39,7 @@ static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig( Napi::Object o = obj.Get("paraformer").As(); - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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; - } + ASSIGN_ATTR_STR(model, model); return c; } @@ -88,15 +55,7 @@ static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig( Napi::Object o = obj.Get("nemoCtc").As(); - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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; - } + ASSIGN_ATTR_STR(model, model); return c; } @@ -112,45 +71,10 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( Napi::Object o = obj.Get("whisper").As(); - if (o.Has("encoder") && o.Get("encoder").IsString()) { - Napi::String encoder = o.Get("encoder").As(); - std::string s = encoder.Utf8Value(); - 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(); - 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("language") && o.Get("language").IsString()) { - Napi::String language = o.Get("language").As(); - std::string s = language.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; - - c.language = p; - } - - if (o.Has("task") && o.Get("task").IsString()) { - Napi::String task = o.Get("task").As(); - std::string s = task.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; - - c.task = p; - } + ASSIGN_ATTR_STR(encoder, encoder); + ASSIGN_ATTR_STR(decoder, decoder); + ASSIGN_ATTR_STR(language, language); + ASSIGN_ATTR_STR(task, languagek); return c; } @@ -166,15 +90,7 @@ static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig( Napi::Object o = obj.Get("tdnn").As(); - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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; - } + ASSIGN_ATTR_STR(model, model); return c; } @@ -195,19 +111,8 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) { c.whisper = GetOfflineWhisperModelConfig(o); c.tdnn = GetOfflineTdnnModelConfig(o); - if (o.Has("tokens") && o.Get("tokens").IsString()) { - Napi::String tokens = o.Get("tokens").As(); - 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().Int32Value(); - } + ASSIGN_ATTR_STR(tokens, tokens); + ASSIGN_ATTR_INT32(num_threads, numThreads); if (o.Has("debug") && (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()) { - Napi::String provider = o.Get("provider").As(); - 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(); - 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; - } + ASSIGN_ATTR_STR(provider, provider); + ASSIGN_ATTR_STR(model_type, modelType); return c; } @@ -251,19 +139,8 @@ static SherpaOnnxOfflineLMConfig GetOfflineLMConfig(Napi::Object obj) { Napi::Object o = obj.Get("lmConfig").As(); - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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().FloatValue(); - } + ASSIGN_ATTR_STR(model, model); + ASSIGN_ATTR_FLOAT(scale, scale); return c; } @@ -295,34 +172,10 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) { c.model_config = GetOfflineModelConfig(o); c.lm_config = GetOfflineLMConfig(o); - if (o.Has("decodingMethod") && o.Get("decodingMethod").IsString()) { - Napi::String decoding_method = o.Get("decodingMethod").As(); - 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 (o.Has("maxActivePaths") && o.Get("maxActivePaths").IsNumber()) { - c.max_active_paths = - o.Get("maxActivePaths").As().Int32Value(); - } - - if (o.Has("hotwordsFile") && o.Get("hotwordsFile").IsString()) { - Napi::String hotwords_file = o.Get("hotwordsFile").As(); - 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().FloatValue(); - } + ASSIGN_ATTR_STR(decoding_method, decodingMethod); + ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); + ASSIGN_ATTR_STR(hotwords_file, hotwordsFile); + ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore); SherpaOnnxOfflineRecognizer *recognizer = CreateOfflineRecognizer(&c); diff --git a/scripts/node-addon-api/src/non-streaming-tts.cc b/scripts/node-addon-api/src/non-streaming-tts.cc index 24dc40965..9b67b6a7a 100644 --- a/scripts/node-addon-api/src/non-streaming-tts.cc +++ b/scripts/node-addon-api/src/non-streaming-tts.cc @@ -4,7 +4,8 @@ #include -#include "napi.h" // NOLINT +#include "macros.h" // NOLINT +#include "napi.h" // NOLINT #include "sherpa-onnx/c-api/c-api.h" static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig( @@ -17,68 +18,14 @@ static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig( } Napi::Object o = obj.Get("vits").As(); - - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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("lexicon") && o.Get("lexicon").IsString()) { - Napi::String lexicon = o.Get("lexicon").As(); - 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(); - 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(); - 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().FloatValue(); - } - - if (o.Has("noiseScaleW") && o.Get("noiseScaleW").IsNumber()) { - c.noise_scale_w = o.Get("noiseScaleW").As().FloatValue(); - } - - if (o.Has("lengthScale") && o.Get("lengthScale").IsNumber()) { - c.length_scale = o.Get("lengthScale").As().FloatValue(); - } - - if (o.Has("dictDir") && o.Get("dictDir").IsString()) { - Napi::String dict_dir = o.Get("dictDir").As(); - 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; - } + ASSIGN_ATTR_STR(model, model); + ASSIGN_ATTR_STR(lexicon, lexicon); + ASSIGN_ATTR_STR(tokens, tokens); + ASSIGN_ATTR_STR(data_dir, dataDir); + ASSIGN_ATTR_FLOAT(noise_scale, noiseScale); + ASSIGN_ATTR_FLOAT(noise_scale_w, noiseScaleW); + ASSIGN_ATTR_FLOAT(length_scale, lengthScale); + ASSIGN_ATTR_STR(dict_dir, dictDir); return c; } @@ -96,9 +43,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig( c.vits = GetOfflineTtsVitsModelConfig(o); - if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) { - c.num_threads = o.Get("numThreads").As().Int32Value(); - } + ASSIGN_ATTR_INT32(num_threads, num_threads); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -109,15 +54,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig( } } - if (o.Has("provider") && o.Get("provider").IsString()) { - Napi::String provider = o.Get("provider").As(); - 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; - } + ASSIGN_ATTR_STR(provider, provider); return c; } @@ -148,30 +85,9 @@ static Napi::External CreateOfflineTtsWrapper( c.model = GetOfflineTtsModelConfig(o); - if (o.Has("ruleFsts") && o.Get("ruleFsts").IsString()) { - Napi::String rule_fsts = o.Get("ruleFsts").As(); - std::string s = rule_fsts.Utf8Value(); - 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().Int32Value(); - } - - if (o.Has("ruleFars") && o.Get("ruleFars").IsString()) { - Napi::String rule_fars = o.Get("ruleFars").As(); - 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; - } + ASSIGN_ATTR_STR(rule_fsts, ruleFsts); + ASSIGN_ATTR_INT32(max_num_sentences, maxNumSentences); + ASSIGN_ATTR_STR(rule_fars, ruleFars); SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c); diff --git a/scripts/node-addon-api/src/spoken-language-identification.cc b/scripts/node-addon-api/src/spoken-language-identification.cc index f256ace52..f508384ea 100644 --- a/scripts/node-addon-api/src/spoken-language-identification.cc +++ b/scripts/node-addon-api/src/spoken-language-identification.cc @@ -4,7 +4,8 @@ #include -#include "napi.h" // NOLINT +#include "macros.h" // NOLINT +#include "napi.h" // NOLINT #include "sherpa-onnx/c-api/c-api.h" static SherpaOnnxSpokenLanguageIdentificationWhisperConfig @@ -18,29 +19,9 @@ GetSpokenLanguageIdentificationWhisperConfig(Napi::Object obj) { Napi::Object o = obj.Get("whisper").As(); - if (o.Has("encoder") && o.Get("encoder").IsString()) { - Napi::String encoder = o.Get("encoder").As(); - std::string s = encoder.Utf8Value(); - 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(); - 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().Int32Value(); - } + ASSIGN_ATTR_STR(encoder, encoder); + ASSIGN_ATTR_STR(decoder, decoder); + ASSIGN_ATTR_INT32(tail_paddings, tailPaddings); return c; } @@ -70,9 +51,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { memset(&c, 0, sizeof(c)); c.whisper = GetSpokenLanguageIdentificationWhisperConfig(o); - if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) { - c.num_threads = o.Get("numThreads").As().Int32Value(); - } + ASSIGN_ATTR_INT32(num_threads, numThreads); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -82,16 +61,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { c.debug = o.Get("debug").As().Int32Value(); } } - - if (o.Has("provider") && o.Get("provider").IsString()) { - Napi::String provider = o.Get("provider").As(); - 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; - } + ASSIGN_ATTR_STR(provider, provider); const SherpaOnnxSpokenLanguageIdentification *slid = SherpaOnnxCreateSpokenLanguageIdentification(&c); diff --git a/scripts/node-addon-api/src/streaming-asr.cc b/scripts/node-addon-api/src/streaming-asr.cc index 4cd2187eb..cd99a64c1 100644 --- a/scripts/node-addon-api/src/streaming-asr.cc +++ b/scripts/node-addon-api/src/streaming-asr.cc @@ -3,7 +3,8 @@ // Copyright (c) 2024 Xiaomi Corporation #include -#include "napi.h" // NOLINT +#include "macros.h" // NOLINT +#include "napi.h" // NOLINT #include "sherpa-onnx/c-api/c-api.h" /* { @@ -23,13 +24,8 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) { Napi::Object o = obj.Get("featConfig").As(); - if (o.Has("sampleRate") && o.Get("sampleRate").IsNumber()) { - c.sample_rate = o.Get("sampleRate").As().Int32Value(); - } - - if (o.Has("featureDim") && o.Get("featureDim").IsNumber()) { - c.feature_dim = o.Get("featureDim").As().Int32Value(); - } + ASSIGN_ATTR_INT32(sample_rate, sampleRate); + ASSIGN_ATTR_INT32(feature_dim, featureDim); return c; } @@ -54,35 +50,9 @@ static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( Napi::Object o = obj.Get("transducer").As(); - if (o.Has("encoder") && o.Get("encoder").IsString()) { - Napi::String encoder = o.Get("encoder").As(); - std::string s = encoder.Utf8Value(); - 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(); - 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("joiner") && o.Get("joiner").IsString()) { - Napi::String joiner = o.Get("joiner").As(); - std::string s = joiner.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; - - c.joiner = p; - } + ASSIGN_ATTR_STR(encoder, encoder); + ASSIGN_ATTR_STR(decoder, decoder); + ASSIGN_ATTR_STR(joiner, joiner); return c; } @@ -98,15 +68,7 @@ GetOnlineZipformer2CtcModelConfig(Napi::Object obj) { Napi::Object o = obj.Get("zipformer2Ctc").As(); - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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; - } + ASSIGN_ATTR_STR(model, model); return c; } @@ -122,25 +84,9 @@ static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig( Napi::Object o = obj.Get("paraformer").As(); - if (o.Has("encoder") && o.Get("encoder").IsString()) { - Napi::String encoder = o.Get("encoder").As(); - std::string s = encoder.Utf8Value(); - 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(); - std::string s = decoder.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; + ASSIGN_ATTR_STR(encoder, encoder); + ASSIGN_ATTR_STR(decoder, decoder); - c.decoder = p; - } return c; } @@ -158,29 +104,9 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { c.paraformer = GetOnlineParaformerModelConfig(o); c.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o); - if (o.Has("tokens") && o.Get("tokens").IsString()) { - Napi::String tokens = o.Get("tokens").As(); - 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().Int32Value(); - } - - if (o.Has("provider") && o.Get("provider").IsString()) { - Napi::String provider = o.Get("provider").As(); - 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; - } + ASSIGN_ATTR_STR(tokens, tokens); + ASSIGN_ATTR_INT32(num_threads, numThreads); + ASSIGN_ATTR_STR(provider, provider); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -191,15 +117,7 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { } } - if (o.Has("modelType") && o.Get("modelType").IsString()) { - Napi::String model_type = o.Get("modelType").As(); - 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; - } + ASSIGN_ATTR_STR(model_type, modelType); return c; } @@ -216,19 +134,8 @@ static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig( Napi::Object o = obj.Get("ctcFstDecoderConfig").As(); - if (o.Has("graph") && o.Get("graph").IsString()) { - Napi::String graph = o.Get("graph").As(); - std::string s = graph.Utf8Value(); - char *p = new char[s.size() + 1]; - std::copy(s.begin(), s.end(), p); - p[s.size()] = 0; - - c.graph = p; - } - - if (o.Has("maxActive") && o.Get("maxActive").IsNumber()) { - c.max_active = o.Get("maxActive").As().Int32Value(); - } + ASSIGN_ATTR_STR(graph, graph); + ASSIGN_ATTR_INT32(max_active, maxActive); return c; } @@ -258,20 +165,8 @@ static Napi::External CreateOnlineRecognizerWrapper( c.feat_config = GetFeatureConfig(o); c.model_config = GetOnlineModelConfig(o); - if (o.Has("decodingMethod") && o.Get("decodingMethod").IsString()) { - Napi::String decoding_method = o.Get("decodingMethod").As(); - 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 (o.Has("maxActivePaths") && o.Get("maxActivePaths").IsNumber()) { - c.max_active_paths = - o.Get("maxActivePaths").As().Int32Value(); - } + ASSIGN_ATTR_STR(decoding_method, decodingMethod); + ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); // enableEndpoint can be either a boolean or an integer if (o.Has("enableEndpoint") && (o.Get("enableEndpoint").IsNumber() || @@ -284,37 +179,11 @@ static Napi::External CreateOnlineRecognizerWrapper( } } - if (o.Has("rule1MinTrailingSilence") && - o.Get("rule1MinTrailingSilence").IsNumber()) { - c.rule1_min_trailing_silence = - o.Get("rule1MinTrailingSilence").As().FloatValue(); - } - - if (o.Has("rule2MinTrailingSilence") && - o.Get("rule2MinTrailingSilence").IsNumber()) { - c.rule2_min_trailing_silence = - o.Get("rule2MinTrailingSilence").As().FloatValue(); - } - - if (o.Has("rule3MinUtteranceLength") && - o.Get("rule3MinUtteranceLength").IsNumber()) { - c.rule3_min_utterance_length = - o.Get("rule3MinUtteranceLength").As().FloatValue(); - } - - if (o.Has("hotwordsFile") && o.Get("hotwordsFile").IsString()) { - Napi::String hotwords_file = o.Get("hotwordsFile").As(); - 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().FloatValue(); - } + ASSIGN_ATTR_FLOAT(rule1_min_trailing_silence, rule1MinTrailingSilence); + ASSIGN_ATTR_FLOAT(rule2_min_trailing_silence, rule2MinTrailingSilence); + ASSIGN_ATTR_FLOAT(rule3_min_utterance_length, rule3MinUtteranceLength); + ASSIGN_ATTR_STR(hotwords_file, hotwordsFile); + ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore); c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o); diff --git a/scripts/node-addon-api/src/vad.cc b/scripts/node-addon-api/src/vad.cc index 2c61c29fe..2322bc006 100644 --- a/scripts/node-addon-api/src/vad.cc +++ b/scripts/node-addon-api/src/vad.cc @@ -4,7 +4,8 @@ #include -#include "napi.h" // NOLINT +#include "macros.h" // NOLINT +#include "napi.h" // NOLINT #include "sherpa-onnx/c-api/c-api.h" static Napi::External CreateCircularBufferWrapper( @@ -247,34 +248,11 @@ static SherpaOnnxSileroVadModelConfig GetSileroVadConfig( } Napi::Object o = obj.Get("sileroVad").As(); - - if (o.Has("model") && o.Get("model").IsString()) { - Napi::String model = o.Get("model").As(); - 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("threshold") && o.Get("threshold").IsNumber()) { - c.threshold = o.Get("threshold").As().FloatValue(); - } - - if (o.Has("minSilenceDuration") && o.Get("minSilenceDuration").IsNumber()) { - c.min_silence_duration = - o.Get("minSilenceDuration").As().FloatValue(); - } - - if (o.Has("minSpeechDuration") && o.Get("minSpeechDuration").IsNumber()) { - c.min_speech_duration = - o.Get("minSpeechDuration").As().FloatValue(); - } - - if (o.Has("windowSize") && o.Get("windowSize").IsNumber()) { - c.window_size = o.Get("windowSize").As().Int32Value(); - } + ASSIGN_ATTR_STR(model, model); + ASSIGN_ATTR_FLOAT(threshold, threshold); + ASSIGN_ATTR_FLOAT(min_silence_duration, minSilenceDuration); + ASSIGN_ATTR_FLOAT(min_speech_duration, minSpeechDuration); + ASSIGN_ATTR_INT32(window_size, windowSize); return c; } @@ -313,23 +291,9 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) { memset(&c, 0, sizeof(c)); c.silero_vad = GetSileroVadConfig(o); - if (o.Has("sampleRate") && o.Get("sampleRate").IsNumber()) { - c.sample_rate = o.Get("sampleRate").As().Int32Value(); - } - - if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) { - c.num_threads = o.Get("numThreads").As().Int32Value(); - } - - if (o.Has("provider") && o.Get("provider").IsString()) { - Napi::String provider = o.Get("provider").As(); - 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; - } + ASSIGN_ATTR_INT32(sample_rate, sampleRate); + ASSIGN_ATTR_INT32(num_threads, numThreads); + ASSIGN_ATTR_STR(provider, provider); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { From 4c49d68ba217ace8187c0342ccbab58d51216778 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 14 May 2024 09:53:19 +0800 Subject: [PATCH 3/5] fix CI --- .../workflows/test-nodejs-addon-npm-aarch64.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/test-nodejs-addon-npm-aarch64.yaml b/.github/workflows/test-nodejs-addon-npm-aarch64.yaml index 1dc88ac07..07ab8d878 100644 --- a/.github/workflows/test-nodejs-addon-npm-aarch64.yaml +++ b/.github/workflows/test-nodejs-addon-npm-aarch64.yaml @@ -77,4 +77,19 @@ jobs: 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 From c797b9893608a89daa1aa0296457ffac8ed219c1 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 14 May 2024 10:03:08 +0800 Subject: [PATCH 4/5] fix style issues --- scripts/node-addon-api/src/macros.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/node-addon-api/src/macros.h b/scripts/node-addon-api/src/macros.h index cf5cb0033..2271fa925 100644 --- a/scripts/node-addon-api/src/macros.h +++ b/scripts/node-addon-api/src/macros.h @@ -1,8 +1,11 @@ // scripts/node-addon-api/src/macros.h // -// Copyright (c) 2023 Xiaomi Corporation -#ifndef SCRIPTS_NODE_ADDONE_API_SRC_MACROS_H_ -#define SCRIPTS_NODE_ADDONE_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 +#include #define ASSIGN_ATTR_STR(c_name, js_name) \ do { \ @@ -31,4 +34,4 @@ } \ } while (0) -#endif // SCRIPTS_NODE_ADDONE_API_SRC_MACROS_H_ +#endif // SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_ From 2153c41a2d5f6a5b9aa664e27c7da0ccf3e55a79 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 14 May 2024 10:06:17 +0800 Subject: [PATCH 5/5] rename macros --- scripts/node-addon-api/src/macros.h | 6 +-- .../node-addon-api/src/non-streaming-asr.cc | 40 ++++++++--------- .../node-addon-api/src/non-streaming-tts.cc | 26 +++++------ .../src/spoken-language-identification.cc | 10 ++--- scripts/node-addon-api/src/streaming-asr.cc | 45 ++++++++++--------- scripts/node-addon-api/src/vad.cc | 16 +++---- 6 files changed, 73 insertions(+), 70 deletions(-) diff --git a/scripts/node-addon-api/src/macros.h b/scripts/node-addon-api/src/macros.h index 2271fa925..bed930620 100644 --- a/scripts/node-addon-api/src/macros.h +++ b/scripts/node-addon-api/src/macros.h @@ -7,7 +7,7 @@ #include #include -#define ASSIGN_ATTR_STR(c_name, js_name) \ +#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(); \ @@ -20,14 +20,14 @@ } \ } while (0) -#define ASSIGN_ATTR_INT32(c_name, js_name) \ +#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().Int32Value(); \ } \ } while (0) -#define ASSIGN_ATTR_FLOAT(c_name, js_name) \ +#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().FloatValue(); \ diff --git a/scripts/node-addon-api/src/non-streaming-asr.cc b/scripts/node-addon-api/src/non-streaming-asr.cc index 32b4d7480..f45ca2f0e 100644 --- a/scripts/node-addon-api/src/non-streaming-asr.cc +++ b/scripts/node-addon-api/src/non-streaming-asr.cc @@ -21,9 +21,9 @@ static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig( Napi::Object o = obj.Get("transducer").As(); - ASSIGN_ATTR_STR(encoder, encoder); - ASSIGN_ATTR_STR(decoder, decoder); - ASSIGN_ATTR_STR(joiner, joiner); + SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(joiner, joiner); return c; } @@ -39,7 +39,7 @@ static SherpaOnnxOfflineParaformerModelConfig GetOfflineParaformerModelConfig( Napi::Object o = obj.Get("paraformer").As(); - ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); return c; } @@ -55,7 +55,7 @@ static SherpaOnnxOfflineNemoEncDecCtcModelConfig GetOfflineNeMoCtcModelConfig( Napi::Object o = obj.Get("nemoCtc").As(); - ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); return c; } @@ -71,10 +71,10 @@ static SherpaOnnxOfflineWhisperModelConfig GetOfflineWhisperModelConfig( Napi::Object o = obj.Get("whisper").As(); - ASSIGN_ATTR_STR(encoder, encoder); - ASSIGN_ATTR_STR(decoder, decoder); - ASSIGN_ATTR_STR(language, language); - ASSIGN_ATTR_STR(task, languagek); + SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(language, language); + SHERPA_ONNX_ASSIGN_ATTR_STR(task, languagek); return c; } @@ -90,7 +90,7 @@ static SherpaOnnxOfflineTdnnModelConfig GetOfflineTdnnModelConfig( Napi::Object o = obj.Get("tdnn").As(); - ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); return c; } @@ -111,8 +111,8 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) { c.whisper = GetOfflineWhisperModelConfig(o); c.tdnn = GetOfflineTdnnModelConfig(o); - ASSIGN_ATTR_STR(tokens, tokens); - ASSIGN_ATTR_INT32(num_threads, numThreads); + SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens); + SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -123,8 +123,8 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) { } } - ASSIGN_ATTR_STR(provider, provider); - ASSIGN_ATTR_STR(model_type, modelType); + SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); + SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType); return c; } @@ -139,8 +139,8 @@ static SherpaOnnxOfflineLMConfig GetOfflineLMConfig(Napi::Object obj) { Napi::Object o = obj.Get("lmConfig").As(); - ASSIGN_ATTR_STR(model, model); - ASSIGN_ATTR_FLOAT(scale, scale); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(scale, scale); return c; } @@ -172,10 +172,10 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) { c.model_config = GetOfflineModelConfig(o); c.lm_config = GetOfflineLMConfig(o); - ASSIGN_ATTR_STR(decoding_method, decodingMethod); - ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); - ASSIGN_ATTR_STR(hotwords_file, hotwordsFile); - ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod); + SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); + SHERPA_ONNX_ASSIGN_ATTR_STR(hotwords_file, hotwordsFile); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore); SherpaOnnxOfflineRecognizer *recognizer = CreateOfflineRecognizer(&c); diff --git a/scripts/node-addon-api/src/non-streaming-tts.cc b/scripts/node-addon-api/src/non-streaming-tts.cc index 9b67b6a7a..2adc0bbc1 100644 --- a/scripts/node-addon-api/src/non-streaming-tts.cc +++ b/scripts/node-addon-api/src/non-streaming-tts.cc @@ -18,14 +18,14 @@ static SherpaOnnxOfflineTtsVitsModelConfig GetOfflineTtsVitsModelConfig( } Napi::Object o = obj.Get("vits").As(); - ASSIGN_ATTR_STR(model, model); - ASSIGN_ATTR_STR(lexicon, lexicon); - ASSIGN_ATTR_STR(tokens, tokens); - ASSIGN_ATTR_STR(data_dir, dataDir); - ASSIGN_ATTR_FLOAT(noise_scale, noiseScale); - ASSIGN_ATTR_FLOAT(noise_scale_w, noiseScaleW); - ASSIGN_ATTR_FLOAT(length_scale, lengthScale); - ASSIGN_ATTR_STR(dict_dir, dictDir); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_STR(lexicon, lexicon); + SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens); + SHERPA_ONNX_ASSIGN_ATTR_STR(data_dir, dataDir); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(noise_scale, noiseScale); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(noise_scale_w, noiseScaleW); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(length_scale, lengthScale); + SHERPA_ONNX_ASSIGN_ATTR_STR(dict_dir, dictDir); return c; } @@ -43,7 +43,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig( c.vits = GetOfflineTtsVitsModelConfig(o); - ASSIGN_ATTR_INT32(num_threads, num_threads); + SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, num_threads); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -54,7 +54,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig( } } - ASSIGN_ATTR_STR(provider, provider); + SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); return c; } @@ -85,9 +85,9 @@ static Napi::External CreateOfflineTtsWrapper( c.model = GetOfflineTtsModelConfig(o); - ASSIGN_ATTR_STR(rule_fsts, ruleFsts); - ASSIGN_ATTR_INT32(max_num_sentences, maxNumSentences); - ASSIGN_ATTR_STR(rule_fars, ruleFars); + SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fsts, ruleFsts); + SHERPA_ONNX_ASSIGN_ATTR_INT32(max_num_sentences, maxNumSentences); + SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fars, ruleFars); SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c); diff --git a/scripts/node-addon-api/src/spoken-language-identification.cc b/scripts/node-addon-api/src/spoken-language-identification.cc index f508384ea..f9672e98e 100644 --- a/scripts/node-addon-api/src/spoken-language-identification.cc +++ b/scripts/node-addon-api/src/spoken-language-identification.cc @@ -19,9 +19,9 @@ GetSpokenLanguageIdentificationWhisperConfig(Napi::Object obj) { Napi::Object o = obj.Get("whisper").As(); - ASSIGN_ATTR_STR(encoder, encoder); - ASSIGN_ATTR_STR(decoder, decoder); - ASSIGN_ATTR_INT32(tail_paddings, tailPaddings); + SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder); + SHERPA_ONNX_ASSIGN_ATTR_INT32(tail_paddings, tailPaddings); return c; } @@ -51,7 +51,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { memset(&c, 0, sizeof(c)); c.whisper = GetSpokenLanguageIdentificationWhisperConfig(o); - ASSIGN_ATTR_INT32(num_threads, numThreads); + SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -61,7 +61,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { c.debug = o.Get("debug").As().Int32Value(); } } - ASSIGN_ATTR_STR(provider, provider); + SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); const SherpaOnnxSpokenLanguageIdentification *slid = SherpaOnnxCreateSpokenLanguageIdentification(&c); diff --git a/scripts/node-addon-api/src/streaming-asr.cc b/scripts/node-addon-api/src/streaming-asr.cc index cd99a64c1..66f568aab 100644 --- a/scripts/node-addon-api/src/streaming-asr.cc +++ b/scripts/node-addon-api/src/streaming-asr.cc @@ -24,8 +24,8 @@ SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) { Napi::Object o = obj.Get("featConfig").As(); - ASSIGN_ATTR_INT32(sample_rate, sampleRate); - ASSIGN_ATTR_INT32(feature_dim, featureDim); + SHERPA_ONNX_ASSIGN_ATTR_INT32(sample_rate, sampleRate); + SHERPA_ONNX_ASSIGN_ATTR_INT32(feature_dim, featureDim); return c; } @@ -50,9 +50,9 @@ static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig( Napi::Object o = obj.Get("transducer").As(); - ASSIGN_ATTR_STR(encoder, encoder); - ASSIGN_ATTR_STR(decoder, decoder); - ASSIGN_ATTR_STR(joiner, joiner); + SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(joiner, joiner); return c; } @@ -68,7 +68,7 @@ GetOnlineZipformer2CtcModelConfig(Napi::Object obj) { Napi::Object o = obj.Get("zipformer2Ctc").As(); - ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); return c; } @@ -84,8 +84,8 @@ static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig( Napi::Object o = obj.Get("paraformer").As(); - ASSIGN_ATTR_STR(encoder, encoder); - ASSIGN_ATTR_STR(decoder, decoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(encoder, encoder); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoder, decoder); return c; } @@ -104,9 +104,9 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { c.paraformer = GetOnlineParaformerModelConfig(o); c.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o); - ASSIGN_ATTR_STR(tokens, tokens); - ASSIGN_ATTR_INT32(num_threads, numThreads); - ASSIGN_ATTR_STR(provider, provider); + SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens); + SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); + SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { @@ -117,7 +117,7 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { } } - ASSIGN_ATTR_STR(model_type, modelType); + SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType); return c; } @@ -134,8 +134,8 @@ static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig( Napi::Object o = obj.Get("ctcFstDecoderConfig").As(); - ASSIGN_ATTR_STR(graph, graph); - ASSIGN_ATTR_INT32(max_active, maxActive); + SHERPA_ONNX_ASSIGN_ATTR_STR(graph, graph); + SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active, maxActive); return c; } @@ -165,8 +165,8 @@ static Napi::External CreateOnlineRecognizerWrapper( c.feat_config = GetFeatureConfig(o); c.model_config = GetOnlineModelConfig(o); - ASSIGN_ATTR_STR(decoding_method, decodingMethod); - ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); + SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod); + SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths); // enableEndpoint can be either a boolean or an integer if (o.Has("enableEndpoint") && (o.Get("enableEndpoint").IsNumber() || @@ -179,11 +179,14 @@ static Napi::External CreateOnlineRecognizerWrapper( } } - ASSIGN_ATTR_FLOAT(rule1_min_trailing_silence, rule1MinTrailingSilence); - ASSIGN_ATTR_FLOAT(rule2_min_trailing_silence, rule2MinTrailingSilence); - ASSIGN_ATTR_FLOAT(rule3_min_utterance_length, rule3MinUtteranceLength); - ASSIGN_ATTR_STR(hotwords_file, hotwordsFile); - ASSIGN_ATTR_FLOAT(hotwords_score, hotwordsScore); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(rule1_min_trailing_silence, + rule1MinTrailingSilence); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(rule2_min_trailing_silence, + 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); c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o); diff --git a/scripts/node-addon-api/src/vad.cc b/scripts/node-addon-api/src/vad.cc index 2322bc006..418c299a1 100644 --- a/scripts/node-addon-api/src/vad.cc +++ b/scripts/node-addon-api/src/vad.cc @@ -248,11 +248,11 @@ static SherpaOnnxSileroVadModelConfig GetSileroVadConfig( } Napi::Object o = obj.Get("sileroVad").As(); - ASSIGN_ATTR_STR(model, model); - ASSIGN_ATTR_FLOAT(threshold, threshold); - ASSIGN_ATTR_FLOAT(min_silence_duration, minSilenceDuration); - ASSIGN_ATTR_FLOAT(min_speech_duration, minSpeechDuration); - ASSIGN_ATTR_INT32(window_size, windowSize); + SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(threshold, threshold); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(min_silence_duration, minSilenceDuration); + SHERPA_ONNX_ASSIGN_ATTR_FLOAT(min_speech_duration, minSpeechDuration); + SHERPA_ONNX_ASSIGN_ATTR_INT32(window_size, windowSize); return c; } @@ -291,9 +291,9 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) { memset(&c, 0, sizeof(c)); c.silero_vad = GetSileroVadConfig(o); - ASSIGN_ATTR_INT32(sample_rate, sampleRate); - ASSIGN_ATTR_INT32(num_threads, numThreads); - ASSIGN_ATTR_STR(provider, provider); + SHERPA_ONNX_ASSIGN_ATTR_INT32(sample_rate, sampleRate); + SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); + SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); if (o.Has("debug") && (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) {