Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
snakers4
GitHub Repository: snakers4/silero-vad
Path: blob/master/examples/cpp_libtorch/main.cc
1179 views
1
#include <iostream>
2
#include "silero_torch.h"
3
#include "wav.h"
4
5
int main(int argc, char* argv[]) {
6
7
if(argc != 4){
8
std::cerr<<"Usage : "<<argv[0]<<" <wav.path> <SampleRate> <Threshold>"<<std::endl;
9
std::cerr<<"Usage : "<<argv[0]<<" sample.wav 16000 0.5"<<std::endl;
10
return 1;
11
}
12
13
std::string wav_path = argv[1];
14
float sample_rate = std::stof(argv[2]);
15
float threshold = std::stof(argv[3]);
16
17
18
//Load Model
19
std::string model_path = "../../src/silero_vad/data/silero_vad.jit";
20
silero::VadIterator vad(model_path);
21
22
vad.threshold=threshold; //(Default:0.5)
23
vad.sample_rate=sample_rate; //16000Hz,8000Hz. (Default:16000)
24
vad.print_as_samples=true; //if true, it prints time-stamp with samples. otherwise, in seconds
25
//(Default:false)
26
27
vad.SetVariables();
28
29
// Read wav
30
wav::WavReader wav_reader(wav_path);
31
std::vector<float> input_wav(wav_reader.num_samples());
32
33
for (int i = 0; i < wav_reader.num_samples(); i++)
34
{
35
input_wav[i] = static_cast<float>(*(wav_reader.data() + i));
36
}
37
38
vad.SpeechProbs(input_wav);
39
40
std::vector<silero::SpeechSegment> speeches = vad.GetSpeechTimestamps();
41
for(const auto& speech : speeches){
42
if(vad.print_as_samples){
43
std::cout<<"{'start': "<<static_cast<int>(speech.start)<<", 'end': "<<static_cast<int>(speech.end)<<"}"<<std::endl;
44
}
45
else{
46
std::cout<<"{'start': "<<speech.start<<", 'end': "<<speech.end<<"}"<<std::endl;
47
}
48
}
49
50
51
return 0;
52
}
53
54
55
56