Skip to content

Commit 15d756e

Browse files
authored
Add OfflinePunctuation APIs for HarmonyOS (#3359)
1 parent 86717a1 commit 15d756e

25 files changed

Lines changed: 458 additions & 207 deletions

harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/keyword-spotting.cc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
1919
const Napi::CallbackInfo &info) {
2020
Napi::Env env = info.Env();
2121
#if __OHOS__
22-
if (info.Length() != 2) {
22+
if (info.Length() != 1 && info.Length() != 2) {
2323
std::ostringstream os;
24-
os << "Expect only 2 arguments. Given: " << info.Length();
24+
os << "Expect 1 or 2 arguments. Given: " << info.Length();
2525

2626
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
2727

@@ -44,6 +44,18 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
4444
return {};
4545
}
4646

47+
#if __OHOS__
48+
bool use_resource_manager =
49+
info.Length() == 2 && !info[1].IsUndefined() && !info[1].IsNull();
50+
if (use_resource_manager && !info[1].IsObject()) {
51+
Napi::TypeError::New(
52+
env, "You should pass a resource manager as the second argument.")
53+
.ThrowAsJavaScriptException();
54+
55+
return {};
56+
}
57+
#endif
58+
4759
Napi::Object o = info[0].As<Napi::Object>();
4860
SherpaOnnxKeywordSpotterConfig c;
4961
memset(&c, 0, sizeof(c));
@@ -59,13 +71,18 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
5971
SHERPA_ONNX_ASSIGN_ATTR_INT32(keywords_buf_size, keywordsBufSize);
6072

6173
#if __OHOS__
62-
std::unique_ptr<NativeResourceManager,
63-
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
64-
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
65-
&OH_ResourceManager_ReleaseNativeResourceManager);
74+
const SherpaOnnxKeywordSpotter *kws = nullptr;
6675

67-
const SherpaOnnxKeywordSpotter *kws =
68-
SherpaOnnxCreateKeywordSpotterOHOS(&c, mgr.get());
76+
if (use_resource_manager) {
77+
std::unique_ptr<NativeResourceManager,
78+
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
79+
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
80+
&OH_ResourceManager_ReleaseNativeResourceManager);
81+
82+
kws = SherpaOnnxCreateKeywordSpotterOHOS(&c, mgr.get());
83+
} else {
84+
kws = SherpaOnnxCreateKeywordSpotter(&c);
85+
}
6986
#else
7087
const SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c);
7188
#endif

harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-asr.cc

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) {
457457
Napi::Env env = info.Env();
458458
#if __OHOS__
459459
// the last argument is the NativeResourceManager
460-
if (info.Length() != 2) {
460+
if (info.Length() != 1 && info.Length() != 2) {
461461
std::ostringstream os;
462-
os << "Expect only 2 arguments. Given: " << info.Length();
462+
os << "Expect 1 or 2 arguments. Given: " << info.Length();
463463

464464
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
465465

@@ -483,18 +483,35 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) {
483483
return {};
484484
}
485485

486+
#if __OHOS__
487+
bool use_resource_manager =
488+
info.Length() == 2 && !info[1].IsUndefined() && !info[1].IsNull();
489+
if (use_resource_manager && !info[1].IsObject()) {
490+
Napi::TypeError::New(
491+
env, "You should pass a resource manager as the second argument.")
492+
.ThrowAsJavaScriptException();
493+
494+
return {};
495+
}
496+
#endif
497+
486498
Napi::Object o = info[0].As<Napi::Object>();
487499

488500
SherpaOnnxOfflineRecognizerConfig c = ParseConfig(o);
489501

490502
#if __OHOS__
491-
std::unique_ptr<NativeResourceManager,
492-
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
493-
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
494-
&OH_ResourceManager_ReleaseNativeResourceManager);
503+
const SherpaOnnxOfflineRecognizer *recognizer = nullptr;
495504

496-
const SherpaOnnxOfflineRecognizer *recognizer =
497-
SherpaOnnxCreateOfflineRecognizerOHOS(&c, mgr.get());
505+
if (use_resource_manager) {
506+
std::unique_ptr<NativeResourceManager,
507+
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
508+
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
509+
&OH_ResourceManager_ReleaseNativeResourceManager);
510+
511+
recognizer = SherpaOnnxCreateOfflineRecognizerOHOS(&c, mgr.get());
512+
} else {
513+
recognizer = SherpaOnnxCreateOfflineRecognizer(&c);
514+
}
498515
#else
499516
const SherpaOnnxOfflineRecognizer *recognizer =
500517
SherpaOnnxCreateOfflineRecognizer(&c);
@@ -617,7 +634,7 @@ static void AcceptWaveformOfflineWrapper(const Napi::CallbackInfo &info) {
617634
}
618635

619636
if (!info[0].IsExternal()) {
620-
Napi::TypeError::New(env, "Argument 0 should be an online stream pointer.")
637+
Napi::TypeError::New(env, "Argument 0 should be an offline stream pointer.")
621638
.ThrowAsJavaScriptException();
622639

623640
return;

harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ CreateOfflineSpeakerDiarizationWrapper(const Napi::CallbackInfo &info) {
106106
Napi::Env env = info.Env();
107107

108108
#if __OHOS__
109-
if (info.Length() != 2) {
109+
if (info.Length() != 1 && info.Length() != 2) {
110110
std::ostringstream os;
111-
os << "Expect only 2 arguments. Given: " << info.Length();
111+
os << "Expect 1 or 2 arguments. Given: " << info.Length();
112112

113113
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
114114

@@ -132,6 +132,18 @@ CreateOfflineSpeakerDiarizationWrapper(const Napi::CallbackInfo &info) {
132132
return {};
133133
}
134134

135+
#if __OHOS__
136+
bool use_resource_manager =
137+
info.Length() == 2 && !info[1].IsUndefined() && !info[1].IsNull();
138+
if (use_resource_manager && !info[1].IsObject()) {
139+
Napi::TypeError::New(
140+
env, "You should pass a resource manager as the second argument.")
141+
.ThrowAsJavaScriptException();
142+
143+
return {};
144+
}
145+
#endif
146+
135147
Napi::Object o = info[0].As<Napi::Object>();
136148

137149
SherpaOnnxOfflineSpeakerDiarizationConfig c;
@@ -145,13 +157,18 @@ CreateOfflineSpeakerDiarizationWrapper(const Napi::CallbackInfo &info) {
145157
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(min_duration_off, minDurationOff);
146158

147159
#if __OHOS__
148-
std::unique_ptr<NativeResourceManager,
149-
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
150-
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
151-
&OH_ResourceManager_ReleaseNativeResourceManager);
160+
const SherpaOnnxOfflineSpeakerDiarization *sd = nullptr;
152161

153-
const SherpaOnnxOfflineSpeakerDiarization *sd =
154-
SherpaOnnxCreateOfflineSpeakerDiarizationOHOS(&c, mgr.get());
162+
if (use_resource_manager) {
163+
std::unique_ptr<NativeResourceManager,
164+
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
165+
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
166+
&OH_ResourceManager_ReleaseNativeResourceManager);
167+
168+
sd = SherpaOnnxCreateOfflineSpeakerDiarizationOHOS(&c, mgr.get());
169+
} else {
170+
sd = SherpaOnnxCreateOfflineSpeakerDiarization(&c);
171+
}
155172
#else
156173
const SherpaOnnxOfflineSpeakerDiarization *sd =
157174
SherpaOnnxCreateOfflineSpeakerDiarization(&c);

harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
391391
Napi::Env env = info.Env();
392392
#if __OHOS__
393393
// the last argument is the NativeResourceManager
394-
if (info.Length() != 2) {
394+
if (info.Length() != 1 && info.Length() != 2) {
395395
std::ostringstream os;
396-
os << "Expect only 2 arguments. Given: " << info.Length();
396+
os << "Expect 1 or 2 arguments. Given: " << info.Length();
397397

398398
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
399399

@@ -417,6 +417,18 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
417417
return {};
418418
}
419419

420+
#if __OHOS__
421+
bool use_resource_manager =
422+
info.Length() == 2 && !info[1].IsUndefined() && !info[1].IsNull();
423+
if (use_resource_manager && !info[1].IsObject()) {
424+
Napi::TypeError::New(
425+
env, "You should pass a resource manager as the second argument.")
426+
.ThrowAsJavaScriptException();
427+
428+
return {};
429+
}
430+
#endif
431+
420432
Napi::Object o = info[0].As<Napi::Object>();
421433

422434
SherpaOnnxOfflineTtsConfig c;
@@ -427,12 +439,17 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
427439
SHERPA_ONNX_ASSIGN_TTS_ATTR();
428440

429441
#if __OHOS__
430-
std::unique_ptr<NativeResourceManager,
431-
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
432-
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
433-
&OH_ResourceManager_ReleaseNativeResourceManager);
434-
const SherpaOnnxOfflineTts *tts =
435-
SherpaOnnxCreateOfflineTtsOHOS(&c, mgr.get());
442+
const SherpaOnnxOfflineTts *tts = nullptr;
443+
444+
if (use_resource_manager) {
445+
std::unique_ptr<NativeResourceManager,
446+
decltype(&OH_ResourceManager_ReleaseNativeResourceManager)>
447+
mgr(OH_ResourceManager_InitNativeResourceManager(env, info[1]),
448+
&OH_ResourceManager_ReleaseNativeResourceManager);
449+
tts = SherpaOnnxCreateOfflineTtsOHOS(&c, mgr.get());
450+
} else {
451+
tts = SherpaOnnxCreateOfflineTts(&c);
452+
}
436453
#else
437454
const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&c);
438455
#endif

0 commit comments

Comments
 (0)