diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/audio-tagging.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/audio-tagging.cc index 650bc598d..cfd0fb7e0 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/audio-tagging.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/audio-tagging.cc @@ -1,7 +1,9 @@ // scripts/node-addon-api/src/audio-tagging.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include +#include #include "macros.h" // NOLINT #include "napi.h" // NOLINT @@ -39,14 +41,7 @@ static SherpaOnnxAudioTaggingModelConfig GetAudioTaggingModelConfig( SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); return c; diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/keyword-spotting.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/keyword-spotting.cc index 3bd8f18db..51abbcaae 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/keyword-spotting.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/keyword-spotting.cc @@ -1,6 +1,7 @@ // scripts/node-addon-api/src/keyword-spotting.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include #include diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/macros.h b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/macros.h index fc70abb01..52f5c9b95 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/macros.h +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/macros.h @@ -4,9 +4,6 @@ #ifndef SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_ #define SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_ -#include -#include - #if __OHOS__ #include "hilog/log.h" #include "rawfile/raw_file_manager.h" @@ -19,45 +16,67 @@ #define LOG_TAG "sherpa_onnx" #endif -#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(); \ - std::string s = _str.Utf8Value(); \ - char *p = new char[s.size() + 1]; \ - std::copy(s.begin(), s.end(), p); \ - p[s.size()] = 0; \ - \ - c.c_name = p; \ - } else if (o.Has(#js_name) && o.Get(#js_name).IsTypedArray()) { \ - Napi::Uint8Array _array = o.Get(#js_name).As(); \ - char *p = new char[_array.ElementLength() + 1]; \ - std::copy(_array.Data(), _array.Data() + _array.ElementLength(), p); \ - p[_array.ElementLength()] = '\0'; \ - \ - c.c_name = p; \ - } \ - } while (0) +#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \ + do { \ + if (o.Has(#js_name)) { \ + Napi::Value _value = o.Get(#js_name); \ + if (_value.IsString()) { \ + Napi::String _str = _value.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; \ + } else if (_value.IsTypedArray()) { \ + Napi::Uint8Array _array = _value.As(); \ + char *p = new char[_array.ElementLength() + 1]; \ + std::copy(_array.Data(), _array.Data() + _array.ElementLength(), p); \ + p[_array.ElementLength()] = '\0'; \ + \ + c.c_name = p; \ + } \ + } \ + } while (0) -#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \ - do { \ - if (o.Has(#js_name) && o.Get(#js_name).IsNumber()) { \ - c.c_name = o.Get(#js_name).As().Int32Value(); \ - } \ - } while (0) +#define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \ + do { \ + if (o.Has(#js_name)) { \ + Napi::Value _value = o.Get(#js_name); \ + if (_value.IsNumber()) { \ + c.c_name = _value.As().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().FloatValue(); \ - } \ - } while (0) +#define SHERPA_ONNX_ASSIGN_ATTR_FLOAT(c_name, js_name) \ + do { \ + if (o.Has(#js_name)) { \ + Napi::Value _value = o.Get(#js_name); \ + if (_value.IsNumber()) { \ + c.c_name = _value.As().FloatValue(); \ + } \ + } \ + } while (0) -#define SHERPA_ONNX_DELETE_C_STR(p) \ - do { \ - if (p) { \ - delete[] p; \ - } \ - } while (0) +// js_name may be either a boolean or a number; a number is taken as an int32 +#define SHERPA_ONNX_ASSIGN_ATTR_BOOL(c_name, js_name) \ + do { \ + if (o.Has(#js_name)) { \ + Napi::Value _value = o.Get(#js_name); \ + if (_value.IsBoolean()) { \ + c.c_name = _value.As().Value(); \ + } else if (_value.IsNumber()) { \ + c.c_name = _value.As().Int32Value(); \ + } \ + } \ + } while (0) -#endif // SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_ +#define SHERPA_ONNX_DELETE_C_STR(p) \ + do { \ + if (p) { \ + delete[] p; \ + } \ + } while (0) + +#endif // SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_ diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-asr.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-asr.cc index 7ba06fcc5..2c69498bf 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-asr.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-asr.cc @@ -1,8 +1,10 @@ // scripts/node-addon-api/src/non-streaming-asr.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include +#include #include "macros.h" // NOLINT #include "napi.h" // NOLINT @@ -381,14 +383,7 @@ static SherpaOnnxOfflineModelConfig GetOfflineModelConfig(Napi::Object obj) { 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())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc index 52ddb3bec..00758a14a 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -44,14 +45,7 @@ GetOfflineSpeakerSegmentationModelConfig(Napi::Object obj) { SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); @@ -72,14 +66,7 @@ GetSpeakerEmbeddingExtractorConfig(Napi::Object obj) { SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc index 189d2ded8..44af24509 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc @@ -458,14 +458,7 @@ static SherpaOnnxOfflineTtsModelConfig GetOfflineTtsModelConfig( SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-punctuation.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-punctuation.cc index d8d3e6e14..b80097182 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-punctuation.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-punctuation.cc @@ -1,6 +1,7 @@ // scripts/node-addon-api/src/offline-punctuation.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include @@ -23,14 +24,7 @@ static SherpaOnnxOfflinePunctuationModelConfig GetOfflinePunctuationModelConfig( SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); return c; diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-source-separation.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-source-separation.cc index 7a7fce79d..d8926b339 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-source-separation.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/offline-source-separation.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "macros.h" // NOLINT @@ -61,13 +62,7 @@ static void GetModelConfig(Napi::Object obj, c.num_threads = static_cast(value); } SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); - if (o.Has("debug")) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else if (o.Get("debug").IsNumber()) { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); } static void DeleteConfig(SherpaOnnxOfflineSourceSeparationConfig *c) { diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/online-punctuation.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/online-punctuation.cc index eec52bd66..980310f14 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/online-punctuation.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/online-punctuation.cc @@ -1,6 +1,7 @@ // scripts/node-addon-api/src/online-punctuation.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include @@ -25,14 +26,7 @@ static SherpaOnnxOnlinePunctuationModelConfig GetOnlinePunctuationModelConfig( SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); return c; diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/resampler.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/resampler.cc index 5e15def72..a9c78226c 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/resampler.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/resampler.cc @@ -2,6 +2,7 @@ // // Copyright (c) 2026 Xiaomi Corporation +#include #include #include "macros.h" // NOLINT diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speaker-identification.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speaker-identification.cc index 2766f9539..1b908ba39 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speaker-identification.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speaker-identification.cc @@ -63,14 +63,7 @@ CreateSpeakerEmbeddingExtractorWrapper(const Napi::CallbackInfo &info) { SHERPA_ONNX_ASSIGN_ATTR_STR(model, model); SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speech-denoiser.h b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speech-denoiser.h index 8294b96e9..5bf84ada0 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speech-denoiser.h +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/speech-denoiser.h @@ -6,6 +6,7 @@ #define SHERPA_ONNX_HARMONY_OS_SHERPAONNXHAR_SHERPA_ONNX_SRC_MAIN_CPP_SPEECH_DENOISER_H_ #include +#include #include "macros.h" // NOLINT #include "napi.h" // NOLINT @@ -73,14 +74,7 @@ GetSpeechDenoiserModelConfig(Napi::Object obj) { SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/spoken-language-identification.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/spoken-language-identification.cc index a9e8c7e72..b75e2e54d 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/spoken-language-identification.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/spoken-language-identification.cc @@ -2,6 +2,7 @@ // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include @@ -54,14 +55,7 @@ CreateSpokenLanguageIdentificationWrapper(const Napi::CallbackInfo &info) { SHERPA_ONNX_ASSIGN_ATTR_INT32(num_threads, numThreads); - if (o.Has("debug") && - (o.Get("debug").IsNumber() || o.Get("debug").IsBoolean())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider); const SherpaOnnxSpokenLanguageIdentification *slid = diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/streaming-asr.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/streaming-asr.cc index 8c902a745..296972e0e 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/streaming-asr.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/streaming-asr.cc @@ -1,6 +1,7 @@ // scripts/node-addon-api/src/streaming-asr.cc // // Copyright (c) 2024 Xiaomi Corporation +#include #include #include #include @@ -146,14 +147,7 @@ SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) { 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())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType); SHERPA_ONNX_ASSIGN_ATTR_STR(modeling_unit, modelingUnit); diff --git a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/vad.cc b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/vad.cc index 4b33cf64c..f8e55ae6f 100644 --- a/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/vad.cc +++ b/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/vad.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include "macros.h" // NOLINT #include "napi.h" // NOLINT @@ -376,14 +377,7 @@ CreateVoiceActivityDetectorWrapper(const Napi::CallbackInfo &info) { 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())) { - if (o.Get("debug").IsBoolean()) { - c.debug = o.Get("debug").As().Value(); - } else { - c.debug = o.Get("debug").As().Int32Value(); - } - } + SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug); #if __OHOS__ const SherpaOnnxVoiceActivityDetector *vad = nullptr;