diff --git a/sherpa-onnx/c-api/c-api.cc b/sherpa-onnx/c-api/c-api.cc index d2091bc56..c9dafe84b 100644 --- a/sherpa-onnx/c-api/c-api.cc +++ b/sherpa-onnx/c-api/c-api.cc @@ -352,27 +352,7 @@ struct SherpaOnnxOfflineStream { : impl(std::move(p)) {} }; -static sherpa_onnx::OfflineRecognizerConfig convertConfig( - const SherpaOnnxOfflineRecognizerConfig *config); - -const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer( - const SherpaOnnxOfflineRecognizerConfig *config) { - sherpa_onnx::OfflineRecognizerConfig recognizer_config = - convertConfig(config); - - if (!recognizer_config.Validate()) { - SHERPA_ONNX_LOGE("Errors in config"); - return nullptr; - } - - SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer; - - recognizer->impl = - std::make_unique(recognizer_config); - - return recognizer; -} -sherpa_onnx::OfflineRecognizerConfig convertConfig( +static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig( const SherpaOnnxOfflineRecognizerConfig *config) { sherpa_onnx::OfflineRecognizerConfig recognizer_config; @@ -491,17 +471,39 @@ sherpa_onnx::OfflineRecognizerConfig convertConfig( recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, ""); if (config->model_config.debug) { +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s", recognizer_config.ToString().c_str()); +#else SHERPA_ONNX_LOGE("%s", recognizer_config.ToString().c_str()); +#endif } return recognizer_config; } +const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer( + const SherpaOnnxOfflineRecognizerConfig *config) { + sherpa_onnx::OfflineRecognizerConfig recognizer_config = + GetOfflineRecognizerConfig(config); + + if (!recognizer_config.Validate()) { + SHERPA_ONNX_LOGE("Errors in config"); + return nullptr; + } + + SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer; + + recognizer->impl = + std::make_unique(recognizer_config); + + return recognizer; +} + void SherpaOnnxOfflineRecognizerSetConfig( const SherpaOnnxOfflineRecognizer *recognizer, const SherpaOnnxOfflineRecognizerConfig *config) { sherpa_onnx::OfflineRecognizerConfig recognizer_config = - convertConfig(config); + GetOfflineRecognizerConfig(config); recognizer->impl->SetConfig(recognizer_config); } @@ -977,25 +979,6 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( return p; } -#ifdef __OHOS__ -SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS( - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds, - NativeResourceManager *mgr) { - if (mgr == nullptr) { - return SherpaOnnxCreateVoiceActivityDetector(config, - buffer_size_in_seconds); - } - - auto vad_config = GetVadModelConfig(config); - - SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector; - p->impl = std::make_unique( - mgr, vad_config, buffer_size_in_seconds); - - return p; -} -#endif - void SherpaOnnxDestroyVoiceActivityDetector( SherpaOnnxVoiceActivityDetector *p) { delete p; @@ -1891,4 +1874,42 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( return ans; } +#ifdef __OHOS__ + +const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS( + const SherpaOnnxOfflineRecognizerConfig *config, + NativeResourceManager *mgr) { + if (mgr == nullptr) { + return SherpaOnnxCreateOfflineRecognizer(config); + } + + sherpa_onnx::OfflineRecognizerConfig recognizer_config = + GetOfflineRecognizerConfig(config); + + SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer; + + recognizer->impl = + std::make_unique(mgr, recognizer_config); + + return recognizer; +} + +SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS( + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds, + NativeResourceManager *mgr) { + if (mgr == nullptr) { + return SherpaOnnxCreateVoiceActivityDetector(config, + buffer_size_in_seconds); + } + + auto vad_config = GetVadModelConfig(config); + + SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector; + p->impl = std::make_unique( + mgr, vad_config, buffer_size_in_seconds); + + return p; +} +#endif + #endif diff --git a/sherpa-onnx/c-api/c-api.h b/sherpa-onnx/c-api/c-api.h index 6ea5c27fb..31e86b8e5 100644 --- a/sherpa-onnx/c-api/c-api.h +++ b/sherpa-onnx/c-api/c-api.h @@ -841,21 +841,6 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector * SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds); -#ifdef __OHOS__ - -// Return an instance of VoiceActivityDetector. -// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free -// the returned pointer to avoid memory leak. -// -// It is for HarmonyOS -typedef struct NativeResourceManager NativeResourceManager; - -SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector * -SherpaOnnxCreateVoiceActivityDetectorOHOS( - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds, - NativeResourceManager *mgr); -#endif - SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector( SherpaOnnxVoiceActivityDetector *p); @@ -1537,6 +1522,29 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg( SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult( const SherpaOnnxOfflineSpeakerDiarizationResult *r); +#ifdef __OHOS__ + +// It is for HarmonyOS +typedef struct NativeResourceManager NativeResourceManager; + +/// @param config Config for the recognizer. +/// @return Return a pointer to the recognizer. The user has to invoke +// SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory +// leak. +SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer * +SherpaOnnxCreateOfflineRecognizerOHOS( + const SherpaOnnxOfflineRecognizerConfig *config, + NativeResourceManager *mgr); + +// Return an instance of VoiceActivityDetector. +// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free +// the returned pointer to avoid memory leak. +SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector * +SherpaOnnxCreateVoiceActivityDetectorOHOS( + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds, + NativeResourceManager *mgr); +#endif + #if defined(__GNUC__) #pragma GCC diagnostic pop #endif diff --git a/sherpa-onnx/c-api/cxx-api.h b/sherpa-onnx/c-api/cxx-api.h index ade52b9aa..2a476efa1 100644 --- a/sherpa-onnx/c-api/cxx-api.h +++ b/sherpa-onnx/c-api/cxx-api.h @@ -214,11 +214,6 @@ struct SHERPA_ONNX_API OfflineTdnnModelConfig { std::string model; }; -struct SHERPA_ONNX_API SherpaOnnxOfflineLMConfig { - std::string model; - float scale = 1.0; -}; - struct SHERPA_ONNX_API OfflineSenseVoiceModelConfig { std::string model; std::string language; diff --git a/sherpa-onnx/csrc/offline-ctc-model.cc b/sherpa-onnx/csrc/offline-ctc-model.cc index 2cbd936ea..daff5654a 100644 --- a/sherpa-onnx/csrc/offline-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-ctc-model.cc @@ -9,6 +9,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h" #include "sherpa-onnx/csrc/offline-tdnn-ctc-model.h" @@ -48,7 +57,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length, if (debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); - SHERPA_ONNX_LOGE("%s", os.str().c_str()); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else + SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; @@ -144,10 +157,9 @@ std::unique_ptr OfflineCtcModel::Create( return nullptr; } -#if __ANDROID_API__ >= 9 - +template std::unique_ptr OfflineCtcModel::Create( - AAssetManager *mgr, const OfflineModelConfig &config) { + Manager *mgr, const OfflineModelConfig &config) { // TODO(fangjun): Refactor it. We don't need to use model_type here ModelType model_type = ModelType::kUnknown; @@ -196,6 +208,15 @@ std::unique_ptr OfflineCtcModel::Create( return nullptr; } + +#if __ANDROID_API__ >= 9 +template std::unique_ptr OfflineCtcModel::Create( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template std::unique_ptr OfflineCtcModel::Create( + NativeResourceManager *mgr, const OfflineModelConfig &config); #endif } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-ctc-model.h b/sherpa-onnx/csrc/offline-ctc-model.h index ead532b48..5ad4fcdcf 100644 --- a/sherpa-onnx/csrc/offline-ctc-model.h +++ b/sherpa-onnx/csrc/offline-ctc-model.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" @@ -25,10 +20,9 @@ class OfflineCtcModel { static std::unique_ptr Create( const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 + template static std::unique_ptr Create( - AAssetManager *mgr, const OfflineModelConfig &config); -#endif + Manager *mgr, const OfflineModelConfig &config); /** Run the forward method of the model. * diff --git a/sherpa-onnx/csrc/offline-lm.cc b/sherpa-onnx/csrc/offline-lm.cc index e199ea5e4..0c42b7ff9 100644 --- a/sherpa-onnx/csrc/offline-lm.cc +++ b/sherpa-onnx/csrc/offline-lm.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/offline-rnn-lm.h" namespace sherpa_onnx { @@ -16,12 +25,11 @@ std::unique_ptr OfflineLM::Create(const OfflineLMConfig &config) { return std::make_unique(config); } -#if __ANDROID_API__ >= 9 -std::unique_ptr OfflineLM::Create(AAssetManager *mgr, +template +std::unique_ptr OfflineLM::Create(Manager *mgr, const OfflineLMConfig &config) { return std::make_unique(mgr, config); } -#endif void OfflineLM::ComputeLMScore(float scale, int32_t context_size, std::vector *hyps) { @@ -75,4 +83,14 @@ void OfflineLM::ComputeLMScore(float scale, int32_t context_size, } } +#if __ANDROID_API__ >= 9 +template std::unique_ptr OfflineLM::Create( + AAssetManager *mgr, const OfflineLMConfig &config); +#endif + +#if __OHOS__ +template std::unique_ptr OfflineLM::Create( + NativeResourceManager *mgr, const OfflineLMConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-lm.h b/sherpa-onnx/csrc/offline-lm.h index 07082c149..a9af82020 100644 --- a/sherpa-onnx/csrc/offline-lm.h +++ b/sherpa-onnx/csrc/offline-lm.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/hypothesis.h" #include "sherpa-onnx/csrc/offline-lm-config.h" @@ -25,10 +20,9 @@ class OfflineLM { static std::unique_ptr Create(const OfflineLMConfig &config); -#if __ANDROID_API__ >= 9 - static std::unique_ptr Create(AAssetManager *mgr, + template + static std::unique_ptr Create(Manager *mgr, const OfflineLMConfig &config); -#endif /** Rescore a batch of sentences. * diff --git a/sherpa-onnx/csrc/offline-moonshine-model.cc b/sherpa-onnx/csrc/offline-moonshine-model.cc index bf9624d4d..dbd18a92d 100644 --- a/sherpa-onnx/csrc/offline-moonshine-model.cc +++ b/sherpa-onnx/csrc/offline-moonshine-model.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -43,8 +52,8 @@ class OfflineMoonshineModel::Impl { } } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -69,7 +78,6 @@ class OfflineMoonshineModel::Impl { InitCachedDecoder(buf.data(), buf.size()); } } -#endif Ort::Value ForwardPreprocessor(Ort::Value audio) { auto features = preprocessor_sess_->Run( @@ -242,11 +250,10 @@ class OfflineMoonshineModel::Impl { OfflineMoonshineModel::OfflineMoonshineModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineMoonshineModel::OfflineMoonshineModel(AAssetManager *mgr, +template +OfflineMoonshineModel::OfflineMoonshineModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineMoonshineModel::~OfflineMoonshineModel() = default; @@ -279,4 +286,14 @@ OrtAllocator *OfflineMoonshineModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineMoonshineModel::OfflineMoonshineModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineMoonshineModel::OfflineMoonshineModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-moonshine-model.h b/sherpa-onnx/csrc/offline-moonshine-model.h index 7065b1445..a8f9b408d 100644 --- a/sherpa-onnx/csrc/offline-moonshine-model.h +++ b/sherpa-onnx/csrc/offline-moonshine-model.h @@ -9,11 +9,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" @@ -25,9 +20,8 @@ class OfflineMoonshineModel { public: explicit OfflineMoonshineModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineMoonshineModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineMoonshineModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineMoonshineModel(); diff --git a/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.cc b/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.cc index 14dc7dbe4..18db415b4 100644 --- a/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.cc @@ -4,6 +4,15 @@ #include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h" +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -23,8 +32,8 @@ class OfflineNemoEncDecCtcModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -32,7 +41,6 @@ class OfflineNemoEncDecCtcModel::Impl { auto buf = ReadFile(mgr, config_.nemo_ctc.model); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features, Ort::Value features_length) { @@ -88,7 +96,11 @@ class OfflineNemoEncDecCtcModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -126,11 +138,10 @@ OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 +template OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( - AAssetManager *mgr, const OfflineModelConfig &config) + Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineNemoEncDecCtcModel::~OfflineNemoEncDecCtcModel() = default; @@ -156,4 +167,14 @@ std::string OfflineNemoEncDecCtcModel::FeatureNormalizationMethod() const { bool OfflineNemoEncDecCtcModel::IsGigaAM() const { return impl_->IsGigaAM(); } +#if __ANDROID_API__ >= 9 +template OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineNemoEncDecCtcModel::OfflineNemoEncDecCtcModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h b/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h index c961e694e..08ff068c1 100644 --- a/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h +++ b/sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-ctc-model.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -29,10 +24,8 @@ class OfflineNemoEncDecCtcModel : public OfflineCtcModel { public: explicit OfflineNemoEncDecCtcModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineNemoEncDecCtcModel(AAssetManager *mgr, - const OfflineModelConfig &config); -#endif + template + OfflineNemoEncDecCtcModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineNemoEncDecCtcModel() override; diff --git a/sherpa-onnx/csrc/offline-paraformer-model.cc b/sherpa-onnx/csrc/offline-paraformer-model.cc index 9c61cb350..c8c65c8c9 100644 --- a/sherpa-onnx/csrc/offline-paraformer-model.cc +++ b/sherpa-onnx/csrc/offline-paraformer-model.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -26,8 +35,8 @@ class OfflineParaformerModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -35,7 +44,6 @@ class OfflineParaformerModel::Impl { auto buf = ReadFile(mgr, config_.paraformer.model); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features, Ort::Value features_length) { @@ -72,7 +80,11 @@ class OfflineParaformerModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -109,11 +121,10 @@ class OfflineParaformerModel::Impl { OfflineParaformerModel::OfflineParaformerModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineParaformerModel::OfflineParaformerModel(AAssetManager *mgr, +template +OfflineParaformerModel::OfflineParaformerModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineParaformerModel::~OfflineParaformerModel() = default; @@ -141,4 +152,14 @@ OrtAllocator *OfflineParaformerModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineParaformerModel::OfflineParaformerModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineParaformerModel::OfflineParaformerModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-paraformer-model.h b/sherpa-onnx/csrc/offline-paraformer-model.h index d5c2329f6..219f80796 100644 --- a/sherpa-onnx/csrc/offline-paraformer-model.h +++ b/sherpa-onnx/csrc/offline-paraformer-model.h @@ -7,11 +7,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" @@ -21,9 +16,8 @@ class OfflineParaformerModel { public: explicit OfflineParaformerModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineParaformerModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineParaformerModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineParaformerModel(); diff --git a/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h b/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h index 1199ff109..2721ecdf3 100644 --- a/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h @@ -12,11 +12,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/offline-ctc-decoder.h" #include "sherpa-onnx/csrc/offline-ctc-fst-decoder.h" #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h" @@ -80,16 +75,14 @@ class OfflineRecognizerCtcImpl : public OfflineRecognizerImpl { Init(); } -#if __ANDROID_API__ >= 9 - OfflineRecognizerCtcImpl(AAssetManager *mgr, - const OfflineRecognizerConfig &config) + template + OfflineRecognizerCtcImpl(Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), symbol_table_(mgr, config_.model_config.tokens), model_(OfflineCtcModel::Create(mgr, config_.model_config)) { Init(); } -#endif void Init() { if (!config_.model_config.telespeech_ctc.empty()) { diff --git a/sherpa-onnx/csrc/offline-recognizer-impl.cc b/sherpa-onnx/csrc/offline-recognizer-impl.cc index 99c41d307..2a7a8dab9 100644 --- a/sherpa-onnx/csrc/offline-recognizer-impl.cc +++ b/sherpa-onnx/csrc/offline-recognizer-impl.cc @@ -13,6 +13,10 @@ #include "android/asset_manager.h" #include "android/asset_manager_jni.h" +#elif __OHOS__ +#include + +#include "rawfile/raw_file_manager.h" #endif #include "fst/extensions/far/far.h" @@ -211,9 +215,9 @@ std::unique_ptr OfflineRecognizerImpl::Create( exit(-1); } -#if __ANDROID_API__ >= 9 +template std::unique_ptr OfflineRecognizerImpl::Create( - AAssetManager *mgr, const OfflineRecognizerConfig &config) { + Manager *mgr, const OfflineRecognizerConfig &config) { if (!config.model_config.sense_voice.model.empty()) { return std::make_unique(mgr, config); } @@ -389,7 +393,6 @@ std::unique_ptr OfflineRecognizerImpl::Create( exit(-1); } -#endif OfflineRecognizerImpl::OfflineRecognizerImpl( const OfflineRecognizerConfig &config) @@ -436,9 +439,9 @@ OfflineRecognizerImpl::OfflineRecognizerImpl( } } -#if __ANDROID_API__ >= 9 +template OfflineRecognizerImpl::OfflineRecognizerImpl( - AAssetManager *mgr, const OfflineRecognizerConfig &config) + Manager *mgr, const OfflineRecognizerConfig &config) : config_(config) { if (!config.rule_fsts.empty()) { std::vector files; @@ -482,7 +485,6 @@ OfflineRecognizerImpl::OfflineRecognizerImpl( } // for (const auto &f : files) } // if (!config.rule_fars.empty()) } -#endif std::string OfflineRecognizerImpl::ApplyInverseTextNormalization( std::string text) const { @@ -499,4 +501,19 @@ void OfflineRecognizerImpl::SetConfig(const OfflineRecognizerConfig &config) { config_ = config; } +#if __ANDROID_API__ >= 9 +template OfflineRecognizerImpl::OfflineRecognizerImpl( + AAssetManager *mgr, const OfflineRecognizerConfig &config); + +template std::unique_ptr OfflineRecognizerImpl::Create( + AAssetManager *mgr, const OfflineRecognizerConfig &config); +#endif + +#if __OHOS__ +template OfflineRecognizerImpl::OfflineRecognizerImpl( + NativeResourceManager *mgr, const OfflineRecognizerConfig &config); +template std::unique_ptr OfflineRecognizerImpl::Create( + NativeResourceManager *mgr, const OfflineRecognizerConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-recognizer-impl.h b/sherpa-onnx/csrc/offline-recognizer-impl.h index 32010bf70..8a6e6fcce 100644 --- a/sherpa-onnx/csrc/offline-recognizer-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-impl.h @@ -9,11 +9,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "kaldifst/csrc/text-normalizer.h" #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-recognizer.h" @@ -28,13 +23,12 @@ class OfflineRecognizerImpl { static std::unique_ptr Create( const OfflineRecognizerConfig &config); -#if __ANDROID_API__ >= 9 - OfflineRecognizerImpl(AAssetManager *mgr, - const OfflineRecognizerConfig &config); + template + OfflineRecognizerImpl(Manager *mgr, const OfflineRecognizerConfig &config); + template static std::unique_ptr Create( - AAssetManager *mgr, const OfflineRecognizerConfig &config); -#endif + Manager *mgr, const OfflineRecognizerConfig &config); virtual ~OfflineRecognizerImpl() = default; diff --git a/sherpa-onnx/csrc/offline-recognizer-moonshine-impl.h b/sherpa-onnx/csrc/offline-recognizer-moonshine-impl.h index 7d52a41b2..deec9852d 100644 --- a/sherpa-onnx/csrc/offline-recognizer-moonshine-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-moonshine-impl.h @@ -12,11 +12,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/offline-moonshine-decoder.h" #include "sherpa-onnx/csrc/offline-moonshine-greedy-search-decoder.h" @@ -59,8 +54,8 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl { Init(); } -#if __ANDROID_API__ >= 9 - OfflineRecognizerMoonshineImpl(AAssetManager *mgr, + template + OfflineRecognizerMoonshineImpl(Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), @@ -70,8 +65,6 @@ class OfflineRecognizerMoonshineImpl : public OfflineRecognizerImpl { Init(); } -#endif - void Init() { if (config_.decoding_method == "greedy_search") { decoder_ = diff --git a/sherpa-onnx/csrc/offline-recognizer-paraformer-impl.h b/sherpa-onnx/csrc/offline-recognizer-paraformer-impl.h index 525c92cc2..5b80c99e3 100644 --- a/sherpa-onnx/csrc/offline-recognizer-paraformer-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-paraformer-impl.h @@ -11,11 +11,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/offline-paraformer-decoder.h" #include "sherpa-onnx/csrc/offline-paraformer-greedy-search-decoder.h" @@ -105,8 +100,8 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl { InitFeatConfig(); } -#if __ANDROID_API__ >= 9 - OfflineRecognizerParaformerImpl(AAssetManager *mgr, + template + OfflineRecognizerParaformerImpl(Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), @@ -124,7 +119,6 @@ class OfflineRecognizerParaformerImpl : public OfflineRecognizerImpl { InitFeatConfig(); } -#endif std::unique_ptr CreateStream() const override { return std::make_unique(config_.feat_config); diff --git a/sherpa-onnx/csrc/offline-recognizer-sense-voice-impl.h b/sherpa-onnx/csrc/offline-recognizer-sense-voice-impl.h index 6cebf23c7..7ee5e41c0 100644 --- a/sherpa-onnx/csrc/offline-recognizer-sense-voice-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-sense-voice-impl.h @@ -11,11 +11,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/offline-ctc-greedy-search-decoder.h" #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/offline-recognizer-impl.h" @@ -83,8 +78,8 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl { InitFeatConfig(); } -#if __ANDROID_API__ >= 9 - OfflineRecognizerSenseVoiceImpl(AAssetManager *mgr, + template + OfflineRecognizerSenseVoiceImpl(Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), @@ -103,7 +98,6 @@ class OfflineRecognizerSenseVoiceImpl : public OfflineRecognizerImpl { InitFeatConfig(); } -#endif std::unique_ptr CreateStream() const override { return std::make_unique(config_.feat_config); diff --git a/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h b/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h index 05759ac5b..64f3798fa 100644 --- a/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h @@ -14,11 +14,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/context-graph.h" #include "sherpa-onnx/csrc/log.h" #include "sherpa-onnx/csrc/macros.h" @@ -109,9 +104,9 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { } } -#if __ANDROID_API__ >= 9 + template explicit OfflineRecognizerTransducerImpl( - AAssetManager *mgr, const OfflineRecognizerConfig &config) + Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), symbol_table_(mgr, config_.model_config.tokens), @@ -148,7 +143,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { exit(-1); } } -#endif std::unique_ptr CreateStream( const std::string &hotwords) const override { @@ -246,10 +240,7 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { } } - OfflineRecognizerConfig GetConfig() const override { - return config_; - } - + OfflineRecognizerConfig GetConfig() const override { return config_; } void InitHotwords() { // each line in hotwords_file contains space-separated words @@ -271,8 +262,8 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { hotwords_, config_.hotwords_score, boost_scores_); } -#if __ANDROID_API__ >= 9 - void InitHotwords(AAssetManager *mgr) { + template + void InitHotwords(Manager *mgr) { // each line in hotwords_file contains space-separated words auto buf = ReadFile(mgr, config_.hotwords_file); @@ -294,7 +285,6 @@ class OfflineRecognizerTransducerImpl : public OfflineRecognizerImpl { hotwords_graph_ = std::make_shared( hotwords_, config_.hotwords_score, boost_scores_); } -#endif private: OfflineRecognizerConfig config_; diff --git a/sherpa-onnx/csrc/offline-recognizer-transducer-nemo-impl.h b/sherpa-onnx/csrc/offline-recognizer-transducer-nemo-impl.h index 6727b0983..167d1021e 100644 --- a/sherpa-onnx/csrc/offline-recognizer-transducer-nemo-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-transducer-nemo-impl.h @@ -14,11 +14,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-recognizer-impl.h" #include "sherpa-onnx/csrc/offline-recognizer.h" @@ -57,9 +52,9 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl { PostInit(); } -#if __ANDROID_API__ >= 9 + template explicit OfflineRecognizerTransducerNeMoImpl( - AAssetManager *mgr, const OfflineRecognizerConfig &config) + Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), symbol_table_(mgr, config_.model_config.tokens), @@ -76,7 +71,6 @@ class OfflineRecognizerTransducerNeMoImpl : public OfflineRecognizerImpl { PostInit(); } -#endif std::unique_ptr CreateStream() const override { return std::make_unique(config_.feat_config); diff --git a/sherpa-onnx/csrc/offline-recognizer-whisper-impl.h b/sherpa-onnx/csrc/offline-recognizer-whisper-impl.h index 023700e77..fc6cc74ed 100644 --- a/sherpa-onnx/csrc/offline-recognizer-whisper-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-whisper-impl.h @@ -12,11 +12,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/offline-recognizer-impl.h" #include "sherpa-onnx/csrc/offline-recognizer.h" @@ -60,8 +55,8 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { Init(); } -#if __ANDROID_API__ >= 9 - OfflineRecognizerWhisperImpl(AAssetManager *mgr, + template + OfflineRecognizerWhisperImpl(Manager *mgr, const OfflineRecognizerConfig &config) : OfflineRecognizerImpl(mgr, config), config_(config), @@ -71,8 +66,6 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { Init(); } -#endif - void Init() { // tokens.txt from whisper is base64 encoded, so we need to decode it symbol_table_.ApplyBase64Decode(); @@ -105,9 +98,7 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl { config_.model_config.whisper = config.model_config.whisper; } - OfflineRecognizerConfig GetConfig() const override { - return config_; - } + OfflineRecognizerConfig GetConfig() const override { return config_; } private: void DecodeStream(OfflineStream *s) const { diff --git a/sherpa-onnx/csrc/offline-recognizer.cc b/sherpa-onnx/csrc/offline-recognizer.cc index f73e35ad6..80bc20900 100644 --- a/sherpa-onnx/csrc/offline-recognizer.cc +++ b/sherpa-onnx/csrc/offline-recognizer.cc @@ -6,11 +6,21 @@ #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/file-utils.h" #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-lm-config.h" #include "sherpa-onnx/csrc/offline-recognizer-impl.h" #include "sherpa-onnx/csrc/text-utils.h" + namespace sherpa_onnx { void OfflineRecognizerConfig::Register(ParseOptions *po) { @@ -132,11 +142,10 @@ std::string OfflineRecognizerConfig::ToString() const { return os.str(); } -#if __ANDROID_API__ >= 9 -OfflineRecognizer::OfflineRecognizer(AAssetManager *mgr, +template +OfflineRecognizer::OfflineRecognizer(Manager *mgr, const OfflineRecognizerConfig &config) : impl_(OfflineRecognizerImpl::Create(mgr, config)) {} -#endif OfflineRecognizer::OfflineRecognizer(const OfflineRecognizerConfig &config) : impl_(OfflineRecognizerImpl::Create(config)) {} @@ -157,11 +166,21 @@ void OfflineRecognizer::DecodeStreams(OfflineStream **ss, int32_t n) const { } void OfflineRecognizer::SetConfig(const OfflineRecognizerConfig &config) { - impl_->SetConfig(config); + impl_->SetConfig(config); } OfflineRecognizerConfig OfflineRecognizer::GetConfig() const { return impl_->GetConfig(); } +#if __ANDROID_API__ >= 9 +template OfflineRecognizer::OfflineRecognizer( + AAssetManager *mgr, const OfflineRecognizerConfig &config); +#endif + +#if __OHOS__ +template OfflineRecognizer::OfflineRecognizer( + NativeResourceManager *mgr, const OfflineRecognizerConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-recognizer.h b/sherpa-onnx/csrc/offline-recognizer.h index 8f0b47a08..3c78ea9b6 100644 --- a/sherpa-onnx/csrc/offline-recognizer.h +++ b/sherpa-onnx/csrc/offline-recognizer.h @@ -9,11 +9,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "sherpa-onnx/csrc/features.h" #include "sherpa-onnx/csrc/offline-ctc-fst-decoder-config.h" #include "sherpa-onnx/csrc/offline-lm-config.h" @@ -82,9 +77,8 @@ class OfflineRecognizer { public: ~OfflineRecognizer(); -#if __ANDROID_API__ >= 9 - OfflineRecognizer(AAssetManager *mgr, const OfflineRecognizerConfig &config); -#endif + template + OfflineRecognizer(Manager *mgr, const OfflineRecognizerConfig &config); explicit OfflineRecognizer(const OfflineRecognizerConfig &config); @@ -120,10 +114,10 @@ class OfflineRecognizer { void DecodeStreams(OfflineStream **ss, int32_t n) const; /** Onnxruntime Session objects are not affected by this method. - * The exact behavior can be defined by a specific recognizer impl. - * For instance, for the whisper recognizer, you can retrieve the language and task from - * the config and ignore any remaining fields in `config`. - */ + * The exact behavior can be defined by a specific recognizer impl. + * For instance, for the whisper recognizer, you can retrieve the language and + * task from the config and ignore any remaining fields in `config`. + */ void SetConfig(const OfflineRecognizerConfig &config); OfflineRecognizerConfig GetConfig() const; diff --git a/sherpa-onnx/csrc/offline-rnn-lm.cc b/sherpa-onnx/csrc/offline-rnn-lm.cc index 70cd0ad47..665b775ba 100644 --- a/sherpa-onnx/csrc/offline-rnn-lm.cc +++ b/sherpa-onnx/csrc/offline-rnn-lm.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" @@ -27,8 +36,8 @@ class OfflineRnnLM::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineLMConfig &config) + template + Impl(Manager *mgr, const OfflineLMConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_{GetSessionOptions(config)}, @@ -36,7 +45,6 @@ class OfflineRnnLM::Impl { auto buf = ReadFile(mgr, config_.model); Init(buf.data(), buf.size()); } -#endif Ort::Value Rescore(Ort::Value x, Ort::Value x_lens) { std::array inputs = {std::move(x), std::move(x_lens)}; @@ -76,10 +84,9 @@ class OfflineRnnLM::Impl { OfflineRnnLM::OfflineRnnLM(const OfflineLMConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineRnnLM::OfflineRnnLM(AAssetManager *mgr, const OfflineLMConfig &config) +template +OfflineRnnLM::OfflineRnnLM(Manager *mgr, const OfflineLMConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineRnnLM::~OfflineRnnLM() = default; @@ -87,4 +94,14 @@ Ort::Value OfflineRnnLM::Rescore(Ort::Value x, Ort::Value x_lens) { return impl_->Rescore(std::move(x), std::move(x_lens)); } +#if __ANDROID_API__ >= 9 +template OfflineRnnLM::OfflineRnnLM(AAssetManager *mgr, + const OfflineLMConfig &config); +#endif + +#if __OHOS__ +template OfflineRnnLM::OfflineRnnLM(NativeResourceManager *mgr, + const OfflineLMConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-rnn-lm.h b/sherpa-onnx/csrc/offline-rnn-lm.h index f4dd904f7..2d16a19e9 100644 --- a/sherpa-onnx/csrc/offline-rnn-lm.h +++ b/sherpa-onnx/csrc/offline-rnn-lm.h @@ -7,11 +7,6 @@ #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-lm-config.h" #include "sherpa-onnx/csrc/offline-lm.h" @@ -24,9 +19,8 @@ class OfflineRnnLM : public OfflineLM { explicit OfflineRnnLM(const OfflineLMConfig &config); -#if __ANDROID_API__ >= 9 - OfflineRnnLM(AAssetManager *mgr, const OfflineLMConfig &config); -#endif + template + OfflineRnnLM(Manager *mgr, const OfflineLMConfig &config); /** Rescore a batch of sentences. * diff --git a/sherpa-onnx/csrc/offline-sense-voice-model.cc b/sherpa-onnx/csrc/offline-sense-voice-model.cc index a914ccf4a..04e7cd224 100644 --- a/sherpa-onnx/csrc/offline-sense-voice-model.cc +++ b/sherpa-onnx/csrc/offline-sense-voice-model.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -26,8 +35,8 @@ class OfflineSenseVoiceModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -35,7 +44,6 @@ class OfflineSenseVoiceModel::Impl { auto buf = ReadFile(mgr, config_.sense_voice.model); Init(buf.data(), buf.size()); } -#endif Ort::Value Forward(Ort::Value features, Ort::Value features_length, Ort::Value language, Ort::Value text_norm) { @@ -72,7 +80,11 @@ class OfflineSenseVoiceModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -129,11 +141,10 @@ class OfflineSenseVoiceModel::Impl { OfflineSenseVoiceModel::OfflineSenseVoiceModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineSenseVoiceModel::OfflineSenseVoiceModel(AAssetManager *mgr, +template +OfflineSenseVoiceModel::OfflineSenseVoiceModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineSenseVoiceModel::~OfflineSenseVoiceModel() = default; @@ -154,4 +165,14 @@ OrtAllocator *OfflineSenseVoiceModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineSenseVoiceModel::OfflineSenseVoiceModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineSenseVoiceModel::OfflineSenseVoiceModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-sense-voice-model.h b/sherpa-onnx/csrc/offline-sense-voice-model.h index 29d31b286..e82680c56 100644 --- a/sherpa-onnx/csrc/offline-sense-voice-model.h +++ b/sherpa-onnx/csrc/offline-sense-voice-model.h @@ -7,11 +7,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/offline-sense-voice-model-meta-data.h" @@ -22,9 +17,8 @@ class OfflineSenseVoiceModel { public: explicit OfflineSenseVoiceModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineSenseVoiceModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineSenseVoiceModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineSenseVoiceModel(); diff --git a/sherpa-onnx/csrc/offline-tdnn-ctc-model.cc b/sherpa-onnx/csrc/offline-tdnn-ctc-model.cc index d7db0040c..de441c481 100644 --- a/sherpa-onnx/csrc/offline-tdnn-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-tdnn-ctc-model.cc @@ -6,6 +6,15 @@ #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -25,8 +34,8 @@ class OfflineTdnnCtcModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -34,7 +43,6 @@ class OfflineTdnnCtcModel::Impl { auto buf = ReadFile(mgr, config_.tdnn.model); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features) { auto nnet_out = @@ -79,7 +87,11 @@ class OfflineTdnnCtcModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -106,11 +118,10 @@ class OfflineTdnnCtcModel::Impl { OfflineTdnnCtcModel::OfflineTdnnCtcModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineTdnnCtcModel::OfflineTdnnCtcModel(AAssetManager *mgr, +template +OfflineTdnnCtcModel::OfflineTdnnCtcModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineTdnnCtcModel::~OfflineTdnnCtcModel() = default; @@ -125,4 +136,14 @@ OrtAllocator *OfflineTdnnCtcModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineTdnnCtcModel::OfflineTdnnCtcModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineTdnnCtcModel::OfflineTdnnCtcModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-tdnn-ctc-model.h b/sherpa-onnx/csrc/offline-tdnn-ctc-model.h index b6b5c7e59..0c2e43afe 100644 --- a/sherpa-onnx/csrc/offline-tdnn-ctc-model.h +++ b/sherpa-onnx/csrc/offline-tdnn-ctc-model.h @@ -7,11 +7,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-ctc-model.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -27,9 +22,8 @@ class OfflineTdnnCtcModel : public OfflineCtcModel { public: explicit OfflineTdnnCtcModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineTdnnCtcModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineTdnnCtcModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineTdnnCtcModel() override; diff --git a/sherpa-onnx/csrc/offline-telespeech-ctc-model.cc b/sherpa-onnx/csrc/offline-telespeech-ctc-model.cc index aeb918cd3..d87e47a0d 100644 --- a/sherpa-onnx/csrc/offline-telespeech-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-telespeech-ctc-model.cc @@ -4,6 +4,15 @@ #include "sherpa-onnx/csrc/offline-telespeech-ctc-model.h" +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -23,8 +32,8 @@ class OfflineTeleSpeechCtcModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -32,7 +41,6 @@ class OfflineTeleSpeechCtcModel::Impl { auto buf = ReadFile(mgr, config_.telespeech_ctc); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features, Ort::Value /*features_length*/) { @@ -85,7 +93,11 @@ class OfflineTeleSpeechCtcModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } { @@ -117,11 +129,10 @@ OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 +template OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( - AAssetManager *mgr, const OfflineModelConfig &config) + Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineTeleSpeechCtcModel::~OfflineTeleSpeechCtcModel() = default; @@ -141,4 +152,14 @@ OrtAllocator *OfflineTeleSpeechCtcModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineTeleSpeechCtcModel::OfflineTeleSpeechCtcModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-telespeech-ctc-model.h b/sherpa-onnx/csrc/offline-telespeech-ctc-model.h index 42ef300ff..3fe701a02 100644 --- a/sherpa-onnx/csrc/offline-telespeech-ctc-model.h +++ b/sherpa-onnx/csrc/offline-telespeech-ctc-model.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-ctc-model.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -31,10 +26,8 @@ class OfflineTeleSpeechCtcModel : public OfflineCtcModel { public: explicit OfflineTeleSpeechCtcModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineTeleSpeechCtcModel(AAssetManager *mgr, - const OfflineModelConfig &config); -#endif + template + OfflineTeleSpeechCtcModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineTeleSpeechCtcModel() override; diff --git a/sherpa-onnx/csrc/offline-transducer-model.cc b/sherpa-onnx/csrc/offline-transducer-model.cc index 910ae3475..da519cc2f 100644 --- a/sherpa-onnx/csrc/offline-transducer-model.cc +++ b/sherpa-onnx/csrc/offline-transducer-model.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-transducer-decoder.h" #include "sherpa-onnx/csrc/onnx-utils.h" @@ -38,8 +47,8 @@ class OfflineTransducerModel::Impl { } } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -59,7 +68,6 @@ class OfflineTransducerModel::Impl { InitJoiner(buf.data(), buf.size()); } } -#endif std::pair RunEncoder(Ort::Value features, Ort::Value features_length) { @@ -161,7 +169,11 @@ class OfflineTransducerModel::Impl { std::ostringstream os; os << "---encoder---\n"; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } } @@ -244,11 +256,10 @@ class OfflineTransducerModel::Impl { OfflineTransducerModel::OfflineTransducerModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineTransducerModel::OfflineTransducerModel(AAssetManager *mgr, +template +OfflineTransducerModel::OfflineTransducerModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineTransducerModel::~OfflineTransducerModel() = default; @@ -291,4 +302,14 @@ Ort::Value OfflineTransducerModel::BuildDecoderInput( return impl_->BuildDecoderInput(results, end_index); } +#if __ANDROID_API__ >= 9 +template OfflineTransducerModel::OfflineTransducerModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineTransducerModel::OfflineTransducerModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-transducer-model.h b/sherpa-onnx/csrc/offline-transducer-model.h index 31a238cb7..e8ca93a3c 100644 --- a/sherpa-onnx/csrc/offline-transducer-model.h +++ b/sherpa-onnx/csrc/offline-transducer-model.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/hypothesis.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -25,9 +20,8 @@ class OfflineTransducerModel { public: explicit OfflineTransducerModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineTransducerModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineTransducerModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineTransducerModel(); diff --git a/sherpa-onnx/csrc/offline-transducer-nemo-model.cc b/sherpa-onnx/csrc/offline-transducer-nemo-model.cc index 7dd5d31b8..bd6f1ab57 100644 --- a/sherpa-onnx/csrc/offline-transducer-nemo-model.cc +++ b/sherpa-onnx/csrc/offline-transducer-nemo-model.cc @@ -9,6 +9,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-transducer-decoder.h" #include "sherpa-onnx/csrc/onnx-utils.h" @@ -40,8 +49,8 @@ class OfflineTransducerNeMoModel::Impl { } } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -61,7 +70,6 @@ class OfflineTransducerNeMoModel::Impl { InitJoiner(buf.data(), buf.size()); } } -#endif std::vector RunEncoder(Ort::Value features, Ort::Value features_length) { @@ -172,7 +180,11 @@ class OfflineTransducerNeMoModel::Impl { std::ostringstream os; os << "---encoder---\n"; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -256,11 +268,10 @@ OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 +template OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( - AAssetManager *mgr, const OfflineModelConfig &config) + Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineTransducerNeMoModel::~OfflineTransducerNeMoModel() = default; @@ -305,4 +316,14 @@ std::string OfflineTransducerNeMoModel::FeatureNormalizationMethod() const { bool OfflineTransducerNeMoModel::IsGigaAM() const { return impl_->IsGigaAM(); } +#if __ANDROID_API__ >= 9 +template OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineTransducerNeMoModel::OfflineTransducerNeMoModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-transducer-nemo-model.h b/sherpa-onnx/csrc/offline-transducer-nemo-model.h index e4017a4c4..697f749e2 100644 --- a/sherpa-onnx/csrc/offline-transducer-nemo-model.h +++ b/sherpa-onnx/csrc/offline-transducer-nemo-model.h @@ -9,11 +9,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" @@ -26,10 +21,8 @@ class OfflineTransducerNeMoModel { public: explicit OfflineTransducerNeMoModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineTransducerNeMoModel(AAssetManager *mgr, - const OfflineModelConfig &config); -#endif + template + OfflineTransducerNeMoModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineTransducerNeMoModel(); diff --git a/sherpa-onnx/csrc/offline-wenet-ctc-model.cc b/sherpa-onnx/csrc/offline-wenet-ctc-model.cc index d696aa1c7..5a9397178 100644 --- a/sherpa-onnx/csrc/offline-wenet-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-wenet-ctc-model.cc @@ -4,6 +4,15 @@ #include "sherpa-onnx/csrc/offline-wenet-ctc-model.h" +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -23,8 +32,8 @@ class OfflineWenetCtcModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -32,7 +41,6 @@ class OfflineWenetCtcModel::Impl { auto buf = ReadFile(mgr, config_.wenet_ctc.model); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features, Ort::Value features_length) { @@ -63,7 +71,11 @@ class OfflineWenetCtcModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -92,11 +104,10 @@ class OfflineWenetCtcModel::Impl { OfflineWenetCtcModel::OfflineWenetCtcModel(const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineWenetCtcModel::OfflineWenetCtcModel(AAssetManager *mgr, +template +OfflineWenetCtcModel::OfflineWenetCtcModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineWenetCtcModel::~OfflineWenetCtcModel() = default; @@ -115,4 +126,14 @@ OrtAllocator *OfflineWenetCtcModel::Allocator() const { return impl_->Allocator(); } +#if __ANDROID_API__ >= 9 +template OfflineWenetCtcModel::OfflineWenetCtcModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineWenetCtcModel::OfflineWenetCtcModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-wenet-ctc-model.h b/sherpa-onnx/csrc/offline-wenet-ctc-model.h index 4eb78b73a..ba5f21246 100644 --- a/sherpa-onnx/csrc/offline-wenet-ctc-model.h +++ b/sherpa-onnx/csrc/offline-wenet-ctc-model.h @@ -8,11 +8,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-ctc-model.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -31,9 +26,8 @@ class OfflineWenetCtcModel : public OfflineCtcModel { public: explicit OfflineWenetCtcModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineWenetCtcModel(AAssetManager *mgr, const OfflineModelConfig &config); -#endif + template + OfflineWenetCtcModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineWenetCtcModel() override; diff --git a/sherpa-onnx/csrc/offline-whisper-model.cc b/sherpa-onnx/csrc/offline-whisper-model.cc index 0747a329b..360374cdb 100644 --- a/sherpa-onnx/csrc/offline-whisper-model.cc +++ b/sherpa-onnx/csrc/offline-whisper-model.cc @@ -11,6 +11,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -52,8 +61,8 @@ class OfflineWhisperModel::Impl { } } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -69,7 +78,8 @@ class OfflineWhisperModel::Impl { } } - Impl(AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config) + template + Impl(Manager *mgr, const SpokenLanguageIdentificationConfig &config) : lid_config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -84,7 +94,6 @@ class OfflineWhisperModel::Impl { InitDecoder(buf.data(), buf.size()); } } -#endif std::pair ForwardEncoder(Ort::Value features) { auto encoder_out = encoder_sess_->Run( @@ -237,7 +246,11 @@ class OfflineWhisperModel::Impl { std::ostringstream os; os << "---encoder---\n"; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } Ort::AllocatorWithDefaultOptions allocator; // used in the macro below @@ -338,17 +351,16 @@ OfflineWhisperModel::OfflineWhisperModel( const SpokenLanguageIdentificationConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -OfflineWhisperModel::OfflineWhisperModel(AAssetManager *mgr, +template +OfflineWhisperModel::OfflineWhisperModel(Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} +template OfflineWhisperModel::OfflineWhisperModel( - AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config) + Manager *mgr, const SpokenLanguageIdentificationConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif - OfflineWhisperModel::~OfflineWhisperModel() = default; std::pair OfflineWhisperModel::ForwardEncoder( @@ -453,4 +465,21 @@ void OfflineWhisperModel::NormalizeFeatures(float *features, int32_t num_frames, } } +#if __ANDROID_API__ >= 9 +template OfflineWhisperModel::OfflineWhisperModel( + AAssetManager *mgr, const OfflineModelConfig &config); + +template OfflineWhisperModel::OfflineWhisperModel( + AAssetManager *mgr, const SpokenLanguageIdentificationConfig &config); +#endif + +#if __OHOS__ +template OfflineWhisperModel::OfflineWhisperModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); + +template OfflineWhisperModel::OfflineWhisperModel( + NativeResourceManager *mgr, + const SpokenLanguageIdentificationConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-whisper-model.h b/sherpa-onnx/csrc/offline-whisper-model.h index 866714bc5..60d76928c 100644 --- a/sherpa-onnx/csrc/offline-whisper-model.h +++ b/sherpa-onnx/csrc/offline-whisper-model.h @@ -11,11 +11,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-model-config.h" #include "sherpa-onnx/csrc/spoken-language-identification.h" @@ -29,11 +24,12 @@ class OfflineWhisperModel { explicit OfflineWhisperModel( const SpokenLanguageIdentificationConfig &config); -#if __ANDROID_API__ >= 9 - OfflineWhisperModel(AAssetManager *mgr, const OfflineModelConfig &config); - OfflineWhisperModel(AAssetManager *mgr, + template + OfflineWhisperModel(Manager *mgr, const OfflineModelConfig &config); + + template + OfflineWhisperModel(Manager *mgr, const SpokenLanguageIdentificationConfig &config); -#endif ~OfflineWhisperModel(); diff --git a/sherpa-onnx/csrc/offline-zipformer-ctc-model.cc b/sherpa-onnx/csrc/offline-zipformer-ctc-model.cc index a783ce506..8cfa30c26 100644 --- a/sherpa-onnx/csrc/offline-zipformer-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-zipformer-ctc-model.cc @@ -6,6 +6,15 @@ #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -25,8 +34,8 @@ class OfflineZipformerCtcModel::Impl { Init(buf.data(), buf.size()); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const OfflineModelConfig &config) + template + Impl(Manager *mgr, const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -34,7 +43,6 @@ class OfflineZipformerCtcModel::Impl { auto buf = ReadFile(mgr, config_.zipformer_ctc.model); Init(buf.data(), buf.size()); } -#endif std::vector Forward(Ort::Value features, Ort::Value features_length) { @@ -64,7 +72,11 @@ class OfflineZipformerCtcModel::Impl { if (config_.debug) { std::ostringstream os; PrintModelMetadata(os, meta_data); +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str()); +#else SHERPA_ONNX_LOGE("%s\n", os.str().c_str()); +#endif } // get vocab size from the output[0].shape, which is (N, T, vocab_size) @@ -93,11 +105,10 @@ OfflineZipformerCtcModel::OfflineZipformerCtcModel( const OfflineModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 +template OfflineZipformerCtcModel::OfflineZipformerCtcModel( - AAssetManager *mgr, const OfflineModelConfig &config) + Manager *mgr, const OfflineModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif OfflineZipformerCtcModel::~OfflineZipformerCtcModel() = default; @@ -118,4 +129,14 @@ int32_t OfflineZipformerCtcModel::SubsamplingFactor() const { return impl_->SubsamplingFactor(); } +#if __ANDROID_API__ >= 9 +template OfflineZipformerCtcModel::OfflineZipformerCtcModel( + AAssetManager *mgr, const OfflineModelConfig &config); +#endif + +#if __OHOS__ +template OfflineZipformerCtcModel::OfflineZipformerCtcModel( + NativeResourceManager *mgr, const OfflineModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-zipformer-ctc-model.h b/sherpa-onnx/csrc/offline-zipformer-ctc-model.h index c4e835636..f76f96d54 100644 --- a/sherpa-onnx/csrc/offline-zipformer-ctc-model.h +++ b/sherpa-onnx/csrc/offline-zipformer-ctc-model.h @@ -7,11 +7,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - #include "onnxruntime_cxx_api.h" // NOLINT #include "sherpa-onnx/csrc/offline-ctc-model.h" #include "sherpa-onnx/csrc/offline-model-config.h" @@ -28,10 +23,8 @@ class OfflineZipformerCtcModel : public OfflineCtcModel { public: explicit OfflineZipformerCtcModel(const OfflineModelConfig &config); -#if __ANDROID_API__ >= 9 - OfflineZipformerCtcModel(AAssetManager *mgr, - const OfflineModelConfig &config); -#endif + template + OfflineZipformerCtcModel(Manager *mgr, const OfflineModelConfig &config); ~OfflineZipformerCtcModel() override; diff --git a/sherpa-onnx/csrc/silero-vad-model.cc b/sherpa-onnx/csrc/silero-vad-model.cc index 10f8027ba..80f0cbd65 100644 --- a/sherpa-onnx/csrc/silero-vad-model.cc +++ b/sherpa-onnx/csrc/silero-vad-model.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/session.h" @@ -37,7 +46,6 @@ class SileroVadModel::Impl { min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; } -#if __ANDROID_API__ >= 9 || defined(__OHOS__) template Impl(Manager *mgr, const VadModelConfig &config) : config_(config), @@ -59,7 +67,6 @@ class SileroVadModel::Impl { min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; } -#endif void Reset() { if (is_v5_) { @@ -433,16 +440,9 @@ class SileroVadModel::Impl { SileroVadModel::SileroVadModel(const VadModelConfig &config) : impl_(std::make_unique(config)) {} -#if __ANDROID_API__ >= 9 -SileroVadModel::SileroVadModel(AAssetManager *mgr, const VadModelConfig &config) +template +SileroVadModel::SileroVadModel(Manager *mgr, const VadModelConfig &config) : impl_(std::make_unique(mgr, config)) {} -#endif - -#if __OHOS__ -SileroVadModel::SileroVadModel(NativeResourceManager *mgr, - const VadModelConfig &config) - : impl_(std::make_unique(mgr, config)) {} -#endif SileroVadModel::~SileroVadModel() = default; @@ -472,4 +472,14 @@ void SileroVadModel::SetThreshold(float threshold) { impl_->SetThreshold(threshold); } +#if __ANDROID_API__ >= 9 +template SileroVadModel::SileroVadModel(AAssetManager *mgr, + const VadModelConfig &config); +#endif + +#if __OHOS__ +template SileroVadModel::SileroVadModel(NativeResourceManager *mgr, + const VadModelConfig &config); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/silero-vad-model.h b/sherpa-onnx/csrc/silero-vad-model.h index 7c4c2f90f..839139995 100644 --- a/sherpa-onnx/csrc/silero-vad-model.h +++ b/sherpa-onnx/csrc/silero-vad-model.h @@ -6,15 +6,6 @@ #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - -#if __OHOS__ -#include "rawfile/raw_file_manager.h" -#endif - #include "sherpa-onnx/csrc/vad-model.h" namespace sherpa_onnx { @@ -23,13 +14,8 @@ class SileroVadModel : public VadModel { public: explicit SileroVadModel(const VadModelConfig &config); -#if __ANDROID_API__ >= 9 - SileroVadModel(AAssetManager *mgr, const VadModelConfig &config); -#endif - -#if __OHOS__ - SileroVadModel(NativeResourceManager *mgr, const VadModelConfig &config); -#endif + template + SileroVadModel(Manager *mgr, const VadModelConfig &config); ~SileroVadModel() override; diff --git a/sherpa-onnx/csrc/symbol-table.cc b/sherpa-onnx/csrc/symbol-table.cc index 77b976431..723fec68b 100644 --- a/sherpa-onnx/csrc/symbol-table.cc +++ b/sherpa-onnx/csrc/symbol-table.cc @@ -15,6 +15,10 @@ #include "android/asset_manager.h" #include "android/asset_manager_jni.h" +#elif __OHOS__ +#include + +#include "rawfile/raw_file_manager.h" #endif #include "sherpa-onnx/csrc/base64-decode.h" @@ -99,14 +103,13 @@ SymbolTable::SymbolTable(const std::string &filename, bool is_file) { } } -#if __ANDROID_API__ >= 9 -SymbolTable::SymbolTable(AAssetManager *mgr, const std::string &filename) { +template +SymbolTable::SymbolTable(Manager *mgr, const std::string &filename) { auto buf = ReadFile(mgr, filename); std::istrstream is(buf.data(), buf.size()); Init(is); } -#endif void SymbolTable::Init(std::istream &is) { sym2id_ = ReadTokens(is, &id2sym_); } @@ -169,4 +172,14 @@ void SymbolTable::ApplyBase64Decode() { } } +#if __ANDROID_API__ >= 9 +template SymbolTable::SymbolTable(AAssetManager *mgr, + const std::string &filename); +#endif + +#if __OHOS__ +template SymbolTable::SymbolTable(NativeResourceManager *mgr, + const std::string &filename); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/symbol-table.h b/sherpa-onnx/csrc/symbol-table.h index 75a96144e..20d8d206b 100644 --- a/sherpa-onnx/csrc/symbol-table.h +++ b/sherpa-onnx/csrc/symbol-table.h @@ -10,11 +10,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - namespace sherpa_onnx { // The same token can be mapped to different integer IDs, so @@ -39,9 +34,8 @@ class SymbolTable { /// Fields are separated by space(s). explicit SymbolTable(const std::string &filename, bool is_file = true); -#if __ANDROID_API__ >= 9 - SymbolTable(AAssetManager *mgr, const std::string &filename); -#endif + template + SymbolTable(Manager *mgr, const std::string &filename); /// Return a string representation of this symbol table std::string ToString() const; diff --git a/sherpa-onnx/csrc/vad-model.cc b/sherpa-onnx/csrc/vad-model.cc index 658745046..58203bb9c 100644 --- a/sherpa-onnx/csrc/vad-model.cc +++ b/sherpa-onnx/csrc/vad-model.cc @@ -4,6 +4,15 @@ #include "sherpa-onnx/csrc/vad-model.h" +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/silero-vad-model.h" namespace sherpa_onnx { @@ -13,20 +22,20 @@ std::unique_ptr VadModel::Create(const VadModelConfig &config) { return std::make_unique(config); } -#if __ANDROID_API__ >= 9 -std::unique_ptr VadModel::Create(AAssetManager *mgr, +template +std::unique_ptr VadModel::Create(Manager *mgr, const VadModelConfig &config) { // TODO(fangjun): Support other VAD models. return std::make_unique(mgr, config); } + +#if __ANDROID_API__ >= 9 +template std::unique_ptr VadModel::Create( + AAssetManager *mgr, const VadModelConfig &config); #endif #if __OHOS__ -std::unique_ptr VadModel::Create(NativeResourceManager *mgr, - const VadModelConfig &config) { - // TODO(fangjun): Support other VAD models. - return std::make_unique(mgr, config); -} +template std::unique_ptr VadModel::Create( + NativeResourceManager *mgr, const VadModelConfig &config); #endif - } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/vad-model.h b/sherpa-onnx/csrc/vad-model.h index 3a425b193..aac68bb17 100644 --- a/sherpa-onnx/csrc/vad-model.h +++ b/sherpa-onnx/csrc/vad-model.h @@ -6,15 +6,6 @@ #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - -#if __OHOS__ -#include "rawfile/raw_file_manager.h" -#endif - #include "sherpa-onnx/csrc/vad-model-config.h" namespace sherpa_onnx { @@ -25,15 +16,9 @@ class VadModel { static std::unique_ptr Create(const VadModelConfig &config); -#if __ANDROID_API__ >= 9 - static std::unique_ptr Create(AAssetManager *mgr, - const VadModelConfig &config); -#endif - -#if __OHOS__ - static std::unique_ptr Create(NativeResourceManager *mgr, + template + static std::unique_ptr Create(Manager *mgr, const VadModelConfig &config); -#endif // reset the internal model states virtual void Reset() = 0; diff --git a/sherpa-onnx/csrc/voice-activity-detector.cc b/sherpa-onnx/csrc/voice-activity-detector.cc index 8c6038b7a..8bc8f7c94 100644 --- a/sherpa-onnx/csrc/voice-activity-detector.cc +++ b/sherpa-onnx/csrc/voice-activity-detector.cc @@ -8,6 +8,15 @@ #include #include +#if __ANDROID_API__ >= 9 +#include "android/asset_manager.h" +#include "android/asset_manager_jni.h" +#endif + +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/circular-buffer.h" #include "sherpa-onnx/csrc/vad-model.h" @@ -22,7 +31,6 @@ class VoiceActivityDetector::Impl { Init(); } -#if __ANDROID_API__ >= 9 || defined(__OHOS__) template Impl(Manager *mgr, const VadModelConfig &config, float buffer_size_in_seconds = 60) @@ -31,7 +39,6 @@ class VoiceActivityDetector::Impl { buffer_(buffer_size_in_seconds * config.sample_rate) { Init(); } -#endif void AcceptWaveform(const float *samples, int32_t n) { if (buffer_.Size() > max_utterance_length_) { @@ -178,19 +185,11 @@ VoiceActivityDetector::VoiceActivityDetector( const VadModelConfig &config, float buffer_size_in_seconds /*= 60*/) : impl_(std::make_unique(config, buffer_size_in_seconds)) {} -#if __ANDROID_API__ >= 9 +template VoiceActivityDetector::VoiceActivityDetector( - AAssetManager *mgr, const VadModelConfig &config, + Manager *mgr, const VadModelConfig &config, float buffer_size_in_seconds /*= 60*/) : impl_(std::make_unique(mgr, config, buffer_size_in_seconds)) {} -#endif - -#if __OHOS__ -VoiceActivityDetector::VoiceActivityDetector( - NativeResourceManager *mgr, const VadModelConfig &config, - float buffer_size_in_seconds /*= 60*/) - : impl_(std::make_unique(mgr, config, buffer_size_in_seconds)) {} -#endif VoiceActivityDetector::~VoiceActivityDetector() = default; @@ -220,4 +219,16 @@ const VadModelConfig &VoiceActivityDetector::GetConfig() const { return impl_->GetConfig(); } +#if __ANDROID_API__ >= 9 +template VoiceActivityDetector::VoiceActivityDetector( + AAssetManager *mgr, const VadModelConfig &config, + float buffer_size_in_seconds = 60); +#endif + +#if __OHOS__ +template VoiceActivityDetector::VoiceActivityDetector( + NativeResourceManager *mgr, const VadModelConfig &config, + float buffer_size_in_seconds = 60); +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/voice-activity-detector.h b/sherpa-onnx/csrc/voice-activity-detector.h index 9cc1a8897..015cedf04 100644 --- a/sherpa-onnx/csrc/voice-activity-detector.h +++ b/sherpa-onnx/csrc/voice-activity-detector.h @@ -7,15 +7,6 @@ #include #include -#if __ANDROID_API__ >= 9 -#include "android/asset_manager.h" -#include "android/asset_manager_jni.h" -#endif - -#if __OHOS__ -#include "rawfile/raw_file_manager.h" -#endif - #include "sherpa-onnx/csrc/vad-model-config.h" namespace sherpa_onnx { @@ -30,16 +21,9 @@ class VoiceActivityDetector { explicit VoiceActivityDetector(const VadModelConfig &config, float buffer_size_in_seconds = 60); -#if __ANDROID_API__ >= 9 - VoiceActivityDetector(AAssetManager *mgr, const VadModelConfig &config, - float buffer_size_in_seconds = 60); -#endif - -#if __OHOS__ - VoiceActivityDetector(NativeResourceManager *mgr, - const VadModelConfig &config, + template + VoiceActivityDetector(Manager *mgr, const VadModelConfig &config, float buffer_size_in_seconds = 60); -#endif ~VoiceActivityDetector();