Fixed VAD error handling

This commit is contained in:
Daniel Wolf 2016-06-30 20:17:28 +02:00
parent ed27b8470c
commit 6de7ba020a
1 changed files with 8 additions and 2 deletions

View File

@ -31,14 +31,20 @@ BoundedTimeline<void> webRtcDetectVoiceActivity(AudioStream& audioStream, Progre
// Detect activity // Detect activity
BoundedTimeline<void> activity(audioStream.getTruncatedRange()); BoundedTimeline<void> activity(audioStream.getTruncatedRange());
centiseconds time = centiseconds::zero(); centiseconds time = centiseconds::zero();
const size_t bufferCapacity = audioStream.getSampleRate() / 100;
auto processBuffer = [&](const vector<int16_t>& buffer) { auto processBuffer = [&](const vector<int16_t>& buffer) {
bool isActive = WebRtcVad_Process(vadHandle, audioStream.getSampleRate(), buffer.data(), buffer.size()) == 1; // WebRTC is picky regarding buffer size
if (buffer.size() < bufferCapacity) return;
int result = WebRtcVad_Process(vadHandle, audioStream.getSampleRate(), buffer.data(), buffer.size()) == 1;
if (result == -1) throw runtime_error("Error processing audio buffer using WebRTC VAD.");
bool isActive = result != 0;
if (isActive) { if (isActive) {
activity.set(time, time + centiseconds(1)); activity.set(time, time + centiseconds(1));
} }
time += centiseconds(1); time += centiseconds(1);
}; };
const size_t bufferCapacity = audioStream.getSampleRate() / 100;
process16bitAudioStream(audioStream, processBuffer, bufferCapacity, progressSink); process16bitAudioStream(audioStream, processBuffer, bufferCapacity, progressSink);
return activity; return activity;