Skip to content

Commit f2df116

Browse files
authored
Add CXX API examples for ten-vad. (#2380)
1 parent ceb1bc5 commit f2df116

File tree

3 files changed

+98
-18
lines changed

3 files changed

+98
-18
lines changed

.github/workflows/cxx-api.yaml

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ jobs:
196196
197197
rm $name
198198
199-
- name: Test VAD
199+
- name: Test silero-vad
200200
shell: bash
201201
run: |
202202
name=vad-cxx-api
@@ -223,17 +223,60 @@ jobs:
223223
224224
./$name
225225
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
228228
229-
ls -lh vad-test
229+
ls -lh vad-test-silero-vad
230230
231231
rm $name
232+
rm -fv *.onnx
233+
rm -fv *.wav
232234
233235
- uses: actions/upload-artifact@v4
234236
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
237280

238281
- name: Test Speech Enhancement (GTCRN)
239282
shell: bash

cxx-api-examples/vad-cxx-api.cc

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
// This file demonstrates how to use VAD to remove silences from a file
77
// clang-format off
88
//
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+
//
1015
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
1116
//
1217
// clang-format on
@@ -19,14 +24,45 @@ int32_t main() {
1924
using namespace sherpa_onnx::cxx; // NOLINT
2025

2126
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+
}
2348

2449
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+
3066
config.sample_rate = 16000;
3167
config.debug = true;
3268

@@ -43,7 +79,8 @@ int32_t main() {
4379
}
4480
bool is_eof = false;
4581
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;
4784

4885
int32_t sample_rate = config.sample_rate;
4986

sherpa-onnx/c-api/cxx-api.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,10 @@ class SHERPA_ONNX_API LinearResampler
649649
explicit LinearResampler(const SherpaOnnxLinearResampler *p);
650650
};
651651

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);
656656

657657
} // namespace sherpa_onnx::cxx
658658

0 commit comments

Comments
 (0)