Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
snakers4
GitHub Repository: snakers4/silero-vad
Path: blob/master/examples/cpp_libtorch/silero_torch.h
1179 views
1
//Author : Nathan Lee
2
//Created On : 2024-11-18
3
//Description : silero 5.1 system for torch-script(c++).
4
//Version : 1.0
5
6
#ifndef SILERO_TORCH_H
7
#define SILERO_TORCH_H
8
9
#include <string>
10
#include <memory>
11
#include <stdexcept>
12
#include <iostream>
13
#include <memory>
14
#include <vector>
15
#include <fstream>
16
#include <chrono>
17
18
#include <torch/torch.h>
19
#include <torch/script.h>
20
21
22
namespace silero{
23
24
struct SpeechSegment{
25
int start;
26
int end;
27
};
28
29
class VadIterator{
30
public:
31
32
VadIterator(const std::string &model_path, float threshold = 0.5, int sample_rate = 16000,
33
int window_size_ms = 32, int speech_pad_ms = 30, int min_silence_duration_ms = 100,
34
int min_speech_duration_ms = 250, int max_duration_merge_ms = 300, bool print_as_samples = false);
35
~VadIterator();
36
37
38
void SpeechProbs(std::vector<float>& input_wav);
39
std::vector<silero::SpeechSegment> GetSpeechTimestamps();
40
void SetVariables();
41
42
float threshold;
43
int sample_rate;
44
int window_size_ms;
45
int min_speech_duration_ms;
46
int max_duration_merge_ms;
47
bool print_as_samples;
48
49
private:
50
torch::jit::script::Module model;
51
std::vector<float> outputs_prob;
52
int min_silence_samples;
53
int min_speech_samples;
54
int speech_pad_samples;
55
int window_size_samples;
56
int duration_merge_samples;
57
int current_sample = 0;
58
59
int total_sample_size=0;
60
61
int min_silence_duration_ms;
62
int speech_pad_ms;
63
bool triggered = false;
64
int temp_end = 0;
65
66
void init_engine(int window_size_ms);
67
void init_torch_model(const std::string& model_path);
68
void reset_states();
69
std::vector<SpeechSegment> DoVad();
70
std::vector<SpeechSegment> mergeSpeeches(const std::vector<SpeechSegment>& speeches, int duration_merge_samples);
71
72
};
73
74
}
75
#endif // SILERO_TORCH_H
76
77