silero-vad-model.cc
8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// sherpa-onnx/csrc/silero-vad-model.cc
//
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/silero-vad-model.h"
#include <string>
#include <utility>
#include <vector>
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/session.h"
namespace sherpa_onnx {
class SileroVadModel::Impl {
public:
explicit Impl(const VadModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
auto buf = ReadFile(config.silero_vad.model);
Init(buf.data(), buf.size());
sample_rate_ = config.sample_rate;
if (sample_rate_ != 16000) {
SHERPA_ONNX_LOGE("Expected sample rate 16000. Given: %d",
config.sample_rate);
exit(-1);
}
min_silence_samples_ =
sample_rate_ * config_.silero_vad.min_silence_duration;
min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration;
}
#if __ANDROID_API__ >= 9
Impl(AAssetManager *mgr, const VadModelConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
allocator_{} {
auto buf = ReadFile(mgr, config.silero_vad.model);
Init(buf.data(), buf.size());
sample_rate_ = config.sample_rate;
if (sample_rate_ != 16000) {
SHERPA_ONNX_LOGE("Expected sample rate 16000. Given: %d",
config.sample_rate);
exit(-1);
}
min_silence_samples_ =
sample_rate_ * config_.silero_vad.min_silence_duration;
min_speech_samples_ = sample_rate_ * config_.silero_vad.min_speech_duration;
}
#endif
void Reset() {
// 2 - number of LSTM layer
// 1 - batch size
// 64 - hidden dim
std::array<int64_t, 3> shape{2, 1, 64};
Ort::Value h =
Ort::Value::CreateTensor<float>(allocator_, shape.data(), shape.size());
Ort::Value c =
Ort::Value::CreateTensor<float>(allocator_, shape.data(), shape.size());
Fill<float>(&h, 0);
Fill<float>(&c, 0);
states_.clear();
states_.reserve(2);
states_.push_back(std::move(h));
states_.push_back(std::move(c));
triggered_ = false;
current_sample_ = 0;
temp_start_ = 0;
temp_end_ = 0;
}
bool IsSpeech(const float *samples, int32_t n) {
if (n != config_.silero_vad.window_size) {
SHERPA_ONNX_LOGE("n: %d != window_size: %d", n,
config_.silero_vad.window_size);
exit(-1);
}
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
std::array<int64_t, 2> x_shape = {1, n};
Ort::Value x =
Ort::Value::CreateTensor(memory_info, const_cast<float *>(samples), n,
x_shape.data(), x_shape.size());
int64_t sr_shape = 1;
Ort::Value sr =
Ort::Value::CreateTensor(memory_info, &sample_rate_, 1, &sr_shape, 1);
std::array<Ort::Value, 4> inputs = {std::move(x), std::move(sr),
std::move(states_[0]),
std::move(states_[1])};
auto out =
sess_->Run({}, input_names_ptr_.data(), inputs.data(), inputs.size(),
output_names_ptr_.data(), output_names_ptr_.size());
states_[0] = std::move(out[1]);
states_[1] = std::move(out[2]);
float prob = out[0].GetTensorData<float>()[0];
float threshold = config_.silero_vad.threshold;
current_sample_ += config_.silero_vad.window_size;
if (prob > threshold && temp_end_ != 0) {
temp_end_ = 0;
}
if (prob > threshold && temp_start_ == 0) {
// start speaking, but we require that it must satisfy
// min_speech_duration
temp_start_ = current_sample_;
return false;
}
if (prob > threshold && temp_start_ != 0 && !triggered_) {
if (current_sample_ - temp_start_ < min_speech_samples_) {
return false;
}
triggered_ = true;
return true;
}
if ((prob < threshold) && !triggered_) {
// silence
temp_start_ = 0;
temp_end_ = 0;
return false;
}
if ((prob > threshold - 0.15) && triggered_) {
// speaking
return true;
}
if ((prob > threshold) && !triggered_) {
// start speaking
triggered_ = true;
return true;
}
if ((prob < threshold) && triggered_) {
// stop to speak
if (temp_end_ == 0) {
temp_end_ = current_sample_;
}
if (current_sample_ - temp_end_ < min_silence_samples_) {
// continue speaking
return true;
}
// stopped speaking
temp_start_ = 0;
temp_end_ = 0;
triggered_ = false;
return false;
}
return false;
}
int32_t WindowSize() const { return config_.silero_vad.window_size; }
int32_t MinSilenceDurationSamples() const { return min_silence_samples_; }
int32_t MinSpeechDurationSamples() const { return min_speech_samples_; }
private:
void Init(void *model_data, size_t model_data_length) {
sess_ = std::make_unique<Ort::Session>(env_, model_data, model_data_length,
sess_opts_);
GetInputNames(sess_.get(), &input_names_, &input_names_ptr_);
GetOutputNames(sess_.get(), &output_names_, &output_names_ptr_);
Check();
Reset();
}
void Check() {
if (input_names_.size() != 4) {
SHERPA_ONNX_LOGE("Expect 4 inputs. Given: %d",
static_cast<int32_t>(input_names_.size()));
exit(-1);
}
if (input_names_[0] != "input") {
SHERPA_ONNX_LOGE("Input[0]: %s. Expected: input",
input_names_[0].c_str());
exit(-1);
}
if (input_names_[1] != "sr") {
SHERPA_ONNX_LOGE("Input[1]: %s. Expected: sr", input_names_[1].c_str());
exit(-1);
}
if (input_names_[2] != "h") {
SHERPA_ONNX_LOGE("Input[2]: %s. Expected: h", input_names_[2].c_str());
exit(-1);
}
if (input_names_[3] != "c") {
SHERPA_ONNX_LOGE("Input[3]: %s. Expected: c", input_names_[3].c_str());
exit(-1);
}
// Now for outputs
if (output_names_.size() != 3) {
SHERPA_ONNX_LOGE("Expect 3 outputs. Given: %d",
static_cast<int32_t>(output_names_.size()));
exit(-1);
}
if (output_names_[0] != "output") {
SHERPA_ONNX_LOGE("Output[0]: %s. Expected: output",
output_names_[0].c_str());
exit(-1);
}
if (output_names_[1] != "hn") {
SHERPA_ONNX_LOGE("Output[1]: %s. Expected: sr", output_names_[1].c_str());
exit(-1);
}
if (output_names_[2] != "cn") {
SHERPA_ONNX_LOGE("Output[2]: %s. Expected: sr", output_names_[2].c_str());
exit(-1);
}
}
private:
VadModelConfig config_;
Ort::Env env_;
Ort::SessionOptions sess_opts_;
Ort::AllocatorWithDefaultOptions allocator_;
std::unique_ptr<Ort::Session> sess_;
std::vector<std::string> input_names_;
std::vector<const char *> input_names_ptr_;
std::vector<std::string> output_names_;
std::vector<const char *> output_names_ptr_;
std::vector<Ort::Value> states_;
int64_t sample_rate_;
int32_t min_silence_samples_;
int32_t min_speech_samples_;
bool triggered_ = false;
int32_t current_sample_ = 0;
int32_t temp_start_ = 0;
int32_t temp_end_ = 0;
};
SileroVadModel::SileroVadModel(const VadModelConfig &config)
: impl_(std::make_unique<Impl>(config)) {}
#if __ANDROID_API__ >= 9
SileroVadModel::SileroVadModel(AAssetManager *mgr, const VadModelConfig &config)
: impl_(std::make_unique<Impl>(mgr, config)) {}
#endif
SileroVadModel::~SileroVadModel() = default;
void SileroVadModel::Reset() { return impl_->Reset(); }
bool SileroVadModel::IsSpeech(const float *samples, int32_t n) {
return impl_->IsSpeech(samples, n);
}
int32_t SileroVadModel::WindowSize() const { return impl_->WindowSize(); }
int32_t SileroVadModel::MinSilenceDurationSamples() const {
return impl_->MinSilenceDurationSamples();
}
int32_t SileroVadModel::MinSpeechDurationSamples() const {
return impl_->MinSpeechDurationSamples();
}
} // namespace sherpa_onnx