Add non-streaming ASR APIs for node-addon-api (#868)
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
}
|
||||
};
|
||||
*/
|
||||
static SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
||||
SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
||||
SherpaOnnxFeatureConfig config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
|
||||
@@ -113,6 +113,39 @@ GetOnlineZipformer2CtcModelConfig(Napi::Object obj) {
|
||||
return config;
|
||||
}
|
||||
|
||||
static SherpaOnnxOnlineParaformerModelConfig GetOnlineParaformerModelConfig(
|
||||
Napi::Object obj) {
|
||||
SherpaOnnxOnlineParaformerModelConfig config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
|
||||
if (!obj.Has("paraformer") || !obj.Get("paraformer").IsObject()) {
|
||||
return config;
|
||||
}
|
||||
|
||||
Napi::Object o = obj.Get("paraformer").As<Napi::Object>();
|
||||
|
||||
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
||||
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
SherpaOnnxOnlineModelConfig config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
@@ -124,6 +157,7 @@ static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
|
||||
Napi::Object o = obj.Get("modelConfig").As<Napi::Object>();
|
||||
|
||||
config.transducer = GetOnlineTransducerModelConfig(o);
|
||||
config.paraformer = GetOnlineParaformerModelConfig(o);
|
||||
config.zipformer2_ctc = GetOnlineZipformer2CtcModelConfig(o);
|
||||
|
||||
if (o.Has("tokens") && o.Get("tokens").IsString()) {
|
||||
@@ -290,35 +324,6 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
||||
|
||||
c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(config);
|
||||
|
||||
#if 0
|
||||
printf("encoder: %s\n", c.model_config.transducer.encoder
|
||||
? c.model_config.transducer.encoder
|
||||
: "no");
|
||||
printf("decoder: %s\n", c.model_config.transducer.decoder
|
||||
? c.model_config.transducer.decoder
|
||||
: "no");
|
||||
printf("joiner: %s\n", c.model_config.transducer.joiner
|
||||
? c.model_config.transducer.joiner
|
||||
: "no");
|
||||
|
||||
printf("tokens: %s\n", c.model_config.tokens ? c.model_config.tokens : "no");
|
||||
printf("num_threads: %d\n", c.model_config.num_threads);
|
||||
printf("provider: %s\n",
|
||||
c.model_config.provider ? c.model_config.provider : "no");
|
||||
printf("debug: %d\n", c.model_config.debug);
|
||||
printf("model_type: %s\n",
|
||||
c.model_config.model_type ? c.model_config.model_type : "no");
|
||||
|
||||
printf("decoding_method: %s\n", c.decoding_method ? c.decoding_method : "no");
|
||||
printf("max_active_paths: %d\n", c.max_active_paths);
|
||||
printf("enable_endpoint: %d\n", c.enable_endpoint);
|
||||
printf("rule1_min_trailing_silence: %.3f\n", c.rule1_min_trailing_silence);
|
||||
printf("rule2_min_trailing_silence: %.3f\n", c.rule2_min_trailing_silence);
|
||||
printf("rule3_min_utterance_length: %.3f\n", c.rule3_min_utterance_length);
|
||||
printf("hotwords_file: %s\n", c.hotwords_file ? c.hotwords_file : "no");
|
||||
printf("hotwords_score: %.3f\n", c.hotwords_score);
|
||||
#endif
|
||||
|
||||
SherpaOnnxOnlineRecognizer *recognizer = CreateOnlineRecognizer(&c);
|
||||
|
||||
if (c.model_config.transducer.encoder) {
|
||||
@@ -333,6 +338,14 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
||||
delete[] c.model_config.transducer.joiner;
|
||||
}
|
||||
|
||||
if (c.model_config.paraformer.encoder) {
|
||||
delete[] c.model_config.paraformer.encoder;
|
||||
}
|
||||
|
||||
if (c.model_config.paraformer.decoder) {
|
||||
delete[] c.model_config.paraformer.decoder;
|
||||
}
|
||||
|
||||
if (c.model_config.zipformer2_ctc.model) {
|
||||
delete[] c.model_config.zipformer2_ctc.model;
|
||||
}
|
||||
@@ -389,7 +402,8 @@ static Napi::External<SherpaOnnxOnlineStream> CreateOnlineStreamWrapper(
|
||||
|
||||
if (!info[0].IsExternal()) {
|
||||
Napi::TypeError::New(
|
||||
env, "You should pass a recognizer pointer as the only argument")
|
||||
env,
|
||||
"You should pass an online recognizer pointer as the only argument")
|
||||
.ThrowAsJavaScriptException();
|
||||
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user