Refactor the parsing macro for node-addon. (#4134)

This commit is contained in:
Fangjun Kuang authored and GitHub committed 2026-10-29 13:55:40 +08:00
1 parent 3c07bb1e9a
commit f04a8f77f1
15 files changed
+57 -114

No files matched your search

@@ -1,7 +1,9 @@
// scripts/node-addon-api/src/audio-tagging.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <sstream>
#include <string>
#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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
return c;
@@ -1,6 +1,7 @@
// scripts/node-addon-api/src/keyword-spotting.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
@@ -4,9 +4,6 @@
#ifndef SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_
#define SCRIPTS_NODE_ADDON_API_SRC_MACROS_H_
#include <algorithm>
#include <string>
#if __OHOS__
#include "hilog/log.h"
#include "rawfile/raw_file_manager.h"
@@ -21,35 +18,57 @@
#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>(); \
if (o.Has(#js_name)) { \
Napi::Value _value = o.Get(#js_name); \
if (_value.IsString()) { \
Napi::String _str = _value.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; \
} else if (o.Has(#js_name) && o.Get(#js_name).IsTypedArray()) { \
Napi::Uint8Array _array = o.Get(#js_name).As<Napi::Uint8Array>(); \
} else if (_value.IsTypedArray()) { \
Napi::Uint8Array _array = _value.As<Napi::Uint8Array>(); \
char *p = new char[_array.ElementLength() + 1]; \
std::copy(_array.Data(), _array.Data() + _array.ElementLength(), p); \
p[_array.ElementLength()] = '\0'; \
\
c.c_name = p; \
} \
} \
} while (0)
#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(); \
if (o.Has(#js_name)) { \
Napi::Value _value = o.Get(#js_name); \
if (_value.IsNumber()) { \
c.c_name = _value.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(); \
if (o.Has(#js_name)) { \
Napi::Value _value = o.Get(#js_name); \
if (_value.IsNumber()) { \
c.c_name = _value.As<Napi::Number>().FloatValue(); \
} \
} \
} 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<Napi::Boolean>().Value(); \
} else if (_value.IsNumber()) { \
c.c_name = _value.As<Napi::Number>().Int32Value(); \
} \
} \
} while (0)
@@ -1,8 +1,10 @@
// scripts/node-addon-api/src/non-streaming-asr.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
@@ -5,6 +5,7 @@
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
@@ -1,6 +1,7 @@
// scripts/node-addon-api/src/offline-punctuation.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <sstream>
#include <string>
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
return c;
@@ -7,6 +7,7 @@
#include <exception>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "macros.h" // NOLINT
@@ -61,13 +62,7 @@ static void GetModelConfig(Napi::Object obj,
c.num_threads = static_cast<int32_t>(value);
}
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
if (o.Has("debug")) {
if (o.Get("debug").IsBoolean()) {
c.debug = o.Get("debug").As<Napi::Boolean>().Value();
} else if (o.Get("debug").IsNumber()) {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
}
static void DeleteConfig(SherpaOnnxOfflineSourceSeparationConfig *c) {
@@ -1,6 +1,7 @@
// scripts/node-addon-api/src/online-punctuation.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <sstream>
#include <string>
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
return c;
@@ -2,6 +2,7 @@
//
// Copyright (c) 2026 Xiaomi Corporation
#include <algorithm>
#include <sstream>
#include "macros.h" // NOLINT
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
@@ -6,6 +6,7 @@
#define SHERPA_ONNX_HARMONY_OS_SHERPAONNXHAR_SHERPA_ONNX_SRC_MAIN_CPP_SPEECH_DENOISER_H_
#include <algorithm>
#include <string>
#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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
@@ -2,6 +2,7 @@
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <sstream>
#include <string>
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(provider, provider);
const SherpaOnnxSpokenLanguageIdentification *slid =
@@ -1,6 +1,7 @@
// scripts/node-addon-api/src/streaming-asr.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
@@ -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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
SHERPA_ONNX_ASSIGN_ATTR_STR(modeling_unit, modelingUnit);
@@ -5,6 +5,7 @@
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#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<Napi::Boolean>().Value();
} else {
c.debug = o.Get("debug").As<Napi::Number>().Int32Value();
}
}
SHERPA_ONNX_ASSIGN_ATTR_BOOL(debug, debug);
#if __OHOS__
const SherpaOnnxVoiceActivityDetector *vad = nullptr;