From 2db62a8b0698f4d7afa443aca584269d647bbe1a Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Fri, 22 Nov 2024 20:18:04 +0800 Subject: [PATCH 1/3] Use commandline tools for macOS to build for HarmonyOS --- build-ohos-arm64-v8a.sh | 45 ++++++++++++++++++++++----------------- build-ohos-armeabi-v7a.sh | 31 ++++++++++----------------- build-ohos-x86-64.sh | 45 ++++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 60 deletions(-) diff --git a/build-ohos-arm64-v8a.sh b/build-ohos-arm64-v8a.sh index cedbd495f..3aa8e17a1 100755 --- a/build-ohos-arm64-v8a.sh +++ b/build-ohos-arm64-v8a.sh @@ -19,7 +19,6 @@ cd $dir # rm commandline-tools-linux-x64-5.0.5.200.zip if [ -z $OHOS_SDK_NATIVE_DIR ]; then OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ - export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH # You can find the following content inside OHOS_SDK_NATIVE_DIR # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ # total 524K @@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot fi -# If you don't want to install commandline tools, you can install the SDK -# using DevEco Studio. The following uses API version 10 as an example and -# it has installed the SDK to -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native -# -# Remember to select ``native`` when you install the SDK if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/ - # total 1560 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot + OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/command-line-tools/sdk/default/openharmony/native + # (py38) fangjuns-MacBook-Pro:software fangjun$ ls -lh command-line-tools/sdk/default/openharmony/native/ + # total 752 + # -rw-r--r-- 1 fangjun staff 341K Jan 1 2001 NOTICE.txt + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 build + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:18 build-tools + # -rw-r--r-- 1 fangjun staff 371B Jan 1 2001 compatible_config.json + # drwxr-xr-x 10 fangjun staff 320B Nov 6 21:18 llvm + # -rw-r--r-- 1 fangjun staff 16K Jan 1 2001 nativeapi_syscap_config.json + # -rw-r--r-- 1 fangjun staff 5.9K Jan 1 2001 ndk_system_capability.json + # -rw-r--r-- 1 fangjun staff 167B Jan 1 2001 oh-uni-package.json + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 sysroot fi if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" + exit 1 +fi + +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" exit 1 fi +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake @@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then exit 1 fi - sleep 1 onnxruntime_version=1.16.3 onnxruntime_dir=onnxruntime-ohos-arm64-v8a-$onnxruntime_version diff --git a/build-ohos-armeabi-v7a.sh b/build-ohos-armeabi-v7a.sh index ee1d560ba..4e6de8275 100755 --- a/build-ohos-armeabi-v7a.sh +++ b/build-ohos-armeabi-v7a.sh @@ -35,32 +35,24 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot fi -# If you don't want to install commandline tools, you can install the SDK -# using DevEco Studio. The following uses API version 10 as an example and -# it has installed the SDK to -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native -# -# Remember to select ``native`` when you install the SDK if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/ - # total 1560 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot + echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" + exit 1 fi -if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang does not exist" echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" exit 1 fi +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake @@ -71,7 +63,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then exit 1 fi - sleep 1 onnxruntime_version=1.16.3 onnxruntime_dir=onnxruntime-ohos-armeabi-v7a-$onnxruntime_version diff --git a/build-ohos-x86-64.sh b/build-ohos-x86-64.sh index 6c8787dc0..f2656cc2f 100755 --- a/build-ohos-x86-64.sh +++ b/build-ohos-x86-64.sh @@ -19,7 +19,6 @@ cd $dir # rm commandline-tools-linux-x64-5.0.5.200.zip if [ -z $OHOS_SDK_NATIVE_DIR ]; then OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ - export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH # You can find the following content inside OHOS_SDK_NATIVE_DIR # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ # total 524K @@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot fi -# If you don't want to install commandline tools, you can install the SDK -# using DevEco Studio. The following uses API version 10 as an example and -# it has installed the SDK to -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native -# -# Remember to select ``native`` when you install the SDK if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/ - # total 1560 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot + OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/command-line-tools/sdk/default/openharmony/native + # (py38) fangjuns-MacBook-Pro:software fangjun$ ls -lh command-line-tools/sdk/default/openharmony/native/ + # total 752 + # -rw-r--r-- 1 fangjun staff 341K Jan 1 2001 NOTICE.txt + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 build + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:18 build-tools + # -rw-r--r-- 1 fangjun staff 371B Jan 1 2001 compatible_config.json + # drwxr-xr-x 10 fangjun staff 320B Nov 6 21:18 llvm + # -rw-r--r-- 1 fangjun staff 16K Jan 1 2001 nativeapi_syscap_config.json + # -rw-r--r-- 1 fangjun staff 5.9K Jan 1 2001 ndk_system_capability.json + # -rw-r--r-- 1 fangjun staff 167B Jan 1 2001 oh-uni-package.json + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 sysroot fi if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" + exit 1 +fi + +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + echo "See https://developer.huawei.com/consumer/cn/download/" + echo "or" + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main" exit 1 fi +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake @@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then exit 1 fi - sleep 1 onnxruntime_version=1.16.3 onnxruntime_dir=onnxruntime-ohos-x86_64-$onnxruntime_version From 8243d5329f364044cbb352bc64adbcdca93567ea Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 24 Nov 2024 16:06:42 +0800 Subject: [PATCH 2/3] HarmonyOS support for VAD. --- build-ohos-arm64-v8a.sh | 1 - build-ohos-armeabi-v7a.sh | 1 - build-ohos-x86-64.sh | 1 - sherpa-onnx/c-api/c-api.cc | 38 +++++++++++++++++++-- sherpa-onnx/c-api/c-api.h | 15 ++++++++ sherpa-onnx/csrc/CMakeLists.txt | 6 ++++ sherpa-onnx/csrc/macros.h | 12 +++++++ sherpa-onnx/csrc/onnx-utils.cc | 36 +++++++++++++++++++ sherpa-onnx/csrc/onnx-utils.h | 9 +++++ sherpa-onnx/csrc/silero-vad-model.cc | 11 ++++-- sherpa-onnx/csrc/silero-vad-model.h | 8 +++++ sherpa-onnx/csrc/vad-model.cc | 8 +++++ sherpa-onnx/csrc/vad-model.h | 9 +++++ sherpa-onnx/csrc/voice-activity-detector.cc | 12 +++++-- sherpa-onnx/csrc/voice-activity-detector.h | 10 ++++++ 15 files changed, 168 insertions(+), 9 deletions(-) diff --git a/build-ohos-arm64-v8a.sh b/build-ohos-arm64-v8a.sh index 3aa8e17a1..182575bc1 100755 --- a/build-ohos-arm64-v8a.sh +++ b/build-ohos-arm64-v8a.sh @@ -131,7 +131,6 @@ cmake \ make -j2 make install/strip cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/aarch64-linux-ohos/libc++_shared.so install/lib rm -rf install/share rm -rf install/lib/pkgconfig diff --git a/build-ohos-armeabi-v7a.sh b/build-ohos-armeabi-v7a.sh index 4e6de8275..e0a2ac883 100755 --- a/build-ohos-armeabi-v7a.sh +++ b/build-ohos-armeabi-v7a.sh @@ -121,7 +121,6 @@ cmake \ make -j2 make install/strip cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/arm-linux-ohos/libc++_shared.so install/lib rm -rf install/share rm -rf install/lib/pkgconfig diff --git a/build-ohos-x86-64.sh b/build-ohos-x86-64.sh index f2656cc2f..58ff5da5a 100755 --- a/build-ohos-x86-64.sh +++ b/build-ohos-x86-64.sh @@ -131,7 +131,6 @@ cmake \ make -j2 make install/strip cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/x86_64-linux-ohos/libc++_shared.so install/lib rm -rf install/share rm -rf install/lib/pkgconfig diff --git a/sherpa-onnx/c-api/c-api.cc b/sherpa-onnx/c-api/c-api.cc index f01b4917f..d2091bc56 100644 --- a/sherpa-onnx/c-api/c-api.cc +++ b/sherpa-onnx/c-api/c-api.cc @@ -11,6 +11,10 @@ #include #include +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "sherpa-onnx/csrc/audio-tagging.h" #include "sherpa-onnx/csrc/circular-buffer.h" #include "sherpa-onnx/csrc/display.h" @@ -917,8 +921,8 @@ struct SherpaOnnxVoiceActivityDetector { std::unique_ptr impl; }; -SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds) { +sherpa_onnx::VadModelConfig GetVadModelConfig( + const SherpaOnnxVadModelConfig *config) { sherpa_onnx::VadModelConfig vad_config; vad_config.silero_vad.model = SHERPA_ONNX_OR(config->silero_vad.model, ""); @@ -947,9 +951,20 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( vad_config.debug = SHERPA_ONNX_OR(config->debug, false); if (vad_config.debug) { +#if __OHOS__ + SHERPA_ONNX_LOGE("%{public}s", vad_config.ToString().c_str()); +#else SHERPA_ONNX_LOGE("%s", vad_config.ToString().c_str()); +#endif } + return vad_config; +} + +SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds) { + auto vad_config = GetVadModelConfig(config); + if (!vad_config.Validate()) { SHERPA_ONNX_LOGE("Errors in config"); return nullptr; @@ -962,6 +977,25 @@ 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; diff --git a/sherpa-onnx/c-api/c-api.h b/sherpa-onnx/c-api/c-api.h index 8b4b65786..6ea5c27fb 100644 --- a/sherpa-onnx/c-api/c-api.h +++ b/sherpa-onnx/c-api/c-api.h @@ -841,6 +841,21 @@ 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); diff --git a/sherpa-onnx/csrc/CMakeLists.txt b/sherpa-onnx/csrc/CMakeLists.txt index d241ee1ca..be47a27e6 100644 --- a/sherpa-onnx/csrc/CMakeLists.txt +++ b/sherpa-onnx/csrc/CMakeLists.txt @@ -207,6 +207,12 @@ target_link_libraries(sherpa-onnx-core kaldi-decoder-core ssentencepiece_core ) +if(DEFINED OHOS AND ${OHOS} STREQUAL OHOS) + target_link_libraries(sherpa-onnx-core + hilog_ndk.z + rawfile.z + ) +endif() if(SHERPA_ONNX_ENABLE_GPU) target_link_libraries(sherpa-onnx-core diff --git a/sherpa-onnx/csrc/macros.h b/sherpa-onnx/csrc/macros.h index 7cd4d4627..ac11d6a79 100644 --- a/sherpa-onnx/csrc/macros.h +++ b/sherpa-onnx/csrc/macros.h @@ -8,6 +8,16 @@ #include #include +#if __OHOS__ +#include "hilog/log.h" + +#undef LOG_DOMAIN +#undef LOG_TAG + +// https://gitee.com/openharmony/docs/blob/145a084f0b742e4325915e32f8184817927d1251/en/contribute/OpenHarmony-Log-guide.md#hilog-api-usage-specifications +#define LOG_DOMAIN 0x6666 +#define LOG_TAG "sherpa_onnx" +#endif #if __ANDROID_API__ >= 8 #include "android/log.h" @@ -19,6 +29,8 @@ fprintf(stderr, "\n"); \ __android_log_print(ANDROID_LOG_WARN, "sherpa-onnx", ##__VA_ARGS__); \ } while (0) +#elif defined(__OHOS__) +#define SHERPA_ONNX_LOGE(...) OH_LOG_INFO(LOG_APP, ##__VA_ARGS__) #elif SHERPA_ONNX_ENABLE_WASM #define SHERPA_ONNX_LOGE(...) \ do { \ diff --git a/sherpa-onnx/csrc/onnx-utils.cc b/sherpa-onnx/csrc/onnx-utils.cc index 5e346a69e..6d7e26846 100644 --- a/sherpa-onnx/csrc/onnx-utils.cc +++ b/sherpa-onnx/csrc/onnx-utils.cc @@ -7,9 +7,13 @@ #include #include #include +#include #include #include #include +#include + +#include "sherpa-onnx/csrc/macros.h" #if __ANDROID_API__ >= 9 #include "android/asset_manager.h" @@ -326,6 +330,38 @@ std::vector ReadFile(AAssetManager *mgr, const std::string &filename) { } #endif +#if __OHOS__ +std::vector ReadFile(NativeResourceManager *mgr, + const std::string &filename) { + std::unique_ptr fp( + OH_ResourceManager_OpenRawFile(mgr, filename.c_str()), + OH_ResourceManager_CloseRawFile); + + if (!fp) { + std::ostringstream os; + os << "Read file '" << filename << "' failed."; + SHERPA_ONNX_LOGE("%s", os.str().c_str()); + return {}; + } + + auto len = static_cast(OH_ResourceManager_GetRawFileSize(fp.get())); + + std::vector buffer(len); + + int32_t n = OH_ResourceManager_ReadRawFile(fp.get(), buffer.data(), len); + + if (n != len) { + std::ostringstream os; + os << "Read file '" << filename << "' failed. Number of bytes read: " << n + << ". Expected bytes to read: " << len; + SHERPA_ONNX_LOGE("%s", os.str().c_str()); + return {}; + } + + return buffer; +} +#endif + Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out, const std::vector &hyps_num_split) { std::vector cur_encoder_out_shape = diff --git a/sherpa-onnx/csrc/onnx-utils.h b/sherpa-onnx/csrc/onnx-utils.h index 8a19a2baf..c978fbb77 100644 --- a/sherpa-onnx/csrc/onnx-utils.h +++ b/sherpa-onnx/csrc/onnx-utils.h @@ -22,6 +22,10 @@ #include "android/asset_manager_jni.h" #endif +#if __OHOS__ +#include "rawfile/raw_file_manager.h" +#endif + #include "onnxruntime_cxx_api.h" // NOLINT namespace sherpa_onnx { @@ -103,6 +107,11 @@ std::vector ReadFile(const std::string &filename); std::vector ReadFile(AAssetManager *mgr, const std::string &filename); #endif +#if __OHOS__ +std::vector ReadFile(NativeResourceManager *mgr, + const std::string &filename); +#endif + // TODO(fangjun): Document it Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out, const std::vector &hyps_num_split); diff --git a/sherpa-onnx/csrc/silero-vad-model.cc b/sherpa-onnx/csrc/silero-vad-model.cc index 66841d56d..10f8027ba 100644 --- a/sherpa-onnx/csrc/silero-vad-model.cc +++ b/sherpa-onnx/csrc/silero-vad-model.cc @@ -37,8 +37,9 @@ class SileroVadModel::Impl { min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const VadModelConfig &config) +#if __ANDROID_API__ >= 9 || defined(__OHOS__) + template + Impl(Manager *mgr, const VadModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), @@ -437,6 +438,12 @@ SileroVadModel::SileroVadModel(AAssetManager *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; void SileroVadModel::Reset() { return impl_->Reset(); } diff --git a/sherpa-onnx/csrc/silero-vad-model.h b/sherpa-onnx/csrc/silero-vad-model.h index 169cb7244..7c4c2f90f 100644 --- a/sherpa-onnx/csrc/silero-vad-model.h +++ b/sherpa-onnx/csrc/silero-vad-model.h @@ -11,6 +11,10 @@ #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,6 +27,10 @@ class SileroVadModel : public VadModel { SileroVadModel(AAssetManager *mgr, const VadModelConfig &config); #endif +#if __OHOS__ + SileroVadModel(NativeResourceManager *mgr, const VadModelConfig &config); +#endif + ~SileroVadModel() override; // reset the internal model states diff --git a/sherpa-onnx/csrc/vad-model.cc b/sherpa-onnx/csrc/vad-model.cc index be9a5e7fe..658745046 100644 --- a/sherpa-onnx/csrc/vad-model.cc +++ b/sherpa-onnx/csrc/vad-model.cc @@ -21,4 +21,12 @@ std::unique_ptr VadModel::Create(AAssetManager *mgr, } #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); +} +#endif + } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/vad-model.h b/sherpa-onnx/csrc/vad-model.h index 81028f2f1..3a425b193 100644 --- a/sherpa-onnx/csrc/vad-model.h +++ b/sherpa-onnx/csrc/vad-model.h @@ -11,6 +11,10 @@ #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 { @@ -26,6 +30,11 @@ class VadModel { const VadModelConfig &config); #endif +#if __OHOS__ + static std::unique_ptr Create(NativeResourceManager *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 c20d3476d..8c6038b7a 100644 --- a/sherpa-onnx/csrc/voice-activity-detector.cc +++ b/sherpa-onnx/csrc/voice-activity-detector.cc @@ -22,8 +22,9 @@ class VoiceActivityDetector::Impl { Init(); } -#if __ANDROID_API__ >= 9 - Impl(AAssetManager *mgr, const VadModelConfig &config, +#if __ANDROID_API__ >= 9 || defined(__OHOS__) + template + Impl(Manager *mgr, const VadModelConfig &config, float buffer_size_in_seconds = 60) : model_(VadModel::Create(mgr, config)), config_(config), @@ -184,6 +185,13 @@ VoiceActivityDetector::VoiceActivityDetector( : 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; void VoiceActivityDetector::AcceptWaveform(const float *samples, int32_t n) { diff --git a/sherpa-onnx/csrc/voice-activity-detector.h b/sherpa-onnx/csrc/voice-activity-detector.h index 9eb53c554..9cc1a8897 100644 --- a/sherpa-onnx/csrc/voice-activity-detector.h +++ b/sherpa-onnx/csrc/voice-activity-detector.h @@ -12,6 +12,10 @@ #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 { @@ -31,6 +35,12 @@ class VoiceActivityDetector { float buffer_size_in_seconds = 60); #endif +#if __OHOS__ + VoiceActivityDetector(NativeResourceManager *mgr, + const VadModelConfig &config, + float buffer_size_in_seconds = 60); +#endif + ~VoiceActivityDetector(); void AcceptWaveform(const float *samples, int32_t n); From 390f2e0e68cb00ef79505cc46e6ab1987a53dca4 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 24 Nov 2024 16:21:17 +0800 Subject: [PATCH 3/3] Fix building --- sherpa-onnx/csrc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherpa-onnx/csrc/CMakeLists.txt b/sherpa-onnx/csrc/CMakeLists.txt index be47a27e6..b3c1617e3 100644 --- a/sherpa-onnx/csrc/CMakeLists.txt +++ b/sherpa-onnx/csrc/CMakeLists.txt @@ -207,7 +207,7 @@ target_link_libraries(sherpa-onnx-core kaldi-decoder-core ssentencepiece_core ) -if(DEFINED OHOS AND ${OHOS} STREQUAL OHOS) +if(DEFINED OHOS AND x${OHOS} STREQUAL xOHOS) target_link_libraries(sherpa-onnx-core hilog_ndk.z rawfile.z