diff --git a/examples/cpp_libtorch/ReadMe b/examples/cpp_libtorch/README.md similarity index 84% rename from examples/cpp_libtorch/ReadMe rename to examples/cpp_libtorch/README.md index 91ce492..1c3130e 100644 --- a/examples/cpp_libtorch/ReadMe +++ b/examples/cpp_libtorch/README.md @@ -1,5 +1,6 @@ -This is the source code for Silero-VAD 5.1 in C++, based on LibTorch. +This is the source code for Silero-VAD V5 in C++, based on LibTorch. The primary implementation is the CPU version, and you should compare its results with the Python version. +I only checked the 16kHz results. In addition, Batch and CUDA inference options are also available if you want to explore further. Note that when using batch inference, the speech probabilities might slightly differ from the standard version, likely due to differences in caching. @@ -12,24 +13,24 @@ GCC 11.4.0 (GCC >= 5.1) LibTorch 1.13.0(Other versions are also acceptable) #Download Libtorch: - #cpu + *cpu $wget https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.13.0%2Bcpu.zip $unzip libtorch-shared-with-deps-1.13.0+cpu.zip - #cuda + *cuda $wget https://download.pytorch.org/libtorch/cu116/libtorch-shared-with-deps-1.13.0%2Bcu116.zip $unzip libtorch-shared-with-deps-1.13.0+cu116.zip #complie: - #cpu + *cpu $g++ main.cc silero_torch.cc -I ./libtorch/include/ -I ./libtorch/include/torch/csrc/api/include -L ./libtorch/lib/ -ltorch -ltorch_cpu -lc10 -Wl,-rpath,./libtorch/lib/ -o silero -std=c++14 -D_GLIBCXX_USE_CXX11_ABI=0 - #cuda + *cuda $g++ main.cc silero_torch.cc -I ./libtorch/include/ -I ./libtorch/include/torch/csrc/api/include -L ./libtorch/lib/ -ltorch -ltorch_cuda -ltorch_cpu -lc10 -Wl,-rpath,./libtorch/lib/ -o silero -std=c++14 -D_GLIBCXX_USE_CXX11_ABI=0 -DUSE_GPU - #option to add + *option to add -DUSE_BATCH -DUSE_GPU # Run: -./silero aepyx.wav 0.5 #The sample file 'aepyx.wav' is part of the Voxconverse dataset. - #aepyx.wav : 16kHz, 16-bit +./silero aepyx.wav 16000 0.5 #The sample file 'aepyx.wav' is part of the Voxconverse dataset. + #aepyx.wav : 16kHz, 16-bit diff --git a/examples/cpp_libtorch/main.cc b/examples/cpp_libtorch/main.cc index 8871758..3f774e7 100644 --- a/examples/cpp_libtorch/main.cc +++ b/examples/cpp_libtorch/main.cc @@ -4,25 +4,28 @@ int main(int argc, char* argv[]) { - if(argc != 3){ - std::cerr<<"Usage : "< threshold"< "< input_wav(wav_reader.num_samples()); @@ -34,7 +37,7 @@ int main(int argc, char* argv[]) { vad.SpeechProbs(input_wav); - std::vector speeches = vad.GetSpeechTimestamps(); + std::vector speeches = vad.GetSpeechTimestamps(); for(const auto& speech : speeches){ if(vad.print_as_samples){ std::cout<<"{'start': "<(speech.start)<<", 'end': "<(speech.end)<<"}"< VadIterator::GetSpeechTimestamps() { - std::vector speeches = DoVad(); + std::vector VadIterator::GetSpeechTimestamps() { + std::vector speeches = DoVad(); #ifdef USE_BATCH //When you use BATCH inference. You would better use 'mergeSpeeches' function to arrage time stamp. //It could be better get reasonable output because of distorted probs. duration_merge_samples = sample_rate * max_duration_merge_ms / 1000; - std::vector speeches_merge = mergeSpeeches(speeches, duration_merge_samples); + std::vector speeches_merge = mergeSpeeches(speeches, duration_merge_samples); if(!print_as_samples){ for (auto& speech : speeches_merge) { //samples to second speech.start /= sample_rate; @@ -147,6 +146,9 @@ namespace silero { #endif } + void VadIterator::SetVariables(){ + init_engine(window_size_ms); + } void VadIterator::init_engine(int window_size_ms) { min_silence_samples = sample_rate * min_silence_duration_ms / 1000; @@ -186,8 +188,8 @@ namespace silero { total_sample_size = 0; } - std::vector VadIterator::DoVad() { - std::vector speeches; + std::vector VadIterator::DoVad() { + std::vector speeches; for (size_t i = 0; i < outputs_prob.size(); ++i) { float speech_prob = outputs_prob[i]; @@ -202,7 +204,7 @@ namespace silero { if (speech_prob >= threshold && !triggered) { triggered = true; - Interval segment; + SpeechSegment segment; segment.start = std::max(static_cast(0), current_sample - speech_pad_samples - window_size_samples); speeches.push_back(segment); continue; @@ -216,7 +218,7 @@ namespace silero { if (current_sample - temp_end < min_silence_samples) { continue; } else { - Interval& segment = speeches.back(); + SpeechSegment& segment = speeches.back(); segment.end = temp_end + speech_pad_samples - window_size_samples; temp_end = 0; triggered = false; @@ -226,7 +228,7 @@ namespace silero { if (triggered) { //만약 낮은 확률을 보이다가 마지막프레임 prbos만 딱 확률이 높게 나오면 위에서 triggerd = true 메핑과 동시에 segment start가 돼서 문제가 될것 같은데? start = end 같은값? 후처리가 있으니 문제가 없으려나? std::cout<<"when last triggered is keep working until last Probs"<speech_pad_samples) - (speech.start + this->speech_pad_samples) < min_speech_samples); //min_speech_samples is 4000samples(0.25sec) //여기서 포인트!! 계산 할때는 start,end sample에'speech_pad_samples' 사이즈를 추가한후 길이를 측정함. @@ -252,15 +254,15 @@ namespace silero { return speeches; } - std::vector VadIterator::mergeSpeeches(const std::vector& speeches, int duration_merge_samples) { - std::vector mergedSpeeches; + std::vector VadIterator::mergeSpeeches(const std::vector& speeches, int duration_merge_samples) { + std::vector mergedSpeeches; if (speeches.empty()) { return mergedSpeeches; // 빈 벡터 반환 } // 첫 번째 구간으로 초기화 - Interval currentSegment = speeches[0]; + SpeechSegment currentSegment = speeches[0]; for (size_t i = 1; i < speeches.size(); ++i) { //첫번째 start,end 정보 건너뛰기. 그래서 i=1부터 // 두 구간의 차이가 threshold(duration_merge_samples)보다 작은 경우, 합침 diff --git a/examples/cpp_libtorch/silero_torch.h b/examples/cpp_libtorch/silero_torch.h index 6b8cbad..d8d3bc7 100644 --- a/examples/cpp_libtorch/silero_torch.h +++ b/examples/cpp_libtorch/silero_torch.h @@ -2,7 +2,6 @@ //Created On : 2024-11-18 //Description : silero 5.1 system for torch-script(c++). //Version : 1.0 -//Contact : junghan4242@gmail.com #ifndef SILERO_TORCH_H #define SILERO_TORCH_H @@ -27,11 +26,6 @@ namespace silero{ int end; }; - struct Interval { - float start; - float end; - }; - class VadIterator{ public: @@ -42,10 +36,12 @@ namespace silero{ void SpeechProbs(std::vector& input_wav); - std::vector GetSpeechTimestamps(); + std::vector GetSpeechTimestamps(); + void SetVariables(); float threshold; int sample_rate; + int window_size_ms; int min_speech_duration_ms; int max_duration_merge_ms; bool print_as_samples; @@ -70,8 +66,8 @@ namespace silero{ void init_engine(int window_size_ms); void init_torch_model(const std::string& model_path); void reset_states(); - std::vector DoVad(); - std::vector mergeSpeeches(const std::vector& speeches, int duration_merge_samples); + std::vector DoVad(); + std::vector mergeSpeeches(const std::vector& speeches, int duration_merge_samples); };