voice-activity-detector.cc
6.3 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
// sherpa-onnx/csrc/voice-activity-detector.cc
//
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/voice-activity-detector.h"
#include <algorithm>
#include <queue>
#include <utility>
#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#if __OHOS__
#include "rawfile/raw_file_manager.h"
#endif
#include "sherpa-onnx/csrc/circular-buffer.h"
#include "sherpa-onnx/csrc/vad-model.h"
namespace sherpa_onnx {
class VoiceActivityDetector::Impl {
public:
explicit Impl(const VadModelConfig &config, float buffer_size_in_seconds = 60)
: model_(VadModel::Create(config)),
config_(config),
buffer_(buffer_size_in_seconds * config.sample_rate) {
Init();
}
template <typename Manager>
Impl(Manager *mgr, const VadModelConfig &config,
float buffer_size_in_seconds = 60)
: model_(VadModel::Create(mgr, config)),
config_(config),
buffer_(buffer_size_in_seconds * config.sample_rate) {
Init();
}
void AcceptWaveform(const float *samples, int32_t n) {
if (buffer_.Size() > max_utterance_length_) {
model_->SetMinSilenceDuration(new_min_silence_duration_s_);
model_->SetThreshold(new_threshold_);
} else {
model_->SetMinSilenceDuration(config_.silero_vad.min_silence_duration);
model_->SetThreshold(config_.silero_vad.threshold);
}
int32_t window_size = model_->WindowSize();
int32_t window_shift = model_->WindowShift();
// note n is usually window_size and there is no need to use
// an extra buffer here
last_.insert(last_.end(), samples, samples + n);
if (last_.size() < window_size) {
return;
}
// Note: For v4, window_shift == window_size
int32_t k =
(static_cast<int32_t>(last_.size()) - window_size) / window_shift + 1;
const float *p = last_.data();
bool is_speech = false;
for (int32_t i = 0; i < k; ++i, p += window_shift) {
buffer_.Push(p, window_shift);
// NOTE(fangjun): Please don't use a very large n.
bool this_window_is_speech = model_->IsSpeech(p, window_size);
is_speech = is_speech || this_window_is_speech;
}
last_ = std::vector<float>(
p, static_cast<const float *>(last_.data()) + last_.size());
if (is_speech) {
if (start_ == -1) {
// beginning of speech
start_ = std::max(buffer_.Tail() - 2 * model_->WindowSize() -
model_->MinSpeechDurationSamples(),
buffer_.Head());
}
} else {
// non-speech
if (start_ != -1 && buffer_.Size()) {
// end of speech, save the speech segment
int32_t end = buffer_.Tail() - model_->MinSilenceDurationSamples();
std::vector<float> s = buffer_.Get(start_, end - start_);
SpeechSegment segment;
segment.start = start_;
segment.samples = std::move(s);
segments_.push(std::move(segment));
buffer_.Pop(end - buffer_.Head());
}
if (start_ == -1) {
int32_t end = buffer_.Tail() - 2 * model_->WindowSize() -
model_->MinSpeechDurationSamples();
int32_t n = std::max(0, end - buffer_.Head());
if (n > 0) {
buffer_.Pop(n);
}
}
start_ = -1;
}
}
bool Empty() const { return segments_.empty(); }
void Pop() { segments_.pop(); }
void Clear() { std::queue<SpeechSegment>().swap(segments_); }
const SpeechSegment &Front() const { return segments_.front(); }
void Reset() {
std::queue<SpeechSegment>().swap(segments_);
model_->Reset();
buffer_.Reset();
start_ = -1;
}
void Flush() {
if (start_ == -1 || buffer_.Size() == 0) {
return;
}
int32_t end = buffer_.Tail();
if (end <= start_) {
return;
}
std::vector<float> s = buffer_.Get(start_, end - start_);
SpeechSegment segment;
segment.start = start_;
segment.samples = std::move(s);
segments_.push(std::move(segment));
buffer_.Pop(end - buffer_.Head());
start_ = -1;
}
bool IsSpeechDetected() const { return start_ != -1; }
const VadModelConfig &GetConfig() const { return config_; }
private:
void Init() {
// TODO(fangjun): Currently, we support only one vad model.
// If a new vad model is added, we need to change the place
// where max_speech_duration is placed.
max_utterance_length_ =
config_.sample_rate * config_.silero_vad.max_speech_duration;
}
private:
std::queue<SpeechSegment> segments_;
std::unique_ptr<VadModel> model_;
VadModelConfig config_;
CircularBuffer buffer_;
std::vector<float> last_;
int max_utterance_length_ = -1; // in samples
float new_min_silence_duration_s_ = 0.1;
float new_threshold_ = 0.90;
int32_t start_ = -1;
};
VoiceActivityDetector::VoiceActivityDetector(
const VadModelConfig &config, float buffer_size_in_seconds /*= 60*/)
: impl_(std::make_unique<Impl>(config, buffer_size_in_seconds)) {}
template <typename Manager>
VoiceActivityDetector::VoiceActivityDetector(
Manager *mgr, const VadModelConfig &config,
float buffer_size_in_seconds /*= 60*/)
: impl_(std::make_unique<Impl>(mgr, config, buffer_size_in_seconds)) {}
VoiceActivityDetector::~VoiceActivityDetector() = default;
void VoiceActivityDetector::AcceptWaveform(const float *samples, int32_t n) {
impl_->AcceptWaveform(samples, n);
}
bool VoiceActivityDetector::Empty() const { return impl_->Empty(); }
void VoiceActivityDetector::Pop() { impl_->Pop(); }
void VoiceActivityDetector::Clear() { impl_->Clear(); }
const SpeechSegment &VoiceActivityDetector::Front() const {
return impl_->Front();
}
void VoiceActivityDetector::Reset() const { impl_->Reset(); }
void VoiceActivityDetector::Flush() const { impl_->Flush(); }
bool VoiceActivityDetector::IsSpeechDetected() const {
return impl_->IsSpeechDetected();
}
const VadModelConfig &VoiceActivityDetector::GetConfig() const {
return impl_->GetConfig();
}
#if __ANDROID_API__ >= 9
template VoiceActivityDetector::VoiceActivityDetector(
AAssetManager *mgr, const VadModelConfig &config,
float buffer_size_in_seconds = 60);
#endif
#if __OHOS__
template VoiceActivityDetector::VoiceActivityDetector(
NativeResourceManager *mgr, const VadModelConfig &config,
float buffer_size_in_seconds = 60);
#endif
} // namespace sherpa_onnx