Fangjun Kuang
Committed by GitHub

HarmonyOS support for VAD. (#1561)

@@ -19,7 +19,6 @@ cd $dir @@ -19,7 +19,6 @@ cd $dir
19 # rm commandline-tools-linux-x64-5.0.5.200.zip 19 # rm commandline-tools-linux-x64-5.0.5.200.zip
20 if [ -z $OHOS_SDK_NATIVE_DIR ]; then 20 if [ -z $OHOS_SDK_NATIVE_DIR ]; then
21 OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ 21 OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/
22 - export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH  
23 # You can find the following content inside OHOS_SDK_NATIVE_DIR 22 # You can find the following content inside OHOS_SDK_NATIVE_DIR
24 # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ 23 # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/
25 # total 524K 24 # total 524K
@@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then @@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then
35 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot 34 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot
36 fi 35 fi
37 36
38 -# If you don't want to install commandline tools, you can install the SDK  
39 -# using DevEco Studio. The following uses API version 10 as an example and  
40 -# it has installed the SDK to  
41 -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
42 -#  
43 -# Remember to select ``native`` when you install the SDK  
44 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 37 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then
45 - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
46 - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH  
47 - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/  
48 - # total 1560  
49 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt  
50 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build  
51 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools  
52 - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm  
53 - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json  
54 - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json  
55 - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json  
56 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot 38 + OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/command-line-tools/sdk/default/openharmony/native
  39 + # (py38) fangjuns-MacBook-Pro:software fangjun$ ls -lh command-line-tools/sdk/default/openharmony/native/
  40 + # total 752
  41 + # -rw-r--r-- 1 fangjun staff 341K Jan 1 2001 NOTICE.txt
  42 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 build
  43 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:18 build-tools
  44 + # -rw-r--r-- 1 fangjun staff 371B Jan 1 2001 compatible_config.json
  45 + # drwxr-xr-x 10 fangjun staff 320B Nov 6 21:18 llvm
  46 + # -rw-r--r-- 1 fangjun staff 16K Jan 1 2001 nativeapi_syscap_config.json
  47 + # -rw-r--r-- 1 fangjun staff 5.9K Jan 1 2001 ndk_system_capability.json
  48 + # -rw-r--r-- 1 fangjun staff 167B Jan 1 2001 oh-uni-package.json
  49 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 sysroot
57 fi 50 fi
58 51
59 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 52 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then
60 echo "Please first download Command Line Tools for HarmonyOS" 53 echo "Please first download Command Line Tools for HarmonyOS"
  54 + echo "See https://developer.huawei.com/consumer/cn/download/"
  55 + echo "or"
  56 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
  57 + exit 1
  58 +fi
  59 +
  60 +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang ]; then
  61 + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang does not exist"
  62 + echo "Please first download Command Line Tools for HarmonyOS"
  63 + echo "See https://developer.huawei.com/consumer/cn/download/"
  64 + echo "or"
  65 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
61 exit 1 66 exit 1
62 fi 67 fi
63 68
  69 +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH
64 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH 70 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH
65 71
66 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake 72 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake
@@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then @@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then
71 exit 1 77 exit 1
72 fi 78 fi
73 79
74 -  
75 sleep 1 80 sleep 1
76 onnxruntime_version=1.16.3 81 onnxruntime_version=1.16.3
77 onnxruntime_dir=onnxruntime-ohos-arm64-v8a-$onnxruntime_version 82 onnxruntime_dir=onnxruntime-ohos-arm64-v8a-$onnxruntime_version
@@ -126,7 +131,6 @@ cmake \ @@ -126,7 +131,6 @@ cmake \
126 make -j2 131 make -j2
127 make install/strip 132 make install/strip
128 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib 133 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib
129 -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/aarch64-linux-ohos/libc++_shared.so install/lib  
130 134
131 rm -rf install/share 135 rm -rf install/share
132 rm -rf install/lib/pkgconfig 136 rm -rf install/lib/pkgconfig
@@ -35,32 +35,24 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then @@ -35,32 +35,24 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then
35 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot 35 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot
36 fi 36 fi
37 37
38 -# If you don't want to install commandline tools, you can install the SDK  
39 -# using DevEco Studio. The following uses API version 10 as an example and  
40 -# it has installed the SDK to  
41 -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
42 -#  
43 -# Remember to select ``native`` when you install the SDK  
44 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 38 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then
45 - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
46 - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH  
47 - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/  
48 - # total 1560  
49 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt  
50 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build  
51 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools  
52 - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm  
53 - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json  
54 - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json  
55 - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json  
56 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot 39 + echo "Please first download Command Line Tools for HarmonyOS"
  40 + echo "See https://developer.huawei.com/consumer/cn/download/"
  41 + echo "or"
  42 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
  43 + exit 1
57 fi 44 fi
58 45
59 -if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 46 +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang ]; then
  47 + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang does not exist"
60 echo "Please first download Command Line Tools for HarmonyOS" 48 echo "Please first download Command Line Tools for HarmonyOS"
  49 + echo "See https://developer.huawei.com/consumer/cn/download/"
  50 + echo "or"
  51 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
61 exit 1 52 exit 1
62 fi 53 fi
63 54
  55 +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH
64 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH 56 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH
65 57
66 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake 58 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake
@@ -71,7 +63,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then @@ -71,7 +63,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then
71 exit 1 63 exit 1
72 fi 64 fi
73 65
74 -  
75 sleep 1 66 sleep 1
76 onnxruntime_version=1.16.3 67 onnxruntime_version=1.16.3
77 onnxruntime_dir=onnxruntime-ohos-armeabi-v7a-$onnxruntime_version 68 onnxruntime_dir=onnxruntime-ohos-armeabi-v7a-$onnxruntime_version
@@ -130,7 +121,6 @@ cmake \ @@ -130,7 +121,6 @@ cmake \
130 make -j2 121 make -j2
131 make install/strip 122 make install/strip
132 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib 123 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib
133 -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/arm-linux-ohos/libc++_shared.so install/lib  
134 124
135 rm -rf install/share 125 rm -rf install/share
136 rm -rf install/lib/pkgconfig 126 rm -rf install/lib/pkgconfig
@@ -19,7 +19,6 @@ cd $dir @@ -19,7 +19,6 @@ cd $dir
19 # rm commandline-tools-linux-x64-5.0.5.200.zip 19 # rm commandline-tools-linux-x64-5.0.5.200.zip
20 if [ -z $OHOS_SDK_NATIVE_DIR ]; then 20 if [ -z $OHOS_SDK_NATIVE_DIR ]; then
21 OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ 21 OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/
22 - export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH  
23 # You can find the following content inside OHOS_SDK_NATIVE_DIR 22 # You can find the following content inside OHOS_SDK_NATIVE_DIR
24 # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ 23 # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/
25 # total 524K 24 # total 524K
@@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then @@ -35,32 +34,39 @@ if [ -z $OHOS_SDK_NATIVE_DIR ]; then
35 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot 34 # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot
36 fi 35 fi
37 36
38 -# If you don't want to install commandline tools, you can install the SDK  
39 -# using DevEco Studio. The following uses API version 10 as an example and  
40 -# it has installed the SDK to  
41 -# /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
42 -#  
43 -# Remember to select ``native`` when you install the SDK  
44 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 37 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then
45 - OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native  
46 - # export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH  
47 - # ls -lh /Users/fangjun/software/huawei/OpenHarmony/Sdk/10/native/  
48 - # total 1560  
49 - # -rw-r--r-- 1 fangjun staff 764K Jan 1 2001 NOTICE.txt  
50 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build  
51 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 build-tools  
52 - # drwxr-xr-x 10 fangjun staff 320B Nov 19 22:42 llvm  
53 - # -rw-r--r-- 1 fangjun staff 4.0K Jan 1 2001 nativeapi_syscap_config.json  
54 - # -rw-r--r-- 1 fangjun staff 1.9K Jan 1 2001 ndk_system_capability.json  
55 - # -rw-r--r-- 1 fangjun staff 169B Jan 1 2001 oh-uni-package.json  
56 - # drwxr-xr-x 3 fangjun staff 96B Nov 19 22:42 sysroot 38 + OHOS_SDK_NATIVE_DIR=/Users/fangjun/software/command-line-tools/sdk/default/openharmony/native
  39 + # (py38) fangjuns-MacBook-Pro:software fangjun$ ls -lh command-line-tools/sdk/default/openharmony/native/
  40 + # total 752
  41 + # -rw-r--r-- 1 fangjun staff 341K Jan 1 2001 NOTICE.txt
  42 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 build
  43 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:18 build-tools
  44 + # -rw-r--r-- 1 fangjun staff 371B Jan 1 2001 compatible_config.json
  45 + # drwxr-xr-x 10 fangjun staff 320B Nov 6 21:18 llvm
  46 + # -rw-r--r-- 1 fangjun staff 16K Jan 1 2001 nativeapi_syscap_config.json
  47 + # -rw-r--r-- 1 fangjun staff 5.9K Jan 1 2001 ndk_system_capability.json
  48 + # -rw-r--r-- 1 fangjun staff 167B Jan 1 2001 oh-uni-package.json
  49 + # drwxr-xr-x 3 fangjun staff 96B Nov 6 21:17 sysroot
57 fi 50 fi
58 51
59 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then 52 if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then
60 echo "Please first download Command Line Tools for HarmonyOS" 53 echo "Please first download Command Line Tools for HarmonyOS"
  54 + echo "See https://developer.huawei.com/consumer/cn/download/"
  55 + echo "or"
  56 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
  57 + exit 1
  58 +fi
  59 +
  60 +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang ]; then
  61 + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang does not exist"
  62 + echo "Please first download Command Line Tools for HarmonyOS"
  63 + echo "See https://developer.huawei.com/consumer/cn/download/"
  64 + echo "or"
  65 + echo "https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main"
61 exit 1 66 exit 1
62 fi 67 fi
63 68
  69 +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH
64 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH 70 export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH
65 71
66 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake 72 OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake
@@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then @@ -71,7 +77,6 @@ if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then
71 exit 1 77 exit 1
72 fi 78 fi
73 79
74 -  
75 sleep 1 80 sleep 1
76 onnxruntime_version=1.16.3 81 onnxruntime_version=1.16.3
77 onnxruntime_dir=onnxruntime-ohos-x86_64-$onnxruntime_version 82 onnxruntime_dir=onnxruntime-ohos-x86_64-$onnxruntime_version
@@ -126,7 +131,6 @@ cmake \ @@ -126,7 +131,6 @@ cmake \
126 make -j2 131 make -j2
127 make install/strip 132 make install/strip
128 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib 133 cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib
129 -cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/x86_64-linux-ohos/libc++_shared.so install/lib  
130 134
131 rm -rf install/share 135 rm -rf install/share
132 rm -rf install/lib/pkgconfig 136 rm -rf install/lib/pkgconfig
@@ -11,6 +11,10 @@ @@ -11,6 +11,10 @@
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
14 #include "sherpa-onnx/csrc/audio-tagging.h" 18 #include "sherpa-onnx/csrc/audio-tagging.h"
15 #include "sherpa-onnx/csrc/circular-buffer.h" 19 #include "sherpa-onnx/csrc/circular-buffer.h"
16 #include "sherpa-onnx/csrc/display.h" 20 #include "sherpa-onnx/csrc/display.h"
@@ -917,8 +921,8 @@ struct SherpaOnnxVoiceActivityDetector { @@ -917,8 +921,8 @@ struct SherpaOnnxVoiceActivityDetector {
917 std::unique_ptr<sherpa_onnx::VoiceActivityDetector> impl; 921 std::unique_ptr<sherpa_onnx::VoiceActivityDetector> impl;
918 }; 922 };
919 923
920 -SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(  
921 - const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds) { 924 +sherpa_onnx::VadModelConfig GetVadModelConfig(
  925 + const SherpaOnnxVadModelConfig *config) {
922 sherpa_onnx::VadModelConfig vad_config; 926 sherpa_onnx::VadModelConfig vad_config;
923 927
924 vad_config.silero_vad.model = SHERPA_ONNX_OR(config->silero_vad.model, ""); 928 vad_config.silero_vad.model = SHERPA_ONNX_OR(config->silero_vad.model, "");
@@ -947,9 +951,20 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( @@ -947,9 +951,20 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
947 vad_config.debug = SHERPA_ONNX_OR(config->debug, false); 951 vad_config.debug = SHERPA_ONNX_OR(config->debug, false);
948 952
949 if (vad_config.debug) { 953 if (vad_config.debug) {
  954 +#if __OHOS__
  955 + SHERPA_ONNX_LOGE("%{public}s", vad_config.ToString().c_str());
  956 +#else
950 SHERPA_ONNX_LOGE("%s", vad_config.ToString().c_str()); 957 SHERPA_ONNX_LOGE("%s", vad_config.ToString().c_str());
  958 +#endif
951 } 959 }
952 960
  961 + return vad_config;
  962 +}
  963 +
  964 +SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
  965 + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds) {
  966 + auto vad_config = GetVadModelConfig(config);
  967 +
953 if (!vad_config.Validate()) { 968 if (!vad_config.Validate()) {
954 SHERPA_ONNX_LOGE("Errors in config"); 969 SHERPA_ONNX_LOGE("Errors in config");
955 return nullptr; 970 return nullptr;
@@ -962,6 +977,25 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector( @@ -962,6 +977,25 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
962 return p; 977 return p;
963 } 978 }
964 979
  980 +#ifdef __OHOS__
  981 +SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(
  982 + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
  983 + NativeResourceManager *mgr) {
  984 + if (mgr == nullptr) {
  985 + return SherpaOnnxCreateVoiceActivityDetector(config,
  986 + buffer_size_in_seconds);
  987 + }
  988 +
  989 + auto vad_config = GetVadModelConfig(config);
  990 +
  991 + SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;
  992 + p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(
  993 + mgr, vad_config, buffer_size_in_seconds);
  994 +
  995 + return p;
  996 +}
  997 +#endif
  998 +
965 void SherpaOnnxDestroyVoiceActivityDetector( 999 void SherpaOnnxDestroyVoiceActivityDetector(
966 SherpaOnnxVoiceActivityDetector *p) { 1000 SherpaOnnxVoiceActivityDetector *p) {
967 delete p; 1001 delete p;
@@ -841,6 +841,21 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector * @@ -841,6 +841,21 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
841 SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config, 841 SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config,
842 float buffer_size_in_seconds); 842 float buffer_size_in_seconds);
843 843
  844 +#ifdef __OHOS__
  845 +
  846 +// Return an instance of VoiceActivityDetector.
  847 +// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free
  848 +// the returned pointer to avoid memory leak.
  849 +//
  850 +// It is for HarmonyOS
  851 +typedef struct NativeResourceManager NativeResourceManager;
  852 +
  853 +SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
  854 +SherpaOnnxCreateVoiceActivityDetectorOHOS(
  855 + const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
  856 + NativeResourceManager *mgr);
  857 +#endif
  858 +
844 SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector( 859 SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector(
845 SherpaOnnxVoiceActivityDetector *p); 860 SherpaOnnxVoiceActivityDetector *p);
846 861
@@ -207,6 +207,12 @@ target_link_libraries(sherpa-onnx-core @@ -207,6 +207,12 @@ target_link_libraries(sherpa-onnx-core
207 kaldi-decoder-core 207 kaldi-decoder-core
208 ssentencepiece_core 208 ssentencepiece_core
209 ) 209 )
  210 +if(DEFINED OHOS AND x${OHOS} STREQUAL xOHOS)
  211 + target_link_libraries(sherpa-onnx-core
  212 + hilog_ndk.z
  213 + rawfile.z
  214 + )
  215 +endif()
210 216
211 if(SHERPA_ONNX_ENABLE_GPU) 217 if(SHERPA_ONNX_ENABLE_GPU)
212 target_link_libraries(sherpa-onnx-core 218 target_link_libraries(sherpa-onnx-core
@@ -8,6 +8,16 @@ @@ -8,6 +8,16 @@
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #include <utility> 10 #include <utility>
  11 +#if __OHOS__
  12 +#include "hilog/log.h"
  13 +
  14 +#undef LOG_DOMAIN
  15 +#undef LOG_TAG
  16 +
  17 +// https://gitee.com/openharmony/docs/blob/145a084f0b742e4325915e32f8184817927d1251/en/contribute/OpenHarmony-Log-guide.md#hilog-api-usage-specifications
  18 +#define LOG_DOMAIN 0x6666
  19 +#define LOG_TAG "sherpa_onnx"
  20 +#endif
11 21
12 #if __ANDROID_API__ >= 8 22 #if __ANDROID_API__ >= 8
13 #include "android/log.h" 23 #include "android/log.h"
@@ -19,6 +29,8 @@ @@ -19,6 +29,8 @@
19 fprintf(stderr, "\n"); \ 29 fprintf(stderr, "\n"); \
20 __android_log_print(ANDROID_LOG_WARN, "sherpa-onnx", ##__VA_ARGS__); \ 30 __android_log_print(ANDROID_LOG_WARN, "sherpa-onnx", ##__VA_ARGS__); \
21 } while (0) 31 } while (0)
  32 +#elif defined(__OHOS__)
  33 +#define SHERPA_ONNX_LOGE(...) OH_LOG_INFO(LOG_APP, ##__VA_ARGS__)
22 #elif SHERPA_ONNX_ENABLE_WASM 34 #elif SHERPA_ONNX_ENABLE_WASM
23 #define SHERPA_ONNX_LOGE(...) \ 35 #define SHERPA_ONNX_LOGE(...) \
24 do { \ 36 do { \
@@ -7,9 +7,13 @@ @@ -7,9 +7,13 @@
7 #include <algorithm> 7 #include <algorithm>
8 #include <fstream> 8 #include <fstream>
9 #include <functional> 9 #include <functional>
  10 +#include <memory>
10 #include <numeric> 11 #include <numeric>
11 #include <sstream> 12 #include <sstream>
12 #include <string> 13 #include <string>
  14 +#include <vector>
  15 +
  16 +#include "sherpa-onnx/csrc/macros.h"
13 17
14 #if __ANDROID_API__ >= 9 18 #if __ANDROID_API__ >= 9
15 #include "android/asset_manager.h" 19 #include "android/asset_manager.h"
@@ -326,6 +330,38 @@ std::vector<char> ReadFile(AAssetManager *mgr, const std::string &filename) { @@ -326,6 +330,38 @@ std::vector<char> ReadFile(AAssetManager *mgr, const std::string &filename) {
326 } 330 }
327 #endif 331 #endif
328 332
  333 +#if __OHOS__
  334 +std::vector<char> ReadFile(NativeResourceManager *mgr,
  335 + const std::string &filename) {
  336 + std::unique_ptr<RawFile, decltype(&OH_ResourceManager_CloseRawFile)> fp(
  337 + OH_ResourceManager_OpenRawFile(mgr, filename.c_str()),
  338 + OH_ResourceManager_CloseRawFile);
  339 +
  340 + if (!fp) {
  341 + std::ostringstream os;
  342 + os << "Read file '" << filename << "' failed.";
  343 + SHERPA_ONNX_LOGE("%s", os.str().c_str());
  344 + return {};
  345 + }
  346 +
  347 + auto len = static_cast<int32_t>(OH_ResourceManager_GetRawFileSize(fp.get()));
  348 +
  349 + std::vector<char> buffer(len);
  350 +
  351 + int32_t n = OH_ResourceManager_ReadRawFile(fp.get(), buffer.data(), len);
  352 +
  353 + if (n != len) {
  354 + std::ostringstream os;
  355 + os << "Read file '" << filename << "' failed. Number of bytes read: " << n
  356 + << ". Expected bytes to read: " << len;
  357 + SHERPA_ONNX_LOGE("%s", os.str().c_str());
  358 + return {};
  359 + }
  360 +
  361 + return buffer;
  362 +}
  363 +#endif
  364 +
329 Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out, 365 Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out,
330 const std::vector<int32_t> &hyps_num_split) { 366 const std::vector<int32_t> &hyps_num_split) {
331 std::vector<int64_t> cur_encoder_out_shape = 367 std::vector<int64_t> cur_encoder_out_shape =
@@ -22,6 +22,10 @@ @@ -22,6 +22,10 @@
22 #include "android/asset_manager_jni.h" 22 #include "android/asset_manager_jni.h"
23 #endif 23 #endif
24 24
  25 +#if __OHOS__
  26 +#include "rawfile/raw_file_manager.h"
  27 +#endif
  28 +
25 #include "onnxruntime_cxx_api.h" // NOLINT 29 #include "onnxruntime_cxx_api.h" // NOLINT
26 30
27 namespace sherpa_onnx { 31 namespace sherpa_onnx {
@@ -103,6 +107,11 @@ std::vector<char> ReadFile(const std::string &filename); @@ -103,6 +107,11 @@ std::vector<char> ReadFile(const std::string &filename);
103 std::vector<char> ReadFile(AAssetManager *mgr, const std::string &filename); 107 std::vector<char> ReadFile(AAssetManager *mgr, const std::string &filename);
104 #endif 108 #endif
105 109
  110 +#if __OHOS__
  111 +std::vector<char> ReadFile(NativeResourceManager *mgr,
  112 + const std::string &filename);
  113 +#endif
  114 +
106 // TODO(fangjun): Document it 115 // TODO(fangjun): Document it
107 Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out, 116 Ort::Value Repeat(OrtAllocator *allocator, Ort::Value *cur_encoder_out,
108 const std::vector<int32_t> &hyps_num_split); 117 const std::vector<int32_t> &hyps_num_split);
@@ -37,8 +37,9 @@ class SileroVadModel::Impl { @@ -37,8 +37,9 @@ class SileroVadModel::Impl {
37 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration; 37 min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration;
38 } 38 }
39 39
40 -#if __ANDROID_API__ >= 9  
41 - Impl(AAssetManager *mgr, const VadModelConfig &config) 40 +#if __ANDROID_API__ >= 9 || defined(__OHOS__)
  41 + template <typename Manager>
  42 + Impl(Manager *mgr, const VadModelConfig &config)
42 : config_(config), 43 : config_(config),
43 env_(ORT_LOGGING_LEVEL_ERROR), 44 env_(ORT_LOGGING_LEVEL_ERROR),
44 sess_opts_(GetSessionOptions(config)), 45 sess_opts_(GetSessionOptions(config)),
@@ -437,6 +438,12 @@ SileroVadModel::SileroVadModel(AAssetManager *mgr, const VadModelConfig &config) @@ -437,6 +438,12 @@ SileroVadModel::SileroVadModel(AAssetManager *mgr, const VadModelConfig &config)
437 : impl_(std::make_unique<Impl>(mgr, config)) {} 438 : impl_(std::make_unique<Impl>(mgr, config)) {}
438 #endif 439 #endif
439 440
  441 +#if __OHOS__
  442 +SileroVadModel::SileroVadModel(NativeResourceManager *mgr,
  443 + const VadModelConfig &config)
  444 + : impl_(std::make_unique<Impl>(mgr, config)) {}
  445 +#endif
  446 +
440 SileroVadModel::~SileroVadModel() = default; 447 SileroVadModel::~SileroVadModel() = default;
441 448
442 void SileroVadModel::Reset() { return impl_->Reset(); } 449 void SileroVadModel::Reset() { return impl_->Reset(); }
@@ -11,6 +11,10 @@ @@ -11,6 +11,10 @@
11 #include "android/asset_manager_jni.h" 11 #include "android/asset_manager_jni.h"
12 #endif 12 #endif
13 13
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
14 #include "sherpa-onnx/csrc/vad-model.h" 18 #include "sherpa-onnx/csrc/vad-model.h"
15 19
16 namespace sherpa_onnx { 20 namespace sherpa_onnx {
@@ -23,6 +27,10 @@ class SileroVadModel : public VadModel { @@ -23,6 +27,10 @@ class SileroVadModel : public VadModel {
23 SileroVadModel(AAssetManager *mgr, const VadModelConfig &config); 27 SileroVadModel(AAssetManager *mgr, const VadModelConfig &config);
24 #endif 28 #endif
25 29
  30 +#if __OHOS__
  31 + SileroVadModel(NativeResourceManager *mgr, const VadModelConfig &config);
  32 +#endif
  33 +
26 ~SileroVadModel() override; 34 ~SileroVadModel() override;
27 35
28 // reset the internal model states 36 // reset the internal model states
@@ -21,4 +21,12 @@ std::unique_ptr<VadModel> VadModel::Create(AAssetManager *mgr, @@ -21,4 +21,12 @@ std::unique_ptr<VadModel> VadModel::Create(AAssetManager *mgr,
21 } 21 }
22 #endif 22 #endif
23 23
  24 +#if __OHOS__
  25 +std::unique_ptr<VadModel> VadModel::Create(NativeResourceManager *mgr,
  26 + const VadModelConfig &config) {
  27 + // TODO(fangjun): Support other VAD models.
  28 + return std::make_unique<SileroVadModel>(mgr, config);
  29 +}
  30 +#endif
  31 +
24 } // namespace sherpa_onnx 32 } // namespace sherpa_onnx
@@ -11,6 +11,10 @@ @@ -11,6 +11,10 @@
11 #include "android/asset_manager_jni.h" 11 #include "android/asset_manager_jni.h"
12 #endif 12 #endif
13 13
  14 +#if __OHOS__
  15 +#include "rawfile/raw_file_manager.h"
  16 +#endif
  17 +
14 #include "sherpa-onnx/csrc/vad-model-config.h" 18 #include "sherpa-onnx/csrc/vad-model-config.h"
15 19
16 namespace sherpa_onnx { 20 namespace sherpa_onnx {
@@ -26,6 +30,11 @@ class VadModel { @@ -26,6 +30,11 @@ class VadModel {
26 const VadModelConfig &config); 30 const VadModelConfig &config);
27 #endif 31 #endif
28 32
  33 +#if __OHOS__
  34 + static std::unique_ptr<VadModel> Create(NativeResourceManager *mgr,
  35 + const VadModelConfig &config);
  36 +#endif
  37 +
29 // reset the internal model states 38 // reset the internal model states
30 virtual void Reset() = 0; 39 virtual void Reset() = 0;
31 40
@@ -22,8 +22,9 @@ class VoiceActivityDetector::Impl { @@ -22,8 +22,9 @@ class VoiceActivityDetector::Impl {
22 Init(); 22 Init();
23 } 23 }
24 24
25 -#if __ANDROID_API__ >= 9  
26 - Impl(AAssetManager *mgr, const VadModelConfig &config, 25 +#if __ANDROID_API__ >= 9 || defined(__OHOS__)
  26 + template <typename Manager>
  27 + Impl(Manager *mgr, const VadModelConfig &config,
27 float buffer_size_in_seconds = 60) 28 float buffer_size_in_seconds = 60)
28 : model_(VadModel::Create(mgr, config)), 29 : model_(VadModel::Create(mgr, config)),
29 config_(config), 30 config_(config),
@@ -184,6 +185,13 @@ VoiceActivityDetector::VoiceActivityDetector( @@ -184,6 +185,13 @@ VoiceActivityDetector::VoiceActivityDetector(
184 : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {} 185 : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {}
185 #endif 186 #endif
186 187
  188 +#if __OHOS__
  189 +VoiceActivityDetector::VoiceActivityDetector(
  190 + NativeResourceManager *mgr, const VadModelConfig &config,
  191 + float buffer_size_in_seconds /*= 60*/)
  192 + : impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {}
  193 +#endif
  194 +
187 VoiceActivityDetector::~VoiceActivityDetector() = default; 195 VoiceActivityDetector::~VoiceActivityDetector() = default;
188 196
189 void VoiceActivityDetector::AcceptWaveform(const float *samples, int32_t n) { 197 void VoiceActivityDetector::AcceptWaveform(const float *samples, int32_t n) {
@@ -12,6 +12,10 @@ @@ -12,6 +12,10 @@
12 #include "android/asset_manager_jni.h" 12 #include "android/asset_manager_jni.h"
13 #endif 13 #endif
14 14
  15 +#if __OHOS__
  16 +#include "rawfile/raw_file_manager.h"
  17 +#endif
  18 +
15 #include "sherpa-onnx/csrc/vad-model-config.h" 19 #include "sherpa-onnx/csrc/vad-model-config.h"
16 20
17 namespace sherpa_onnx { 21 namespace sherpa_onnx {
@@ -31,6 +35,12 @@ class VoiceActivityDetector { @@ -31,6 +35,12 @@ class VoiceActivityDetector {
31 float buffer_size_in_seconds = 60); 35 float buffer_size_in_seconds = 60);
32 #endif 36 #endif
33 37
  38 +#if __OHOS__
  39 + VoiceActivityDetector(NativeResourceManager *mgr,
  40 + const VadModelConfig &config,
  41 + float buffer_size_in_seconds = 60);
  42 +#endif
  43 +
34 ~VoiceActivityDetector(); 44 ~VoiceActivityDetector();
35 45
36 void AcceptWaveform(const float *samples, int32_t n); 46 void AcceptWaveform(const float *samples, int32_t n);