Committed by
GitHub
Add streaming ASR support for HarmonyOS. (#1565)
正在显示
35 个修改的文件
包含
367 行增加
和
206 行删除
| @@ -56,7 +56,7 @@ struct SherpaOnnxDisplay { | @@ -56,7 +56,7 @@ struct SherpaOnnxDisplay { | ||
| 56 | 56 | ||
| 57 | #define SHERPA_ONNX_OR(x, y) (x ? x : y) | 57 | #define SHERPA_ONNX_OR(x, y) (x ? x : y) |
| 58 | 58 | ||
| 59 | -const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( | 59 | +static sherpa_onnx::OnlineRecognizerConfig GetOnlineRecognizerConfig( |
| 60 | const SherpaOnnxOnlineRecognizerConfig *config) { | 60 | const SherpaOnnxOnlineRecognizerConfig *config) { |
| 61 | sherpa_onnx::OnlineRecognizerConfig recognizer_config; | 61 | sherpa_onnx::OnlineRecognizerConfig recognizer_config; |
| 62 | 62 | ||
| @@ -151,9 +151,21 @@ const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( | @@ -151,9 +151,21 @@ const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( | ||
| 151 | recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, ""); | 151 | recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, ""); |
| 152 | 152 | ||
| 153 | if (config->model_config.debug) { | 153 | if (config->model_config.debug) { |
| 154 | +#if __OHOS__ | ||
| 155 | + SHERPA_ONNX_LOGE("%{public}s\n", recognizer_config.ToString().c_str()); | ||
| 156 | +#else | ||
| 154 | SHERPA_ONNX_LOGE("%s\n", recognizer_config.ToString().c_str()); | 157 | SHERPA_ONNX_LOGE("%s\n", recognizer_config.ToString().c_str()); |
| 158 | +#endif | ||
| 155 | } | 159 | } |
| 156 | 160 | ||
| 161 | + return recognizer_config; | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( | ||
| 165 | + const SherpaOnnxOnlineRecognizerConfig *config) { | ||
| 166 | + sherpa_onnx::OnlineRecognizerConfig recognizer_config = | ||
| 167 | + GetOnlineRecognizerConfig(config); | ||
| 168 | + | ||
| 157 | if (!recognizer_config.Validate()) { | 169 | if (!recognizer_config.Validate()) { |
| 158 | SHERPA_ONNX_LOGE("Errors in config!"); | 170 | SHERPA_ONNX_LOGE("Errors in config!"); |
| 159 | return nullptr; | 171 | return nullptr; |
| @@ -1876,6 +1888,20 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( | @@ -1876,6 +1888,20 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( | ||
| 1876 | 1888 | ||
| 1877 | #ifdef __OHOS__ | 1889 | #ifdef __OHOS__ |
| 1878 | 1890 | ||
| 1891 | +const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizerOHOS( | ||
| 1892 | + const SherpaOnnxOnlineRecognizerConfig *config, | ||
| 1893 | + NativeResourceManager *mgr) { | ||
| 1894 | + sherpa_onnx::OnlineRecognizerConfig recognizer_config = | ||
| 1895 | + GetOnlineRecognizerConfig(config); | ||
| 1896 | + | ||
| 1897 | + SherpaOnnxOnlineRecognizer *recognizer = new SherpaOnnxOnlineRecognizer; | ||
| 1898 | + | ||
| 1899 | + recognizer->impl = | ||
| 1900 | + std::make_unique<sherpa_onnx::OnlineRecognizer>(mgr, recognizer_config); | ||
| 1901 | + | ||
| 1902 | + return recognizer; | ||
| 1903 | +} | ||
| 1904 | + | ||
| 1879 | const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS( | 1905 | const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS( |
| 1880 | const SherpaOnnxOfflineRecognizerConfig *config, | 1906 | const SherpaOnnxOfflineRecognizerConfig *config, |
| 1881 | NativeResourceManager *mgr) { | 1907 | NativeResourceManager *mgr) { |
| @@ -1529,6 +1529,13 @@ typedef struct NativeResourceManager NativeResourceManager; | @@ -1529,6 +1529,13 @@ typedef struct NativeResourceManager NativeResourceManager; | ||
| 1529 | 1529 | ||
| 1530 | /// @param config Config for the recognizer. | 1530 | /// @param config Config for the recognizer. |
| 1531 | /// @return Return a pointer to the recognizer. The user has to invoke | 1531 | /// @return Return a pointer to the recognizer. The user has to invoke |
| 1532 | +// SherpaOnnxDestroyOnlineRecognizer() to free it to avoid memory leak. | ||
| 1533 | +SHERPA_ONNX_API const SherpaOnnxOnlineRecognizer * | ||
| 1534 | +SherpaOnnxCreateOnlineRecognizerOHOS( | ||
| 1535 | + const SherpaOnnxOnlineRecognizerConfig *config, NativeResourceManager *mgr); | ||
| 1536 | + | ||
| 1537 | +/// @param config Config for the recognizer. | ||
| 1538 | +/// @return Return a pointer to the recognizer. The user has to invoke | ||
| 1532 | // SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory | 1539 | // SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory |
| 1533 | // leak. | 1540 | // leak. |
| 1534 | SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer * | 1541 | SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer * |
| @@ -5,17 +5,17 @@ | @@ -5,17 +5,17 @@ | ||
| 5 | #include "sherpa-onnx/csrc/offline-recognizer-impl.h" | 5 | #include "sherpa-onnx/csrc/offline-recognizer-impl.h" |
| 6 | 6 | ||
| 7 | #include <string> | 7 | #include <string> |
| 8 | +#include <strstream> | ||
| 8 | #include <utility> | 9 | #include <utility> |
| 9 | #include <vector> | 10 | #include <vector> |
| 10 | 11 | ||
| 11 | #if __ANDROID_API__ >= 9 | 12 | #if __ANDROID_API__ >= 9 |
| 12 | -#include <strstream> | ||
| 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> | 16 | +#endif |
| 18 | 17 | ||
| 18 | +#if __OHOS__ | ||
| 19 | #include "rawfile/raw_file_manager.h" | 19 | #include "rawfile/raw_file_manager.h" |
| 20 | #endif | 20 | #endif |
| 21 | 21 |
| @@ -17,6 +17,10 @@ | @@ -17,6 +17,10 @@ | ||
| 17 | #include "android/asset_manager_jni.h" | 17 | #include "android/asset_manager_jni.h" |
| 18 | #endif | 18 | #endif |
| 19 | 19 | ||
| 20 | +#if __OHOS__ | ||
| 21 | +#include "rawfile/raw_file_manager.h" | ||
| 22 | +#endif | ||
| 23 | + | ||
| 20 | #include "onnxruntime_cxx_api.h" // NOLINT | 24 | #include "onnxruntime_cxx_api.h" // NOLINT |
| 21 | #include "sherpa-onnx/csrc/cat.h" | 25 | #include "sherpa-onnx/csrc/cat.h" |
| 22 | #include "sherpa-onnx/csrc/macros.h" | 26 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -50,9 +54,9 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel( | @@ -50,9 +54,9 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel( | ||
| 50 | } | 54 | } |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 53 | -#if __ANDROID_API__ >= 9 | 57 | +template <typename Manager> |
| 54 | OnlineConformerTransducerModel::OnlineConformerTransducerModel( | 58 | OnlineConformerTransducerModel::OnlineConformerTransducerModel( |
| 55 | - AAssetManager *mgr, const OnlineModelConfig &config) | 59 | + Manager *mgr, const OnlineModelConfig &config) |
| 56 | : env_(ORT_LOGGING_LEVEL_ERROR), | 60 | : env_(ORT_LOGGING_LEVEL_ERROR), |
| 57 | config_(config), | 61 | config_(config), |
| 58 | sess_opts_(GetSessionOptions(config)), | 62 | sess_opts_(GetSessionOptions(config)), |
| @@ -72,7 +76,6 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel( | @@ -72,7 +76,6 @@ OnlineConformerTransducerModel::OnlineConformerTransducerModel( | ||
| 72 | InitJoiner(buf.data(), buf.size()); | 76 | InitJoiner(buf.data(), buf.size()); |
| 73 | } | 77 | } |
| 74 | } | 78 | } |
| 75 | -#endif | ||
| 76 | 79 | ||
| 77 | void OnlineConformerTransducerModel::InitEncoder(void *model_data, | 80 | void OnlineConformerTransducerModel::InitEncoder(void *model_data, |
| 78 | size_t model_data_length) { | 81 | size_t model_data_length) { |
| @@ -91,7 +94,11 @@ void OnlineConformerTransducerModel::InitEncoder(void *model_data, | @@ -91,7 +94,11 @@ void OnlineConformerTransducerModel::InitEncoder(void *model_data, | ||
| 91 | std::ostringstream os; | 94 | std::ostringstream os; |
| 92 | os << "---encoder---\n"; | 95 | os << "---encoder---\n"; |
| 93 | PrintModelMetadata(os, meta_data); | 96 | PrintModelMetadata(os, meta_data); |
| 97 | +#if __OHOS__ | ||
| 98 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 99 | +#else | ||
| 94 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 100 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 101 | +#endif | ||
| 95 | } | 102 | } |
| 96 | 103 | ||
| 97 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 104 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -121,7 +128,11 @@ void OnlineConformerTransducerModel::InitDecoder(void *model_data, | @@ -121,7 +128,11 @@ void OnlineConformerTransducerModel::InitDecoder(void *model_data, | ||
| 121 | std::ostringstream os; | 128 | std::ostringstream os; |
| 122 | os << "---decoder---\n"; | 129 | os << "---decoder---\n"; |
| 123 | PrintModelMetadata(os, meta_data); | 130 | PrintModelMetadata(os, meta_data); |
| 131 | +#if __OHOS__ | ||
| 132 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 133 | +#else | ||
| 124 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 134 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 135 | +#endif | ||
| 125 | } | 136 | } |
| 126 | 137 | ||
| 127 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 138 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -273,4 +284,14 @@ Ort::Value OnlineConformerTransducerModel::RunJoiner(Ort::Value encoder_out, | @@ -273,4 +284,14 @@ Ort::Value OnlineConformerTransducerModel::RunJoiner(Ort::Value encoder_out, | ||
| 273 | return std::move(logit[0]); | 284 | return std::move(logit[0]); |
| 274 | } | 285 | } |
| 275 | 286 | ||
| 287 | +#if __ANDROID_API__ >= 9 | ||
| 288 | +template OnlineConformerTransducerModel::OnlineConformerTransducerModel( | ||
| 289 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 290 | +#endif | ||
| 291 | + | ||
| 292 | +#if __OHOS__ | ||
| 293 | +template OnlineConformerTransducerModel::OnlineConformerTransducerModel( | ||
| 294 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 295 | +#endif | ||
| 296 | + | ||
| 276 | } // namespace sherpa_onnx | 297 | } // namespace sherpa_onnx |
| @@ -10,11 +10,6 @@ | @@ -10,11 +10,6 @@ | ||
| 10 | #include <utility> | 10 | #include <utility> |
| 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 | #include "onnxruntime_cxx_api.h" // NOLINT | 13 | #include "onnxruntime_cxx_api.h" // NOLINT |
| 19 | #include "sherpa-onnx/csrc/online-model-config.h" | 14 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 20 | #include "sherpa-onnx/csrc/online-transducer-model.h" | 15 | #include "sherpa-onnx/csrc/online-transducer-model.h" |
| @@ -25,10 +20,8 @@ class OnlineConformerTransducerModel : public OnlineTransducerModel { | @@ -25,10 +20,8 @@ class OnlineConformerTransducerModel : public OnlineTransducerModel { | ||
| 25 | public: | 20 | public: |
| 26 | explicit OnlineConformerTransducerModel(const OnlineModelConfig &config); | 21 | explicit OnlineConformerTransducerModel(const OnlineModelConfig &config); |
| 27 | 22 | ||
| 28 | -#if __ANDROID_API__ >= 9 | ||
| 29 | - OnlineConformerTransducerModel(AAssetManager *mgr, | ||
| 30 | - const OnlineModelConfig &config); | ||
| 31 | -#endif | 23 | + template <typename Manager> |
| 24 | + OnlineConformerTransducerModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 32 | 25 | ||
| 33 | std::vector<Ort::Value> StackStates( | 26 | std::vector<Ort::Value> StackStates( |
| 34 | const std::vector<std::vector<Ort::Value>> &states) const override; | 27 | const std::vector<std::vector<Ort::Value>> &states) const override; |
| @@ -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/online-nemo-ctc-model.h" | 22 | #include "sherpa-onnx/csrc/online-nemo-ctc-model.h" |
| 14 | #include "sherpa-onnx/csrc/online-wenet-ctc-model.h" | 23 | #include "sherpa-onnx/csrc/online-wenet-ctc-model.h" |
| @@ -31,10 +40,9 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | @@ -31,10 +40,9 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | ||
| 31 | } | 40 | } |
| 32 | } | 41 | } |
| 33 | 42 | ||
| 34 | -#if __ANDROID_API__ >= 9 | ||
| 35 | - | 43 | +template <typename Manager> |
| 36 | std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | 44 | std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( |
| 37 | - AAssetManager *mgr, const OnlineModelConfig &config) { | 45 | + Manager *mgr, const OnlineModelConfig &config) { |
| 38 | if (!config.wenet_ctc.model.empty()) { | 46 | if (!config.wenet_ctc.model.empty()) { |
| 39 | return std::make_unique<OnlineWenetCtcModel>(mgr, config); | 47 | return std::make_unique<OnlineWenetCtcModel>(mgr, config); |
| 40 | } else if (!config.zipformer2_ctc.model.empty()) { | 48 | } else if (!config.zipformer2_ctc.model.empty()) { |
| @@ -46,6 +54,15 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | @@ -46,6 +54,15 @@ std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | ||
| 46 | exit(-1); | 54 | exit(-1); |
| 47 | } | 55 | } |
| 48 | } | 56 | } |
| 57 | + | ||
| 58 | +#if __ANDROID_API__ >= 9 | ||
| 59 | +template std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | ||
| 60 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 61 | +#endif | ||
| 62 | + | ||
| 63 | +#if __OHOS__ | ||
| 64 | +template std::unique_ptr<OnlineCtcModel> OnlineCtcModel::Create( | ||
| 65 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 49 | #endif | 66 | #endif |
| 50 | 67 | ||
| 51 | } // namespace sherpa_onnx | 68 | } // 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/online-model-config.h" | 12 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 18 | 13 | ||
| @@ -25,10 +20,9 @@ class OnlineCtcModel { | @@ -25,10 +20,9 @@ class OnlineCtcModel { | ||
| 25 | static std::unique_ptr<OnlineCtcModel> Create( | 20 | static std::unique_ptr<OnlineCtcModel> Create( |
| 26 | const OnlineModelConfig &config); | 21 | const OnlineModelConfig &config); |
| 27 | 22 | ||
| 28 | -#if __ANDROID_API__ >= 9 | 23 | + template <typename Manager> |
| 29 | static std::unique_ptr<OnlineCtcModel> Create( | 24 | static std::unique_ptr<OnlineCtcModel> Create( |
| 30 | - AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 31 | -#endif | 25 | + Manager *mgr, const OnlineModelConfig &config); |
| 32 | 26 | ||
| 33 | // Return a list of tensors containing the initial states | 27 | // Return a list of tensors containing the initial states |
| 34 | virtual std::vector<Ort::Value> GetInitStates() const = 0; | 28 | virtual std::vector<Ort::Value> GetInitStates() const = 0; |
| @@ -16,6 +16,10 @@ | @@ -16,6 +16,10 @@ | ||
| 16 | #include "android/asset_manager_jni.h" | 16 | #include "android/asset_manager_jni.h" |
| 17 | #endif | 17 | #endif |
| 18 | 18 | ||
| 19 | +#if __OHOS__ | ||
| 20 | +#include "rawfile/raw_file_manager.h" | ||
| 21 | +#endif | ||
| 22 | + | ||
| 19 | #include "onnxruntime_cxx_api.h" // NOLINT | 23 | #include "onnxruntime_cxx_api.h" // NOLINT |
| 20 | #include "sherpa-onnx/csrc/cat.h" | 24 | #include "sherpa-onnx/csrc/cat.h" |
| 21 | #include "sherpa-onnx/csrc/macros.h" | 25 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -48,9 +52,9 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel( | @@ -48,9 +52,9 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel( | ||
| 48 | } | 52 | } |
| 49 | } | 53 | } |
| 50 | 54 | ||
| 51 | -#if __ANDROID_API__ >= 9 | 55 | +template <typename Manager> |
| 52 | OnlineLstmTransducerModel::OnlineLstmTransducerModel( | 56 | OnlineLstmTransducerModel::OnlineLstmTransducerModel( |
| 53 | - AAssetManager *mgr, const OnlineModelConfig &config) | 57 | + Manager *mgr, const OnlineModelConfig &config) |
| 54 | : env_(ORT_LOGGING_LEVEL_ERROR), | 58 | : env_(ORT_LOGGING_LEVEL_ERROR), |
| 55 | config_(config), | 59 | config_(config), |
| 56 | sess_opts_(GetSessionOptions(config)), | 60 | sess_opts_(GetSessionOptions(config)), |
| @@ -70,7 +74,6 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel( | @@ -70,7 +74,6 @@ OnlineLstmTransducerModel::OnlineLstmTransducerModel( | ||
| 70 | InitJoiner(buf.data(), buf.size()); | 74 | InitJoiner(buf.data(), buf.size()); |
| 71 | } | 75 | } |
| 72 | } | 76 | } |
| 73 | -#endif | ||
| 74 | 77 | ||
| 75 | void OnlineLstmTransducerModel::InitEncoder(void *model_data, | 78 | void OnlineLstmTransducerModel::InitEncoder(void *model_data, |
| 76 | size_t model_data_length) { | 79 | size_t model_data_length) { |
| @@ -89,7 +92,11 @@ void OnlineLstmTransducerModel::InitEncoder(void *model_data, | @@ -89,7 +92,11 @@ void OnlineLstmTransducerModel::InitEncoder(void *model_data, | ||
| 89 | std::ostringstream os; | 92 | std::ostringstream os; |
| 90 | os << "---encoder---\n"; | 93 | os << "---encoder---\n"; |
| 91 | PrintModelMetadata(os, meta_data); | 94 | PrintModelMetadata(os, meta_data); |
| 95 | +#if __OHOS__ | ||
| 96 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 97 | +#else | ||
| 92 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 98 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 99 | +#endif | ||
| 93 | } | 100 | } |
| 94 | 101 | ||
| 95 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 102 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -261,4 +268,14 @@ Ort::Value OnlineLstmTransducerModel::RunJoiner(Ort::Value encoder_out, | @@ -261,4 +268,14 @@ Ort::Value OnlineLstmTransducerModel::RunJoiner(Ort::Value encoder_out, | ||
| 261 | return std::move(logit[0]); | 268 | return std::move(logit[0]); |
| 262 | } | 269 | } |
| 263 | 270 | ||
| 271 | +#if __ANDROID_API__ >= 9 | ||
| 272 | +template OnlineLstmTransducerModel::OnlineLstmTransducerModel( | ||
| 273 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 274 | +#endif | ||
| 275 | + | ||
| 276 | +#if __OHOS__ | ||
| 277 | +template OnlineLstmTransducerModel::OnlineLstmTransducerModel( | ||
| 278 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 279 | +#endif | ||
| 280 | + | ||
| 264 | } // namespace sherpa_onnx | 281 | } // 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/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 19 | #include "sherpa-onnx/csrc/online-transducer-model.h" | 14 | #include "sherpa-onnx/csrc/online-transducer-model.h" |
| @@ -24,10 +19,8 @@ class OnlineLstmTransducerModel : public OnlineTransducerModel { | @@ -24,10 +19,8 @@ class OnlineLstmTransducerModel : public OnlineTransducerModel { | ||
| 24 | public: | 19 | public: |
| 25 | explicit OnlineLstmTransducerModel(const OnlineModelConfig &config); | 20 | explicit OnlineLstmTransducerModel(const OnlineModelConfig &config); |
| 26 | 21 | ||
| 27 | -#if __ANDROID_API__ >= 9 | ||
| 28 | - OnlineLstmTransducerModel(AAssetManager *mgr, | ||
| 29 | - const OnlineModelConfig &config); | ||
| 30 | -#endif | 22 | + template <typename Manager> |
| 23 | + OnlineLstmTransducerModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 31 | 24 | ||
| 32 | std::vector<Ort::Value> StackStates( | 25 | std::vector<Ort::Value> StackStates( |
| 33 | const std::vector<std::vector<Ort::Value>> &states) const override; | 26 | const std::vector<std::vector<Ort::Value>> &states) const override; |
| @@ -13,6 +13,10 @@ | @@ -13,6 +13,10 @@ | ||
| 13 | #include "android/asset_manager_jni.h" | 13 | #include "android/asset_manager_jni.h" |
| 14 | #endif | 14 | #endif |
| 15 | 15 | ||
| 16 | +#if __OHOS__ | ||
| 17 | +#include "rawfile/raw_file_manager.h" | ||
| 18 | +#endif | ||
| 19 | + | ||
| 16 | #include "sherpa-onnx/csrc/cat.h" | 20 | #include "sherpa-onnx/csrc/cat.h" |
| 17 | #include "sherpa-onnx/csrc/macros.h" | 21 | #include "sherpa-onnx/csrc/macros.h" |
| 18 | #include "sherpa-onnx/csrc/onnx-utils.h" | 22 | #include "sherpa-onnx/csrc/onnx-utils.h" |
| @@ -36,8 +40,8 @@ class OnlineNeMoCtcModel::Impl { | @@ -36,8 +40,8 @@ class OnlineNeMoCtcModel::Impl { | ||
| 36 | } | 40 | } |
| 37 | } | 41 | } |
| 38 | 42 | ||
| 39 | -#if __ANDROID_API__ >= 9 | ||
| 40 | - Impl(AAssetManager *mgr, const OnlineModelConfig &config) | 43 | + template <typename Manager> |
| 44 | + Impl(Manager *mgr, const OnlineModelConfig &config) | ||
| 41 | : config_(config), | 45 | : config_(config), |
| 42 | env_(ORT_LOGGING_LEVEL_ERROR), | 46 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 43 | sess_opts_(GetSessionOptions(config)), | 47 | sess_opts_(GetSessionOptions(config)), |
| @@ -47,7 +51,6 @@ class OnlineNeMoCtcModel::Impl { | @@ -47,7 +51,6 @@ class OnlineNeMoCtcModel::Impl { | ||
| 47 | Init(buf.data(), buf.size()); | 51 | Init(buf.data(), buf.size()); |
| 48 | } | 52 | } |
| 49 | } | 53 | } |
| 50 | -#endif | ||
| 51 | 54 | ||
| 52 | std::vector<Ort::Value> Forward(Ort::Value x, | 55 | std::vector<Ort::Value> Forward(Ort::Value x, |
| 53 | std::vector<Ort::Value> states) { | 56 | std::vector<Ort::Value> states) { |
| @@ -202,7 +205,11 @@ class OnlineNeMoCtcModel::Impl { | @@ -202,7 +205,11 @@ class OnlineNeMoCtcModel::Impl { | ||
| 202 | if (config_.debug) { | 205 | if (config_.debug) { |
| 203 | std::ostringstream os; | 206 | std::ostringstream os; |
| 204 | PrintModelMetadata(os, meta_data); | 207 | PrintModelMetadata(os, meta_data); |
| 205 | - SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); | 208 | +#if __OHOS__ |
| 209 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 210 | +#else | ||
| 211 | + SHERPA_ONNX_LOGE("%s", os.str().c_str()); | ||
| 212 | +#endif | ||
| 206 | } | 213 | } |
| 207 | 214 | ||
| 208 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 215 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -286,11 +293,10 @@ class OnlineNeMoCtcModel::Impl { | @@ -286,11 +293,10 @@ class OnlineNeMoCtcModel::Impl { | ||
| 286 | OnlineNeMoCtcModel::OnlineNeMoCtcModel(const OnlineModelConfig &config) | 293 | OnlineNeMoCtcModel::OnlineNeMoCtcModel(const OnlineModelConfig &config) |
| 287 | : impl_(std::make_unique<Impl>(config)) {} | 294 | : impl_(std::make_unique<Impl>(config)) {} |
| 288 | 295 | ||
| 289 | -#if __ANDROID_API__ >= 9 | ||
| 290 | -OnlineNeMoCtcModel::OnlineNeMoCtcModel(AAssetManager *mgr, | 296 | +template <typename Manager> |
| 297 | +OnlineNeMoCtcModel::OnlineNeMoCtcModel(Manager *mgr, | ||
| 291 | const OnlineModelConfig &config) | 298 | const OnlineModelConfig &config) |
| 292 | : impl_(std::make_unique<Impl>(mgr, config)) {} | 299 | : impl_(std::make_unique<Impl>(mgr, config)) {} |
| 293 | -#endif | ||
| 294 | 300 | ||
| 295 | OnlineNeMoCtcModel::~OnlineNeMoCtcModel() = default; | 301 | OnlineNeMoCtcModel::~OnlineNeMoCtcModel() = default; |
| 296 | 302 | ||
| @@ -323,4 +329,14 @@ std::vector<std::vector<Ort::Value>> OnlineNeMoCtcModel::UnStackStates( | @@ -323,4 +329,14 @@ std::vector<std::vector<Ort::Value>> OnlineNeMoCtcModel::UnStackStates( | ||
| 323 | return impl_->UnStackStates(std::move(states)); | 329 | return impl_->UnStackStates(std::move(states)); |
| 324 | } | 330 | } |
| 325 | 331 | ||
| 332 | +#if __ANDROID_API__ >= 9 | ||
| 333 | +template OnlineNeMoCtcModel::OnlineNeMoCtcModel( | ||
| 334 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 335 | +#endif | ||
| 336 | + | ||
| 337 | +#if __OHOS__ | ||
| 338 | +template OnlineNeMoCtcModel::OnlineNeMoCtcModel( | ||
| 339 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 340 | +#endif | ||
| 341 | + | ||
| 326 | } // namespace sherpa_onnx | 342 | } // 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/online-ctc-model.h" | 12 | #include "sherpa-onnx/csrc/online-ctc-model.h" |
| 18 | #include "sherpa-onnx/csrc/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| @@ -23,9 +18,8 @@ class OnlineNeMoCtcModel : public OnlineCtcModel { | @@ -23,9 +18,8 @@ class OnlineNeMoCtcModel : public OnlineCtcModel { | ||
| 23 | public: | 18 | public: |
| 24 | explicit OnlineNeMoCtcModel(const OnlineModelConfig &config); | 19 | explicit OnlineNeMoCtcModel(const OnlineModelConfig &config); |
| 25 | 20 | ||
| 26 | -#if __ANDROID_API__ >= 9 | ||
| 27 | - OnlineNeMoCtcModel(AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 28 | -#endif | 21 | + template <typename Manager> |
| 22 | + OnlineNeMoCtcModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 29 | 23 | ||
| 30 | ~OnlineNeMoCtcModel() override; | 24 | ~OnlineNeMoCtcModel() override; |
| 31 | 25 |
| @@ -13,6 +13,10 @@ | @@ -13,6 +13,10 @@ | ||
| 13 | #include "android/asset_manager_jni.h" | 13 | #include "android/asset_manager_jni.h" |
| 14 | #endif | 14 | #endif |
| 15 | 15 | ||
| 16 | +#if __OHOS__ | ||
| 17 | +#include "rawfile/raw_file_manager.h" | ||
| 18 | +#endif | ||
| 19 | + | ||
| 16 | #include "sherpa-onnx/csrc/macros.h" | 20 | #include "sherpa-onnx/csrc/macros.h" |
| 17 | #include "sherpa-onnx/csrc/onnx-utils.h" | 21 | #include "sherpa-onnx/csrc/onnx-utils.h" |
| 18 | #include "sherpa-onnx/csrc/session.h" | 22 | #include "sherpa-onnx/csrc/session.h" |
| @@ -38,8 +42,8 @@ class OnlineParaformerModel::Impl { | @@ -38,8 +42,8 @@ class OnlineParaformerModel::Impl { | ||
| 38 | } | 42 | } |
| 39 | } | 43 | } |
| 40 | 44 | ||
| 41 | -#if __ANDROID_API__ >= 9 | ||
| 42 | - Impl(AAssetManager *mgr, const OnlineModelConfig &config) | 45 | + template <typename Manager> |
| 46 | + Impl(Manager *mgr, const OnlineModelConfig &config) | ||
| 43 | : config_(config), | 47 | : config_(config), |
| 44 | env_(ORT_LOGGING_LEVEL_ERROR), | 48 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 45 | sess_opts_(GetSessionOptions(config)), | 49 | sess_opts_(GetSessionOptions(config)), |
| @@ -54,7 +58,6 @@ class OnlineParaformerModel::Impl { | @@ -54,7 +58,6 @@ class OnlineParaformerModel::Impl { | ||
| 54 | InitDecoder(buf.data(), buf.size()); | 58 | InitDecoder(buf.data(), buf.size()); |
| 55 | } | 59 | } |
| 56 | } | 60 | } |
| 57 | -#endif | ||
| 58 | 61 | ||
| 59 | std::vector<Ort::Value> ForwardEncoder(Ort::Value features, | 62 | std::vector<Ort::Value> ForwardEncoder(Ort::Value features, |
| 60 | Ort::Value features_length) { | 63 | Ort::Value features_length) { |
| @@ -123,7 +126,11 @@ class OnlineParaformerModel::Impl { | @@ -123,7 +126,11 @@ class OnlineParaformerModel::Impl { | ||
| 123 | if (config_.debug) { | 126 | if (config_.debug) { |
| 124 | std::ostringstream os; | 127 | std::ostringstream os; |
| 125 | PrintModelMetadata(os, meta_data); | 128 | PrintModelMetadata(os, meta_data); |
| 126 | - SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); | 129 | +#if __OHOS__ |
| 130 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 131 | +#else | ||
| 132 | + SHERPA_ONNX_LOGE("%s", os.str().c_str()); | ||
| 133 | +#endif | ||
| 127 | } | 134 | } |
| 128 | 135 | ||
| 129 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 136 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -191,11 +198,10 @@ class OnlineParaformerModel::Impl { | @@ -191,11 +198,10 @@ class OnlineParaformerModel::Impl { | ||
| 191 | OnlineParaformerModel::OnlineParaformerModel(const OnlineModelConfig &config) | 198 | OnlineParaformerModel::OnlineParaformerModel(const OnlineModelConfig &config) |
| 192 | : impl_(std::make_unique<Impl>(config)) {} | 199 | : impl_(std::make_unique<Impl>(config)) {} |
| 193 | 200 | ||
| 194 | -#if __ANDROID_API__ >= 9 | ||
| 195 | -OnlineParaformerModel::OnlineParaformerModel(AAssetManager *mgr, | 201 | +template <typename Manager> |
| 202 | +OnlineParaformerModel::OnlineParaformerModel(Manager *mgr, | ||
| 196 | const OnlineModelConfig &config) | 203 | const OnlineModelConfig &config) |
| 197 | : impl_(std::make_unique<Impl>(mgr, config)) {} | 204 | : impl_(std::make_unique<Impl>(mgr, config)) {} |
| 198 | -#endif | ||
| 199 | 205 | ||
| 200 | OnlineParaformerModel::~OnlineParaformerModel() = default; | 206 | OnlineParaformerModel::~OnlineParaformerModel() = default; |
| 201 | 207 | ||
| @@ -246,4 +252,14 @@ OrtAllocator *OnlineParaformerModel::Allocator() const { | @@ -246,4 +252,14 @@ OrtAllocator *OnlineParaformerModel::Allocator() const { | ||
| 246 | return impl_->Allocator(); | 252 | return impl_->Allocator(); |
| 247 | } | 253 | } |
| 248 | 254 | ||
| 255 | +#if __ANDROID_API__ >= 9 | ||
| 256 | +template OnlineParaformerModel::OnlineParaformerModel( | ||
| 257 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 258 | +#endif | ||
| 259 | + | ||
| 260 | +#if __OHOS__ | ||
| 261 | +template OnlineParaformerModel::OnlineParaformerModel( | ||
| 262 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 263 | +#endif | ||
| 264 | + | ||
| 249 | } // namespace sherpa_onnx | 265 | } // 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/online-model-config.h" | 12 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 18 | 13 | ||
| @@ -22,9 +17,8 @@ class OnlineParaformerModel { | @@ -22,9 +17,8 @@ class OnlineParaformerModel { | ||
| 22 | public: | 17 | public: |
| 23 | explicit OnlineParaformerModel(const OnlineModelConfig &config); | 18 | explicit OnlineParaformerModel(const OnlineModelConfig &config); |
| 24 | 19 | ||
| 25 | -#if __ANDROID_API__ >= 9 | ||
| 26 | - OnlineParaformerModel(AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 27 | -#endif | 20 | + template <typename Manager> |
| 21 | + OnlineParaformerModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 28 | 22 | ||
| 29 | ~OnlineParaformerModel(); | 23 | ~OnlineParaformerModel(); |
| 30 | 24 |
| @@ -88,8 +88,8 @@ class OnlineRecognizerCtcImpl : public OnlineRecognizerImpl { | @@ -88,8 +88,8 @@ class OnlineRecognizerCtcImpl : public OnlineRecognizerImpl { | ||
| 88 | InitDecoder(); | 88 | InitDecoder(); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | -#if __ANDROID_API__ >= 9 | ||
| 92 | - explicit OnlineRecognizerCtcImpl(AAssetManager *mgr, | 91 | + template <typename Manager> |
| 92 | + explicit OnlineRecognizerCtcImpl(Manager *mgr, | ||
| 93 | const OnlineRecognizerConfig &config) | 93 | const OnlineRecognizerConfig &config) |
| 94 | : OnlineRecognizerImpl(mgr, config), | 94 | : OnlineRecognizerImpl(mgr, config), |
| 95 | config_(config), | 95 | config_(config), |
| @@ -104,7 +104,6 @@ class OnlineRecognizerCtcImpl : public OnlineRecognizerImpl { | @@ -104,7 +104,6 @@ class OnlineRecognizerCtcImpl : public OnlineRecognizerImpl { | ||
| 104 | 104 | ||
| 105 | InitDecoder(); | 105 | InitDecoder(); |
| 106 | } | 106 | } |
| 107 | -#endif | ||
| 108 | 107 | ||
| 109 | std::unique_ptr<OnlineStream> CreateStream() const override { | 108 | std::unique_ptr<OnlineStream> CreateStream() const override { |
| 110 | auto stream = std::make_unique<OnlineStream>(config_.feat_config); | 109 | auto stream = std::make_unique<OnlineStream>(config_.feat_config); |
| @@ -4,15 +4,18 @@ | @@ -4,15 +4,18 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" | 5 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" |
| 6 | 6 | ||
| 7 | +#include <strstream> | ||
| 7 | #include <utility> | 8 | #include <utility> |
| 8 | 9 | ||
| 9 | #if __ANDROID_API__ >= 9 | 10 | #if __ANDROID_API__ >= 9 |
| 10 | -#include <strstream> | ||
| 11 | - | ||
| 12 | #include "android/asset_manager.h" | 11 | #include "android/asset_manager.h" |
| 13 | #include "android/asset_manager_jni.h" | 12 | #include "android/asset_manager_jni.h" |
| 14 | #endif | 13 | #endif |
| 15 | 14 | ||
| 15 | +#if __OHOS__ | ||
| 16 | +#include "rawfile/raw_file_manager.h" | ||
| 17 | +#endif | ||
| 18 | + | ||
| 16 | #include "fst/extensions/far/far.h" | 19 | #include "fst/extensions/far/far.h" |
| 17 | #include "kaldifst/csrc/kaldi-fst-io.h" | 20 | #include "kaldifst/csrc/kaldi-fst-io.h" |
| 18 | #include "sherpa-onnx/csrc/macros.h" | 21 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -61,9 +64,9 @@ std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | @@ -61,9 +64,9 @@ std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | ||
| 61 | exit(-1); | 64 | exit(-1); |
| 62 | } | 65 | } |
| 63 | 66 | ||
| 64 | -#if __ANDROID_API__ >= 9 | 67 | +template <typename Manager> |
| 65 | std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | 68 | std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( |
| 66 | - AAssetManager *mgr, const OnlineRecognizerConfig &config) { | 69 | + Manager *mgr, const OnlineRecognizerConfig &config) { |
| 67 | if (!config.model_config.transducer.encoder.empty()) { | 70 | if (!config.model_config.transducer.encoder.empty()) { |
| 68 | Ort::Env env(ORT_LOGGING_LEVEL_ERROR); | 71 | Ort::Env env(ORT_LOGGING_LEVEL_ERROR); |
| 69 | 72 | ||
| @@ -97,7 +100,6 @@ std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | @@ -97,7 +100,6 @@ std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | ||
| 97 | SHERPA_ONNX_LOGE("Please specify a model"); | 100 | SHERPA_ONNX_LOGE("Please specify a model"); |
| 98 | exit(-1); | 101 | exit(-1); |
| 99 | } | 102 | } |
| 100 | -#endif | ||
| 101 | 103 | ||
| 102 | OnlineRecognizerImpl::OnlineRecognizerImpl(const OnlineRecognizerConfig &config) | 104 | OnlineRecognizerImpl::OnlineRecognizerImpl(const OnlineRecognizerConfig &config) |
| 103 | : config_(config) { | 105 | : config_(config) { |
| @@ -143,8 +145,8 @@ OnlineRecognizerImpl::OnlineRecognizerImpl(const OnlineRecognizerConfig &config) | @@ -143,8 +145,8 @@ OnlineRecognizerImpl::OnlineRecognizerImpl(const OnlineRecognizerConfig &config) | ||
| 143 | } | 145 | } |
| 144 | } | 146 | } |
| 145 | 147 | ||
| 146 | -#if __ANDROID_API__ >= 9 | ||
| 147 | -OnlineRecognizerImpl::OnlineRecognizerImpl(AAssetManager *mgr, | 148 | +template <typename Manager> |
| 149 | +OnlineRecognizerImpl::OnlineRecognizerImpl(Manager *mgr, | ||
| 148 | const OnlineRecognizerConfig &config) | 150 | const OnlineRecognizerConfig &config) |
| 149 | : config_(config) { | 151 | : config_(config) { |
| 150 | if (!config.rule_fsts.empty()) { | 152 | if (!config.rule_fsts.empty()) { |
| @@ -189,7 +191,6 @@ OnlineRecognizerImpl::OnlineRecognizerImpl(AAssetManager *mgr, | @@ -189,7 +191,6 @@ OnlineRecognizerImpl::OnlineRecognizerImpl(AAssetManager *mgr, | ||
| 189 | } // for (const auto &f : files) | 191 | } // for (const auto &f : files) |
| 190 | } // if (!config.rule_fars.empty()) | 192 | } // if (!config.rule_fars.empty()) |
| 191 | } | 193 | } |
| 192 | -#endif | ||
| 193 | 194 | ||
| 194 | std::string OnlineRecognizerImpl::ApplyInverseTextNormalization( | 195 | std::string OnlineRecognizerImpl::ApplyInverseTextNormalization( |
| 195 | std::string text) const { | 196 | std::string text) const { |
| @@ -202,4 +203,20 @@ std::string OnlineRecognizerImpl::ApplyInverseTextNormalization( | @@ -202,4 +203,20 @@ std::string OnlineRecognizerImpl::ApplyInverseTextNormalization( | ||
| 202 | return text; | 203 | return text; |
| 203 | } | 204 | } |
| 204 | 205 | ||
| 206 | +#if __ANDROID_API__ >= 9 | ||
| 207 | +template OnlineRecognizerImpl::OnlineRecognizerImpl( | ||
| 208 | + AAssetManager *mgr, const OnlineRecognizerConfig &config); | ||
| 209 | + | ||
| 210 | +template std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | ||
| 211 | + AAssetManager *mgr, const OnlineRecognizerConfig &config); | ||
| 212 | +#endif | ||
| 213 | + | ||
| 214 | +#if __OHOS__ | ||
| 215 | +template OnlineRecognizerImpl::OnlineRecognizerImpl( | ||
| 216 | + NativeResourceManager *mgr, const OnlineRecognizerConfig &config); | ||
| 217 | + | ||
| 218 | +template std::unique_ptr<OnlineRecognizerImpl> OnlineRecognizerImpl::Create( | ||
| 219 | + NativeResourceManager *mgr, const OnlineRecognizerConfig &config); | ||
| 220 | +#endif | ||
| 221 | + | ||
| 205 | } // namespace sherpa_onnx | 222 | } // 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/online-recognizer.h" | 14 | #include "sherpa-onnx/csrc/online-recognizer.h" |
| @@ -28,13 +23,12 @@ class OnlineRecognizerImpl { | @@ -28,13 +23,12 @@ class OnlineRecognizerImpl { | ||
| 28 | static std::unique_ptr<OnlineRecognizerImpl> Create( | 23 | static std::unique_ptr<OnlineRecognizerImpl> Create( |
| 29 | const OnlineRecognizerConfig &config); | 24 | const OnlineRecognizerConfig &config); |
| 30 | 25 | ||
| 31 | -#if __ANDROID_API__ >= 9 | ||
| 32 | - OnlineRecognizerImpl(AAssetManager *mgr, | ||
| 33 | - const OnlineRecognizerConfig &config); | 26 | + template <typename Manager> |
| 27 | + OnlineRecognizerImpl(Manager *mgr, const OnlineRecognizerConfig &config); | ||
| 34 | 28 | ||
| 29 | + template <typename Manager> | ||
| 35 | static std::unique_ptr<OnlineRecognizerImpl> Create( | 30 | static std::unique_ptr<OnlineRecognizerImpl> Create( |
| 36 | - AAssetManager *mgr, const OnlineRecognizerConfig &config); | ||
| 37 | -#endif | 31 | + Manager *mgr, const OnlineRecognizerConfig &config); |
| 38 | 32 | ||
| 39 | virtual ~OnlineRecognizerImpl() = default; | 33 | virtual ~OnlineRecognizerImpl() = default; |
| 40 | 34 |
| @@ -120,8 +120,8 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl { | @@ -120,8 +120,8 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl { | ||
| 120 | config_.feat_config.normalize_samples = false; | 120 | config_.feat_config.normalize_samples = false; |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | -#if __ANDROID_API__ >= 9 | ||
| 124 | - explicit OnlineRecognizerParaformerImpl(AAssetManager *mgr, | 123 | + template <typename Manager> |
| 124 | + explicit OnlineRecognizerParaformerImpl(Manager *mgr, | ||
| 125 | const OnlineRecognizerConfig &config) | 125 | const OnlineRecognizerConfig &config) |
| 126 | : OnlineRecognizerImpl(mgr, config), | 126 | : OnlineRecognizerImpl(mgr, config), |
| 127 | config_(config), | 127 | config_(config), |
| @@ -138,7 +138,7 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl { | @@ -138,7 +138,7 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl { | ||
| 138 | // [-32768, 32767], so we set normalize_samples to false | 138 | // [-32768, 32767], so we set normalize_samples to false |
| 139 | config_.feat_config.normalize_samples = false; | 139 | config_.feat_config.normalize_samples = false; |
| 140 | } | 140 | } |
| 141 | -#endif | 141 | + |
| 142 | OnlineRecognizerParaformerImpl(const OnlineRecognizerParaformerImpl &) = | 142 | OnlineRecognizerParaformerImpl(const OnlineRecognizerParaformerImpl &) = |
| 143 | delete; | 143 | delete; |
| 144 | 144 |
| @@ -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/file-utils.h" | 17 | #include "sherpa-onnx/csrc/file-utils.h" |
| 23 | #include "sherpa-onnx/csrc/macros.h" | 18 | #include "sherpa-onnx/csrc/macros.h" |
| 24 | #include "sherpa-onnx/csrc/online-lm.h" | 19 | #include "sherpa-onnx/csrc/online-lm.h" |
| @@ -130,8 +125,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | @@ -130,8 +125,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | ||
| 130 | } | 125 | } |
| 131 | } | 126 | } |
| 132 | 127 | ||
| 133 | -#if __ANDROID_API__ >= 9 | ||
| 134 | - explicit OnlineRecognizerTransducerImpl(AAssetManager *mgr, | 128 | + template <typename Manager> |
| 129 | + explicit OnlineRecognizerTransducerImpl(Manager *mgr, | ||
| 135 | const OnlineRecognizerConfig &config) | 130 | const OnlineRecognizerConfig &config) |
| 136 | : OnlineRecognizerImpl(mgr, config), | 131 | : OnlineRecognizerImpl(mgr, config), |
| 137 | config_(config), | 132 | config_(config), |
| @@ -178,7 +173,6 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | @@ -178,7 +173,6 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | ||
| 178 | exit(-1); | 173 | exit(-1); |
| 179 | } | 174 | } |
| 180 | } | 175 | } |
| 181 | -#endif | ||
| 182 | 176 | ||
| 183 | std::unique_ptr<OnlineStream> CreateStream() const override { | 177 | std::unique_ptr<OnlineStream> CreateStream() const override { |
| 184 | auto stream = | 178 | auto stream = |
| @@ -429,8 +423,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | @@ -429,8 +423,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | ||
| 429 | hotwords_, config_.hotwords_score, boost_scores_); | 423 | hotwords_, config_.hotwords_score, boost_scores_); |
| 430 | } | 424 | } |
| 431 | 425 | ||
| 432 | -#if __ANDROID_API__ >= 9 | ||
| 433 | - void InitHotwords(AAssetManager *mgr) { | 426 | + template <typename Manager> |
| 427 | + void InitHotwords(Manager *mgr) { | ||
| 434 | // each line in hotwords_file contains space-separated words | 428 | // each line in hotwords_file contains space-separated words |
| 435 | 429 | ||
| 436 | auto buf = ReadFile(mgr, config_.hotwords_file); | 430 | auto buf = ReadFile(mgr, config_.hotwords_file); |
| @@ -452,7 +446,6 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | @@ -452,7 +446,6 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl { | ||
| 452 | hotwords_graph_ = std::make_shared<ContextGraph>( | 446 | hotwords_graph_ = std::make_shared<ContextGraph>( |
| 453 | hotwords_, config_.hotwords_score, boost_scores_); | 447 | hotwords_, config_.hotwords_score, boost_scores_); |
| 454 | } | 448 | } |
| 455 | -#endif | ||
| 456 | 449 | ||
| 457 | void InitHotwordsFromBufStr() { | 450 | void InitHotwordsFromBufStr() { |
| 458 | // each line in hotwords_file contains space-separated words | 451 | // each line in hotwords_file contains space-separated words |
| @@ -16,11 +16,6 @@ | @@ -16,11 +16,6 @@ | ||
| 16 | #include <utility> | 16 | #include <utility> |
| 17 | #include <vector> | 17 | #include <vector> |
| 18 | 18 | ||
| 19 | -#if __ANDROID_API__ >= 9 | ||
| 20 | -#include "android/asset_manager.h" | ||
| 21 | -#include "android/asset_manager_jni.h" | ||
| 22 | -#endif | ||
| 23 | - | ||
| 24 | #include "sherpa-onnx/csrc/macros.h" | 19 | #include "sherpa-onnx/csrc/macros.h" |
| 25 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" | 20 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" |
| 26 | #include "sherpa-onnx/csrc/online-recognizer.h" | 21 | #include "sherpa-onnx/csrc/online-recognizer.h" |
| @@ -65,9 +60,9 @@ class OnlineRecognizerTransducerNeMoImpl : public OnlineRecognizerImpl { | @@ -65,9 +60,9 @@ class OnlineRecognizerTransducerNeMoImpl : public OnlineRecognizerImpl { | ||
| 65 | PostInit(); | 60 | PostInit(); |
| 66 | } | 61 | } |
| 67 | 62 | ||
| 68 | -#if __ANDROID_API__ >= 9 | 63 | + template <typename Manager> |
| 69 | explicit OnlineRecognizerTransducerNeMoImpl( | 64 | explicit OnlineRecognizerTransducerNeMoImpl( |
| 70 | - AAssetManager *mgr, const OnlineRecognizerConfig &config) | 65 | + Manager *mgr, const OnlineRecognizerConfig &config) |
| 71 | : OnlineRecognizerImpl(mgr, config), | 66 | : OnlineRecognizerImpl(mgr, config), |
| 72 | config_(config), | 67 | config_(config), |
| 73 | symbol_table_(mgr, config.model_config.tokens), | 68 | symbol_table_(mgr, config.model_config.tokens), |
| @@ -85,7 +80,6 @@ class OnlineRecognizerTransducerNeMoImpl : public OnlineRecognizerImpl { | @@ -85,7 +80,6 @@ class OnlineRecognizerTransducerNeMoImpl : public OnlineRecognizerImpl { | ||
| 85 | 80 | ||
| 86 | PostInit(); | 81 | PostInit(); |
| 87 | } | 82 | } |
| 88 | -#endif | ||
| 89 | 83 | ||
| 90 | std::unique_ptr<OnlineStream> CreateStream() const override { | 84 | std::unique_ptr<OnlineStream> CreateStream() const override { |
| 91 | auto stream = std::make_unique<OnlineStream>(config_.feat_config); | 85 | auto stream = std::make_unique<OnlineStream>(config_.feat_config); |
| @@ -13,6 +13,15 @@ | @@ -13,6 +13,15 @@ | ||
| 13 | #include <utility> | 13 | #include <utility> |
| 14 | #include <vector> | 14 | #include <vector> |
| 15 | 15 | ||
| 16 | +#if __ANDROID_API__ >= 9 | ||
| 17 | +#include "android/asset_manager.h" | ||
| 18 | +#include "android/asset_manager_jni.h" | ||
| 19 | +#endif | ||
| 20 | + | ||
| 21 | +#if __OHOS__ | ||
| 22 | +#include "rawfile/raw_file_manager.h" | ||
| 23 | +#endif | ||
| 24 | + | ||
| 16 | #include "sherpa-onnx/csrc/file-utils.h" | 25 | #include "sherpa-onnx/csrc/file-utils.h" |
| 17 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" | 26 | #include "sherpa-onnx/csrc/online-recognizer-impl.h" |
| 18 | #include "sherpa-onnx/csrc/text-utils.h" | 27 | #include "sherpa-onnx/csrc/text-utils.h" |
| @@ -197,11 +206,10 @@ std::string OnlineRecognizerConfig::ToString() const { | @@ -197,11 +206,10 @@ std::string OnlineRecognizerConfig::ToString() const { | ||
| 197 | OnlineRecognizer::OnlineRecognizer(const OnlineRecognizerConfig &config) | 206 | OnlineRecognizer::OnlineRecognizer(const OnlineRecognizerConfig &config) |
| 198 | : impl_(OnlineRecognizerImpl::Create(config)) {} | 207 | : impl_(OnlineRecognizerImpl::Create(config)) {} |
| 199 | 208 | ||
| 200 | -#if __ANDROID_API__ >= 9 | ||
| 201 | -OnlineRecognizer::OnlineRecognizer(AAssetManager *mgr, | 209 | +template <typename Manager> |
| 210 | +OnlineRecognizer::OnlineRecognizer(Manager *mgr, | ||
| 202 | const OnlineRecognizerConfig &config) | 211 | const OnlineRecognizerConfig &config) |
| 203 | : impl_(OnlineRecognizerImpl::Create(mgr, config)) {} | 212 | : impl_(OnlineRecognizerImpl::Create(mgr, config)) {} |
| 204 | -#endif | ||
| 205 | 213 | ||
| 206 | OnlineRecognizer::~OnlineRecognizer() = default; | 214 | OnlineRecognizer::~OnlineRecognizer() = default; |
| 207 | 215 | ||
| @@ -238,4 +246,14 @@ bool OnlineRecognizer::IsEndpoint(OnlineStream *s) const { | @@ -238,4 +246,14 @@ bool OnlineRecognizer::IsEndpoint(OnlineStream *s) const { | ||
| 238 | 246 | ||
| 239 | void OnlineRecognizer::Reset(OnlineStream *s) const { impl_->Reset(s); } | 247 | void OnlineRecognizer::Reset(OnlineStream *s) const { impl_->Reset(s); } |
| 240 | 248 | ||
| 249 | +#if __ANDROID_API__ >= 9 | ||
| 250 | +template OnlineRecognizer::OnlineRecognizer( | ||
| 251 | + AAssetManager *mgr, const OnlineRecognizerConfig &config); | ||
| 252 | +#endif | ||
| 253 | + | ||
| 254 | +#if __OHOS__ | ||
| 255 | +template OnlineRecognizer::OnlineRecognizer( | ||
| 256 | + NativeResourceManager *mgr, const OnlineRecognizerConfig &config); | ||
| 257 | +#endif | ||
| 258 | + | ||
| 241 | } // namespace sherpa_onnx | 259 | } // 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/endpoint.h" | 12 | #include "sherpa-onnx/csrc/endpoint.h" |
| 18 | #include "sherpa-onnx/csrc/features.h" | 13 | #include "sherpa-onnx/csrc/features.h" |
| 19 | #include "sherpa-onnx/csrc/online-ctc-fst-decoder-config.h" | 14 | #include "sherpa-onnx/csrc/online-ctc-fst-decoder-config.h" |
| @@ -149,9 +144,8 @@ class OnlineRecognizer { | @@ -149,9 +144,8 @@ class OnlineRecognizer { | ||
| 149 | public: | 144 | public: |
| 150 | explicit OnlineRecognizer(const OnlineRecognizerConfig &config); | 145 | explicit OnlineRecognizer(const OnlineRecognizerConfig &config); |
| 151 | 146 | ||
| 152 | -#if __ANDROID_API__ >= 9 | ||
| 153 | - OnlineRecognizer(AAssetManager *mgr, const OnlineRecognizerConfig &config); | ||
| 154 | -#endif | 147 | + template <typename Manager> |
| 148 | + OnlineRecognizer(Manager *mgr, const OnlineRecognizerConfig &config); | ||
| 155 | 149 | ||
| 156 | ~OnlineRecognizer(); | 150 | ~OnlineRecognizer(); |
| 157 | 151 |
| @@ -9,6 +9,10 @@ | @@ -9,6 +9,10 @@ | ||
| 9 | #include "android/asset_manager_jni.h" | 9 | #include "android/asset_manager_jni.h" |
| 10 | #endif | 10 | #endif |
| 11 | 11 | ||
| 12 | +#if __OHOS__ | ||
| 13 | +#include "rawfile/raw_file_manager.h" | ||
| 14 | +#endif | ||
| 15 | + | ||
| 12 | #include <algorithm> | 16 | #include <algorithm> |
| 13 | #include <memory> | 17 | #include <memory> |
| 14 | #include <sstream> | 18 | #include <sstream> |
| @@ -49,7 +53,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length, | @@ -49,7 +53,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length, | ||
| 49 | if (debug) { | 53 | if (debug) { |
| 50 | std::ostringstream os; | 54 | std::ostringstream os; |
| 51 | PrintModelMetadata(os, meta_data); | 55 | PrintModelMetadata(os, meta_data); |
| 56 | +#if __OHOS__ | ||
| 57 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 58 | +#else | ||
| 52 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 59 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 60 | +#endif | ||
| 53 | } | 61 | } |
| 54 | 62 | ||
| 55 | Ort::AllocatorWithDefaultOptions allocator; | 63 | Ort::AllocatorWithDefaultOptions allocator; |
| @@ -155,9 +163,9 @@ Ort::Value OnlineTransducerModel::BuildDecoderInput( | @@ -155,9 +163,9 @@ Ort::Value OnlineTransducerModel::BuildDecoderInput( | ||
| 155 | return decoder_input; | 163 | return decoder_input; |
| 156 | } | 164 | } |
| 157 | 165 | ||
| 158 | -#if __ANDROID_API__ >= 9 | 166 | +template <typename Manager> |
| 159 | std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( | 167 | std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( |
| 160 | - AAssetManager *mgr, const OnlineModelConfig &config) { | 168 | + Manager *mgr, const OnlineModelConfig &config) { |
| 161 | if (!config.model_type.empty()) { | 169 | if (!config.model_type.empty()) { |
| 162 | const auto &model_type = config.model_type; | 170 | const auto &model_type = config.model_type; |
| 163 | if (model_type == "conformer") { | 171 | if (model_type == "conformer") { |
| @@ -195,6 +203,15 @@ std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( | @@ -195,6 +203,15 @@ std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( | ||
| 195 | // unreachable code | 203 | // unreachable code |
| 196 | return nullptr; | 204 | return nullptr; |
| 197 | } | 205 | } |
| 206 | + | ||
| 207 | +#if __ANDROID_API__ >= 9 | ||
| 208 | +template std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( | ||
| 209 | + Manager *mgr, const OnlineModelConfig &config); | ||
| 210 | +#endif | ||
| 211 | + | ||
| 212 | +#if __OHOS__ | ||
| 213 | +template std::unique_ptr<OnlineTransducerModel> OnlineTransducerModel::Create( | ||
| 214 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 198 | #endif | 215 | #endif |
| 199 | 216 | ||
| 200 | } // namespace sherpa_onnx | 217 | } // 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/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| @@ -30,10 +25,9 @@ class OnlineTransducerModel { | @@ -30,10 +25,9 @@ class OnlineTransducerModel { | ||
| 30 | static std::unique_ptr<OnlineTransducerModel> Create( | 25 | static std::unique_ptr<OnlineTransducerModel> Create( |
| 31 | const OnlineModelConfig &config); | 26 | const OnlineModelConfig &config); |
| 32 | 27 | ||
| 33 | -#if __ANDROID_API__ >= 9 | 28 | + template <typename Manager> |
| 34 | static std::unique_ptr<OnlineTransducerModel> Create( | 29 | static std::unique_ptr<OnlineTransducerModel> Create( |
| 35 | - AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 36 | -#endif | 30 | + Manager *mgr, const OnlineModelConfig &config); |
| 37 | 31 | ||
| 38 | /** Stack a list of individual states into a batch. | 32 | /** Stack a list of individual states into a batch. |
| 39 | * | 33 | * |
| @@ -20,6 +20,10 @@ | @@ -20,6 +20,10 @@ | ||
| 20 | #include "android/asset_manager_jni.h" | 20 | #include "android/asset_manager_jni.h" |
| 21 | #endif | 21 | #endif |
| 22 | 22 | ||
| 23 | +#if __OHOS__ | ||
| 24 | +#include "rawfile/raw_file_manager.h" | ||
| 25 | +#endif | ||
| 26 | + | ||
| 23 | #include "sherpa-onnx/csrc/cat.h" | 27 | #include "sherpa-onnx/csrc/cat.h" |
| 24 | #include "sherpa-onnx/csrc/macros.h" | 28 | #include "sherpa-onnx/csrc/macros.h" |
| 25 | #include "sherpa-onnx/csrc/online-transducer-decoder.h" | 29 | #include "sherpa-onnx/csrc/online-transducer-decoder.h" |
| @@ -54,8 +58,8 @@ class OnlineTransducerNeMoModel::Impl { | @@ -54,8 +58,8 @@ class OnlineTransducerNeMoModel::Impl { | ||
| 54 | } | 58 | } |
| 55 | } | 59 | } |
| 56 | 60 | ||
| 57 | -#if __ANDROID_API__ >= 9 | ||
| 58 | - Impl(AAssetManager *mgr, const OnlineModelConfig &config) | 61 | + template <typename Manager> |
| 62 | + Impl(Manager *mgr, const OnlineModelConfig &config) | ||
| 59 | : config_(config), | 63 | : config_(config), |
| 60 | env_(ORT_LOGGING_LEVEL_ERROR), | 64 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 61 | sess_opts_(GetSessionOptions(config)), | 65 | sess_opts_(GetSessionOptions(config)), |
| @@ -75,7 +79,6 @@ class OnlineTransducerNeMoModel::Impl { | @@ -75,7 +79,6 @@ class OnlineTransducerNeMoModel::Impl { | ||
| 75 | InitJoiner(buf.data(), buf.size()); | 79 | InitJoiner(buf.data(), buf.size()); |
| 76 | } | 80 | } |
| 77 | } | 81 | } |
| 78 | -#endif | ||
| 79 | 82 | ||
| 80 | std::vector<Ort::Value> RunEncoder(Ort::Value features, | 83 | std::vector<Ort::Value> RunEncoder(Ort::Value features, |
| 81 | std::vector<Ort::Value> states) { | 84 | std::vector<Ort::Value> states) { |
| @@ -302,7 +305,11 @@ class OnlineTransducerNeMoModel::Impl { | @@ -302,7 +305,11 @@ class OnlineTransducerNeMoModel::Impl { | ||
| 302 | std::ostringstream os; | 305 | std::ostringstream os; |
| 303 | os << "---encoder---\n"; | 306 | os << "---encoder---\n"; |
| 304 | PrintModelMetadata(os, meta_data); | 307 | PrintModelMetadata(os, meta_data); |
| 305 | - SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); | 308 | +#if __OHOS__ |
| 309 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 310 | +#else | ||
| 311 | + SHERPA_ONNX_LOGE("%s", os.str().c_str()); | ||
| 312 | +#endif | ||
| 306 | } | 313 | } |
| 307 | 314 | ||
| 308 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 315 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -460,11 +467,10 @@ OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( | @@ -460,11 +467,10 @@ OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( | ||
| 460 | const OnlineModelConfig &config) | 467 | const OnlineModelConfig &config) |
| 461 | : impl_(std::make_unique<Impl>(config)) {} | 468 | : impl_(std::make_unique<Impl>(config)) {} |
| 462 | 469 | ||
| 463 | -#if __ANDROID_API__ >= 9 | 470 | +template <typename Manager> |
| 464 | OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( | 471 | OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( |
| 465 | - AAssetManager *mgr, const OnlineModelConfig &config) | 472 | + Manager *mgr, const OnlineModelConfig &config) |
| 466 | : impl_(std::make_unique<Impl>(mgr, config)) {} | 473 | : impl_(std::make_unique<Impl>(mgr, config)) {} |
| 467 | -#endif | ||
| 468 | 474 | ||
| 469 | OnlineTransducerNeMoModel::~OnlineTransducerNeMoModel() = default; | 475 | OnlineTransducerNeMoModel::~OnlineTransducerNeMoModel() = default; |
| 470 | 476 | ||
| @@ -528,4 +534,14 @@ std::vector<std::vector<Ort::Value>> OnlineTransducerNeMoModel::UnStackStates( | @@ -528,4 +534,14 @@ std::vector<std::vector<Ort::Value>> OnlineTransducerNeMoModel::UnStackStates( | ||
| 528 | return impl_->UnStackStates(std::move(states)); | 534 | return impl_->UnStackStates(std::move(states)); |
| 529 | } | 535 | } |
| 530 | 536 | ||
| 537 | +#if __ANDROID_API__ >= 9 | ||
| 538 | +template OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( | ||
| 539 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 540 | +#endif | ||
| 541 | + | ||
| 542 | +#if __OHOS__ | ||
| 543 | +template OnlineTransducerNeMoModel::OnlineTransducerNeMoModel( | ||
| 544 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 545 | +#endif | ||
| 546 | + | ||
| 531 | } // namespace sherpa_onnx | 547 | } // 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/online-model-config.h" | 15 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 21 | 16 | ||
| @@ -28,10 +23,8 @@ class OnlineTransducerNeMoModel { | @@ -28,10 +23,8 @@ class OnlineTransducerNeMoModel { | ||
| 28 | public: | 23 | public: |
| 29 | explicit OnlineTransducerNeMoModel(const OnlineModelConfig &config); | 24 | explicit OnlineTransducerNeMoModel(const OnlineModelConfig &config); |
| 30 | 25 | ||
| 31 | -#if __ANDROID_API__ >= 9 | ||
| 32 | - OnlineTransducerNeMoModel(AAssetManager *mgr, | ||
| 33 | - const OnlineModelConfig &config); | ||
| 34 | -#endif | 26 | + template <typename Manager> |
| 27 | + OnlineTransducerNeMoModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 35 | 28 | ||
| 36 | ~OnlineTransducerNeMoModel(); | 29 | ~OnlineTransducerNeMoModel(); |
| 37 | // A list of 3 tensors: | 30 | // A list of 3 tensors: |
| @@ -13,6 +13,10 @@ | @@ -13,6 +13,10 @@ | ||
| 13 | #include "android/asset_manager_jni.h" | 13 | #include "android/asset_manager_jni.h" |
| 14 | #endif | 14 | #endif |
| 15 | 15 | ||
| 16 | +#if __OHOS__ | ||
| 17 | +#include "rawfile/raw_file_manager.h" | ||
| 18 | +#endif | ||
| 19 | + | ||
| 16 | #include "sherpa-onnx/csrc/macros.h" | 20 | #include "sherpa-onnx/csrc/macros.h" |
| 17 | #include "sherpa-onnx/csrc/onnx-utils.h" | 21 | #include "sherpa-onnx/csrc/onnx-utils.h" |
| 18 | #include "sherpa-onnx/csrc/session.h" | 22 | #include "sherpa-onnx/csrc/session.h" |
| @@ -33,8 +37,8 @@ class OnlineWenetCtcModel::Impl { | @@ -33,8 +37,8 @@ class OnlineWenetCtcModel::Impl { | ||
| 33 | } | 37 | } |
| 34 | } | 38 | } |
| 35 | 39 | ||
| 36 | -#if __ANDROID_API__ >= 9 | ||
| 37 | - Impl(AAssetManager *mgr, const OnlineModelConfig &config) | 40 | + template <typename Manager> |
| 41 | + Impl(Manager *mgr, const OnlineModelConfig &config) | ||
| 38 | : config_(config), | 42 | : config_(config), |
| 39 | env_(ORT_LOGGING_LEVEL_ERROR), | 43 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 40 | sess_opts_(GetSessionOptions(config)), | 44 | sess_opts_(GetSessionOptions(config)), |
| @@ -44,7 +48,6 @@ class OnlineWenetCtcModel::Impl { | @@ -44,7 +48,6 @@ class OnlineWenetCtcModel::Impl { | ||
| 44 | Init(buf.data(), buf.size()); | 48 | Init(buf.data(), buf.size()); |
| 45 | } | 49 | } |
| 46 | } | 50 | } |
| 47 | -#endif | ||
| 48 | 51 | ||
| 49 | std::vector<Ort::Value> Forward(Ort::Value x, | 52 | std::vector<Ort::Value> Forward(Ort::Value x, |
| 50 | std::vector<Ort::Value> states) { | 53 | std::vector<Ort::Value> states) { |
| @@ -139,7 +142,11 @@ class OnlineWenetCtcModel::Impl { | @@ -139,7 +142,11 @@ class OnlineWenetCtcModel::Impl { | ||
| 139 | if (config_.debug) { | 142 | if (config_.debug) { |
| 140 | std::ostringstream os; | 143 | std::ostringstream os; |
| 141 | PrintModelMetadata(os, meta_data); | 144 | PrintModelMetadata(os, meta_data); |
| 142 | - SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); | 145 | +#if __OHOS__ |
| 146 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 147 | +#else | ||
| 148 | + SHERPA_ONNX_LOGE("%s", os.str().c_str()); | ||
| 149 | +#endif | ||
| 143 | } | 150 | } |
| 144 | 151 | ||
| 145 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 152 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -212,11 +219,10 @@ class OnlineWenetCtcModel::Impl { | @@ -212,11 +219,10 @@ class OnlineWenetCtcModel::Impl { | ||
| 212 | OnlineWenetCtcModel::OnlineWenetCtcModel(const OnlineModelConfig &config) | 219 | OnlineWenetCtcModel::OnlineWenetCtcModel(const OnlineModelConfig &config) |
| 213 | : impl_(std::make_unique<Impl>(config)) {} | 220 | : impl_(std::make_unique<Impl>(config)) {} |
| 214 | 221 | ||
| 215 | -#if __ANDROID_API__ >= 9 | ||
| 216 | -OnlineWenetCtcModel::OnlineWenetCtcModel(AAssetManager *mgr, | 222 | +template <typename Manager> |
| 223 | +OnlineWenetCtcModel::OnlineWenetCtcModel(Manager *mgr, | ||
| 217 | const OnlineModelConfig &config) | 224 | const OnlineModelConfig &config) |
| 218 | : impl_(std::make_unique<Impl>(mgr, config)) {} | 225 | : impl_(std::make_unique<Impl>(mgr, config)) {} |
| 219 | -#endif | ||
| 220 | 226 | ||
| 221 | OnlineWenetCtcModel::~OnlineWenetCtcModel() = default; | 227 | OnlineWenetCtcModel::~OnlineWenetCtcModel() = default; |
| 222 | 228 | ||
| @@ -258,4 +264,14 @@ std::vector<std::vector<Ort::Value>> OnlineWenetCtcModel::UnStackStates( | @@ -258,4 +264,14 @@ std::vector<std::vector<Ort::Value>> OnlineWenetCtcModel::UnStackStates( | ||
| 258 | return ans; | 264 | return ans; |
| 259 | } | 265 | } |
| 260 | 266 | ||
| 267 | +#if __ANDROID_API__ >= 9 | ||
| 268 | +template OnlineWenetCtcModel::OnlineWenetCtcModel( | ||
| 269 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 270 | +#endif | ||
| 271 | + | ||
| 272 | +#if __OHOS__ | ||
| 273 | +template OnlineWenetCtcModel::OnlineWenetCtcModel( | ||
| 274 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 275 | +#endif | ||
| 276 | + | ||
| 261 | } // namespace sherpa_onnx | 277 | } // 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/online-ctc-model.h" | 12 | #include "sherpa-onnx/csrc/online-ctc-model.h" |
| 18 | #include "sherpa-onnx/csrc/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| @@ -23,9 +18,8 @@ class OnlineWenetCtcModel : public OnlineCtcModel { | @@ -23,9 +18,8 @@ class OnlineWenetCtcModel : public OnlineCtcModel { | ||
| 23 | public: | 18 | public: |
| 24 | explicit OnlineWenetCtcModel(const OnlineModelConfig &config); | 19 | explicit OnlineWenetCtcModel(const OnlineModelConfig &config); |
| 25 | 20 | ||
| 26 | -#if __ANDROID_API__ >= 9 | ||
| 27 | - OnlineWenetCtcModel(AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 28 | -#endif | 21 | + template <typename Manager> |
| 22 | + OnlineWenetCtcModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 29 | 23 | ||
| 30 | ~OnlineWenetCtcModel() override; | 24 | ~OnlineWenetCtcModel() override; |
| 31 | 25 |
| @@ -17,6 +17,10 @@ | @@ -17,6 +17,10 @@ | ||
| 17 | #include "android/asset_manager_jni.h" | 17 | #include "android/asset_manager_jni.h" |
| 18 | #endif | 18 | #endif |
| 19 | 19 | ||
| 20 | +#if __OHOS__ | ||
| 21 | +#include "rawfile/raw_file_manager.h" | ||
| 22 | +#endif | ||
| 23 | + | ||
| 20 | #include "onnxruntime_cxx_api.h" // NOLINT | 24 | #include "onnxruntime_cxx_api.h" // NOLINT |
| 21 | #include "sherpa-onnx/csrc/cat.h" | 25 | #include "sherpa-onnx/csrc/cat.h" |
| 22 | #include "sherpa-onnx/csrc/macros.h" | 26 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -50,9 +54,9 @@ OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | @@ -50,9 +54,9 @@ OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | ||
| 50 | } | 54 | } |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 53 | -#if __ANDROID_API__ >= 9 | 57 | +template <typename Manager> |
| 54 | OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | 58 | OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( |
| 55 | - AAssetManager *mgr, const OnlineModelConfig &config) | 59 | + Manager *mgr, const OnlineModelConfig &config) |
| 56 | : env_(ORT_LOGGING_LEVEL_ERROR), | 60 | : env_(ORT_LOGGING_LEVEL_ERROR), |
| 57 | config_(config), | 61 | config_(config), |
| 58 | sess_opts_(GetSessionOptions(config)), | 62 | sess_opts_(GetSessionOptions(config)), |
| @@ -72,7 +76,6 @@ OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | @@ -72,7 +76,6 @@ OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | ||
| 72 | InitJoiner(buf.data(), buf.size()); | 76 | InitJoiner(buf.data(), buf.size()); |
| 73 | } | 77 | } |
| 74 | } | 78 | } |
| 75 | -#endif | ||
| 76 | 79 | ||
| 77 | void OnlineZipformerTransducerModel::InitEncoder(void *model_data, | 80 | void OnlineZipformerTransducerModel::InitEncoder(void *model_data, |
| 78 | size_t model_data_length) { | 81 | size_t model_data_length) { |
| @@ -91,7 +94,11 @@ void OnlineZipformerTransducerModel::InitEncoder(void *model_data, | @@ -91,7 +94,11 @@ void OnlineZipformerTransducerModel::InitEncoder(void *model_data, | ||
| 91 | std::ostringstream os; | 94 | std::ostringstream os; |
| 92 | os << "---encoder---\n"; | 95 | os << "---encoder---\n"; |
| 93 | PrintModelMetadata(os, meta_data); | 96 | PrintModelMetadata(os, meta_data); |
| 97 | +#if __OHOS__ | ||
| 98 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 99 | +#else | ||
| 94 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 100 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 101 | +#endif | ||
| 95 | } | 102 | } |
| 96 | 103 | ||
| 97 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 104 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -480,4 +487,14 @@ Ort::Value OnlineZipformerTransducerModel::RunJoiner(Ort::Value encoder_out, | @@ -480,4 +487,14 @@ Ort::Value OnlineZipformerTransducerModel::RunJoiner(Ort::Value encoder_out, | ||
| 480 | return std::move(logit[0]); | 487 | return std::move(logit[0]); |
| 481 | } | 488 | } |
| 482 | 489 | ||
| 490 | +#if __ANDROID_API__ >= 9 | ||
| 491 | +template OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | ||
| 492 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 493 | +#endif | ||
| 494 | + | ||
| 495 | +#if __OHOS__ | ||
| 496 | +template OnlineZipformerTransducerModel::OnlineZipformerTransducerModel( | ||
| 497 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 498 | +#endif | ||
| 499 | + | ||
| 483 | } // namespace sherpa_onnx | 500 | } // 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/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 19 | #include "sherpa-onnx/csrc/online-transducer-model.h" | 14 | #include "sherpa-onnx/csrc/online-transducer-model.h" |
| @@ -24,10 +19,8 @@ class OnlineZipformerTransducerModel : public OnlineTransducerModel { | @@ -24,10 +19,8 @@ class OnlineZipformerTransducerModel : public OnlineTransducerModel { | ||
| 24 | public: | 19 | public: |
| 25 | explicit OnlineZipformerTransducerModel(const OnlineModelConfig &config); | 20 | explicit OnlineZipformerTransducerModel(const OnlineModelConfig &config); |
| 26 | 21 | ||
| 27 | -#if __ANDROID_API__ >= 9 | ||
| 28 | - OnlineZipformerTransducerModel(AAssetManager *mgr, | ||
| 29 | - const OnlineModelConfig &config); | ||
| 30 | -#endif | 22 | + template <typename Manager> |
| 23 | + OnlineZipformerTransducerModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 31 | 24 | ||
| 32 | std::vector<Ort::Value> StackStates( | 25 | std::vector<Ort::Value> StackStates( |
| 33 | const std::vector<std::vector<Ort::Value>> &states) const override; | 26 | const std::vector<std::vector<Ort::Value>> &states) const override; |
| @@ -15,6 +15,10 @@ | @@ -15,6 +15,10 @@ | ||
| 15 | #include "android/asset_manager_jni.h" | 15 | #include "android/asset_manager_jni.h" |
| 16 | #endif | 16 | #endif |
| 17 | 17 | ||
| 18 | +#if __OHOS__ | ||
| 19 | +#include "rawfile/raw_file_manager.h" | ||
| 20 | +#endif | ||
| 21 | + | ||
| 18 | #include "sherpa-onnx/csrc/cat.h" | 22 | #include "sherpa-onnx/csrc/cat.h" |
| 19 | #include "sherpa-onnx/csrc/macros.h" | 23 | #include "sherpa-onnx/csrc/macros.h" |
| 20 | #include "sherpa-onnx/csrc/onnx-utils.h" | 24 | #include "sherpa-onnx/csrc/onnx-utils.h" |
| @@ -37,8 +41,8 @@ class OnlineZipformer2CtcModel::Impl { | @@ -37,8 +41,8 @@ class OnlineZipformer2CtcModel::Impl { | ||
| 37 | } | 41 | } |
| 38 | } | 42 | } |
| 39 | 43 | ||
| 40 | -#if __ANDROID_API__ >= 9 | ||
| 41 | - Impl(AAssetManager *mgr, const OnlineModelConfig &config) | 44 | + template <typename Manager> |
| 45 | + Impl(Manager *mgr, const OnlineModelConfig &config) | ||
| 42 | : config_(config), | 46 | : config_(config), |
| 43 | env_(ORT_LOGGING_LEVEL_ERROR), | 47 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 44 | sess_opts_(GetSessionOptions(config)), | 48 | sess_opts_(GetSessionOptions(config)), |
| @@ -48,7 +52,6 @@ class OnlineZipformer2CtcModel::Impl { | @@ -48,7 +52,6 @@ class OnlineZipformer2CtcModel::Impl { | ||
| 48 | Init(buf.data(), buf.size()); | 52 | Init(buf.data(), buf.size()); |
| 49 | } | 53 | } |
| 50 | } | 54 | } |
| 51 | -#endif | ||
| 52 | 55 | ||
| 53 | std::vector<Ort::Value> Forward(Ort::Value features, | 56 | std::vector<Ort::Value> Forward(Ort::Value features, |
| 54 | std::vector<Ort::Value> states) { | 57 | std::vector<Ort::Value> states) { |
| @@ -255,7 +258,11 @@ class OnlineZipformer2CtcModel::Impl { | @@ -255,7 +258,11 @@ class OnlineZipformer2CtcModel::Impl { | ||
| 255 | std::ostringstream os; | 258 | std::ostringstream os; |
| 256 | os << "---zipformer2_ctc---\n"; | 259 | os << "---zipformer2_ctc---\n"; |
| 257 | PrintModelMetadata(os, meta_data); | 260 | PrintModelMetadata(os, meta_data); |
| 261 | +#if __OHOS__ | ||
| 262 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 263 | +#else | ||
| 258 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 264 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 265 | +#endif | ||
| 259 | } | 266 | } |
| 260 | 267 | ||
| 261 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 268 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -415,11 +422,10 @@ OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( | @@ -415,11 +422,10 @@ OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( | ||
| 415 | const OnlineModelConfig &config) | 422 | const OnlineModelConfig &config) |
| 416 | : impl_(std::make_unique<Impl>(config)) {} | 423 | : impl_(std::make_unique<Impl>(config)) {} |
| 417 | 424 | ||
| 418 | -#if __ANDROID_API__ >= 9 | 425 | +template <typename Manager> |
| 419 | OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( | 426 | OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( |
| 420 | - AAssetManager *mgr, const OnlineModelConfig &config) | 427 | + Manager *mgr, const OnlineModelConfig &config) |
| 421 | : impl_(std::make_unique<Impl>(mgr, config)) {} | 428 | : impl_(std::make_unique<Impl>(mgr, config)) {} |
| 422 | -#endif | ||
| 423 | 429 | ||
| 424 | OnlineZipformer2CtcModel::~OnlineZipformer2CtcModel() = default; | 430 | OnlineZipformer2CtcModel::~OnlineZipformer2CtcModel() = default; |
| 425 | 431 | ||
| @@ -458,4 +464,14 @@ std::vector<std::vector<Ort::Value>> OnlineZipformer2CtcModel::UnStackStates( | @@ -458,4 +464,14 @@ std::vector<std::vector<Ort::Value>> OnlineZipformer2CtcModel::UnStackStates( | ||
| 458 | return impl_->UnStackStates(std::move(states)); | 464 | return impl_->UnStackStates(std::move(states)); |
| 459 | } | 465 | } |
| 460 | 466 | ||
| 467 | +#if __ANDROID_API__ >= 9 | ||
| 468 | +template OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( | ||
| 469 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 470 | +#endif | ||
| 471 | + | ||
| 472 | +#if __OHOS__ | ||
| 473 | +template OnlineZipformer2CtcModel::OnlineZipformer2CtcModel( | ||
| 474 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 475 | +#endif | ||
| 476 | + | ||
| 461 | } // namespace sherpa_onnx | 477 | } // 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/online-ctc-model.h" | 12 | #include "sherpa-onnx/csrc/online-ctc-model.h" |
| 18 | #include "sherpa-onnx/csrc/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| @@ -23,9 +18,8 @@ class OnlineZipformer2CtcModel : public OnlineCtcModel { | @@ -23,9 +18,8 @@ class OnlineZipformer2CtcModel : public OnlineCtcModel { | ||
| 23 | public: | 18 | public: |
| 24 | explicit OnlineZipformer2CtcModel(const OnlineModelConfig &config); | 19 | explicit OnlineZipformer2CtcModel(const OnlineModelConfig &config); |
| 25 | 20 | ||
| 26 | -#if __ANDROID_API__ >= 9 | ||
| 27 | - OnlineZipformer2CtcModel(AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 28 | -#endif | 21 | + template <typename Manager> |
| 22 | + OnlineZipformer2CtcModel(Manager *mgr, const OnlineModelConfig &config); | ||
| 29 | 23 | ||
| 30 | ~OnlineZipformer2CtcModel() override; | 24 | ~OnlineZipformer2CtcModel() override; |
| 31 | 25 |
| @@ -19,6 +19,10 @@ | @@ -19,6 +19,10 @@ | ||
| 19 | #include "android/asset_manager_jni.h" | 19 | #include "android/asset_manager_jni.h" |
| 20 | #endif | 20 | #endif |
| 21 | 21 | ||
| 22 | +#if __OHOS__ | ||
| 23 | +#include "rawfile/raw_file_manager.h" | ||
| 24 | +#endif | ||
| 25 | + | ||
| 22 | #include "onnxruntime_cxx_api.h" // NOLINT | 26 | #include "onnxruntime_cxx_api.h" // NOLINT |
| 23 | #include "sherpa-onnx/csrc/cat.h" | 27 | #include "sherpa-onnx/csrc/cat.h" |
| 24 | #include "sherpa-onnx/csrc/macros.h" | 28 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -54,9 +58,9 @@ OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | @@ -54,9 +58,9 @@ OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | ||
| 54 | } | 58 | } |
| 55 | } | 59 | } |
| 56 | 60 | ||
| 57 | -#if __ANDROID_API__ >= 9 | 61 | +template <typename Manager> |
| 58 | OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | 62 | OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( |
| 59 | - AAssetManager *mgr, const OnlineModelConfig &config) | 63 | + Manager *mgr, const OnlineModelConfig &config) |
| 60 | : env_(ORT_LOGGING_LEVEL_ERROR), | 64 | : env_(ORT_LOGGING_LEVEL_ERROR), |
| 61 | config_(config), | 65 | config_(config), |
| 62 | encoder_sess_opts_(GetSessionOptions(config)), | 66 | encoder_sess_opts_(GetSessionOptions(config)), |
| @@ -78,7 +82,6 @@ OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | @@ -78,7 +82,6 @@ OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | ||
| 78 | InitJoiner(buf.data(), buf.size()); | 82 | InitJoiner(buf.data(), buf.size()); |
| 79 | } | 83 | } |
| 80 | } | 84 | } |
| 81 | -#endif | ||
| 82 | 85 | ||
| 83 | void OnlineZipformer2TransducerModel::InitEncoder(void *model_data, | 86 | void OnlineZipformer2TransducerModel::InitEncoder(void *model_data, |
| 84 | size_t model_data_length) { | 87 | size_t model_data_length) { |
| @@ -97,7 +100,11 @@ void OnlineZipformer2TransducerModel::InitEncoder(void *model_data, | @@ -97,7 +100,11 @@ void OnlineZipformer2TransducerModel::InitEncoder(void *model_data, | ||
| 97 | std::ostringstream os; | 100 | std::ostringstream os; |
| 98 | os << "---encoder---\n"; | 101 | os << "---encoder---\n"; |
| 99 | PrintModelMetadata(os, meta_data); | 102 | PrintModelMetadata(os, meta_data); |
| 103 | +#if __OHOS__ | ||
| 104 | + SHERPA_ONNX_LOGE("%{public}s", os.str().c_str()); | ||
| 105 | +#else | ||
| 100 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); | 106 | SHERPA_ONNX_LOGE("%s", os.str().c_str()); |
| 107 | +#endif | ||
| 101 | } | 108 | } |
| 102 | 109 | ||
| 103 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below | 110 | Ort::AllocatorWithDefaultOptions allocator; // used in the macro below |
| @@ -474,4 +481,14 @@ Ort::Value OnlineZipformer2TransducerModel::RunJoiner(Ort::Value encoder_out, | @@ -474,4 +481,14 @@ Ort::Value OnlineZipformer2TransducerModel::RunJoiner(Ort::Value encoder_out, | ||
| 474 | return std::move(logit[0]); | 481 | return std::move(logit[0]); |
| 475 | } | 482 | } |
| 476 | 483 | ||
| 484 | +#if __ANDROID_API__ >= 9 | ||
| 485 | +template OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | ||
| 486 | + AAssetManager *mgr, const OnlineModelConfig &config); | ||
| 487 | +#endif | ||
| 488 | + | ||
| 489 | +#if __OHOS__ | ||
| 490 | +template OnlineZipformer2TransducerModel::OnlineZipformer2TransducerModel( | ||
| 491 | + NativeResourceManager *mgr, const OnlineModelConfig &config); | ||
| 492 | +#endif | ||
| 493 | + | ||
| 477 | } // namespace sherpa_onnx | 494 | } // 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/online-model-config.h" | 13 | #include "sherpa-onnx/csrc/online-model-config.h" |
| 19 | #include "sherpa-onnx/csrc/online-transducer-model.h" | 14 | #include "sherpa-onnx/csrc/online-transducer-model.h" |
| @@ -24,10 +19,9 @@ class OnlineZipformer2TransducerModel : public OnlineTransducerModel { | @@ -24,10 +19,9 @@ class OnlineZipformer2TransducerModel : public OnlineTransducerModel { | ||
| 24 | public: | 19 | public: |
| 25 | explicit OnlineZipformer2TransducerModel(const OnlineModelConfig &config); | 20 | explicit OnlineZipformer2TransducerModel(const OnlineModelConfig &config); |
| 26 | 21 | ||
| 27 | -#if __ANDROID_API__ >= 9 | ||
| 28 | - OnlineZipformer2TransducerModel(AAssetManager *mgr, | 22 | + template <typename Manager> |
| 23 | + OnlineZipformer2TransducerModel(Manager *mgr, | ||
| 29 | const OnlineModelConfig &config); | 24 | const OnlineModelConfig &config); |
| 30 | -#endif | ||
| 31 | 25 | ||
| 32 | std::vector<Ort::Value> StackStates( | 26 | std::vector<Ort::Value> StackStates( |
| 33 | const std::vector<std::vector<Ort::Value>> &states) const override; | 27 | const std::vector<std::vector<Ort::Value>> &states) const override; |
| @@ -8,16 +8,16 @@ | @@ -8,16 +8,16 @@ | ||
| 8 | #include <fstream> | 8 | #include <fstream> |
| 9 | #include <sstream> | 9 | #include <sstream> |
| 10 | #include <string> | 10 | #include <string> |
| 11 | +#include <strstream> | ||
| 11 | #include <utility> | 12 | #include <utility> |
| 12 | 13 | ||
| 13 | #if __ANDROID_API__ >= 9 | 14 | #if __ANDROID_API__ >= 9 |
| 14 | -#include <strstream> | ||
| 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> | 18 | +#endif |
| 20 | 19 | ||
| 20 | +#if __OHOS__ | ||
| 21 | #include "rawfile/raw_file_manager.h" | 21 | #include "rawfile/raw_file_manager.h" |
| 22 | #endif | 22 | #endif |
| 23 | 23 |
| @@ -4,6 +4,7 @@ | @@ -4,6 +4,7 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/python/csrc/vad-model.h" | 5 | #include "sherpa-onnx/python/csrc/vad-model.h" |
| 6 | 6 | ||
| 7 | +#include <memory> | ||
| 7 | #include <vector> | 8 | #include <vector> |
| 8 | 9 | ||
| 9 | #include "sherpa-onnx/csrc/vad-model.h" | 10 | #include "sherpa-onnx/csrc/vad-model.h" |
| @@ -13,8 +14,10 @@ namespace sherpa_onnx { | @@ -13,8 +14,10 @@ namespace sherpa_onnx { | ||
| 13 | void PybindVadModel(py::module *m) { | 14 | void PybindVadModel(py::module *m) { |
| 14 | using PyClass = VadModel; | 15 | using PyClass = VadModel; |
| 15 | py::class_<PyClass>(*m, "VadModel") | 16 | py::class_<PyClass>(*m, "VadModel") |
| 16 | - .def_static("create", &PyClass::Create, py::arg("config"), | ||
| 17 | - py::call_guard<py::gil_scoped_release>()) | 17 | + .def_static("create", |
| 18 | + (std::unique_ptr<VadModel>(*)(const VadModelConfig &))( | ||
| 19 | + &PyClass::Create), | ||
| 20 | + py::arg("config"), py::call_guard<py::gil_scoped_release>()) | ||
| 18 | .def("reset", &PyClass::Reset, py::call_guard<py::gil_scoped_release>()) | 21 | .def("reset", &PyClass::Reset, py::call_guard<py::gil_scoped_release>()) |
| 19 | .def( | 22 | .def( |
| 20 | "is_speech", | 23 | "is_speech", |
-
请 注册 或 登录 后发表评论