Skip to content

Commit 2b1b057

Browse files
committed
Add streaming ASR support for HarmonyOS.
1 parent 298b6b6 commit 2b1b057

35 files changed

+367
-206
lines changed

sherpa-onnx/c-api/c-api.cc

+27-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct SherpaOnnxDisplay {
5656

5757
#define SHERPA_ONNX_OR(x, y) (x ? x : y)
5858

59-
const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer(
59+
static sherpa_onnx::OnlineRecognizerConfig GetOnlineRecognizerConfig(
6060
const SherpaOnnxOnlineRecognizerConfig *config) {
6161
sherpa_onnx::OnlineRecognizerConfig recognizer_config;
6262

@@ -151,9 +151,21 @@ const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer(
151151
recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, "");
152152

153153
if (config->model_config.debug) {
154+
#if __OHOS__
155+
SHERPA_ONNX_LOGE("%{public}s\n", recognizer_config.ToString().c_str());
156+
#else
154157
SHERPA_ONNX_LOGE("%s\n", recognizer_config.ToString().c_str());
158+
#endif
155159
}
156160

161+
return recognizer_config;
162+
}
163+
164+
const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer(
165+
const SherpaOnnxOnlineRecognizerConfig *config) {
166+
sherpa_onnx::OnlineRecognizerConfig recognizer_config =
167+
GetOnlineRecognizerConfig(config);
168+
157169
if (!recognizer_config.Validate()) {
158170
SHERPA_ONNX_LOGE("Errors in config!");
159171
return nullptr;
@@ -1876,6 +1888,20 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
18761888

18771889
#ifdef __OHOS__
18781890

1891+
const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizerOHOS(
1892+
const SherpaOnnxOnlineRecognizerConfig *config,
1893+
NativeResourceManager *mgr) {
1894+
sherpa_onnx::OnlineRecognizerConfig recognizer_config =
1895+
GetOnlineRecognizerConfig(config);
1896+
1897+
SherpaOnnxOnlineRecognizer *recognizer = new SherpaOnnxOnlineRecognizer;
1898+
1899+
recognizer->impl =
1900+
std::make_unique<sherpa_onnx::OnlineRecognizer>(mgr, recognizer_config);
1901+
1902+
return recognizer;
1903+
}
1904+
18791905
const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS(
18801906
const SherpaOnnxOfflineRecognizerConfig *config,
18811907
NativeResourceManager *mgr) {

sherpa-onnx/c-api/c-api.h

+7
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,13 @@ SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult(
15271527
// It is for HarmonyOS
15281528
typedef struct NativeResourceManager NativeResourceManager;
15291529

1530+
/// @param config Config for the recognizer.
1531+
/// @return Return a pointer to the recognizer. The user has to invoke
1532+
// SherpaOnnxDestroyOnlineRecognizer() to free it to avoid memory leak.
1533+
SHERPA_ONNX_API const SherpaOnnxOnlineRecognizer *
1534+
SherpaOnnxCreateOnlineRecognizerOHOS(
1535+
const SherpaOnnxOnlineRecognizerConfig *config, NativeResourceManager *mgr);
1536+
15301537
/// @param config Config for the recognizer.
15311538
/// @return Return a pointer to the recognizer. The user has to invoke
15321539
// SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory

sherpa-onnx/csrc/offline-recognizer-impl.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
#include "sherpa-onnx/csrc/offline-recognizer-impl.h"
66

77
#include <string>
8+
#include <strstream>
89
#include <utility>
910
#include <vector>
1011

1112
#if __ANDROID_API__ >= 9
12-
#include <strstream>
1313

1414
#include "android/asset_manager.h"
1515
#include "android/asset_manager_jni.h"
16-
#elif __OHOS__
17-
#include <strstream>
16+
#endif
1817

18+
#if __OHOS__
1919
#include "rawfile/raw_file_manager.h"
2020
#endif
2121

sherpa-onnx/csrc/online-conformer-transducer-model.cc

+24-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "android/asset_manager_jni.h"
1818
#endif
1919

20+
#if __OHOS__
21+
#include "rawfile/raw_file_manager.h"
22+
#endif
23+
2024
#include "onnxruntime_cxx_api.h" // NOLINT
2125
#include "sherpa-onnx/csrc/cat.h"
2226
#include "sherpa-onnx/csrc/macros.h"
@@ -50,9 +54,9 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel(
5054
}
5155
}
5256

53-
#if __ANDROID_API__ >= 9
57+
template <typename Manager>
5458
OnlineConformerTransducerModel::OnlineConformerTransducerModel(
55-
AAssetManager *mgr, const OnlineModelConfig &config)
59+
Manager *mgr, const OnlineModelConfig &config)
5660
: env_(ORT_LOGGING_LEVEL_ERROR),
5761
config_(config),
5862
sess_opts_(GetSessionOptions(config)),
@@ -72,7 +76,6 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel(
7276
InitJoiner(buf.data(), buf.size());
7377
}
7478
}
75-
#endif
7679

7780
void OnlineConformerTransducerModel::InitEncoder(void *model_data,
7881
size_t model_data_length) {
@@ -91,7 +94,11 @@ void OnlineConformerTransducerModel::InitEncoder(void *model_data,
9194
std::ostringstream os;
9295
os << "---encoder---\n";
9396
PrintModelMetadata(os, meta_data);
97+
#if __OHOS__
98+
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
99+
#else
94100
SHERPA_ONNX_LOGE("%s", os.str().c_str());
101+
#endif
95102
}
96103

97104
Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -121,7 +128,11 @@ void OnlineConformerTransducerModel::InitDecoder(void *model_data,
121128
std::ostringstream os;
122129
os << "---decoder---\n";
123130
PrintModelMetadata(os, meta_data);
131+
#if __OHOS__
132+
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
133+
#else
124134
SHERPA_ONNX_LOGE("%s", os.str().c_str());
135+
#endif
125136
}
126137

127138
Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -273,4 +284,14 @@ Ort::Value OnlineConformerTransducerModel::RunJoiner(Ort::Value encoder_out,
273284
return std::move(logit[0]);
274285
}
275286

287+
#if __ANDROID_API__ >= 9
288+
template OnlineConformerTransducerModel::OnlineConformerTransducerModel(
289+
AAssetManager *mgr, const OnlineModelConfig &config);
290+
#endif
291+
292+
#if __OHOS__
293+
template OnlineConformerTransducerModel::OnlineConformerTransducerModel(
294+
NativeResourceManager *mgr, const OnlineModelConfig &config);
295+
#endif
296+
276297
} // namespace sherpa_onnx

sherpa-onnx/csrc/online-conformer-transducer-model.h

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
#include <utility>
1111
#include <vector>
1212

13-
#if __ANDROID_API__ >= 9
14-
#include "android/asset_manager.h"
15-
#include "android/asset_manager_jni.h"
16-
#endif
17-
1813
#include "onnxruntime_cxx_api.h" // NOLINT
1914
#include "sherpa-onnx/csrc/online-model-config.h"
2015
#include "sherpa-onnx/csrc/online-transducer-model.h"
@@ -25,10 +20,8 @@ class OnlineConformerTransducerModel : public OnlineTransducerModel {
2520
public:
2621
explicit OnlineConformerTransducerModel(const OnlineModelConfig &config);
2722

28-
#if __ANDROID_API__ >= 9
29-
OnlineConformerTransducerModel(AAssetManager *mgr,
30-
const OnlineModelConfig &config);
31-
#endif
23+
template <typename Manager>
24+
OnlineConformerTransducerModel(Manager *mgr, const OnlineModelConfig &config);
3225

3326
std::vector<Ort::Value> StackStates(
3427
const std::vector<std::vector<Ort::Value>> &states) const override;

sherpa-onnx/csrc/online-ctc-model.cc

+20-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
#include <sstream>
1010
#include <string>
1111

12+
#if __ANDROID_API__ >= 9
13+
#include "android/asset_manager.h"
14+
#include "android/asset_manager_jni.h"
15+
#endif
16+
17+
#if __OHOS__
18+
#include "rawfile/raw_file_manager.h"
19+
#endif
20+
1221
#include "sherpa-onnx/csrc/macros.h"
1322
#include "sherpa-onnx/csrc/online-nemo-ctc-model.h"
1423
#include "sherpa-onnx/csrc/online-wenet-ctc-model.h"
@@ -31,10 +40,9 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create(
3140
}
3241
}
3342

34-
#if __ANDROID_API__ >= 9
35-
43+
template <typename Manager>
3644
std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create(
37-
AAssetManager *mgr, const OnlineModelConfig &config) {
45+
Manager *mgr, const OnlineModelConfig &config) {
3846
if (!config.wenet_ctc.model.empty()) {
3947
return std::make_unique<OnlineWenetCtcModel>(mgr, config);
4048
} else if (!config.zipformer2_ctc.model.empty()) {
@@ -46,6 +54,15 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create(
4654
exit(-1);
4755
}
4856
}
57+
58+
#if __ANDROID_API__ >= 9
59+
template std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create(
60+
AAssetManager *mgr, const OnlineModelConfig &config);
61+
#endif
62+
63+
#if __OHOS__
64+
template std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create(
65+
NativeResourceManager *mgr, const OnlineModelConfig &config);
4966
#endif
5067

5168
} // namespace sherpa_onnx

sherpa-onnx/csrc/online-ctc-model.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
#include <utility>
99
#include <vector>
1010

11-
#if __ANDROID_API__ >= 9
12-
#include "android/asset_manager.h"
13-
#include "android/asset_manager_jni.h"
14-
#endif
15-
1611
#include "onnxruntime_cxx_api.h" // NOLINT
1712
#include "sherpa-onnx/csrc/online-model-config.h"
1813

@@ -25,10 +20,9 @@ class OnlineCtcModel {
2520
static std::unique_ptr<OnlineCtcModel> Create(
2621
const OnlineModelConfig &config);
2722

28-
#if __ANDROID_API__ >= 9
23+
template <typename Manager>
2924
static std::unique_ptr<OnlineCtcModel> Create(
30-
AAssetManager *mgr, const OnlineModelConfig &config);
31-
#endif
25+
Manager *mgr, const OnlineModelConfig &config);
3226

3327
// Return a list of tensors containing the initial states
3428
virtual std::vector<Ort::Value> GetInitStates() const = 0;

sherpa-onnx/csrc/online-lstm-transducer-model.cc

+20-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include "android/asset_manager_jni.h"
1717
#endif
1818

19+
#if __OHOS__
20+
#include "rawfile/raw_file_manager.h"
21+
#endif
22+
1923
#include "onnxruntime_cxx_api.h" // NOLINT
2024
#include "sherpa-onnx/csrc/cat.h"
2125
#include "sherpa-onnx/csrc/macros.h"
@@ -48,9 +52,9 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel(
4852
}
4953
}
5054

51-
#if __ANDROID_API__ >= 9
55+
template <typename Manager>
5256
OnlineLstmTransducerModel::OnlineLstmTransducerModel(
53-
AAssetManager *mgr, const OnlineModelConfig &config)
57+
Manager *mgr, const OnlineModelConfig &config)
5458
: env_(ORT_LOGGING_LEVEL_ERROR),
5559
config_(config),
5660
sess_opts_(GetSessionOptions(config)),
@@ -70,7 +74,6 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel(
7074
InitJoiner(buf.data(), buf.size());
7175
}
7276
}
73-
#endif
7477

7578
void OnlineLstmTransducerModel::InitEncoder(void *model_data,
7679
size_t model_data_length) {
@@ -89,7 +92,11 @@ void OnlineLstmTransducerModel::InitEncoder(void *model_data,
8992
std::ostringstream os;
9093
os << "---encoder---\n";
9194
PrintModelMetadata(os, meta_data);
95+
#if __OHOS__
96+
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
97+
#else
9298
SHERPA_ONNX_LOGE("%s", os.str().c_str());
99+
#endif
93100
}
94101

95102
Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -261,4 +268,14 @@ Ort::Value OnlineLstmTransducerModel::RunJoiner(Ort::Value encoder_out,
261268
return std::move(logit[0]);
262269
}
263270

271+
#if __ANDROID_API__ >= 9
272+
template OnlineLstmTransducerModel::OnlineLstmTransducerModel(
273+
AAssetManager *mgr, const OnlineModelConfig &config);
274+
#endif
275+
276+
#if __OHOS__
277+
template OnlineLstmTransducerModel::OnlineLstmTransducerModel(
278+
NativeResourceManager *mgr, const OnlineModelConfig &config);
279+
#endif
280+
264281
} // namespace sherpa_onnx

sherpa-onnx/csrc/online-lstm-transducer-model.h

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#include <utility>
1010
#include <vector>
1111

12-
#if __ANDROID_API__ >= 9
13-
#include "android/asset_manager.h"
14-
#include "android/asset_manager_jni.h"
15-
#endif
16-
1712
#include "onnxruntime_cxx_api.h" // NOLINT
1813
#include "sherpa-onnx/csrc/online-model-config.h"
1914
#include "sherpa-onnx/csrc/online-transducer-model.h"
@@ -24,10 +19,8 @@ class OnlineLstmTransducerModel : public OnlineTransducerModel {
2419
public:
2520
explicit OnlineLstmTransducerModel(const OnlineModelConfig &config);
2621

27-
#if __ANDROID_API__ >= 9
28-
OnlineLstmTransducerModel(AAssetManager *mgr,
29-
const OnlineModelConfig &config);
30-
#endif
22+
template <typename Manager>
23+
OnlineLstmTransducerModel(Manager *mgr, const OnlineModelConfig &config);
3124

3225
std::vector<Ort::Value> StackStates(
3326
const std::vector<std::vector<Ort::Value>> &states) const override;

0 commit comments

Comments
 (0)