Fangjun Kuang
Committed by GitHub

Add CXX API examples for ten-vad. (#2380)

@@ -196,7 +196,7 @@ jobs: @@ -196,7 +196,7 @@ jobs:
196 196
197 rm $name 197 rm $name
198 198
199 - - name: Test VAD 199 + - name: Test silero-vad
200 shell: bash 200 shell: bash
201 run: | 201 run: |
202 name=vad-cxx-api 202 name=vad-cxx-api
@@ -223,17 +223,60 @@ jobs: @@ -223,17 +223,60 @@ jobs:
223 223
224 ./$name 224 ./$name
225 225
226 - mkdir vad-test  
227 - cp -v lei-jun-test*.wav vad-test 226 + mkdir vad-test-silero-vad
  227 + cp -v lei-jun-test*.wav vad-test-silero-vad
228 228
229 - ls -lh vad-test 229 + ls -lh vad-test-silero-vad
230 230
231 rm $name 231 rm $name
  232 + rm -fv *.onnx
  233 + rm -fv *.wav
232 234
233 - uses: actions/upload-artifact@v4 235 - uses: actions/upload-artifact@v4
234 with: 236 with:
235 - name: vad-test-wavs-cxx-${{ matrix.os }}  
236 - path: ./vad-test/*.wav 237 + name: silero-vad-test-wavs-cxx-${{ matrix.os }}
  238 + path: ./vad-test-silero-vad/*.wav
  239 +
  240 + - name: Test ten-vad
  241 + shell: bash
  242 + run: |
  243 + name=vad-cxx-api
  244 + g++ -std=c++17 -o $name ./cxx-api-examples/$name.cc \
  245 + -I ./build/install/include \
  246 + -L ./build/install/lib/ \
  247 + -l sherpa-onnx-cxx-api \
  248 + -l sherpa-onnx-c-api \
  249 + -l onnxruntime
  250 +
  251 + ls -lh $name
  252 +
  253 + export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
  254 + export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
  255 +
  256 + if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
  257 + ldd ./$name
  258 + echo "----"
  259 + readelf -d ./$name
  260 + fi
  261 +
  262 + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
  263 + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx
  264 +
  265 + ./$name
  266 +
  267 + mkdir vad-test-ten-vad
  268 + cp -v lei-jun-test*.wav vad-test-ten-vad
  269 +
  270 + ls -lh vad-test-ten-vad
  271 +
  272 + rm $name
  273 + rm -fv *.onnx
  274 + rm -rf *.wav
  275 +
  276 + - uses: actions/upload-artifact@v4
  277 + with:
  278 + name: ten-vad-test-wavs-cxx-${{ matrix.os }}
  279 + path: ./vad-test-ten-vad/*.wav
237 280
238 - name: Test Speech Enhancement (GTCRN) 281 - name: Test Speech Enhancement (GTCRN)
239 shell: bash 282 shell: bash
@@ -6,7 +6,12 @@ @@ -6,7 +6,12 @@
6 // This file demonstrates how to use VAD to remove silences from a file 6 // This file demonstrates how to use VAD to remove silences from a file
7 // clang-format off 7 // clang-format off
8 // 8 //
9 -// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx 9 +// To use silero-vad:
  10 +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx
  11 +//
  12 +// To use ten-vad:
  13 +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx
  14 +//
10 // wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav 15 // wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
11 // 16 //
12 // clang-format on 17 // clang-format on
@@ -19,14 +24,45 @@ int32_t main() { @@ -19,14 +24,45 @@ int32_t main() {
19 using namespace sherpa_onnx::cxx; // NOLINT 24 using namespace sherpa_onnx::cxx; // NOLINT
20 25
21 std::string wave_filename = "./lei-jun-test.wav"; 26 std::string wave_filename = "./lei-jun-test.wav";
22 - std::string vad_filename = "./silero_vad.onnx"; 27 + if (!FileExists(wave_filename)) {
  28 + fprintf(stderr, "Please download %s\n", wave_filename.c_str());
  29 + return -1;
  30 + }
  31 +
  32 + std::string vad_filename;
  33 + bool use_silero_vad = false;
  34 + bool use_ten_vad = false;
  35 +
  36 + if (FileExists("./silero_vad.onnx")) {
  37 + printf("Use silero-vad\n");
  38 + vad_filename = "./silero_vad.onnx";
  39 + use_silero_vad = true;
  40 + } else if (FileExists("./ten-vad.onnx")) {
  41 + printf("Use ten-vad\n");
  42 + vad_filename = "./ten-vad.onnx";
  43 + use_ten_vad = true;
  44 + } else {
  45 + fprintf(stderr, "Please provide either silero_vad.onnx or ten-vad.onnx\n");
  46 + return -1;
  47 + }
23 48
24 VadModelConfig config; 49 VadModelConfig config;
25 - config.silero_vad.model = vad_filename;  
26 - config.silero_vad.threshold = 0.1;  
27 - config.silero_vad.min_silence_duration = 0.5;  
28 - config.silero_vad.min_speech_duration = 0.25;  
29 - config.silero_vad.max_speech_duration = 20; 50 + if (use_silero_vad) {
  51 + config.silero_vad.model = vad_filename;
  52 + config.silero_vad.threshold = 0.3;
  53 + config.silero_vad.min_silence_duration = 0.5;
  54 + config.silero_vad.min_speech_duration = 0.25;
  55 + config.silero_vad.max_speech_duration = 20;
  56 + config.silero_vad.window_size = 512;
  57 + } else if (use_ten_vad) {
  58 + config.ten_vad.model = vad_filename;
  59 + config.ten_vad.threshold = 0.3;
  60 + config.ten_vad.min_silence_duration = 0.5;
  61 + config.ten_vad.min_speech_duration = 0.25;
  62 + config.ten_vad.max_speech_duration = 20;
  63 + config.ten_vad.window_size = 256;
  64 + }
  65 +
30 config.sample_rate = 16000; 66 config.sample_rate = 16000;
31 config.debug = true; 67 config.debug = true;
32 68
@@ -43,7 +79,8 @@ int32_t main() { @@ -43,7 +79,8 @@ int32_t main() {
43 } 79 }
44 bool is_eof = false; 80 bool is_eof = false;
45 int32_t i = 0; 81 int32_t i = 0;
46 - int32_t window_size = config.silero_vad.window_size; 82 + int32_t window_size = use_silero_vad ? config.silero_vad.window_size
  83 + : config.ten_vad.window_size;
47 84
48 int32_t sample_rate = config.sample_rate; 85 int32_t sample_rate = config.sample_rate;
49 86
@@ -649,10 +649,10 @@ class SHERPA_ONNX_API LinearResampler @@ -649,10 +649,10 @@ class SHERPA_ONNX_API LinearResampler
649 explicit LinearResampler(const SherpaOnnxLinearResampler *p); 649 explicit LinearResampler(const SherpaOnnxLinearResampler *p);
650 }; 650 };
651 651
652 -std::string GetVersionStr();  
653 -std::string GetGitSha1();  
654 -std::string GetGitDate();  
655 -bool FileExists(const std::string &filename); 652 +SHERPA_ONNX_API std::string GetVersionStr();
  653 +SHERPA_ONNX_API std::string GetGitSha1();
  654 +SHERPA_ONNX_API std::string GetGitDate();
  655 +SHERPA_ONNX_API bool FileExists(const std::string &filename);
656 656
657 } // namespace sherpa_onnx::cxx 657 } // namespace sherpa_onnx::cxx
658 658