Fangjun Kuang
Committed by GitHub

Add non-streaming ASR support for HarmonyOS. (#1564)

正在显示 50 个修改的文件 包含 648 行增加454 行删除
@@ -352,27 +352,7 @@ struct SherpaOnnxOfflineStream { @@ -352,27 +352,7 @@ struct SherpaOnnxOfflineStream {
352 : impl(std::move(p)) {} 352 : impl(std::move(p)) {}
353 }; 353 };
354 354
355 -static sherpa_onnx::OfflineRecognizerConfig convertConfig(  
356 - const SherpaOnnxOfflineRecognizerConfig *config);  
357 -  
358 -const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(  
359 - const SherpaOnnxOfflineRecognizerConfig *config) {  
360 - sherpa_onnx::OfflineRecognizerConfig recognizer_config =  
361 - convertConfig(config);  
362 -  
363 - if (!recognizer_config.Validate()) {  
364 - SHERPA_ONNX_LOGE("Errors in config");  
365 - return nullptr;  
366 - }  
367 -  
368 - SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;  
369 -  
370 - recognizer->impl =  
371 - std::make_unique<sherpa_onnx::OfflineRecognizer>(recognizer_config);  
372 -  
373 - return recognizer;  
374 -}  
375 -sherpa_onnx::OfflineRecognizerConfig convertConfig( 355 +static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(
376 const SherpaOnnxOfflineRecognizerConfig *config) { 356 const SherpaOnnxOfflineRecognizerConfig *config) {
377 sherpa_onnx::OfflineRecognizerConfig recognizer_config; 357 sherpa_onnx::OfflineRecognizerConfig recognizer_config;
378 358
@@ -491,17 +471,39 @@ sherpa_onnx::OfflineRecognizerConfig convertConfig( @@ -491,17 +471,39 @@ sherpa_onnx::OfflineRecognizerConfig convertConfig(
491 recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, ""); 471 recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, "");
492 472
493 if (config->model_config.debug) { 473 if (config->model_config.debug) {
  474 +#if __OHOS__
  475 + SHERPA_ONNX_LOGE("%{public}s", recognizer_config.ToString().c_str());
  476 +#else
494 SHERPA_ONNX_LOGE("%s", recognizer_config.ToString().c_str()); 477 SHERPA_ONNX_LOGE("%s", recognizer_config.ToString().c_str());
  478 +#endif
495 } 479 }
496 480
497 return recognizer_config; 481 return recognizer_config;
498 } 482 }
499 483
  484 +const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(
  485 + const SherpaOnnxOfflineRecognizerConfig *config) {
  486 + sherpa_onnx::OfflineRecognizerConfig recognizer_config =
  487 + GetOfflineRecognizerConfig(config);
  488 +
  489 + if (!recognizer_config.Validate()) {
  490 + SHERPA_ONNX_LOGE("Errors in config");
  491 + return nullptr;
  492 + }
  493 +
  494 + SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;
  495 +
  496 + recognizer->impl =
  497 + std::make_unique<sherpa_onnx::OfflineRecognizer>(recognizer_config);
  498 +
  499 + return recognizer;
  500 +}
  501 +
500 void SherpaOnnxOfflineRecognizerSetConfig( 502 void SherpaOnnxOfflineRecognizerSetConfig(
501 const SherpaOnnxOfflineRecognizer *recognizer, 503 const SherpaOnnxOfflineRecognizer *recognizer,
502 const SherpaOnnxOfflineRecognizerConfig *config) { 504 const SherpaOnnxOfflineRecognizerConfig *config) {
503 sherpa_onnx::OfflineRecognizerConfig recognizer_config = 505 sherpa_onnx::OfflineRecognizerConfig recognizer_config =
504 - convertConfig(config); 506 + GetOfflineRecognizerConfig(config);
505 recognizer->impl->SetConfig(recognizer_config); 507 recognizer->impl->SetConfig(recognizer_config);
506 } 508 }
507 509
@@ -977,25 +979,6 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( @@ -977,25 +979,6 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
977 return p; 979 return p;
978 } 980 }
979 981
980 -#ifdef __OHOS__  
981 -SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(  
982 - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,  
983 - NativeResourceManager *mgr) {  
984 - if (mgr == nullptr) {  
985 - return SherpaOnnxCreateVoiceActivityDetector(config,  
986 - buffer_size_in_seconds);  
987 - }  
988 -  
989 - auto vad_config = GetVadModelConfig(config);  
990 -  
991 - SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;  
992 - p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(  
993 - mgr, vad_config, buffer_size_in_seconds);  
994 -  
995 - return p;  
996 -}  
997 -#endif  
998 -  
999 void SherpaOnnxDestroyVoiceActivityDetector( 982 void SherpaOnnxDestroyVoiceActivityDetector(
1000 SherpaOnnxVoiceActivityDetector *p) { 983 SherpaOnnxVoiceActivityDetector *p) {
1001 delete p; 984 delete p;
@@ -1891,4 +1874,42 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( @@ -1891,4 +1874,42 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
1891 return ans; 1874 return ans;
1892 } 1875 }
1893 1876
  1877 +#ifdef __OHOS__
  1878 +
  1879 +const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS(
  1880 + const SherpaOnnxOfflineRecognizerConfig *config,
  1881 + NativeResourceManager *mgr) {
  1882 + if (mgr == nullptr) {
  1883 + return SherpaOnnxCreateOfflineRecognizer(config);
  1884 + }
  1885 +
  1886 + sherpa_onnx::OfflineRecognizerConfig recognizer_config =
  1887 + GetOfflineRecognizerConfig(config);
  1888 +
  1889 + SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;
  1890 +
  1891 + recognizer->impl =
  1892 + std::make_unique<sherpa_onnx::OfflineRecognizer>(mgr, recognizer_config);
  1893 +
  1894 + return recognizer;
  1895 +}
  1896 +
  1897 +SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(
  1898 + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
  1899 + NativeResourceManager *mgr) {
  1900 + if (mgr == nullptr) {
  1901 + return SherpaOnnxCreateVoiceActivityDetector(config,
  1902 + buffer_size_in_seconds);
  1903 + }
  1904 +
  1905 + auto vad_config = GetVadModelConfig(config);
  1906 +
  1907 + SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;
  1908 + p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(
  1909 + mgr, vad_config, buffer_size_in_seconds);
  1910 +
  1911 + return p;
  1912 +}
  1913 +#endif
  1914 +
1894 #endif 1915 #endif
@@ -841,21 +841,6 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector * @@ -841,21 +841,6 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
841 SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config, 841 SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config,
842 float buffer_size_in_seconds); 842 float buffer_size_in_seconds);
843 843
844 -#ifdef __OHOS__  
845 -  
846 -// Return an instance of VoiceActivityDetector.  
847 -// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free  
848 -// the returned pointer to avoid memory leak.  
849 -//  
850 -// It is for HarmonyOS  
851 -typedef struct NativeResourceManager NativeResourceManager;  
852 -  
853 -SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *  
854 -SherpaOnnxCreateVoiceActivityDetectorOHOS(  
855 - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,  
856 - NativeResourceManager *mgr);  
857 -#endif  
858 -  
859 SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector( 844 SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector(
860 SherpaOnnxVoiceActivityDetector *p); 845 SherpaOnnxVoiceActivityDetector *p);
861 846
@@ -1537,6 +1522,29 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( @@ -1537,6 +1522,29 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
1537 SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult( 1522 SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult(
1538 const SherpaOnnxOfflineSpeakerDiarizationResult *r); 1523 const SherpaOnnxOfflineSpeakerDiarizationResult *r);
1539 1524
  1525 +#ifdef __OHOS__
  1526 +
  1527 +// It is for HarmonyOS
  1528 +typedef struct NativeResourceManager NativeResourceManager;
  1529 +
  1530 +/// @param config Config for the recognizer.
  1531 +/// @return Return a pointer to the recognizer. The user has to invoke
  1532 +// SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory
  1533 +// leak.
  1534 +SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer *
  1535 +SherpaOnnxCreateOfflineRecognizerOHOS(
  1536 + const SherpaOnnxOfflineRecognizerConfig *config,
  1537 + NativeResourceManager *mgr);
  1538 +
  1539 +// Return an instance of VoiceActivityDetector.
  1540 +// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free
  1541 +// the returned pointer to avoid memory leak.
  1542 +SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
  1543 +SherpaOnnxCreateVoiceActivityDetectorOHOS(
  1544 + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
  1545 + NativeResourceManager *mgr);
  1546 +#endif
  1547 +
1540 #if defined(__GNUC__) 1548 #if defined(__GNUC__)
1541 #pragma GCC diagnostic pop 1549 #pragma GCC diagnostic pop
1542 #endif 1550 #endif
@@ -214,11 +214,6 @@ struct SHERPA_ONNX_API OfflineTdnnModelConfig { @@ -214,11 +214,6 @@ struct SHERPA_ONNX_API OfflineTdnnModelConfig {
214 std::string model; 214 std::string model;
215 }; 215 };
216 216
217 -struct SHERPA_ONNX_API SherpaOnnxOfflineLMConfig {  
218 - std::string model;  
219 - float scale = 1.0;  
220 -};  
221 -  
222 struct SHERPA_ONNX_API OfflineSenseVoiceModelConfig { 217 struct SHERPA_ONNX_API OfflineSenseVoiceModelConfig {
223 std::string model; 218 std::string model;
224 std::string language; 219 std::string language;
@@ -9,6 +9,15 @@ @@ -9,6 +9,15 @@
9 #include <sstream> 9 #include <sstream>
10 #include <string> 10 #include <string>
11 11
  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 +
12 #include "sherpa-onnx/csrc/macros.h" 21 #include "sherpa-onnx/csrc/macros.h"
13 #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h" 22 #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h"
14 #include "sherpa-onnx/csrc/offline-tdnn-ctc-model.h" 23 #include "sherpa-onnx/csrc/offline-tdnn-ctc-model.h"
@@ -48,7 +57,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length, @@ -48,7 +57,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length,
48 if (debug) { 57 if (debug) {
49 std::ostringstream os; 58 std::ostringstream os;
50 PrintModelMetadata(os, meta_data); 59 PrintModelMetadata(os, meta_data);
51 - SHERPA_ONNX_LOGE("%s", os.str().c_str()); 60 +#if __OHOS__
  61 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  62 +#else
  63 + SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  64 +#endif
52 } 65 }
53 66
54 Ort::AllocatorWithDefaultOptions allocator; 67 Ort::AllocatorWithDefaultOptions allocator;
@@ -144,10 +157,9 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create( @@ -144,10 +157,9 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
144 return nullptr; 157 return nullptr;
145 } 158 }
146 159
147 -#if __ANDROID_API__ >= 9  
148 - 160 +template <typename Manager>
149 std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create( 161 std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
150 - AAssetManager *mgr, const OfflineModelConfig &config) { 162 + Manager *mgr, const OfflineModelConfig &config) {
151 // TODO(fangjun): Refactor it. We don't need to use model_type here 163 // TODO(fangjun): Refactor it. We don't need to use model_type here
152 ModelType model_type = ModelType::kUnknown; 164 ModelType model_type = ModelType::kUnknown;
153 165
@@ -196,6 +208,15 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create( @@ -196,6 +208,15 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
196 208
197 return nullptr; 209 return nullptr;
198 } 210 }
  211 +
  212 +#if __ANDROID_API__ >= 9
  213 +template std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
  214 + AAssetManager *mgr, const OfflineModelConfig &config);
  215 +#endif
  216 +
  217 +#if __OHOS__
  218 +template std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
  219 + NativeResourceManager *mgr, const OfflineModelConfig &config);
199 #endif 220 #endif
200 221
201 } // namespace sherpa_onnx 222 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/offline-model-config.h" 12 #include "sherpa-onnx/csrc/offline-model-config.h"
18 13
@@ -25,10 +20,9 @@ class OfflineCtcModel { @@ -25,10 +20,9 @@ class OfflineCtcModel {
25 static std::unique_ptr<OfflineCtcModel> Create( 20 static std::unique_ptr<OfflineCtcModel> Create(
26 const OfflineModelConfig &config); 21 const OfflineModelConfig &config);
27 22
28 -#if __ANDROID_API__ >= 9 23 + template <typename Manager>
29 static std::unique_ptr<OfflineCtcModel> Create( 24 static std::unique_ptr<OfflineCtcModel> Create(
30 - AAssetManager *mgr, const OfflineModelConfig &config);  
31 -#endif 25 + Manager *mgr, const OfflineModelConfig &config);
32 26
33 /** Run the forward method of the model. 27 /** Run the forward method of the model.
34 * 28 *
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/offline-rnn-lm.h" 20 #include "sherpa-onnx/csrc/offline-rnn-lm.h"
12 21
13 namespace sherpa_onnx { 22 namespace sherpa_onnx {
@@ -16,12 +25,11 @@ std::unique_ptr<OfflineLM> OfflineLM::Create(const OfflineLMConfig &config) { @@ -16,12 +25,11 @@ std::unique_ptr<OfflineLM> OfflineLM::Create(const OfflineLMConfig &config) {
16 return std::make_unique<OfflineRnnLM>(config); 25 return std::make_unique<OfflineRnnLM>(config);
17 } 26 }
18 27
19 -#if __ANDROID_API__ >= 9  
20 -std::unique_ptr<OfflineLM> OfflineLM::Create(AAssetManager *mgr, 28 +template <typename Manager>
  29 +std::unique_ptr<OfflineLM> OfflineLM::Create(Manager *mgr,
21 const OfflineLMConfig &config) { 30 const OfflineLMConfig &config) {
22 return std::make_unique<OfflineRnnLM>(mgr, config); 31 return std::make_unique<OfflineRnnLM>(mgr, config);
23 } 32 }
24 -#endif  
25 33
26 void OfflineLM::ComputeLMScore(float scale, int32_t context_size, 34 void OfflineLM::ComputeLMScore(float scale, int32_t context_size,
27 std::vector<Hypotheses> *hyps) { 35 std::vector<Hypotheses> *hyps) {
@@ -75,4 +83,14 @@ void OfflineLM::ComputeLMScore(float scale, int32_t context_size, @@ -75,4 +83,14 @@ void OfflineLM::ComputeLMScore(float scale, int32_t context_size,
75 } 83 }
76 } 84 }
77 85
  86 +#if __ANDROID_API__ >= 9
  87 +template std::unique_ptr<OfflineLM> OfflineLM::Create(
  88 + AAssetManager *mgr, const OfflineLMConfig &config);
  89 +#endif
  90 +
  91 +#if __OHOS__
  92 +template std::unique_ptr<OfflineLM> OfflineLM::Create(
  93 + NativeResourceManager *mgr, const OfflineLMConfig &config);
  94 +#endif
  95 +
78 } // namespace sherpa_onnx 96 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <memory> 8 #include <memory>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/hypothesis.h" 12 #include "sherpa-onnx/csrc/hypothesis.h"
18 #include "sherpa-onnx/csrc/offline-lm-config.h" 13 #include "sherpa-onnx/csrc/offline-lm-config.h"
@@ -25,10 +20,9 @@ class OfflineLM { @@ -25,10 +20,9 @@ class OfflineLM {
25 20
26 static std::unique_ptr<OfflineLM> Create(const OfflineLMConfig &config); 21 static std::unique_ptr<OfflineLM> Create(const OfflineLMConfig &config);
27 22
28 -#if __ANDROID_API__ >= 9  
29 - static std::unique_ptr<OfflineLM> Create(AAssetManager *mgr, 23 + template <typename Manager>
  24 + static std::unique_ptr<OfflineLM> Create(Manager *mgr,
30 const OfflineLMConfig &config); 25 const OfflineLMConfig &config);
31 -#endif  
32 26
33 /** Rescore a batch of sentences. 27 /** Rescore a batch of sentences.
34 * 28 *
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/macros.h" 20 #include "sherpa-onnx/csrc/macros.h"
12 #include "sherpa-onnx/csrc/onnx-utils.h" 21 #include "sherpa-onnx/csrc/onnx-utils.h"
13 #include "sherpa-onnx/csrc/session.h" 22 #include "sherpa-onnx/csrc/session.h"
@@ -43,8 +52,8 @@ class OfflineMoonshineModel::Impl { @@ -43,8 +52,8 @@ class OfflineMoonshineModel::Impl {
43 } 52 }
44 } 53 }
45 54
46 -#if __ANDROID_API__ >= 9  
47 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 55 + template <typename Manager>
  56 + Impl(Manager *mgr, const OfflineModelConfig &config)
48 : config_(config), 57 : config_(config),
49 env_(ORT_LOGGING_LEVEL_ERROR), 58 env_(ORT_LOGGING_LEVEL_ERROR),
50 sess_opts_(GetSessionOptions(config)), 59 sess_opts_(GetSessionOptions(config)),
@@ -69,7 +78,6 @@ class OfflineMoonshineModel::Impl { @@ -69,7 +78,6 @@ class OfflineMoonshineModel::Impl {
69 InitCachedDecoder(buf.data(), buf.size()); 78 InitCachedDecoder(buf.data(), buf.size());
70 } 79 }
71 } 80 }
72 -#endif  
73 81
74 Ort::Value ForwardPreprocessor(Ort::Value audio) { 82 Ort::Value ForwardPreprocessor(Ort::Value audio) {
75 auto features = preprocessor_sess_->Run( 83 auto features = preprocessor_sess_->Run(
@@ -242,11 +250,10 @@ class OfflineMoonshineModel::Impl { @@ -242,11 +250,10 @@ class OfflineMoonshineModel::Impl {
242 OfflineMoonshineModel::OfflineMoonshineModel(const OfflineModelConfig &config) 250 OfflineMoonshineModel::OfflineMoonshineModel(const OfflineModelConfig &config)
243 : impl_(std::make_unique<Impl>(config)) {} 251 : impl_(std::make_unique<Impl>(config)) {}
244 252
245 -#if __ANDROID_API__ >= 9  
246 -OfflineMoonshineModel::OfflineMoonshineModel(AAssetManager *mgr, 253 +template <typename Manager>
  254 +OfflineMoonshineModel::OfflineMoonshineModel(Manager *mgr,
247 const OfflineModelConfig &config) 255 const OfflineModelConfig &config)
248 : impl_(std::make_unique<Impl>(mgr, config)) {} 256 : impl_(std::make_unique<Impl>(mgr, config)) {}
249 -#endif  
250 257
251 OfflineMoonshineModel::~OfflineMoonshineModel() = default; 258 OfflineMoonshineModel::~OfflineMoonshineModel() = default;
252 259
@@ -279,4 +286,14 @@ OrtAllocator *OfflineMoonshineModel::Allocator() const { @@ -279,4 +286,14 @@ OrtAllocator *OfflineMoonshineModel::Allocator() const {
279 return impl_->Allocator(); 286 return impl_->Allocator();
280 } 287 }
281 288
  289 +#if __ANDROID_API__ >= 9
  290 +template OfflineMoonshineModel::OfflineMoonshineModel(
  291 + AAssetManager *mgr, const OfflineModelConfig &config);
  292 +#endif
  293 +
  294 +#if __OHOS__
  295 +template OfflineMoonshineModel::OfflineMoonshineModel(
  296 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  297 +#endif
  298 +
282 } // namespace sherpa_onnx 299 } // namespace sherpa_onnx
@@ -9,11 +9,6 @@ @@ -9,11 +9,6 @@
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 -#if __ANDROID_API__ >= 9  
13 -#include "android/asset_manager.h"  
14 -#include "android/asset_manager_jni.h"  
15 -#endif  
16 -  
17 #include "onnxruntime_cxx_api.h" // NOLINT 12 #include "onnxruntime_cxx_api.h" // NOLINT
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
19 14
@@ -25,9 +20,8 @@ class OfflineMoonshineModel { @@ -25,9 +20,8 @@ class OfflineMoonshineModel {
25 public: 20 public:
26 explicit OfflineMoonshineModel(const OfflineModelConfig &config); 21 explicit OfflineMoonshineModel(const OfflineModelConfig &config);
27 22
28 -#if __ANDROID_API__ >= 9  
29 - OfflineMoonshineModel(AAssetManager *mgr, const OfflineModelConfig &config);  
30 -#endif 23 + template <typename Manager>
  24 + OfflineMoonshineModel(Manager *mgr, const OfflineModelConfig &config);
31 25
32 ~OfflineMoonshineModel(); 26 ~OfflineMoonshineModel();
33 27
@@ -4,6 +4,15 @@ @@ -4,6 +4,15 @@
4 4
5 #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h" 5 #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h"
6 6
  7 +#if __ANDROID_API__ >= 9
  8 +#include "android/asset_manager.h"
  9 +#include "android/asset_manager_jni.h"
  10 +#endif
  11 +
  12 +#if __OHOS__
  13 +#include "rawfile/raw_file_manager.h"
  14 +#endif
  15 +
7 #include "sherpa-onnx/csrc/macros.h" 16 #include "sherpa-onnx/csrc/macros.h"
8 #include "sherpa-onnx/csrc/onnx-utils.h" 17 #include "sherpa-onnx/csrc/onnx-utils.h"
9 #include "sherpa-onnx/csrc/session.h" 18 #include "sherpa-onnx/csrc/session.h"
@@ -23,8 +32,8 @@ class OfflineNemoEncDecCtcModel::Impl { @@ -23,8 +32,8 @@ class OfflineNemoEncDecCtcModel::Impl {
23 Init(buf.data(), buf.size()); 32 Init(buf.data(), buf.size());
24 } 33 }
25 34
26 -#if __ANDROID_API__ >= 9  
27 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 35 + template <typename Manager>
  36 + Impl(Manager *mgr, const OfflineModelConfig &config)
28 : config_(config), 37 : config_(config),
29 env_(ORT_LOGGING_LEVEL_ERROR), 38 env_(ORT_LOGGING_LEVEL_ERROR),
30 sess_opts_(GetSessionOptions(config)), 39 sess_opts_(GetSessionOptions(config)),
@@ -32,7 +41,6 @@ class OfflineNemoEncDecCtcModel::Impl { @@ -32,7 +41,6 @@ class OfflineNemoEncDecCtcModel::Impl {
32 auto buf = ReadFile(mgr, config_.nemo_ctc.model); 41 auto buf = ReadFile(mgr, config_.nemo_ctc.model);
33 Init(buf.data(), buf.size()); 42 Init(buf.data(), buf.size());
34 } 43 }
35 -#endif  
36 44
37 std::vector<Ort::Value> Forward(Ort::Value features, 45 std::vector<Ort::Value> Forward(Ort::Value features,
38 Ort::Value features_length) { 46 Ort::Value features_length) {
@@ -88,7 +96,11 @@ class OfflineNemoEncDecCtcModel::Impl { @@ -88,7 +96,11 @@ class OfflineNemoEncDecCtcModel::Impl {
88 if (config_.debug) { 96 if (config_.debug) {
89 std::ostringstream os; 97 std::ostringstream os;
90 PrintModelMetadata(os, meta_data); 98 PrintModelMetadata(os, meta_data);
  99 +#if __OHOS__
  100 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  101 +#else
91 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 102 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  103 +#endif
92 } 104 }
93 105
94 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 106 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -126,11 +138,10 @@ OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( @@ -126,11 +138,10 @@ OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel(
126 const OfflineModelConfig &config) 138 const OfflineModelConfig &config)
127 : impl_(std::make_unique<Impl>(config)) {} 139 : impl_(std::make_unique<Impl>(config)) {}
128 140
129 -#if __ANDROID_API__ >= 9 141 +template <typename Manager>
130 OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( 142 OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel(
131 - AAssetManager *mgr, const OfflineModelConfig &config) 143 + Manager *mgr, const OfflineModelConfig &config)
132 : impl_(std::make_unique<Impl>(mgr, config)) {} 144 : impl_(std::make_unique<Impl>(mgr, config)) {}
133 -#endif  
134 145
135 OfflineNemoEncDecCtcModel::~OfflineNemoEncDecCtcModel() = default; 146 OfflineNemoEncDecCtcModel::~OfflineNemoEncDecCtcModel() = default;
136 147
@@ -156,4 +167,14 @@ std::string OfflineNemoEncDecCtcModel::FeatureNormalizationMethod() const { @@ -156,4 +167,14 @@ std::string OfflineNemoEncDecCtcModel::FeatureNormalizationMethod() const {
156 167
157 bool OfflineNemoEncDecCtcModel::IsGigaAM() const { return impl_->IsGigaAM(); } 168 bool OfflineNemoEncDecCtcModel::IsGigaAM() const { return impl_->IsGigaAM(); }
158 169
  170 +#if __ANDROID_API__ >= 9
  171 +template OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel(
  172 + AAssetManager *mgr, const OfflineModelConfig &config);
  173 +#endif
  174 +
  175 +#if __OHOS__
  176 +template OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel(
  177 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  178 +#endif
  179 +
159 } // namespace sherpa_onnx 180 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/offline-ctc-model.h" 12 #include "sherpa-onnx/csrc/offline-ctc-model.h"
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -29,10 +24,8 @@ class OfflineNemoEncDecCtcModel : public OfflineCtcModel { @@ -29,10 +24,8 @@ class OfflineNemoEncDecCtcModel : public OfflineCtcModel {
29 public: 24 public:
30 explicit OfflineNemoEncDecCtcModel(const OfflineModelConfig &config); 25 explicit OfflineNemoEncDecCtcModel(const OfflineModelConfig &config);
31 26
32 -#if __ANDROID_API__ >= 9  
33 - OfflineNemoEncDecCtcModel(AAssetManager *mgr,  
34 - const OfflineModelConfig &config);  
35 -#endif 27 + template <typename Manager>
  28 + OfflineNemoEncDecCtcModel(Manager *mgr, const OfflineModelConfig &config);
36 29
37 ~OfflineNemoEncDecCtcModel() override; 30 ~OfflineNemoEncDecCtcModel() override;
38 31
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/macros.h" 20 #include "sherpa-onnx/csrc/macros.h"
12 #include "sherpa-onnx/csrc/onnx-utils.h" 21 #include "sherpa-onnx/csrc/onnx-utils.h"
13 #include "sherpa-onnx/csrc/session.h" 22 #include "sherpa-onnx/csrc/session.h"
@@ -26,8 +35,8 @@ class OfflineParaformerModel::Impl { @@ -26,8 +35,8 @@ class OfflineParaformerModel::Impl {
26 Init(buf.data(), buf.size()); 35 Init(buf.data(), buf.size());
27 } 36 }
28 37
29 -#if __ANDROID_API__ >= 9  
30 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 38 + template <typename Manager>
  39 + Impl(Manager *mgr, const OfflineModelConfig &config)
31 : config_(config), 40 : config_(config),
32 env_(ORT_LOGGING_LEVEL_ERROR), 41 env_(ORT_LOGGING_LEVEL_ERROR),
33 sess_opts_(GetSessionOptions(config)), 42 sess_opts_(GetSessionOptions(config)),
@@ -35,7 +44,6 @@ class OfflineParaformerModel::Impl { @@ -35,7 +44,6 @@ class OfflineParaformerModel::Impl {
35 auto buf = ReadFile(mgr, config_.paraformer.model); 44 auto buf = ReadFile(mgr, config_.paraformer.model);
36 Init(buf.data(), buf.size()); 45 Init(buf.data(), buf.size());
37 } 46 }
38 -#endif  
39 47
40 std::vector<Ort::Value> Forward(Ort::Value features, 48 std::vector<Ort::Value> Forward(Ort::Value features,
41 Ort::Value features_length) { 49 Ort::Value features_length) {
@@ -72,7 +80,11 @@ class OfflineParaformerModel::Impl { @@ -72,7 +80,11 @@ class OfflineParaformerModel::Impl {
72 if (config_.debug) { 80 if (config_.debug) {
73 std::ostringstream os; 81 std::ostringstream os;
74 PrintModelMetadata(os, meta_data); 82 PrintModelMetadata(os, meta_data);
  83 +#if __OHOS__
  84 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  85 +#else
75 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 86 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  87 +#endif
76 } 88 }
77 89
78 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 90 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -109,11 +121,10 @@ class OfflineParaformerModel::Impl { @@ -109,11 +121,10 @@ class OfflineParaformerModel::Impl {
109 OfflineParaformerModel::OfflineParaformerModel(const OfflineModelConfig &config) 121 OfflineParaformerModel::OfflineParaformerModel(const OfflineModelConfig &config)
110 : impl_(std::make_unique<Impl>(config)) {} 122 : impl_(std::make_unique<Impl>(config)) {}
111 123
112 -#if __ANDROID_API__ >= 9  
113 -OfflineParaformerModel::OfflineParaformerModel(AAssetManager *mgr, 124 +template <typename Manager>
  125 +OfflineParaformerModel::OfflineParaformerModel(Manager *mgr,
114 const OfflineModelConfig &config) 126 const OfflineModelConfig &config)
115 : impl_(std::make_unique<Impl>(mgr, config)) {} 127 : impl_(std::make_unique<Impl>(mgr, config)) {}
116 -#endif  
117 128
118 OfflineParaformerModel::~OfflineParaformerModel() = default; 129 OfflineParaformerModel::~OfflineParaformerModel() = default;
119 130
@@ -141,4 +152,14 @@ OrtAllocator *OfflineParaformerModel::Allocator() const { @@ -141,4 +152,14 @@ OrtAllocator *OfflineParaformerModel::Allocator() const {
141 return impl_->Allocator(); 152 return impl_->Allocator();
142 } 153 }
143 154
  155 +#if __ANDROID_API__ >= 9
  156 +template OfflineParaformerModel::OfflineParaformerModel(
  157 + AAssetManager *mgr, const OfflineModelConfig &config);
  158 +#endif
  159 +
  160 +#if __OHOS__
  161 +template OfflineParaformerModel::OfflineParaformerModel(
  162 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  163 +#endif
  164 +
144 } // namespace sherpa_onnx 165 } // namespace sherpa_onnx
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 #include "onnxruntime_cxx_api.h" // NOLINT 10 #include "onnxruntime_cxx_api.h" // NOLINT
16 #include "sherpa-onnx/csrc/offline-model-config.h" 11 #include "sherpa-onnx/csrc/offline-model-config.h"
17 12
@@ -21,9 +16,8 @@ class OfflineParaformerModel { @@ -21,9 +16,8 @@ class OfflineParaformerModel {
21 public: 16 public:
22 explicit OfflineParaformerModel(const OfflineModelConfig &config); 17 explicit OfflineParaformerModel(const OfflineModelConfig &config);
23 18
24 -#if __ANDROID_API__ >= 9  
25 - OfflineParaformerModel(AAssetManager *mgr, const OfflineModelConfig &config);  
26 -#endif 19 + template <typename Manager>
  20 + OfflineParaformerModel(Manager *mgr, const OfflineModelConfig &config);
27 21
28 ~OfflineParaformerModel(); 22 ~OfflineParaformerModel();
29 23
@@ -12,11 +12,6 @@ @@ -12,11 +12,6 @@
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 -#if __ANDROID_API__ >= 9  
16 -#include "android/asset_manager.h"  
17 -#include "android/asset_manager_jni.h"  
18 -#endif  
19 -  
20 #include "sherpa-onnx/csrc/offline-ctc-decoder.h" 15 #include "sherpa-onnx/csrc/offline-ctc-decoder.h"
21 #include "sherpa-onnx/csrc/offline-ctc-fst-decoder.h" 16 #include "sherpa-onnx/csrc/offline-ctc-fst-decoder.h"
22 #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h" 17 #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h"
@@ -80,16 +75,14 @@ class OfflineRecognizerCtcImpl : public OfflineRecognizerImpl { @@ -80,16 +75,14 @@ class OfflineRecognizerCtcImpl : public OfflineRecognizerImpl {
80 Init(); 75 Init();
81 } 76 }
82 77
83 -#if __ANDROID_API__ >= 9  
84 - OfflineRecognizerCtcImpl(AAssetManager *mgr,  
85 - const OfflineRecognizerConfig &config) 78 + template <typename Manager>
  79 + OfflineRecognizerCtcImpl(Manager *mgr, const OfflineRecognizerConfig &config)
86 : OfflineRecognizerImpl(mgr, config), 80 : OfflineRecognizerImpl(mgr, config),
87 config_(config), 81 config_(config),
88 symbol_table_(mgr, config_.model_config.tokens), 82 symbol_table_(mgr, config_.model_config.tokens),
89 model_(OfflineCtcModel::Create(mgr, config_.model_config)) { 83 model_(OfflineCtcModel::Create(mgr, config_.model_config)) {
90 Init(); 84 Init();
91 } 85 }
92 -#endif  
93 86
94 void Init() { 87 void Init() {
95 if (!config_.model_config.telespeech_ctc.empty()) { 88 if (!config_.model_config.telespeech_ctc.empty()) {
@@ -13,6 +13,10 @@ @@ -13,6 +13,10 @@
13 13
14 #include "android/asset_manager.h" 14 #include "android/asset_manager.h"
15 #include "android/asset_manager_jni.h" 15 #include "android/asset_manager_jni.h"
  16 +#elif __OHOS__
  17 +#include <strstream>
  18 +
  19 +#include "rawfile/raw_file_manager.h"
16 #endif 20 #endif
17 21
18 #include "fst/extensions/far/far.h" 22 #include "fst/extensions/far/far.h"
@@ -211,9 +215,9 @@ std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create( @@ -211,9 +215,9 @@ std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create(
211 exit(-1); 215 exit(-1);
212 } 216 }
213 217
214 -#if __ANDROID_API__ >= 9 218 +template <typename Manager>
215 std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create( 219 std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create(
216 - AAssetManager *mgr, const OfflineRecognizerConfig &config) { 220 + Manager *mgr, const OfflineRecognizerConfig &config) {
217 if (!config.model_config.sense_voice.model.empty()) { 221 if (!config.model_config.sense_voice.model.empty()) {
218 return std::make_unique<OfflineRecognizerSenseVoiceImpl>(mgr, config); 222 return std::make_unique<OfflineRecognizerSenseVoiceImpl>(mgr, config);
219 } 223 }
@@ -389,7 +393,6 @@ std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create( @@ -389,7 +393,6 @@ std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create(
389 393
390 exit(-1); 394 exit(-1);
391 } 395 }
392 -#endif  
393 396
394 OfflineRecognizerImpl::OfflineRecognizerImpl( 397 OfflineRecognizerImpl::OfflineRecognizerImpl(
395 const OfflineRecognizerConfig &config) 398 const OfflineRecognizerConfig &config)
@@ -436,9 +439,9 @@ OfflineRecognizerImpl::OfflineRecognizerImpl( @@ -436,9 +439,9 @@ OfflineRecognizerImpl::OfflineRecognizerImpl(
436 } 439 }
437 } 440 }
438 441
439 -#if __ANDROID_API__ >= 9 442 +template <typename Manager>
440 OfflineRecognizerImpl::OfflineRecognizerImpl( 443 OfflineRecognizerImpl::OfflineRecognizerImpl(
441 - AAssetManager *mgr, const OfflineRecognizerConfig &config) 444 + Manager *mgr, const OfflineRecognizerConfig &config)
442 : config_(config) { 445 : config_(config) {
443 if (!config.rule_fsts.empty()) { 446 if (!config.rule_fsts.empty()) {
444 std::vector<std::string> files; 447 std::vector<std::string> files;
@@ -482,7 +485,6 @@ OfflineRecognizerImpl::OfflineRecognizerImpl( @@ -482,7 +485,6 @@ OfflineRecognizerImpl::OfflineRecognizerImpl(
482 } // for (const auto &f : files) 485 } // for (const auto &f : files)
483 } // if (!config.rule_fars.empty()) 486 } // if (!config.rule_fars.empty())
484 } 487 }
485 -#endif  
486 488
487 std::string OfflineRecognizerImpl::ApplyInverseTextNormalization( 489 std::string OfflineRecognizerImpl::ApplyInverseTextNormalization(
488 std::string text) const { 490 std::string text) const {
@@ -499,4 +501,19 @@ void OfflineRecognizerImpl::SetConfig(const OfflineRecognizerConfig &config) { @@ -499,4 +501,19 @@ void OfflineRecognizerImpl::SetConfig(const OfflineRecognizerConfig &config) {
499 config_ = config; 501 config_ = config;
500 } 502 }
501 503
  504 +#if __ANDROID_API__ >= 9
  505 +template OfflineRecognizerImpl::OfflineRecognizerImpl(
  506 + AAssetManager *mgr, const OfflineRecognizerConfig &config);
  507 +
  508 +template std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create(
  509 + AAssetManager *mgr, const OfflineRecognizerConfig &config);
  510 +#endif
  511 +
  512 +#if __OHOS__
  513 +template OfflineRecognizerImpl::OfflineRecognizerImpl(
  514 + NativeResourceManager *mgr, const OfflineRecognizerConfig &config);
  515 +template std::unique_ptr<OfflineRecognizerImpl> OfflineRecognizerImpl::Create(
  516 + NativeResourceManager *mgr, const OfflineRecognizerConfig &config);
  517 +#endif
  518 +
502 } // namespace sherpa_onnx 519 } // namespace sherpa_onnx
@@ -9,11 +9,6 @@ @@ -9,11 +9,6 @@
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 -#if __ANDROID_API__ >= 9  
13 -#include "android/asset_manager.h"  
14 -#include "android/asset_manager_jni.h"  
15 -#endif  
16 -  
17 #include "kaldifst/csrc/text-normalizer.h" 12 #include "kaldifst/csrc/text-normalizer.h"
18 #include "sherpa-onnx/csrc/macros.h" 13 #include "sherpa-onnx/csrc/macros.h"
19 #include "sherpa-onnx/csrc/offline-recognizer.h" 14 #include "sherpa-onnx/csrc/offline-recognizer.h"
@@ -28,13 +23,12 @@ class OfflineRecognizerImpl { @@ -28,13 +23,12 @@ class OfflineRecognizerImpl {
28 static std::unique_ptr<OfflineRecognizerImpl> Create( 23 static std::unique_ptr<OfflineRecognizerImpl> Create(
29 const OfflineRecognizerConfig &config); 24 const OfflineRecognizerConfig &config);
30 25
31 -#if __ANDROID_API__ >= 9  
32 - OfflineRecognizerImpl(AAssetManager *mgr,  
33 - const OfflineRecognizerConfig &config); 26 + template <typename Manager>
  27 + OfflineRecognizerImpl(Manager *mgr, const OfflineRecognizerConfig &config);
34 28
  29 + template <typename Manager>
35 static std::unique_ptr<OfflineRecognizerImpl> Create( 30 static std::unique_ptr<OfflineRecognizerImpl> Create(
36 - AAssetManager *mgr, const OfflineRecognizerConfig &config);  
37 -#endif 31 + Manager *mgr, const OfflineRecognizerConfig &config);
38 32
39 virtual ~OfflineRecognizerImpl() = default; 33 virtual ~OfflineRecognizerImpl() = default;
40 34
@@ -12,11 +12,6 @@ @@ -12,11 +12,6 @@
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 -#if __ANDROID_API__ >= 9  
16 -#include "android/asset_manager.h"  
17 -#include "android/asset_manager_jni.h"  
18 -#endif  
19 -  
20 #include "sherpa-onnx/csrc/offline-model-config.h" 15 #include "sherpa-onnx/csrc/offline-model-config.h"
21 #include "sherpa-onnx/csrc/offline-moonshine-decoder.h" 16 #include "sherpa-onnx/csrc/offline-moonshine-decoder.h"
22 #include "sherpa-onnx/csrc/offline-moonshine-greedy-search-decoder.h" 17 #include "sherpa-onnx/csrc/offline-moonshine-greedy-search-decoder.h"
@@ -59,8 +54,8 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl { @@ -59,8 +54,8 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl {
59 Init(); 54 Init();
60 } 55 }
61 56
62 -#if __ANDROID_API__ >= 9  
63 - OfflineRecognizerMoonshineImpl(AAssetManager *mgr, 57 + template <typename Manager>
  58 + OfflineRecognizerMoonshineImpl(Manager *mgr,
64 const OfflineRecognizerConfig &config) 59 const OfflineRecognizerConfig &config)
65 : OfflineRecognizerImpl(mgr, config), 60 : OfflineRecognizerImpl(mgr, config),
66 config_(config), 61 config_(config),
@@ -70,8 +65,6 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl { @@ -70,8 +65,6 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl {
70 Init(); 65 Init();
71 } 66 }
72 67
73 -#endif  
74 -  
75 void Init() { 68 void Init() {
76 if (config_.decoding_method == "greedy_search") { 69 if (config_.decoding_method == "greedy_search") {
77 decoder_ = 70 decoder_ =
@@ -11,11 +11,6 @@ @@ -11,11 +11,6 @@
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 -#if __ANDROID_API__ >= 9  
15 -#include "android/asset_manager.h"  
16 -#include "android/asset_manager_jni.h"  
17 -#endif  
18 -  
19 #include "sherpa-onnx/csrc/offline-model-config.h" 14 #include "sherpa-onnx/csrc/offline-model-config.h"
20 #include "sherpa-onnx/csrc/offline-paraformer-decoder.h" 15 #include "sherpa-onnx/csrc/offline-paraformer-decoder.h"
21 #include "sherpa-onnx/csrc/offline-paraformer-greedy-search-decoder.h" 16 #include "sherpa-onnx/csrc/offline-paraformer-greedy-search-decoder.h"
@@ -105,8 +100,8 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl { @@ -105,8 +100,8 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl {
105 InitFeatConfig(); 100 InitFeatConfig();
106 } 101 }
107 102
108 -#if __ANDROID_API__ >= 9  
109 - OfflineRecognizerParaformerImpl(AAssetManager *mgr, 103 + template <typename Manager>
  104 + OfflineRecognizerParaformerImpl(Manager *mgr,
110 const OfflineRecognizerConfig &config) 105 const OfflineRecognizerConfig &config)
111 : OfflineRecognizerImpl(mgr, config), 106 : OfflineRecognizerImpl(mgr, config),
112 config_(config), 107 config_(config),
@@ -124,7 +119,6 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl { @@ -124,7 +119,6 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl {
124 119
125 InitFeatConfig(); 120 InitFeatConfig();
126 } 121 }
127 -#endif  
128 122
129 std::unique_ptr<OfflineStream> CreateStream() const override { 123 std::unique_ptr<OfflineStream> CreateStream() const override {
130 return std::make_unique<OfflineStream>(config_.feat_config); 124 return std::make_unique<OfflineStream>(config_.feat_config);
@@ -11,11 +11,6 @@ @@ -11,11 +11,6 @@
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 -#if __ANDROID_API__ >= 9  
15 -#include "android/asset_manager.h"  
16 -#include "android/asset_manager_jni.h"  
17 -#endif  
18 -  
19 #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h" 14 #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h"
20 #include "sherpa-onnx/csrc/offline-model-config.h" 15 #include "sherpa-onnx/csrc/offline-model-config.h"
21 #include "sherpa-onnx/csrc/offline-recognizer-impl.h" 16 #include "sherpa-onnx/csrc/offline-recognizer-impl.h"
@@ -83,8 +78,8 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl { @@ -83,8 +78,8 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl {
83 InitFeatConfig(); 78 InitFeatConfig();
84 } 79 }
85 80
86 -#if __ANDROID_API__ >= 9  
87 - OfflineRecognizerSenseVoiceImpl(AAssetManager *mgr, 81 + template <typename Manager>
  82 + OfflineRecognizerSenseVoiceImpl(Manager *mgr,
88 const OfflineRecognizerConfig &config) 83 const OfflineRecognizerConfig &config)
89 : OfflineRecognizerImpl(mgr, config), 84 : OfflineRecognizerImpl(mgr, config),
90 config_(config), 85 config_(config),
@@ -103,7 +98,6 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl { @@ -103,7 +98,6 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl {
103 98
104 InitFeatConfig(); 99 InitFeatConfig();
105 } 100 }
106 -#endif  
107 101
108 std::unique_ptr<OfflineStream> CreateStream() const override { 102 std::unique_ptr<OfflineStream> CreateStream() const override {
109 return std::make_unique<OfflineStream>(config_.feat_config); 103 return std::make_unique<OfflineStream>(config_.feat_config);
@@ -14,11 +14,6 @@ @@ -14,11 +14,6 @@
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 -#if __ANDROID_API__ >= 9  
18 -#include "android/asset_manager.h"  
19 -#include "android/asset_manager_jni.h"  
20 -#endif  
21 -  
22 #include "sherpa-onnx/csrc/context-graph.h" 17 #include "sherpa-onnx/csrc/context-graph.h"
23 #include "sherpa-onnx/csrc/log.h" 18 #include "sherpa-onnx/csrc/log.h"
24 #include "sherpa-onnx/csrc/macros.h" 19 #include "sherpa-onnx/csrc/macros.h"
@@ -109,9 +104,9 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { @@ -109,9 +104,9 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl {
109 } 104 }
110 } 105 }
111 106
112 -#if __ANDROID_API__ >= 9 107 + template <typename Manager>
113 explicit OfflineRecognizerTransducerImpl( 108 explicit OfflineRecognizerTransducerImpl(
114 - AAssetManager *mgr, const OfflineRecognizerConfig &config) 109 + Manager *mgr, const OfflineRecognizerConfig &config)
115 : OfflineRecognizerImpl(mgr, config), 110 : OfflineRecognizerImpl(mgr, config),
116 config_(config), 111 config_(config),
117 symbol_table_(mgr, config_.model_config.tokens), 112 symbol_table_(mgr, config_.model_config.tokens),
@@ -148,7 +143,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { @@ -148,7 +143,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl {
148 exit(-1); 143 exit(-1);
149 } 144 }
150 } 145 }
151 -#endif  
152 146
153 std::unique_ptr<OfflineStream> CreateStream( 147 std::unique_ptr<OfflineStream> CreateStream(
154 const std::string &hotwords) const override { 148 const std::string &hotwords) const override {
@@ -246,10 +240,7 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { @@ -246,10 +240,7 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl {
246 } 240 }
247 } 241 }
248 242
249 - OfflineRecognizerConfig GetConfig() const override {  
250 - return config_;  
251 - }  
252 - 243 + OfflineRecognizerConfig GetConfig() const override { return config_; }
253 244
254 void InitHotwords() { 245 void InitHotwords() {
255 // each line in hotwords_file contains space-separated words 246 // each line in hotwords_file contains space-separated words
@@ -271,8 +262,8 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { @@ -271,8 +262,8 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl {
271 hotwords_, config_.hotwords_score, boost_scores_); 262 hotwords_, config_.hotwords_score, boost_scores_);
272 } 263 }
273 264
274 -#if __ANDROID_API__ >= 9  
275 - void InitHotwords(AAssetManager *mgr) { 265 + template <typename Manager>
  266 + void InitHotwords(Manager *mgr) {
276 // each line in hotwords_file contains space-separated words 267 // each line in hotwords_file contains space-separated words
277 268
278 auto buf = ReadFile(mgr, config_.hotwords_file); 269 auto buf = ReadFile(mgr, config_.hotwords_file);
@@ -294,7 +285,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { @@ -294,7 +285,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl {
294 hotwords_graph_ = std::make_shared<ContextGraph>( 285 hotwords_graph_ = std::make_shared<ContextGraph>(
295 hotwords_, config_.hotwords_score, boost_scores_); 286 hotwords_, config_.hotwords_score, boost_scores_);
296 } 287 }
297 -#endif  
298 288
299 private: 289 private:
300 OfflineRecognizerConfig config_; 290 OfflineRecognizerConfig config_;
@@ -14,11 +14,6 @@ @@ -14,11 +14,6 @@
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 -#if __ANDROID_API__ >= 9  
18 -#include "android/asset_manager.h"  
19 -#include "android/asset_manager_jni.h"  
20 -#endif  
21 -  
22 #include "sherpa-onnx/csrc/macros.h" 17 #include "sherpa-onnx/csrc/macros.h"
23 #include "sherpa-onnx/csrc/offline-recognizer-impl.h" 18 #include "sherpa-onnx/csrc/offline-recognizer-impl.h"
24 #include "sherpa-onnx/csrc/offline-recognizer.h" 19 #include "sherpa-onnx/csrc/offline-recognizer.h"
@@ -57,9 +52,9 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl { @@ -57,9 +52,9 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl {
57 PostInit(); 52 PostInit();
58 } 53 }
59 54
60 -#if __ANDROID_API__ >= 9 55 + template <typename Manager>
61 explicit OfflineRecognizerTransducerNeMoImpl( 56 explicit OfflineRecognizerTransducerNeMoImpl(
62 - AAssetManager *mgr, const OfflineRecognizerConfig &config) 57 + Manager *mgr, const OfflineRecognizerConfig &config)
63 : OfflineRecognizerImpl(mgr, config), 58 : OfflineRecognizerImpl(mgr, config),
64 config_(config), 59 config_(config),
65 symbol_table_(mgr, config_.model_config.tokens), 60 symbol_table_(mgr, config_.model_config.tokens),
@@ -76,7 +71,6 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl { @@ -76,7 +71,6 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl {
76 71
77 PostInit(); 72 PostInit();
78 } 73 }
79 -#endif  
80 74
81 std::unique_ptr<OfflineStream> CreateStream() const override { 75 std::unique_ptr<OfflineStream> CreateStream() const override {
82 return std::make_unique<OfflineStream>(config_.feat_config); 76 return std::make_unique<OfflineStream>(config_.feat_config);
@@ -12,11 +12,6 @@ @@ -12,11 +12,6 @@
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 -#if __ANDROID_API__ >= 9  
16 -#include "android/asset_manager.h"  
17 -#include "android/asset_manager_jni.h"  
18 -#endif  
19 -  
20 #include "sherpa-onnx/csrc/offline-model-config.h" 15 #include "sherpa-onnx/csrc/offline-model-config.h"
21 #include "sherpa-onnx/csrc/offline-recognizer-impl.h" 16 #include "sherpa-onnx/csrc/offline-recognizer-impl.h"
22 #include "sherpa-onnx/csrc/offline-recognizer.h" 17 #include "sherpa-onnx/csrc/offline-recognizer.h"
@@ -60,8 +55,8 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { @@ -60,8 +55,8 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl {
60 Init(); 55 Init();
61 } 56 }
62 57
63 -#if __ANDROID_API__ >= 9  
64 - OfflineRecognizerWhisperImpl(AAssetManager *mgr, 58 + template <typename Manager>
  59 + OfflineRecognizerWhisperImpl(Manager *mgr,
65 const OfflineRecognizerConfig &config) 60 const OfflineRecognizerConfig &config)
66 : OfflineRecognizerImpl(mgr, config), 61 : OfflineRecognizerImpl(mgr, config),
67 config_(config), 62 config_(config),
@@ -71,8 +66,6 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { @@ -71,8 +66,6 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl {
71 Init(); 66 Init();
72 } 67 }
73 68
74 -#endif  
75 -  
76 void Init() { 69 void Init() {
77 // tokens.txt from whisper is base64 encoded, so we need to decode it 70 // tokens.txt from whisper is base64 encoded, so we need to decode it
78 symbol_table_.ApplyBase64Decode(); 71 symbol_table_.ApplyBase64Decode();
@@ -105,9 +98,7 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { @@ -105,9 +98,7 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl {
105 config_.model_config.whisper = config.model_config.whisper; 98 config_.model_config.whisper = config.model_config.whisper;
106 } 99 }
107 100
108 - OfflineRecognizerConfig GetConfig() const override {  
109 - return config_;  
110 - } 101 + OfflineRecognizerConfig GetConfig() const override { return config_; }
111 102
112 private: 103 private:
113 void DecodeStream(OfflineStream *s) const { 104 void DecodeStream(OfflineStream *s) const {
@@ -6,11 +6,21 @@ @@ -6,11 +6,21 @@
6 6
7 #include <memory> 7 #include <memory>
8 8
  9 +#if __ANDROID_API__ >= 9
  10 +#include "android/asset_manager.h"
  11 +#include "android/asset_manager_jni.h"
  12 +#endif
  13 +
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
9 #include "sherpa-onnx/csrc/file-utils.h" 18 #include "sherpa-onnx/csrc/file-utils.h"
10 #include "sherpa-onnx/csrc/macros.h" 19 #include "sherpa-onnx/csrc/macros.h"
11 #include "sherpa-onnx/csrc/offline-lm-config.h" 20 #include "sherpa-onnx/csrc/offline-lm-config.h"
12 #include "sherpa-onnx/csrc/offline-recognizer-impl.h" 21 #include "sherpa-onnx/csrc/offline-recognizer-impl.h"
13 #include "sherpa-onnx/csrc/text-utils.h" 22 #include "sherpa-onnx/csrc/text-utils.h"
  23 +
14 namespace sherpa_onnx { 24 namespace sherpa_onnx {
15 25
16 void OfflineRecognizerConfig::Register(ParseOptions *po) { 26 void OfflineRecognizerConfig::Register(ParseOptions *po) {
@@ -132,11 +142,10 @@ std::string OfflineRecognizerConfig::ToString() const { @@ -132,11 +142,10 @@ std::string OfflineRecognizerConfig::ToString() const {
132 return os.str(); 142 return os.str();
133 } 143 }
134 144
135 -#if __ANDROID_API__ >= 9  
136 -OfflineRecognizer::OfflineRecognizer(AAssetManager *mgr, 145 +template <typename Manager>
  146 +OfflineRecognizer::OfflineRecognizer(Manager *mgr,
137 const OfflineRecognizerConfig &config) 147 const OfflineRecognizerConfig &config)
138 : impl_(OfflineRecognizerImpl::Create(mgr, config)) {} 148 : impl_(OfflineRecognizerImpl::Create(mgr, config)) {}
139 -#endif  
140 149
141 OfflineRecognizer::OfflineRecognizer(const OfflineRecognizerConfig &config) 150 OfflineRecognizer::OfflineRecognizer(const OfflineRecognizerConfig &config)
142 : impl_(OfflineRecognizerImpl::Create(config)) {} 151 : impl_(OfflineRecognizerImpl::Create(config)) {}
@@ -157,11 +166,21 @@ void OfflineRecognizer::DecodeStreams(OfflineStream **ss, int32_t n) const { @@ -157,11 +166,21 @@ void OfflineRecognizer::DecodeStreams(OfflineStream **ss, int32_t n) const {
157 } 166 }
158 167
159 void OfflineRecognizer::SetConfig(const OfflineRecognizerConfig &config) { 168 void OfflineRecognizer::SetConfig(const OfflineRecognizerConfig &config) {
160 - impl_->SetConfig(config); 169 + impl_->SetConfig(config);
161 } 170 }
162 171
163 OfflineRecognizerConfig OfflineRecognizer::GetConfig() const { 172 OfflineRecognizerConfig OfflineRecognizer::GetConfig() const {
164 return impl_->GetConfig(); 173 return impl_->GetConfig();
165 } 174 }
166 175
  176 +#if __ANDROID_API__ >= 9
  177 +template OfflineRecognizer::OfflineRecognizer(
  178 + AAssetManager *mgr, const OfflineRecognizerConfig &config);
  179 +#endif
  180 +
  181 +#if __OHOS__
  182 +template OfflineRecognizer::OfflineRecognizer(
  183 + NativeResourceManager *mgr, const OfflineRecognizerConfig &config);
  184 +#endif
  185 +
167 } // namespace sherpa_onnx 186 } // namespace sherpa_onnx
@@ -9,11 +9,6 @@ @@ -9,11 +9,6 @@
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 -#if __ANDROID_API__ >= 9  
13 -#include "android/asset_manager.h"  
14 -#include "android/asset_manager_jni.h"  
15 -#endif  
16 -  
17 #include "sherpa-onnx/csrc/features.h" 12 #include "sherpa-onnx/csrc/features.h"
18 #include "sherpa-onnx/csrc/offline-ctc-fst-decoder-config.h" 13 #include "sherpa-onnx/csrc/offline-ctc-fst-decoder-config.h"
19 #include "sherpa-onnx/csrc/offline-lm-config.h" 14 #include "sherpa-onnx/csrc/offline-lm-config.h"
@@ -82,9 +77,8 @@ class OfflineRecognizer { @@ -82,9 +77,8 @@ class OfflineRecognizer {
82 public: 77 public:
83 ~OfflineRecognizer(); 78 ~OfflineRecognizer();
84 79
85 -#if __ANDROID_API__ >= 9  
86 - OfflineRecognizer(AAssetManager *mgr, const OfflineRecognizerConfig &config);  
87 -#endif 80 + template <typename Manager>
  81 + OfflineRecognizer(Manager *mgr, const OfflineRecognizerConfig &config);
88 82
89 explicit OfflineRecognizer(const OfflineRecognizerConfig &config); 83 explicit OfflineRecognizer(const OfflineRecognizerConfig &config);
90 84
@@ -120,10 +114,10 @@ class OfflineRecognizer { @@ -120,10 +114,10 @@ class OfflineRecognizer {
120 void DecodeStreams(OfflineStream **ss, int32_t n) const; 114 void DecodeStreams(OfflineStream **ss, int32_t n) const;
121 115
122 /** Onnxruntime Session objects are not affected by this method. 116 /** Onnxruntime Session objects are not affected by this method.
123 - * The exact behavior can be defined by a specific recognizer impl.  
124 - * For instance, for the whisper recognizer, you can retrieve the language and task from  
125 - * the config and ignore any remaining fields in `config`.  
126 - */ 117 + * The exact behavior can be defined by a specific recognizer impl.
  118 + * For instance, for the whisper recognizer, you can retrieve the language and
  119 + * task from the config and ignore any remaining fields in `config`.
  120 + */
127 void SetConfig(const OfflineRecognizerConfig &config); 121 void SetConfig(const OfflineRecognizerConfig &config);
128 122
129 OfflineRecognizerConfig GetConfig() const; 123 OfflineRecognizerConfig GetConfig() const;
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "onnxruntime_cxx_api.h" // NOLINT 20 #include "onnxruntime_cxx_api.h" // NOLINT
12 #include "sherpa-onnx/csrc/macros.h" 21 #include "sherpa-onnx/csrc/macros.h"
13 #include "sherpa-onnx/csrc/onnx-utils.h" 22 #include "sherpa-onnx/csrc/onnx-utils.h"
@@ -27,8 +36,8 @@ class OfflineRnnLM::Impl { @@ -27,8 +36,8 @@ class OfflineRnnLM::Impl {
27 Init(buf.data(), buf.size()); 36 Init(buf.data(), buf.size());
28 } 37 }
29 38
30 -#if __ANDROID_API__ >= 9  
31 - Impl(AAssetManager *mgr, const OfflineLMConfig &config) 39 + template <typename Manager>
  40 + Impl(Manager *mgr, const OfflineLMConfig &config)
32 : config_(config), 41 : config_(config),
33 env_(ORT_LOGGING_LEVEL_ERROR), 42 env_(ORT_LOGGING_LEVEL_ERROR),
34 sess_opts_{GetSessionOptions(config)}, 43 sess_opts_{GetSessionOptions(config)},
@@ -36,7 +45,6 @@ class OfflineRnnLM::Impl { @@ -36,7 +45,6 @@ class OfflineRnnLM::Impl {
36 auto buf = ReadFile(mgr, config_.model); 45 auto buf = ReadFile(mgr, config_.model);
37 Init(buf.data(), buf.size()); 46 Init(buf.data(), buf.size());
38 } 47 }
39 -#endif  
40 48
41 Ort::Value Rescore(Ort::Value x, Ort::Value x_lens) { 49 Ort::Value Rescore(Ort::Value x, Ort::Value x_lens) {
42 std::array<Ort::Value, 2> inputs = {std::move(x), std::move(x_lens)}; 50 std::array<Ort::Value, 2> inputs = {std::move(x), std::move(x_lens)};
@@ -76,10 +84,9 @@ class OfflineRnnLM::Impl { @@ -76,10 +84,9 @@ class OfflineRnnLM::Impl {
76 OfflineRnnLM::OfflineRnnLM(const OfflineLMConfig &config) 84 OfflineRnnLM::OfflineRnnLM(const OfflineLMConfig &config)
77 : impl_(std::make_unique<Impl>(config)) {} 85 : impl_(std::make_unique<Impl>(config)) {}
78 86
79 -#if __ANDROID_API__ >= 9  
80 -OfflineRnnLM::OfflineRnnLM(AAssetManager *mgr, const OfflineLMConfig &config) 87 +template <typename Manager>
  88 +OfflineRnnLM::OfflineRnnLM(Manager *mgr, const OfflineLMConfig &config)
81 : impl_(std::make_unique<Impl>(mgr, config)) {} 89 : impl_(std::make_unique<Impl>(mgr, config)) {}
82 -#endif  
83 90
84 OfflineRnnLM::~OfflineRnnLM() = default; 91 OfflineRnnLM::~OfflineRnnLM() = default;
85 92
@@ -87,4 +94,14 @@ Ort::Value OfflineRnnLM::Rescore(Ort::Value x, Ort::Value x_lens) { @@ -87,4 +94,14 @@ Ort::Value OfflineRnnLM::Rescore(Ort::Value x, Ort::Value x_lens) {
87 return impl_->Rescore(std::move(x), std::move(x_lens)); 94 return impl_->Rescore(std::move(x), std::move(x_lens));
88 } 95 }
89 96
  97 +#if __ANDROID_API__ >= 9
  98 +template OfflineRnnLM::OfflineRnnLM(AAssetManager *mgr,
  99 + const OfflineLMConfig &config);
  100 +#endif
  101 +
  102 +#if __OHOS__
  103 +template OfflineRnnLM::OfflineRnnLM(NativeResourceManager *mgr,
  104 + const OfflineLMConfig &config);
  105 +#endif
  106 +
90 } // namespace sherpa_onnx 107 } // namespace sherpa_onnx
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 7
8 #include <memory> 8 #include <memory>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 #include "onnxruntime_cxx_api.h" // NOLINT 10 #include "onnxruntime_cxx_api.h" // NOLINT
16 #include "sherpa-onnx/csrc/offline-lm-config.h" 11 #include "sherpa-onnx/csrc/offline-lm-config.h"
17 #include "sherpa-onnx/csrc/offline-lm.h" 12 #include "sherpa-onnx/csrc/offline-lm.h"
@@ -24,9 +19,8 @@ class OfflineRnnLM : public OfflineLM { @@ -24,9 +19,8 @@ class OfflineRnnLM : public OfflineLM {
24 19
25 explicit OfflineRnnLM(const OfflineLMConfig &config); 20 explicit OfflineRnnLM(const OfflineLMConfig &config);
26 21
27 -#if __ANDROID_API__ >= 9  
28 - OfflineRnnLM(AAssetManager *mgr, const OfflineLMConfig &config);  
29 -#endif 22 + template <typename Manager>
  23 + OfflineRnnLM(Manager *mgr, const OfflineLMConfig &config);
30 24
31 /** Rescore a batch of sentences. 25 /** Rescore a batch of sentences.
32 * 26 *
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/macros.h" 20 #include "sherpa-onnx/csrc/macros.h"
12 #include "sherpa-onnx/csrc/onnx-utils.h" 21 #include "sherpa-onnx/csrc/onnx-utils.h"
13 #include "sherpa-onnx/csrc/session.h" 22 #include "sherpa-onnx/csrc/session.h"
@@ -26,8 +35,8 @@ class OfflineSenseVoiceModel::Impl { @@ -26,8 +35,8 @@ class OfflineSenseVoiceModel::Impl {
26 Init(buf.data(), buf.size()); 35 Init(buf.data(), buf.size());
27 } 36 }
28 37
29 -#if __ANDROID_API__ >= 9  
30 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 38 + template <typename Manager>
  39 + Impl(Manager *mgr, const OfflineModelConfig &config)
31 : config_(config), 40 : config_(config),
32 env_(ORT_LOGGING_LEVEL_ERROR), 41 env_(ORT_LOGGING_LEVEL_ERROR),
33 sess_opts_(GetSessionOptions(config)), 42 sess_opts_(GetSessionOptions(config)),
@@ -35,7 +44,6 @@ class OfflineSenseVoiceModel::Impl { @@ -35,7 +44,6 @@ class OfflineSenseVoiceModel::Impl {
35 auto buf = ReadFile(mgr, config_.sense_voice.model); 44 auto buf = ReadFile(mgr, config_.sense_voice.model);
36 Init(buf.data(), buf.size()); 45 Init(buf.data(), buf.size());
37 } 46 }
38 -#endif  
39 47
40 Ort::Value Forward(Ort::Value features, Ort::Value features_length, 48 Ort::Value Forward(Ort::Value features, Ort::Value features_length,
41 Ort::Value language, Ort::Value text_norm) { 49 Ort::Value language, Ort::Value text_norm) {
@@ -72,7 +80,11 @@ class OfflineSenseVoiceModel::Impl { @@ -72,7 +80,11 @@ class OfflineSenseVoiceModel::Impl {
72 if (config_.debug) { 80 if (config_.debug) {
73 std::ostringstream os; 81 std::ostringstream os;
74 PrintModelMetadata(os, meta_data); 82 PrintModelMetadata(os, meta_data);
  83 +#if __OHOS__
  84 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  85 +#else
75 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 86 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  87 +#endif
76 } 88 }
77 89
78 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 90 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -129,11 +141,10 @@ class OfflineSenseVoiceModel::Impl { @@ -129,11 +141,10 @@ class OfflineSenseVoiceModel::Impl {
129 OfflineSenseVoiceModel::OfflineSenseVoiceModel(const OfflineModelConfig &config) 141 OfflineSenseVoiceModel::OfflineSenseVoiceModel(const OfflineModelConfig &config)
130 : impl_(std::make_unique<Impl>(config)) {} 142 : impl_(std::make_unique<Impl>(config)) {}
131 143
132 -#if __ANDROID_API__ >= 9  
133 -OfflineSenseVoiceModel::OfflineSenseVoiceModel(AAssetManager *mgr, 144 +template <typename Manager>
  145 +OfflineSenseVoiceModel::OfflineSenseVoiceModel(Manager *mgr,
134 const OfflineModelConfig &config) 146 const OfflineModelConfig &config)
135 : impl_(std::make_unique<Impl>(mgr, config)) {} 147 : impl_(std::make_unique<Impl>(mgr, config)) {}
136 -#endif  
137 148
138 OfflineSenseVoiceModel::~OfflineSenseVoiceModel() = default; 149 OfflineSenseVoiceModel::~OfflineSenseVoiceModel() = default;
139 150
@@ -154,4 +165,14 @@ OrtAllocator *OfflineSenseVoiceModel::Allocator() const { @@ -154,4 +165,14 @@ OrtAllocator *OfflineSenseVoiceModel::Allocator() const {
154 return impl_->Allocator(); 165 return impl_->Allocator();
155 } 166 }
156 167
  168 +#if __ANDROID_API__ >= 9
  169 +template OfflineSenseVoiceModel::OfflineSenseVoiceModel(
  170 + AAssetManager *mgr, const OfflineModelConfig &config);
  171 +#endif
  172 +
  173 +#if __OHOS__
  174 +template OfflineSenseVoiceModel::OfflineSenseVoiceModel(
  175 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  176 +#endif
  177 +
157 } // namespace sherpa_onnx 178 } // namespace sherpa_onnx
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 #include "onnxruntime_cxx_api.h" // NOLINT 10 #include "onnxruntime_cxx_api.h" // NOLINT
16 #include "sherpa-onnx/csrc/offline-model-config.h" 11 #include "sherpa-onnx/csrc/offline-model-config.h"
17 #include "sherpa-onnx/csrc/offline-sense-voice-model-meta-data.h" 12 #include "sherpa-onnx/csrc/offline-sense-voice-model-meta-data.h"
@@ -22,9 +17,8 @@ class OfflineSenseVoiceModel { @@ -22,9 +17,8 @@ class OfflineSenseVoiceModel {
22 public: 17 public:
23 explicit OfflineSenseVoiceModel(const OfflineModelConfig &config); 18 explicit OfflineSenseVoiceModel(const OfflineModelConfig &config);
24 19
25 -#if __ANDROID_API__ >= 9  
26 - OfflineSenseVoiceModel(AAssetManager *mgr, const OfflineModelConfig &config);  
27 -#endif 20 + template <typename Manager>
  21 + OfflineSenseVoiceModel(Manager *mgr, const OfflineModelConfig &config);
28 22
29 ~OfflineSenseVoiceModel(); 23 ~OfflineSenseVoiceModel();
30 24
@@ -6,6 +6,15 @@ @@ -6,6 +6,15 @@
6 6
7 #include <utility> 7 #include <utility>
8 8
  9 +#if __ANDROID_API__ >= 9
  10 +#include "android/asset_manager.h"
  11 +#include "android/asset_manager_jni.h"
  12 +#endif
  13 +
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
9 #include "sherpa-onnx/csrc/macros.h" 18 #include "sherpa-onnx/csrc/macros.h"
10 #include "sherpa-onnx/csrc/onnx-utils.h" 19 #include "sherpa-onnx/csrc/onnx-utils.h"
11 #include "sherpa-onnx/csrc/session.h" 20 #include "sherpa-onnx/csrc/session.h"
@@ -25,8 +34,8 @@ class OfflineTdnnCtcModel::Impl { @@ -25,8 +34,8 @@ class OfflineTdnnCtcModel::Impl {
25 Init(buf.data(), buf.size()); 34 Init(buf.data(), buf.size());
26 } 35 }
27 36
28 -#if __ANDROID_API__ >= 9  
29 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 37 + template <typename Manager>
  38 + Impl(Manager *mgr, const OfflineModelConfig &config)
30 : config_(config), 39 : config_(config),
31 env_(ORT_LOGGING_LEVEL_ERROR), 40 env_(ORT_LOGGING_LEVEL_ERROR),
32 sess_opts_(GetSessionOptions(config)), 41 sess_opts_(GetSessionOptions(config)),
@@ -34,7 +43,6 @@ class OfflineTdnnCtcModel::Impl { @@ -34,7 +43,6 @@ class OfflineTdnnCtcModel::Impl {
34 auto buf = ReadFile(mgr, config_.tdnn.model); 43 auto buf = ReadFile(mgr, config_.tdnn.model);
35 Init(buf.data(), buf.size()); 44 Init(buf.data(), buf.size());
36 } 45 }
37 -#endif  
38 46
39 std::vector<Ort::Value> Forward(Ort::Value features) { 47 std::vector<Ort::Value> Forward(Ort::Value features) {
40 auto nnet_out = 48 auto nnet_out =
@@ -79,7 +87,11 @@ class OfflineTdnnCtcModel::Impl { @@ -79,7 +87,11 @@ class OfflineTdnnCtcModel::Impl {
79 if (config_.debug) { 87 if (config_.debug) {
80 std::ostringstream os; 88 std::ostringstream os;
81 PrintModelMetadata(os, meta_data); 89 PrintModelMetadata(os, meta_data);
  90 +#if __OHOS__
  91 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  92 +#else
82 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 93 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  94 +#endif
83 } 95 }
84 96
85 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 97 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -106,11 +118,10 @@ class OfflineTdnnCtcModel::Impl { @@ -106,11 +118,10 @@ class OfflineTdnnCtcModel::Impl {
106 OfflineTdnnCtcModel::OfflineTdnnCtcModel(const OfflineModelConfig &config) 118 OfflineTdnnCtcModel::OfflineTdnnCtcModel(const OfflineModelConfig &config)
107 : impl_(std::make_unique<Impl>(config)) {} 119 : impl_(std::make_unique<Impl>(config)) {}
108 120
109 -#if __ANDROID_API__ >= 9  
110 -OfflineTdnnCtcModel::OfflineTdnnCtcModel(AAssetManager *mgr, 121 +template <typename Manager>
  122 +OfflineTdnnCtcModel::OfflineTdnnCtcModel(Manager *mgr,
111 const OfflineModelConfig &config) 123 const OfflineModelConfig &config)
112 : impl_(std::make_unique<Impl>(mgr, config)) {} 124 : impl_(std::make_unique<Impl>(mgr, config)) {}
113 -#endif  
114 125
115 OfflineTdnnCtcModel::~OfflineTdnnCtcModel() = default; 126 OfflineTdnnCtcModel::~OfflineTdnnCtcModel() = default;
116 127
@@ -125,4 +136,14 @@ OrtAllocator *OfflineTdnnCtcModel::Allocator() const { @@ -125,4 +136,14 @@ OrtAllocator *OfflineTdnnCtcModel::Allocator() const {
125 return impl_->Allocator(); 136 return impl_->Allocator();
126 } 137 }
127 138
  139 +#if __ANDROID_API__ >= 9
  140 +template OfflineTdnnCtcModel::OfflineTdnnCtcModel(
  141 + AAssetManager *mgr, const OfflineModelConfig &config);
  142 +#endif
  143 +
  144 +#if __OHOS__
  145 +template OfflineTdnnCtcModel::OfflineTdnnCtcModel(
  146 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  147 +#endif
  148 +
128 } // namespace sherpa_onnx 149 } // namespace sherpa_onnx
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 #include "onnxruntime_cxx_api.h" // NOLINT 10 #include "onnxruntime_cxx_api.h" // NOLINT
16 #include "sherpa-onnx/csrc/offline-ctc-model.h" 11 #include "sherpa-onnx/csrc/offline-ctc-model.h"
17 #include "sherpa-onnx/csrc/offline-model-config.h" 12 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -27,9 +22,8 @@ class OfflineTdnnCtcModel : public OfflineCtcModel { @@ -27,9 +22,8 @@ class OfflineTdnnCtcModel : public OfflineCtcModel {
27 public: 22 public:
28 explicit OfflineTdnnCtcModel(const OfflineModelConfig &config); 23 explicit OfflineTdnnCtcModel(const OfflineModelConfig &config);
29 24
30 -#if __ANDROID_API__ >= 9  
31 - OfflineTdnnCtcModel(AAssetManager *mgr, const OfflineModelConfig &config);  
32 -#endif 25 + template <typename Manager>
  26 + OfflineTdnnCtcModel(Manager *mgr, const OfflineModelConfig &config);
33 27
34 ~OfflineTdnnCtcModel() override; 28 ~OfflineTdnnCtcModel() override;
35 29
@@ -4,6 +4,15 @@ @@ -4,6 +4,15 @@
4 4
5 #include "sherpa-onnx/csrc/offline-telespeech-ctc-model.h" 5 #include "sherpa-onnx/csrc/offline-telespeech-ctc-model.h"
6 6
  7 +#if __ANDROID_API__ >= 9
  8 +#include "android/asset_manager.h"
  9 +#include "android/asset_manager_jni.h"
  10 +#endif
  11 +
  12 +#if __OHOS__
  13 +#include "rawfile/raw_file_manager.h"
  14 +#endif
  15 +
7 #include "sherpa-onnx/csrc/macros.h" 16 #include "sherpa-onnx/csrc/macros.h"
8 #include "sherpa-onnx/csrc/onnx-utils.h" 17 #include "sherpa-onnx/csrc/onnx-utils.h"
9 #include "sherpa-onnx/csrc/session.h" 18 #include "sherpa-onnx/csrc/session.h"
@@ -23,8 +32,8 @@ class OfflineTeleSpeechCtcModel::Impl { @@ -23,8 +32,8 @@ class OfflineTeleSpeechCtcModel::Impl {
23 Init(buf.data(), buf.size()); 32 Init(buf.data(), buf.size());
24 } 33 }
25 34
26 -#if __ANDROID_API__ >= 9  
27 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 35 + template <typename Manager>
  36 + Impl(Manager *mgr, const OfflineModelConfig &config)
28 : config_(config), 37 : config_(config),
29 env_(ORT_LOGGING_LEVEL_ERROR), 38 env_(ORT_LOGGING_LEVEL_ERROR),
30 sess_opts_(GetSessionOptions(config)), 39 sess_opts_(GetSessionOptions(config)),
@@ -32,7 +41,6 @@ class OfflineTeleSpeechCtcModel::Impl { @@ -32,7 +41,6 @@ class OfflineTeleSpeechCtcModel::Impl {
32 auto buf = ReadFile(mgr, config_.telespeech_ctc); 41 auto buf = ReadFile(mgr, config_.telespeech_ctc);
33 Init(buf.data(), buf.size()); 42 Init(buf.data(), buf.size());
34 } 43 }
35 -#endif  
36 44
37 std::vector<Ort::Value> Forward(Ort::Value features, 45 std::vector<Ort::Value> Forward(Ort::Value features,
38 Ort::Value /*features_length*/) { 46 Ort::Value /*features_length*/) {
@@ -85,7 +93,11 @@ class OfflineTeleSpeechCtcModel::Impl { @@ -85,7 +93,11 @@ class OfflineTeleSpeechCtcModel::Impl {
85 if (config_.debug) { 93 if (config_.debug) {
86 std::ostringstream os; 94 std::ostringstream os;
87 PrintModelMetadata(os, meta_data); 95 PrintModelMetadata(os, meta_data);
  96 +#if __OHOS__
  97 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  98 +#else
88 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 99 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  100 +#endif
89 } 101 }
90 102
91 { 103 {
@@ -117,11 +129,10 @@ OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( @@ -117,11 +129,10 @@ OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel(
117 const OfflineModelConfig &config) 129 const OfflineModelConfig &config)
118 : impl_(std::make_unique<Impl>(config)) {} 130 : impl_(std::make_unique<Impl>(config)) {}
119 131
120 -#if __ANDROID_API__ >= 9 132 +template <typename Manager>
121 OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( 133 OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel(
122 - AAssetManager *mgr, const OfflineModelConfig &config) 134 + Manager *mgr, const OfflineModelConfig &config)
123 : impl_(std::make_unique<Impl>(mgr, config)) {} 135 : impl_(std::make_unique<Impl>(mgr, config)) {}
124 -#endif  
125 136
126 OfflineTeleSpeechCtcModel::~OfflineTeleSpeechCtcModel() = default; 137 OfflineTeleSpeechCtcModel::~OfflineTeleSpeechCtcModel() = default;
127 138
@@ -141,4 +152,14 @@ OrtAllocator *OfflineTeleSpeechCtcModel::Allocator() const { @@ -141,4 +152,14 @@ OrtAllocator *OfflineTeleSpeechCtcModel::Allocator() const {
141 return impl_->Allocator(); 152 return impl_->Allocator();
142 } 153 }
143 154
  155 +#if __ANDROID_API__ >= 9
  156 +template OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel(
  157 + AAssetManager *mgr, const OfflineModelConfig &config);
  158 +#endif
  159 +
  160 +#if __OHOS__
  161 +template OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel(
  162 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  163 +#endif
  164 +
144 } // namespace sherpa_onnx 165 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/offline-ctc-model.h" 12 #include "sherpa-onnx/csrc/offline-ctc-model.h"
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -31,10 +26,8 @@ class OfflineTeleSpeechCtcModel : public OfflineCtcModel { @@ -31,10 +26,8 @@ class OfflineTeleSpeechCtcModel : public OfflineCtcModel {
31 public: 26 public:
32 explicit OfflineTeleSpeechCtcModel(const OfflineModelConfig &config); 27 explicit OfflineTeleSpeechCtcModel(const OfflineModelConfig &config);
33 28
34 -#if __ANDROID_API__ >= 9  
35 - OfflineTeleSpeechCtcModel(AAssetManager *mgr,  
36 - const OfflineModelConfig &config);  
37 -#endif 29 + template <typename Manager>
  30 + OfflineTeleSpeechCtcModel(Manager *mgr, const OfflineModelConfig &config);
38 31
39 ~OfflineTeleSpeechCtcModel() override; 32 ~OfflineTeleSpeechCtcModel() override;
40 33
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/macros.h" 20 #include "sherpa-onnx/csrc/macros.h"
12 #include "sherpa-onnx/csrc/offline-transducer-decoder.h" 21 #include "sherpa-onnx/csrc/offline-transducer-decoder.h"
13 #include "sherpa-onnx/csrc/onnx-utils.h" 22 #include "sherpa-onnx/csrc/onnx-utils.h"
@@ -38,8 +47,8 @@ class OfflineTransducerModel::Impl { @@ -38,8 +47,8 @@ class OfflineTransducerModel::Impl {
38 } 47 }
39 } 48 }
40 49
41 -#if __ANDROID_API__ >= 9  
42 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 50 + template <typename Manager>
  51 + Impl(Manager *mgr, const OfflineModelConfig &config)
43 : config_(config), 52 : config_(config),
44 env_(ORT_LOGGING_LEVEL_ERROR), 53 env_(ORT_LOGGING_LEVEL_ERROR),
45 sess_opts_(GetSessionOptions(config)), 54 sess_opts_(GetSessionOptions(config)),
@@ -59,7 +68,6 @@ class OfflineTransducerModel::Impl { @@ -59,7 +68,6 @@ class OfflineTransducerModel::Impl {
59 InitJoiner(buf.data(), buf.size()); 68 InitJoiner(buf.data(), buf.size());
60 } 69 }
61 } 70 }
62 -#endif  
63 71
64 std::pair<Ort::Value, Ort::Value> RunEncoder(Ort::Value features, 72 std::pair<Ort::Value, Ort::Value> RunEncoder(Ort::Value features,
65 Ort::Value features_length) { 73 Ort::Value features_length) {
@@ -161,7 +169,11 @@ class OfflineTransducerModel::Impl { @@ -161,7 +169,11 @@ class OfflineTransducerModel::Impl {
161 std::ostringstream os; 169 std::ostringstream os;
162 os << "---encoder---\n"; 170 os << "---encoder---\n";
163 PrintModelMetadata(os, meta_data); 171 PrintModelMetadata(os, meta_data);
  172 +#if __OHOS__
  173 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  174 +#else
164 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 175 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  176 +#endif
165 } 177 }
166 } 178 }
167 179
@@ -244,11 +256,10 @@ class OfflineTransducerModel::Impl { @@ -244,11 +256,10 @@ class OfflineTransducerModel::Impl {
244 OfflineTransducerModel::OfflineTransducerModel(const OfflineModelConfig &config) 256 OfflineTransducerModel::OfflineTransducerModel(const OfflineModelConfig &config)
245 : impl_(std::make_unique<Impl>(config)) {} 257 : impl_(std::make_unique<Impl>(config)) {}
246 258
247 -#if __ANDROID_API__ >= 9  
248 -OfflineTransducerModel::OfflineTransducerModel(AAssetManager *mgr, 259 +template <typename Manager>
  260 +OfflineTransducerModel::OfflineTransducerModel(Manager *mgr,
249 const OfflineModelConfig &config) 261 const OfflineModelConfig &config)
250 : impl_(std::make_unique<Impl>(mgr, config)) {} 262 : impl_(std::make_unique<Impl>(mgr, config)) {}
251 -#endif  
252 263
253 OfflineTransducerModel::~OfflineTransducerModel() = default; 264 OfflineTransducerModel::~OfflineTransducerModel() = default;
254 265
@@ -291,4 +302,14 @@ Ort::Value OfflineTransducerModel::BuildDecoderInput( @@ -291,4 +302,14 @@ Ort::Value OfflineTransducerModel::BuildDecoderInput(
291 return impl_->BuildDecoderInput(results, end_index); 302 return impl_->BuildDecoderInput(results, end_index);
292 } 303 }
293 304
  305 +#if __ANDROID_API__ >= 9
  306 +template OfflineTransducerModel::OfflineTransducerModel(
  307 + AAssetManager *mgr, const OfflineModelConfig &config);
  308 +#endif
  309 +
  310 +#if __OHOS__
  311 +template OfflineTransducerModel::OfflineTransducerModel(
  312 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  313 +#endif
  314 +
294 } // namespace sherpa_onnx 315 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/hypothesis.h" 12 #include "sherpa-onnx/csrc/hypothesis.h"
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -25,9 +20,8 @@ class OfflineTransducerModel { @@ -25,9 +20,8 @@ class OfflineTransducerModel {
25 public: 20 public:
26 explicit OfflineTransducerModel(const OfflineModelConfig &config); 21 explicit OfflineTransducerModel(const OfflineModelConfig &config);
27 22
28 -#if __ANDROID_API__ >= 9  
29 - OfflineTransducerModel(AAssetManager *mgr, const OfflineModelConfig &config);  
30 -#endif 23 + template <typename Manager>
  24 + OfflineTransducerModel(Manager *mgr, const OfflineModelConfig &config);
31 25
32 ~OfflineTransducerModel(); 26 ~OfflineTransducerModel();
33 27
@@ -9,6 +9,15 @@ @@ -9,6 +9,15 @@
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
  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 +
12 #include "sherpa-onnx/csrc/macros.h" 21 #include "sherpa-onnx/csrc/macros.h"
13 #include "sherpa-onnx/csrc/offline-transducer-decoder.h" 22 #include "sherpa-onnx/csrc/offline-transducer-decoder.h"
14 #include "sherpa-onnx/csrc/onnx-utils.h" 23 #include "sherpa-onnx/csrc/onnx-utils.h"
@@ -40,8 +49,8 @@ class OfflineTransducerNeMoModel::Impl { @@ -40,8 +49,8 @@ class OfflineTransducerNeMoModel::Impl {
40 } 49 }
41 } 50 }
42 51
43 -#if __ANDROID_API__ >= 9  
44 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 52 + template <typename Manager>
  53 + Impl(Manager *mgr, const OfflineModelConfig &config)
45 : config_(config), 54 : config_(config),
46 env_(ORT_LOGGING_LEVEL_ERROR), 55 env_(ORT_LOGGING_LEVEL_ERROR),
47 sess_opts_(GetSessionOptions(config)), 56 sess_opts_(GetSessionOptions(config)),
@@ -61,7 +70,6 @@ class OfflineTransducerNeMoModel::Impl { @@ -61,7 +70,6 @@ class OfflineTransducerNeMoModel::Impl {
61 InitJoiner(buf.data(), buf.size()); 70 InitJoiner(buf.data(), buf.size());
62 } 71 }
63 } 72 }
64 -#endif  
65 73
66 std::vector<Ort::Value> RunEncoder(Ort::Value features, 74 std::vector<Ort::Value> RunEncoder(Ort::Value features,
67 Ort::Value features_length) { 75 Ort::Value features_length) {
@@ -172,7 +180,11 @@ class OfflineTransducerNeMoModel::Impl { @@ -172,7 +180,11 @@ class OfflineTransducerNeMoModel::Impl {
172 std::ostringstream os; 180 std::ostringstream os;
173 os << "---encoder---\n"; 181 os << "---encoder---\n";
174 PrintModelMetadata(os, meta_data); 182 PrintModelMetadata(os, meta_data);
  183 +#if __OHOS__
  184 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  185 +#else
175 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 186 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  187 +#endif
176 } 188 }
177 189
178 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 190 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -256,11 +268,10 @@ OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( @@ -256,11 +268,10 @@ OfflineTransducerNeMoModel::OfflineTransducerNeMoModel(
256 const OfflineModelConfig &config) 268 const OfflineModelConfig &config)
257 : impl_(std::make_unique<Impl>(config)) {} 269 : impl_(std::make_unique<Impl>(config)) {}
258 270
259 -#if __ANDROID_API__ >= 9 271 +template <typename Manager>
260 OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( 272 OfflineTransducerNeMoModel::OfflineTransducerNeMoModel(
261 - AAssetManager *mgr, const OfflineModelConfig &config) 273 + Manager *mgr, const OfflineModelConfig &config)
262 : impl_(std::make_unique<Impl>(mgr, config)) {} 274 : impl_(std::make_unique<Impl>(mgr, config)) {}
263 -#endif  
264 275
265 OfflineTransducerNeMoModel::~OfflineTransducerNeMoModel() = default; 276 OfflineTransducerNeMoModel::~OfflineTransducerNeMoModel() = default;
266 277
@@ -305,4 +316,14 @@ std::string OfflineTransducerNeMoModel::FeatureNormalizationMethod() const { @@ -305,4 +316,14 @@ std::string OfflineTransducerNeMoModel::FeatureNormalizationMethod() const {
305 316
306 bool OfflineTransducerNeMoModel::IsGigaAM() const { return impl_->IsGigaAM(); } 317 bool OfflineTransducerNeMoModel::IsGigaAM() const { return impl_->IsGigaAM(); }
307 318
  319 +#if __ANDROID_API__ >= 9
  320 +template OfflineTransducerNeMoModel::OfflineTransducerNeMoModel(
  321 + AAssetManager *mgr, const OfflineModelConfig &config);
  322 +#endif
  323 +
  324 +#if __OHOS__
  325 +template OfflineTransducerNeMoModel::OfflineTransducerNeMoModel(
  326 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  327 +#endif
  328 +
308 } // namespace sherpa_onnx 329 } // namespace sherpa_onnx
@@ -9,11 +9,6 @@ @@ -9,11 +9,6 @@
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 -#if __ANDROID_API__ >= 9  
13 -#include "android/asset_manager.h"  
14 -#include "android/asset_manager_jni.h"  
15 -#endif  
16 -  
17 #include "onnxruntime_cxx_api.h" // NOLINT 12 #include "onnxruntime_cxx_api.h" // NOLINT
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
19 14
@@ -26,10 +21,8 @@ class OfflineTransducerNeMoModel { @@ -26,10 +21,8 @@ class OfflineTransducerNeMoModel {
26 public: 21 public:
27 explicit OfflineTransducerNeMoModel(const OfflineModelConfig &config); 22 explicit OfflineTransducerNeMoModel(const OfflineModelConfig &config);
28 23
29 -#if __ANDROID_API__ >= 9  
30 - OfflineTransducerNeMoModel(AAssetManager *mgr,  
31 - const OfflineModelConfig &config);  
32 -#endif 24 + template <typename Manager>
  25 + OfflineTransducerNeMoModel(Manager *mgr, const OfflineModelConfig &config);
33 26
34 ~OfflineTransducerNeMoModel(); 27 ~OfflineTransducerNeMoModel();
35 28
@@ -4,6 +4,15 @@ @@ -4,6 +4,15 @@
4 4
5 #include "sherpa-onnx/csrc/offline-wenet-ctc-model.h" 5 #include "sherpa-onnx/csrc/offline-wenet-ctc-model.h"
6 6
  7 +#if __ANDROID_API__ >= 9
  8 +#include "android/asset_manager.h"
  9 +#include "android/asset_manager_jni.h"
  10 +#endif
  11 +
  12 +#if __OHOS__
  13 +#include "rawfile/raw_file_manager.h"
  14 +#endif
  15 +
7 #include "sherpa-onnx/csrc/macros.h" 16 #include "sherpa-onnx/csrc/macros.h"
8 #include "sherpa-onnx/csrc/onnx-utils.h" 17 #include "sherpa-onnx/csrc/onnx-utils.h"
9 #include "sherpa-onnx/csrc/session.h" 18 #include "sherpa-onnx/csrc/session.h"
@@ -23,8 +32,8 @@ class OfflineWenetCtcModel::Impl { @@ -23,8 +32,8 @@ class OfflineWenetCtcModel::Impl {
23 Init(buf.data(), buf.size()); 32 Init(buf.data(), buf.size());
24 } 33 }
25 34
26 -#if __ANDROID_API__ >= 9  
27 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 35 + template <typename Manager>
  36 + Impl(Manager *mgr, const OfflineModelConfig &config)
28 : config_(config), 37 : config_(config),
29 env_(ORT_LOGGING_LEVEL_ERROR), 38 env_(ORT_LOGGING_LEVEL_ERROR),
30 sess_opts_(GetSessionOptions(config)), 39 sess_opts_(GetSessionOptions(config)),
@@ -32,7 +41,6 @@ class OfflineWenetCtcModel::Impl { @@ -32,7 +41,6 @@ class OfflineWenetCtcModel::Impl {
32 auto buf = ReadFile(mgr, config_.wenet_ctc.model); 41 auto buf = ReadFile(mgr, config_.wenet_ctc.model);
33 Init(buf.data(), buf.size()); 42 Init(buf.data(), buf.size());
34 } 43 }
35 -#endif  
36 44
37 std::vector<Ort::Value> Forward(Ort::Value features, 45 std::vector<Ort::Value> Forward(Ort::Value features,
38 Ort::Value features_length) { 46 Ort::Value features_length) {
@@ -63,7 +71,11 @@ class OfflineWenetCtcModel::Impl { @@ -63,7 +71,11 @@ class OfflineWenetCtcModel::Impl {
63 if (config_.debug) { 71 if (config_.debug) {
64 std::ostringstream os; 72 std::ostringstream os;
65 PrintModelMetadata(os, meta_data); 73 PrintModelMetadata(os, meta_data);
  74 +#if __OHOS__
  75 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  76 +#else
66 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 77 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  78 +#endif
67 } 79 }
68 80
69 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 81 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -92,11 +104,10 @@ class OfflineWenetCtcModel::Impl { @@ -92,11 +104,10 @@ class OfflineWenetCtcModel::Impl {
92 OfflineWenetCtcModel::OfflineWenetCtcModel(const OfflineModelConfig &config) 104 OfflineWenetCtcModel::OfflineWenetCtcModel(const OfflineModelConfig &config)
93 : impl_(std::make_unique<Impl>(config)) {} 105 : impl_(std::make_unique<Impl>(config)) {}
94 106
95 -#if __ANDROID_API__ >= 9  
96 -OfflineWenetCtcModel::OfflineWenetCtcModel(AAssetManager *mgr, 107 +template <typename Manager>
  108 +OfflineWenetCtcModel::OfflineWenetCtcModel(Manager *mgr,
97 const OfflineModelConfig &config) 109 const OfflineModelConfig &config)
98 : impl_(std::make_unique<Impl>(mgr, config)) {} 110 : impl_(std::make_unique<Impl>(mgr, config)) {}
99 -#endif  
100 111
101 OfflineWenetCtcModel::~OfflineWenetCtcModel() = default; 112 OfflineWenetCtcModel::~OfflineWenetCtcModel() = default;
102 113
@@ -115,4 +126,14 @@ OrtAllocator *OfflineWenetCtcModel::Allocator() const { @@ -115,4 +126,14 @@ OrtAllocator *OfflineWenetCtcModel::Allocator() const {
115 return impl_->Allocator(); 126 return impl_->Allocator();
116 } 127 }
117 128
  129 +#if __ANDROID_API__ >= 9
  130 +template OfflineWenetCtcModel::OfflineWenetCtcModel(
  131 + AAssetManager *mgr, const OfflineModelConfig &config);
  132 +#endif
  133 +
  134 +#if __OHOS__
  135 +template OfflineWenetCtcModel::OfflineWenetCtcModel(
  136 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  137 +#endif
  138 +
118 } // namespace sherpa_onnx 139 } // namespace sherpa_onnx
@@ -8,11 +8,6 @@ @@ -8,11 +8,6 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 -#if __ANDROID_API__ >= 9  
12 -#include "android/asset_manager.h"  
13 -#include "android/asset_manager_jni.h"  
14 -#endif  
15 -  
16 #include "onnxruntime_cxx_api.h" // NOLINT 11 #include "onnxruntime_cxx_api.h" // NOLINT
17 #include "sherpa-onnx/csrc/offline-ctc-model.h" 12 #include "sherpa-onnx/csrc/offline-ctc-model.h"
18 #include "sherpa-onnx/csrc/offline-model-config.h" 13 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -31,9 +26,8 @@ class OfflineWenetCtcModel : public OfflineCtcModel { @@ -31,9 +26,8 @@ class OfflineWenetCtcModel : public OfflineCtcModel {
31 public: 26 public:
32 explicit OfflineWenetCtcModel(const OfflineModelConfig &config); 27 explicit OfflineWenetCtcModel(const OfflineModelConfig &config);
33 28
34 -#if __ANDROID_API__ >= 9  
35 - OfflineWenetCtcModel(AAssetManager *mgr, const OfflineModelConfig &config);  
36 -#endif 29 + template <typename Manager>
  30 + OfflineWenetCtcModel(Manager *mgr, const OfflineModelConfig &config);
37 31
38 ~OfflineWenetCtcModel() override; 32 ~OfflineWenetCtcModel() override;
39 33
@@ -11,6 +11,15 @@ @@ -11,6 +11,15 @@
11 #include <unordered_map> 11 #include <unordered_map>
12 #include <utility> 12 #include <utility>
13 13
  14 +#if __ANDROID_API__ >= 9
  15 +#include "android/asset_manager.h"
  16 +#include "android/asset_manager_jni.h"
  17 +#endif
  18 +
  19 +#if __OHOS__
  20 +#include "rawfile/raw_file_manager.h"
  21 +#endif
  22 +
14 #include "sherpa-onnx/csrc/macros.h" 23 #include "sherpa-onnx/csrc/macros.h"
15 #include "sherpa-onnx/csrc/onnx-utils.h" 24 #include "sherpa-onnx/csrc/onnx-utils.h"
16 #include "sherpa-onnx/csrc/session.h" 25 #include "sherpa-onnx/csrc/session.h"
@@ -52,8 +61,8 @@ class OfflineWhisperModel::Impl { @@ -52,8 +61,8 @@ class OfflineWhisperModel::Impl {
52 } 61 }
53 } 62 }
54 63
55 -#if __ANDROID_API__ >= 9  
56 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 64 + template <typename Manager>
  65 + Impl(Manager *mgr, const OfflineModelConfig &config)
57 : config_(config), 66 : config_(config),
58 env_(ORT_LOGGING_LEVEL_ERROR), 67 env_(ORT_LOGGING_LEVEL_ERROR),
59 sess_opts_(GetSessionOptions(config)), 68 sess_opts_(GetSessionOptions(config)),
@@ -69,7 +78,8 @@ class OfflineWhisperModel::Impl { @@ -69,7 +78,8 @@ class OfflineWhisperModel::Impl {
69 } 78 }
70 } 79 }
71 80
72 - Impl(AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config) 81 + template <typename Manager>
  82 + Impl(Manager *mgr, const SpokenLanguageIdentificationConfig &config)
73 : lid_config_(config), 83 : lid_config_(config),
74 env_(ORT_LOGGING_LEVEL_ERROR), 84 env_(ORT_LOGGING_LEVEL_ERROR),
75 sess_opts_(GetSessionOptions(config)), 85 sess_opts_(GetSessionOptions(config)),
@@ -84,7 +94,6 @@ class OfflineWhisperModel::Impl { @@ -84,7 +94,6 @@ class OfflineWhisperModel::Impl {
84 InitDecoder(buf.data(), buf.size()); 94 InitDecoder(buf.data(), buf.size());
85 } 95 }
86 } 96 }
87 -#endif  
88 97
89 std::pair<Ort::Value, Ort::Value> ForwardEncoder(Ort::Value features) { 98 std::pair<Ort::Value, Ort::Value> ForwardEncoder(Ort::Value features) {
90 auto encoder_out = encoder_sess_->Run( 99 auto encoder_out = encoder_sess_->Run(
@@ -237,7 +246,11 @@ class OfflineWhisperModel::Impl { @@ -237,7 +246,11 @@ class OfflineWhisperModel::Impl {
237 std::ostringstream os; 246 std::ostringstream os;
238 os << "---encoder---\n"; 247 os << "---encoder---\n";
239 PrintModelMetadata(os, meta_data); 248 PrintModelMetadata(os, meta_data);
  249 +#if __OHOS__
  250 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  251 +#else
240 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 252 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  253 +#endif
241 } 254 }
242 255
243 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below 256 Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -338,17 +351,16 @@ OfflineWhisperModel::OfflineWhisperModel( @@ -338,17 +351,16 @@ OfflineWhisperModel::OfflineWhisperModel(
338 const SpokenLanguageIdentificationConfig &config) 351 const SpokenLanguageIdentificationConfig &config)
339 : impl_(std::make_unique<Impl>(config)) {} 352 : impl_(std::make_unique<Impl>(config)) {}
340 353
341 -#if __ANDROID_API__ >= 9  
342 -OfflineWhisperModel::OfflineWhisperModel(AAssetManager *mgr, 354 +template <typename Manager>
  355 +OfflineWhisperModel::OfflineWhisperModel(Manager *mgr,
343 const OfflineModelConfig &config) 356 const OfflineModelConfig &config)
344 : impl_(std::make_unique<Impl>(mgr, config)) {} 357 : impl_(std::make_unique<Impl>(mgr, config)) {}
345 358
  359 +template <typename Manager>
346 OfflineWhisperModel::OfflineWhisperModel( 360 OfflineWhisperModel::OfflineWhisperModel(
347 - AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config) 361 + Manager *mgr, const SpokenLanguageIdentificationConfig &config)
348 : impl_(std::make_unique<Impl>(mgr, config)) {} 362 : impl_(std::make_unique<Impl>(mgr, config)) {}
349 363
350 -#endif  
351 -  
352 OfflineWhisperModel::~OfflineWhisperModel() = default; 364 OfflineWhisperModel::~OfflineWhisperModel() = default;
353 365
354 std::pair<Ort::Value, Ort::Value> OfflineWhisperModel::ForwardEncoder( 366 std::pair<Ort::Value, Ort::Value> OfflineWhisperModel::ForwardEncoder(
@@ -453,4 +465,21 @@ void OfflineWhisperModel::NormalizeFeatures(float *features, int32_t num_frames, @@ -453,4 +465,21 @@ void OfflineWhisperModel::NormalizeFeatures(float *features, int32_t num_frames,
453 } 465 }
454 } 466 }
455 467
  468 +#if __ANDROID_API__ >= 9
  469 +template OfflineWhisperModel::OfflineWhisperModel(
  470 + AAssetManager *mgr, const OfflineModelConfig &config);
  471 +
  472 +template OfflineWhisperModel::OfflineWhisperModel(
  473 + AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config);
  474 +#endif
  475 +
  476 +#if __OHOS__
  477 +template OfflineWhisperModel::OfflineWhisperModel(
  478 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  479 +
  480 +template OfflineWhisperModel::OfflineWhisperModel(
  481 + NativeResourceManager *mgr,
  482 + const SpokenLanguageIdentificationConfig &config);
  483 +#endif
  484 +
456 } // namespace sherpa_onnx 485 } // namespace sherpa_onnx
@@ -11,11 +11,6 @@ @@ -11,11 +11,6 @@
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 -#if __ANDROID_API__ >= 9  
15 -#include "android/asset_manager.h"  
16 -#include "android/asset_manager_jni.h"  
17 -#endif  
18 -  
19 #include "onnxruntime_cxx_api.h" // NOLINT 14 #include "onnxruntime_cxx_api.h" // NOLINT
20 #include "sherpa-onnx/csrc/offline-model-config.h" 15 #include "sherpa-onnx/csrc/offline-model-config.h"
21 #include "sherpa-onnx/csrc/spoken-language-identification.h" 16 #include "sherpa-onnx/csrc/spoken-language-identification.h"
@@ -29,11 +24,12 @@ class OfflineWhisperModel { @@ -29,11 +24,12 @@ class OfflineWhisperModel {
29 explicit OfflineWhisperModel( 24 explicit OfflineWhisperModel(
30 const SpokenLanguageIdentificationConfig &config); 25 const SpokenLanguageIdentificationConfig &config);
31 26
32 -#if __ANDROID_API__ >= 9  
33 - OfflineWhisperModel(AAssetManager *mgr, const OfflineModelConfig &config);  
34 - OfflineWhisperModel(AAssetManager *mgr, 27 + template <typename Manager>
  28 + OfflineWhisperModel(Manager *mgr, const OfflineModelConfig &config);
  29 +
  30 + template <typename Manager>
  31 + OfflineWhisperModel(Manager *mgr,
35 const SpokenLanguageIdentificationConfig &config); 32 const SpokenLanguageIdentificationConfig &config);
36 -#endif  
37 33
38 ~OfflineWhisperModel(); 34 ~OfflineWhisperModel();
39 35
@@ -6,6 +6,15 @@ @@ -6,6 +6,15 @@
6 6
7 #include <string> 7 #include <string>
8 8
  9 +#if __ANDROID_API__ >= 9
  10 +#include "android/asset_manager.h"
  11 +#include "android/asset_manager_jni.h"
  12 +#endif
  13 +
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
9 #include "sherpa-onnx/csrc/macros.h" 18 #include "sherpa-onnx/csrc/macros.h"
10 #include "sherpa-onnx/csrc/onnx-utils.h" 19 #include "sherpa-onnx/csrc/onnx-utils.h"
11 #include "sherpa-onnx/csrc/session.h" 20 #include "sherpa-onnx/csrc/session.h"
@@ -25,8 +34,8 @@ class OfflineZipformerCtcModel::Impl { @@ -25,8 +34,8 @@ class OfflineZipformerCtcModel::Impl {
25 Init(buf.data(), buf.size()); 34 Init(buf.data(), buf.size());
26 } 35 }
27 36
28 -#if __ANDROID_API__ >= 9  
29 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) 37 + template <typename Manager>
  38 + Impl(Manager *mgr, const OfflineModelConfig &config)
30 : config_(config), 39 : config_(config),
31 env_(ORT_LOGGING_LEVEL_ERROR), 40 env_(ORT_LOGGING_LEVEL_ERROR),
32 sess_opts_(GetSessionOptions(config)), 41 sess_opts_(GetSessionOptions(config)),
@@ -34,7 +43,6 @@ class OfflineZipformerCtcModel::Impl { @@ -34,7 +43,6 @@ class OfflineZipformerCtcModel::Impl {
34 auto buf = ReadFile(mgr, config_.zipformer_ctc.model); 43 auto buf = ReadFile(mgr, config_.zipformer_ctc.model);
35 Init(buf.data(), buf.size()); 44 Init(buf.data(), buf.size());
36 } 45 }
37 -#endif  
38 46
39 std::vector<Ort::Value> Forward(Ort::Value features, 47 std::vector<Ort::Value> Forward(Ort::Value features,
40 Ort::Value features_length) { 48 Ort::Value features_length) {
@@ -64,7 +72,11 @@ class OfflineZipformerCtcModel::Impl { @@ -64,7 +72,11 @@ class OfflineZipformerCtcModel::Impl {
64 if (config_.debug) { 72 if (config_.debug) {
65 std::ostringstream os; 73 std::ostringstream os;
66 PrintModelMetadata(os, meta_data); 74 PrintModelMetadata(os, meta_data);
  75 +#if __OHOS__
  76 + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
  77 +#else
67 SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); 78 SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
  79 +#endif
68 } 80 }
69 81
70 // get vocab size from the output[0].shape, which is (N, T, vocab_size) 82 // get vocab size from the output[0].shape, which is (N, T, vocab_size)
@@ -93,11 +105,10 @@ OfflineZipformerCtcModel::OfflineZipformerCtcModel( @@ -93,11 +105,10 @@ OfflineZipformerCtcModel::OfflineZipformerCtcModel(
93 const OfflineModelConfig &config) 105 const OfflineModelConfig &config)
94 : impl_(std::make_unique<Impl>(config)) {} 106 : impl_(std::make_unique<Impl>(config)) {}
95 107
96 -#if __ANDROID_API__ >= 9 108 +template <typename Manager>
97 OfflineZipformerCtcModel::OfflineZipformerCtcModel( 109 OfflineZipformerCtcModel::OfflineZipformerCtcModel(
98 - AAssetManager *mgr, const OfflineModelConfig &config) 110 + Manager *mgr, const OfflineModelConfig &config)
99 : impl_(std::make_unique<Impl>(mgr, config)) {} 111 : impl_(std::make_unique<Impl>(mgr, config)) {}
100 -#endif  
101 112
102 OfflineZipformerCtcModel::~OfflineZipformerCtcModel() = default; 113 OfflineZipformerCtcModel::~OfflineZipformerCtcModel() = default;
103 114
@@ -118,4 +129,14 @@ int32_t OfflineZipformerCtcModel::SubsamplingFactor() const { @@ -118,4 +129,14 @@ int32_t OfflineZipformerCtcModel::SubsamplingFactor() const {
118 return impl_->SubsamplingFactor(); 129 return impl_->SubsamplingFactor();
119 } 130 }
120 131
  132 +#if __ANDROID_API__ >= 9
  133 +template OfflineZipformerCtcModel::OfflineZipformerCtcModel(
  134 + AAssetManager *mgr, const OfflineModelConfig &config);
  135 +#endif
  136 +
  137 +#if __OHOS__
  138 +template OfflineZipformerCtcModel::OfflineZipformerCtcModel(
  139 + NativeResourceManager *mgr, const OfflineModelConfig &config);
  140 +#endif
  141 +
121 } // namespace sherpa_onnx 142 } // namespace sherpa_onnx
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 #include "onnxruntime_cxx_api.h" // NOLINT 10 #include "onnxruntime_cxx_api.h" // NOLINT
16 #include "sherpa-onnx/csrc/offline-ctc-model.h" 11 #include "sherpa-onnx/csrc/offline-ctc-model.h"
17 #include "sherpa-onnx/csrc/offline-model-config.h" 12 #include "sherpa-onnx/csrc/offline-model-config.h"
@@ -28,10 +23,8 @@ class OfflineZipformerCtcModel : public OfflineCtcModel { @@ -28,10 +23,8 @@ class OfflineZipformerCtcModel : public OfflineCtcModel {
28 public: 23 public:
29 explicit OfflineZipformerCtcModel(const OfflineModelConfig &config); 24 explicit OfflineZipformerCtcModel(const OfflineModelConfig &config);
30 25
31 -#if __ANDROID_API__ >= 9  
32 - OfflineZipformerCtcModel(AAssetManager *mgr,  
33 - const OfflineModelConfig &config);  
34 -#endif 26 + template <typename Manager>
  27 + OfflineZipformerCtcModel(Manager *mgr, const OfflineModelConfig &config);
35 28
36 ~OfflineZipformerCtcModel() override; 29 ~OfflineZipformerCtcModel() override;
37 30
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/macros.h" 20 #include "sherpa-onnx/csrc/macros.h"
12 #include "sherpa-onnx/csrc/onnx-utils.h" 21 #include "sherpa-onnx/csrc/onnx-utils.h"
13 #include "sherpa-onnx/csrc/session.h" 22 #include "sherpa-onnx/csrc/session.h"
@@ -37,7 +46,6 @@ class SileroVadModel::Impl { @@ -37,7 +46,6 @@ class SileroVadModel::Impl {
37 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; 46 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration;
38 } 47 }
39 48
40 -#if __ANDROID_API__ >= 9 || defined(__OHOS__)  
41 template <typename Manager> 49 template <typename Manager>
42 Impl(Manager *mgr, const VadModelConfig &config) 50 Impl(Manager *mgr, const VadModelConfig &config)
43 : config_(config), 51 : config_(config),
@@ -59,7 +67,6 @@ class SileroVadModel::Impl { @@ -59,7 +67,6 @@ class SileroVadModel::Impl {
59 67
60 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; 68 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration;
61 } 69 }
62 -#endif  
63 70
64 void Reset() { 71 void Reset() {
65 if (is_v5_) { 72 if (is_v5_) {
@@ -433,16 +440,9 @@ class SileroVadModel::Impl { @@ -433,16 +440,9 @@ class SileroVadModel::Impl {
433 SileroVadModel::SileroVadModel(const VadModelConfig &config) 440 SileroVadModel::SileroVadModel(const VadModelConfig &config)
434 : impl_(std::make_unique<Impl>(config)) {} 441 : impl_(std::make_unique<Impl>(config)) {}
435 442
436 -#if __ANDROID_API__ >= 9  
437 -SileroVadModel::SileroVadModel(AAssetManager *mgr, const VadModelConfig &config) 443 +template <typename Manager>
  444 +SileroVadModel::SileroVadModel(Manager *mgr, const VadModelConfig &config)
438 : impl_(std::make_unique<Impl>(mgr, config)) {} 445 : impl_(std::make_unique<Impl>(mgr, config)) {}
439 -#endif  
440 -  
441 -#if __OHOS__  
442 -SileroVadModel::SileroVadModel(NativeResourceManager *mgr,  
443 - const VadModelConfig &config)  
444 - : impl_(std::make_unique<Impl>(mgr, config)) {}  
445 -#endif  
446 446
447 SileroVadModel::~SileroVadModel() = default; 447 SileroVadModel::~SileroVadModel() = default;
448 448
@@ -472,4 +472,14 @@ void SileroVadModel::SetThreshold(float threshold) { @@ -472,4 +472,14 @@ void SileroVadModel::SetThreshold(float threshold) {
472 impl_->SetThreshold(threshold); 472 impl_->SetThreshold(threshold);
473 } 473 }
474 474
  475 +#if __ANDROID_API__ >= 9
  476 +template SileroVadModel::SileroVadModel(AAssetManager *mgr,
  477 + const VadModelConfig &config);
  478 +#endif
  479 +
  480 +#if __OHOS__
  481 +template SileroVadModel::SileroVadModel(NativeResourceManager *mgr,
  482 + const VadModelConfig &config);
  483 +#endif
  484 +
475 } // namespace sherpa_onnx 485 } // namespace sherpa_onnx
@@ -6,15 +6,6 @@ @@ -6,15 +6,6 @@
6 6
7 #include <memory> 7 #include <memory>
8 8
9 -#if __ANDROID_API__ >= 9  
10 -#include "android/asset_manager.h"  
11 -#include "android/asset_manager_jni.h"  
12 -#endif  
13 -  
14 -#if __OHOS__  
15 -#include "rawfile/raw_file_manager.h"  
16 -#endif  
17 -  
18 #include "sherpa-onnx/csrc/vad-model.h" 9 #include "sherpa-onnx/csrc/vad-model.h"
19 10
20 namespace sherpa_onnx { 11 namespace sherpa_onnx {
@@ -23,13 +14,8 @@ class SileroVadModel : public VadModel { @@ -23,13 +14,8 @@ class SileroVadModel : public VadModel {
23 public: 14 public:
24 explicit SileroVadModel(const VadModelConfig &config); 15 explicit SileroVadModel(const VadModelConfig &config);
25 16
26 -#if __ANDROID_API__ >= 9  
27 - SileroVadModel(AAssetManager *mgr, const VadModelConfig &config);  
28 -#endif  
29 -  
30 -#if __OHOS__  
31 - SileroVadModel(NativeResourceManager *mgr, const VadModelConfig &config);  
32 -#endif 17 + template <typename Manager>
  18 + SileroVadModel(Manager *mgr, const VadModelConfig &config);
33 19
34 ~SileroVadModel() override; 20 ~SileroVadModel() override;
35 21
@@ -15,6 +15,10 @@ @@ -15,6 +15,10 @@
15 15
16 #include "android/asset_manager.h" 16 #include "android/asset_manager.h"
17 #include "android/asset_manager_jni.h" 17 #include "android/asset_manager_jni.h"
  18 +#elif __OHOS__
  19 +#include <strstream>
  20 +
  21 +#include "rawfile/raw_file_manager.h"
18 #endif 22 #endif
19 23
20 #include "sherpa-onnx/csrc/base64-decode.h" 24 #include "sherpa-onnx/csrc/base64-decode.h"
@@ -99,14 +103,13 @@ SymbolTable::SymbolTable(const std::string &filename, bool is_file) { @@ -99,14 +103,13 @@ SymbolTable::SymbolTable(const std::string &filename, bool is_file) {
99 } 103 }
100 } 104 }
101 105
102 -#if __ANDROID_API__ >= 9  
103 -SymbolTable::SymbolTable(AAssetManager *mgr, const std::string &filename) { 106 +template <typename Manager>
  107 +SymbolTable::SymbolTable(Manager *mgr, const std::string &filename) {
104 auto buf = ReadFile(mgr, filename); 108 auto buf = ReadFile(mgr, filename);
105 109
106 std::istrstream is(buf.data(), buf.size()); 110 std::istrstream is(buf.data(), buf.size());
107 Init(is); 111 Init(is);
108 } 112 }
109 -#endif  
110 113
111 void SymbolTable::Init(std::istream &is) { sym2id_ = ReadTokens(is, &id2sym_); } 114 void SymbolTable::Init(std::istream &is) { sym2id_ = ReadTokens(is, &id2sym_); }
112 115
@@ -169,4 +172,14 @@ void SymbolTable::ApplyBase64Decode() { @@ -169,4 +172,14 @@ void SymbolTable::ApplyBase64Decode() {
169 } 172 }
170 } 173 }
171 174
  175 +#if __ANDROID_API__ >= 9
  176 +template SymbolTable::SymbolTable(AAssetManager *mgr,
  177 + const std::string &filename);
  178 +#endif
  179 +
  180 +#if __OHOS__
  181 +template SymbolTable::SymbolTable(NativeResourceManager *mgr,
  182 + const std::string &filename);
  183 +#endif
  184 +
172 } // namespace sherpa_onnx 185 } // namespace sherpa_onnx
@@ -10,11 +10,6 @@ @@ -10,11 +10,6 @@
10 #include <unordered_map> 10 #include <unordered_map>
11 #include <vector> 11 #include <vector>
12 12
13 -#if __ANDROID_API__ >= 9  
14 -#include "android/asset_manager.h"  
15 -#include "android/asset_manager_jni.h"  
16 -#endif  
17 -  
18 namespace sherpa_onnx { 13 namespace sherpa_onnx {
19 14
20 // The same token can be mapped to different integer IDs, so 15 // The same token can be mapped to different integer IDs, so
@@ -39,9 +34,8 @@ class SymbolTable { @@ -39,9 +34,8 @@ class SymbolTable {
39 /// Fields are separated by space(s). 34 /// Fields are separated by space(s).
40 explicit SymbolTable(const std::string &filename, bool is_file = true); 35 explicit SymbolTable(const std::string &filename, bool is_file = true);
41 36
42 -#if __ANDROID_API__ >= 9  
43 - SymbolTable(AAssetManager *mgr, const std::string &filename);  
44 -#endif 37 + template <typename Manager>
  38 + SymbolTable(Manager *mgr, const std::string &filename);
45 39
46 /// Return a string representation of this symbol table 40 /// Return a string representation of this symbol table
47 std::string ToString() const; 41 std::string ToString() const;
@@ -4,6 +4,15 @@ @@ -4,6 +4,15 @@
4 4
5 #include "sherpa-onnx/csrc/vad-model.h" 5 #include "sherpa-onnx/csrc/vad-model.h"
6 6
  7 +#if __ANDROID_API__ >= 9
  8 +#include "android/asset_manager.h"
  9 +#include "android/asset_manager_jni.h"
  10 +#endif
  11 +
  12 +#if __OHOS__
  13 +#include "rawfile/raw_file_manager.h"
  14 +#endif
  15 +
7 #include "sherpa-onnx/csrc/silero-vad-model.h" 16 #include "sherpa-onnx/csrc/silero-vad-model.h"
8 17
9 namespace sherpa_onnx { 18 namespace sherpa_onnx {
@@ -13,20 +22,20 @@ std::unique_ptr<VadModel> VadModel::Create(const VadModelConfig &config) { @@ -13,20 +22,20 @@ std::unique_ptr<VadModel> VadModel::Create(const VadModelConfig &config) {
13 return std::make_unique<SileroVadModel>(config); 22 return std::make_unique<SileroVadModel>(config);
14 } 23 }
15 24
16 -#if __ANDROID_API__ >= 9  
17 -std::unique_ptr<VadModel> VadModel::Create(AAssetManager *mgr, 25 +template <typename Manager>
  26 +std::unique_ptr<VadModel> VadModel::Create(Manager *mgr,
18 const VadModelConfig &config) { 27 const VadModelConfig &config) {
19 // TODO(fangjun): Support other VAD models. 28 // TODO(fangjun): Support other VAD models.
20 return std::make_unique<SileroVadModel>(mgr, config); 29 return std::make_unique<SileroVadModel>(mgr, config);
21 } 30 }
  31 +
  32 +#if __ANDROID_API__ >= 9
  33 +template std::unique_ptr<VadModel> VadModel::Create(
  34 + AAssetManager *mgr, const VadModelConfig &config);
22 #endif 35 #endif
23 36
24 #if __OHOS__ 37 #if __OHOS__
25 -std::unique_ptr<VadModel> VadModel::Create(NativeResourceManager *mgr,  
26 - const VadModelConfig &config) {  
27 - // TODO(fangjun): Support other VAD models.  
28 - return std::make_unique<SileroVadModel>(mgr, config);  
29 -} 38 +template std::unique_ptr<VadModel> VadModel::Create(
  39 + NativeResourceManager *mgr, const VadModelConfig &config);
30 #endif 40 #endif
31 -  
32 } // namespace sherpa_onnx 41 } // namespace sherpa_onnx
@@ -6,15 +6,6 @@ @@ -6,15 +6,6 @@
6 6
7 #include <memory> 7 #include <memory>
8 8
9 -#if __ANDROID_API__ >= 9  
10 -#include "android/asset_manager.h"  
11 -#include "android/asset_manager_jni.h"  
12 -#endif  
13 -  
14 -#if __OHOS__  
15 -#include "rawfile/raw_file_manager.h"  
16 -#endif  
17 -  
18 #include "sherpa-onnx/csrc/vad-model-config.h" 9 #include "sherpa-onnx/csrc/vad-model-config.h"
19 10
20 namespace sherpa_onnx { 11 namespace sherpa_onnx {
@@ -25,15 +16,9 @@ class VadModel { @@ -25,15 +16,9 @@ class VadModel {
25 16
26 static std::unique_ptr<VadModel> Create(const VadModelConfig &config); 17 static std::unique_ptr<VadModel> Create(const VadModelConfig &config);
27 18
28 -#if __ANDROID_API__ >= 9  
29 - static std::unique_ptr<VadModel> Create(AAssetManager *mgr,  
30 - const VadModelConfig &config);  
31 -#endif  
32 -  
33 -#if __OHOS__  
34 - static std::unique_ptr<VadModel> Create(NativeResourceManager *mgr, 19 + template <typename Manager>
  20 + static std::unique_ptr<VadModel> Create(Manager *mgr,
35 const VadModelConfig &config); 21 const VadModelConfig &config);
36 -#endif  
37 22
38 // reset the internal model states 23 // reset the internal model states
39 virtual void Reset() = 0; 24 virtual void Reset() = 0;
@@ -8,6 +8,15 @@ @@ -8,6 +8,15 @@
8 #include <queue> 8 #include <queue>
9 #include <utility> 9 #include <utility>
10 10
  11 +#if __ANDROID_API__ >= 9
  12 +#include "android/asset_manager.h"
  13 +#include "android/asset_manager_jni.h"
  14 +#endif
  15 +
  16 +#if __OHOS__
  17 +#include "rawfile/raw_file_manager.h"
  18 +#endif
  19 +
11 #include "sherpa-onnx/csrc/circular-buffer.h" 20 #include "sherpa-onnx/csrc/circular-buffer.h"
12 #include "sherpa-onnx/csrc/vad-model.h" 21 #include "sherpa-onnx/csrc/vad-model.h"
13 22
@@ -22,7 +31,6 @@ class VoiceActivityDetector::Impl { @@ -22,7 +31,6 @@ class VoiceActivityDetector::Impl {
22 Init(); 31 Init();
23 } 32 }
24 33
25 -#if __ANDROID_API__ >= 9 || defined(__OHOS__)  
26 template <typename Manager> 34 template <typename Manager>
27 Impl(Manager *mgr, const VadModelConfig &config, 35 Impl(Manager *mgr, const VadModelConfig &config,
28 float buffer_size_in_seconds = 60) 36 float buffer_size_in_seconds = 60)
@@ -31,7 +39,6 @@ class VoiceActivityDetector::Impl { @@ -31,7 +39,6 @@ class VoiceActivityDetector::Impl {
31 buffer_(buffer_size_in_seconds * config.sample_rate) { 39 buffer_(buffer_size_in_seconds * config.sample_rate) {
32 Init(); 40 Init();
33 } 41 }
34 -#endif  
35 42
36 void AcceptWaveform(const float *samples, int32_t n) { 43 void AcceptWaveform(const float *samples, int32_t n) {
37 if (buffer_.Size() > max_utterance_length_) { 44 if (buffer_.Size() > max_utterance_length_) {
@@ -178,19 +185,11 @@ VoiceActivityDetector::VoiceActivityDetector( @@ -178,19 +185,11 @@ VoiceActivityDetector::VoiceActivityDetector(
178 const VadModelConfig &config, float buffer_size_in_seconds /*= 60*/) 185 const VadModelConfig &config, float buffer_size_in_seconds /*= 60*/)
179 : impl_(std::make_unique<Impl>(config, buffer_size_in_seconds)) {} 186 : impl_(std::make_unique<Impl>(config, buffer_size_in_seconds)) {}
180 187
181 -#if __ANDROID_API__ >= 9 188 +template <typename Manager>
182 VoiceActivityDetector::VoiceActivityDetector( 189 VoiceActivityDetector::VoiceActivityDetector(
183 - AAssetManager *mgr, const VadModelConfig &config, 190 + Manager *mgr, const VadModelConfig &config,
184 float buffer_size_in_seconds /*= 60*/) 191 float buffer_size_in_seconds /*= 60*/)
185 : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {} 192 : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {}
186 -#endif  
187 -  
188 -#if __OHOS__  
189 -VoiceActivityDetector::VoiceActivityDetector(  
190 - NativeResourceManager *mgr, const VadModelConfig &config,  
191 - float buffer_size_in_seconds /*= 60*/)  
192 - : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {}  
193 -#endif  
194 193
195 VoiceActivityDetector::~VoiceActivityDetector() = default; 194 VoiceActivityDetector::~VoiceActivityDetector() = default;
196 195
@@ -220,4 +219,16 @@ const VadModelConfig &VoiceActivityDetector::GetConfig() const { @@ -220,4 +219,16 @@ const VadModelConfig &VoiceActivityDetector::GetConfig() const {
220 return impl_->GetConfig(); 219 return impl_->GetConfig();
221 } 220 }
222 221
  222 +#if __ANDROID_API__ >= 9
  223 +template VoiceActivityDetector::VoiceActivityDetector(
  224 + AAssetManager *mgr, const VadModelConfig &config,
  225 + float buffer_size_in_seconds = 60);
  226 +#endif
  227 +
  228 +#if __OHOS__
  229 +template VoiceActivityDetector::VoiceActivityDetector(
  230 + NativeResourceManager *mgr, const VadModelConfig &config,
  231 + float buffer_size_in_seconds = 60);
  232 +#endif
  233 +
223 } // namespace sherpa_onnx 234 } // namespace sherpa_onnx
@@ -7,15 +7,6 @@ @@ -7,15 +7,6 @@
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 -#if __ANDROID_API__ >= 9  
11 -#include "android/asset_manager.h"  
12 -#include "android/asset_manager_jni.h"  
13 -#endif  
14 -  
15 -#if __OHOS__  
16 -#include "rawfile/raw_file_manager.h"  
17 -#endif  
18 -  
19 #include "sherpa-onnx/csrc/vad-model-config.h" 10 #include "sherpa-onnx/csrc/vad-model-config.h"
20 11
21 namespace sherpa_onnx { 12 namespace sherpa_onnx {
@@ -30,16 +21,9 @@ class VoiceActivityDetector { @@ -30,16 +21,9 @@ class VoiceActivityDetector {
30 explicit VoiceActivityDetector(const VadModelConfig &config, 21 explicit VoiceActivityDetector(const VadModelConfig &config,
31 float buffer_size_in_seconds = 60); 22 float buffer_size_in_seconds = 60);
32 23
33 -#if __ANDROID_API__ >= 9  
34 - VoiceActivityDetector(AAssetManager *mgr, const VadModelConfig &config,  
35 - float buffer_size_in_seconds = 60);  
36 -#endif  
37 -  
38 -#if __OHOS__  
39 - VoiceActivityDetector(NativeResourceManager *mgr,  
40 - const VadModelConfig &config, 24 + template <typename Manager>
  25 + VoiceActivityDetector(Manager *mgr, const VadModelConfig &config,
41 float buffer_size_in_seconds = 60); 26 float buffer_size_in_seconds = 60);
42 -#endif  
43 27
44 ~VoiceActivityDetector(); 28 ~VoiceActivityDetector();
45 29