Add CXX API for VAD (#2077)
This commit is contained in:
@@ -317,11 +317,12 @@ if(SHERPA_ONNX_ENABLE_BINARY)
|
||||
add_executable(sherpa-onnx-keyword-spotter sherpa-onnx-keyword-spotter.cc)
|
||||
add_executable(sherpa-onnx-offline sherpa-onnx-offline.cc)
|
||||
add_executable(sherpa-onnx-offline-audio-tagging sherpa-onnx-offline-audio-tagging.cc)
|
||||
add_executable(sherpa-onnx-offline-denoiser sherpa-onnx-offline-denoiser.cc)
|
||||
add_executable(sherpa-onnx-offline-language-identification sherpa-onnx-offline-language-identification.cc)
|
||||
add_executable(sherpa-onnx-offline-parallel sherpa-onnx-offline-parallel.cc)
|
||||
add_executable(sherpa-onnx-offline-punctuation sherpa-onnx-offline-punctuation.cc)
|
||||
add_executable(sherpa-onnx-online-punctuation sherpa-onnx-online-punctuation.cc)
|
||||
add_executable(sherpa-onnx-offline-denoiser sherpa-onnx-offline-denoiser.cc)
|
||||
add_executable(sherpa-onnx-vad sherpa-onnx-vad.cc)
|
||||
|
||||
if(SHERPA_ONNX_ENABLE_TTS)
|
||||
add_executable(sherpa-onnx-offline-tts sherpa-onnx-offline-tts.cc)
|
||||
@@ -336,11 +337,12 @@ if(SHERPA_ONNX_ENABLE_BINARY)
|
||||
sherpa-onnx-keyword-spotter
|
||||
sherpa-onnx-offline
|
||||
sherpa-onnx-offline-audio-tagging
|
||||
sherpa-onnx-offline-denoiser
|
||||
sherpa-onnx-offline-language-identification
|
||||
sherpa-onnx-offline-parallel
|
||||
sherpa-onnx-offline-punctuation
|
||||
sherpa-onnx-offline-denoiser
|
||||
sherpa-onnx-online-punctuation
|
||||
sherpa-onnx-vad
|
||||
)
|
||||
if(SHERPA_ONNX_ENABLE_TTS)
|
||||
list(APPEND main_exes
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
#include "sherpa-onnx/csrc/alsa.h"
|
||||
#include "sherpa-onnx/csrc/circular-buffer.h"
|
||||
#include "sherpa-onnx/csrc/voice-activity-detector.h"
|
||||
#include "sherpa-onnx/csrc/wave-writer.h"
|
||||
|
||||
@@ -84,8 +84,6 @@ as the device_name.
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int32_t chunk = 0.1 * alsa.GetActualSampleRate();
|
||||
|
||||
auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(config);
|
||||
|
||||
fprintf(stderr, "Started. Please speak\n");
|
||||
@@ -95,36 +93,34 @@ as the device_name.
|
||||
|
||||
int32_t k = 0;
|
||||
while (!stop) {
|
||||
{
|
||||
const std::vector<float> &samples = alsa.Read(chunk);
|
||||
const std::vector<float> &samples = alsa.Read(window_size);
|
||||
|
||||
vad->AcceptWaveform(samples.data(), samples.size());
|
||||
vad->AcceptWaveform(samples.data(), samples.size());
|
||||
|
||||
if (vad->IsSpeechDetected() && !printed) {
|
||||
printed = true;
|
||||
fprintf(stderr, "\nDetected speech!\n");
|
||||
}
|
||||
if (!vad->IsSpeechDetected()) {
|
||||
printed = false;
|
||||
}
|
||||
if (vad->IsSpeechDetected() && !printed) {
|
||||
printed = true;
|
||||
fprintf(stderr, "\nDetected speech!\n");
|
||||
}
|
||||
if (!vad->IsSpeechDetected()) {
|
||||
printed = false;
|
||||
}
|
||||
|
||||
while (!vad->Empty()) {
|
||||
const auto &segment = vad->Front();
|
||||
float duration =
|
||||
segment.samples.size() / static_cast<float>(sample_rate);
|
||||
while (!vad->Empty()) {
|
||||
const auto &segment = vad->Front();
|
||||
float duration = segment.samples.size() / static_cast<float>(sample_rate);
|
||||
|
||||
fprintf(stderr, "Duration: %.3f seconds\n", duration);
|
||||
fprintf(stderr, "Duration: %.3f seconds\n", duration);
|
||||
|
||||
char filename[128];
|
||||
snprintf(filename, sizeof(filename), "seg-%d-%.3fs.wav", k, duration);
|
||||
k += 1;
|
||||
sherpa_onnx::WriteWave(filename, 16000, segment.samples.data(),
|
||||
segment.samples.size());
|
||||
fprintf(stderr, "Saved to %s\n", filename);
|
||||
fprintf(stderr, "----------\n");
|
||||
std::ostringstream os;
|
||||
os << "seg-" << k << "-" << std::fixed << std::setprecision(3) << duration
|
||||
<< "s.wav";
|
||||
k += 1;
|
||||
sherpa_onnx::WriteWave(os.str(), 16000, segment.samples.data(),
|
||||
segment.samples.size());
|
||||
fprintf(stderr, "Saved to %s\n", os.str().c_str());
|
||||
fprintf(stderr, "----------\n");
|
||||
|
||||
vad->Pop();
|
||||
}
|
||||
vad->Pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
110
sherpa-onnx/csrc/sherpa-onnx-vad.cc
Normal file
110
sherpa-onnx/csrc/sherpa-onnx-vad.cc
Normal file
@@ -0,0 +1,110 @@
|
||||
// sherpa-onnx/csrc/sherpa-onnx-vad.cc
|
||||
//
|
||||
// Copyright (c) 2025 Xiaomi Corporation
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
#include "sherpa-onnx/csrc/voice-activity-detector.h"
|
||||
#include "sherpa-onnx/csrc/wave-reader.h"
|
||||
#include "sherpa-onnx/csrc/wave-writer.h"
|
||||
|
||||
int32_t main(int32_t argc, char *argv[]) {
|
||||
const char *kUsageMessage = R"usage(
|
||||
This program shows how to use VAD in sherpa-onnx
|
||||
to remove silences from a file.
|
||||
|
||||
./bin/sherpa-onnx-vad \
|
||||
--silero-vad-model=/path/to/silero_vad.onnx \
|
||||
/path/to/input.wav
|
||||
/path/to/output.wav
|
||||
|
||||
Please download silero_vad.onnx from
|
||||
https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/silero_vad.onnx
|
||||
|
||||
For instance, use
|
||||
wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/silero_vad.onnx
|
||||
|
||||
input.wav should be 16kHz.
|
||||
)usage";
|
||||
|
||||
sherpa_onnx::ParseOptions po(kUsageMessage);
|
||||
sherpa_onnx::VadModelConfig config;
|
||||
|
||||
config.Register(&po);
|
||||
po.Read(argc, argv);
|
||||
if (po.NumArgs() != 2) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"Please provide only 2 argument2: the input wav and the output wav\n");
|
||||
po.PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", config.ToString().c_str());
|
||||
|
||||
if (!config.Validate()) {
|
||||
fprintf(stderr, "Errors in config!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string wav_filename = po.GetArg(1);
|
||||
int32_t sampling_rate = -1;
|
||||
|
||||
bool is_ok = false;
|
||||
std::vector<float> samples =
|
||||
sherpa_onnx::ReadWave(wav_filename, &sampling_rate, &is_ok);
|
||||
|
||||
if (!is_ok) {
|
||||
fprintf(stderr, "Failed to read '%s'\n", wav_filename.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sampling_rate != 16000) {
|
||||
fprintf(stderr, "Support only 16000Hz. Given: %d\n", sampling_rate);
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(config);
|
||||
|
||||
int32_t window_size = config.silero_vad.window_size;
|
||||
|
||||
int32_t i = 0;
|
||||
bool is_eof = false;
|
||||
|
||||
std::vector<float> samples_without_silence;
|
||||
|
||||
while (!is_eof) {
|
||||
if (i + window_size < samples.size()) {
|
||||
vad->AcceptWaveform(samples.data() + i, window_size);
|
||||
i += window_size;
|
||||
} else {
|
||||
vad->Flush();
|
||||
is_eof = true;
|
||||
}
|
||||
|
||||
while (!vad->Empty()) {
|
||||
const auto &segment = vad->Front();
|
||||
float start_time = segment.start / static_cast<float>(sampling_rate);
|
||||
float end_time = start_time + segment.samples.size() /
|
||||
static_cast<float>(sampling_rate);
|
||||
|
||||
fprintf(stderr, "%.3f -- %.3f\n", start_time, end_time);
|
||||
samples_without_silence.insert(samples_without_silence.end(),
|
||||
segment.samples.begin(),
|
||||
segment.samples.end());
|
||||
vad->Pop();
|
||||
}
|
||||
}
|
||||
|
||||
sherpa_onnx::WriteWave(po.GetArg(2), sampling_rate,
|
||||
samples_without_silence.data(),
|
||||
samples_without_silence.size());
|
||||
|
||||
fprintf(stderr, "Saved to %s\n", po.GetArg(2).c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user