From 229105a965ae64c99d8b8c087a291e24f3a50ec1 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Thu, 4 Aug 2016 20:39:40 +0200 Subject: [PATCH] Fixed erratic progress display --- src/ProgressBar.cpp | 3 ++- src/ProgressBar.h | 6 +++++- src/audio/voiceActivityDetection.cpp | 8 ++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ProgressBar.cpp b/src/ProgressBar.cpp index 3b0d726..26a1e7f 100644 --- a/src/ProgressBar.cpp +++ b/src/ProgressBar.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using std::string; @@ -58,7 +59,7 @@ ProgressBar::~ProgressBar() { void ProgressBar::reportProgress(double value) { // Make sure value is in [0..1] range - value = std::max(0.0, std::min(1.0, value)); + value = boost::algorithm::clamp(value, 0.0, 1.0); currentProgress = value; } diff --git a/src/ProgressBar.h b/src/ProgressBar.h index 59f9964..ba697fc 100644 --- a/src/ProgressBar.h +++ b/src/ProgressBar.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -14,6 +13,11 @@ public: virtual void reportProgress(double value) = 0; }; +class NullProgressSink : public ProgressSink { +public: + void reportProgress(double) override {} +}; + class ProgressForwarder : public ProgressSink { public: ProgressForwarder(std::function callback); diff --git a/src/audio/voiceActivityDetection.cpp b/src/audio/voiceActivityDetection.cpp index 60bb3fc..e3970ee 100644 --- a/src/audio/voiceActivityDetection.cpp +++ b/src/audio/voiceActivityDetection.cpp @@ -29,6 +29,10 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog error = WebRtcVad_set_mode(vadHandle, aggressiveness); if (error) throw runtime_error("Error setting WebRTC VAD aggressiveness."); + ProgressMerger progressMerger(progressSink); + ProgressSink& pass1ProgressSink = progressMerger.addSink(1.0); + ProgressSink& pass2ProgressSink = progressMerger.addSink(0.3); + // Detect activity BoundedTimeline activity(audioClip.getTruncatedRange()); centiseconds time = 0cs; @@ -46,7 +50,7 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog } time += 1cs; }; - process16bitAudioClip(audioClip, processBuffer, bufferCapacity, progressSink); + process16bitAudioClip(audioClip, processBuffer, bufferCapacity, pass1ProgressSink); // WebRTC adapts to the audio. This means results may not be correct at the very beginning. // It sometimes returns false activity at the very beginning, mistaking the background noise for speech. @@ -56,7 +60,7 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog activity.clear(firstActivity); unique_ptr streamStart = audioClip.clone() | segment(TimeRange(0cs, firstActivity.getEnd())); time = 0cs; - process16bitAudioClip(*streamStart, processBuffer, bufferCapacity, progressSink); + process16bitAudioClip(*streamStart, processBuffer, bufferCapacity, pass2ProgressSink); } return activity;