diff --git a/rhubarb/CMakeLists.txt b/rhubarb/CMakeLists.txt index 0fef2b7..0c0fd8c 100644 --- a/rhubarb/CMakeLists.txt +++ b/rhubarb/CMakeLists.txt @@ -467,6 +467,8 @@ add_library(rhubarb-tools src/tools/parallel.h src/tools/platformTools.cpp src/tools/platformTools.h + src/tools/progress.cpp + src/tools/progress.h src/tools/ProgressBar.cpp src/tools/ProgressBar.h src/tools/stringTools.cpp diff --git a/rhubarb/src/audio/processing.cpp b/rhubarb/src/audio/processing.cpp index 39cc8db..316efd7 100644 --- a/rhubarb/src/audio/processing.cpp +++ b/rhubarb/src/audio/processing.cpp @@ -1,4 +1,5 @@ #include "processing.h" +#include using std::function; using std::vector; diff --git a/rhubarb/src/audio/processing.h b/rhubarb/src/audio/processing.h index 8ece651..dc09c56 100644 --- a/rhubarb/src/audio/processing.h +++ b/rhubarb/src/audio/processing.h @@ -3,7 +3,7 @@ #include #include #include "AudioClip.h" -#include "tools/ProgressBar.h" +#include "tools/progress.h" void process16bitAudioClip(const AudioClip& audioClip, std::function&)> processBuffer, size_t bufferCapacity, ProgressSink& progressSink); void process16bitAudioClip(const AudioClip& audioClip, std::function&)> processBuffer, ProgressSink& progressSink); diff --git a/rhubarb/src/audio/voiceActivityDetection.h b/rhubarb/src/audio/voiceActivityDetection.h index 7820680..0e5bcb9 100644 --- a/rhubarb/src/audio/voiceActivityDetection.h +++ b/rhubarb/src/audio/voiceActivityDetection.h @@ -1,6 +1,6 @@ #pragma once #include "AudioClip.h" #include "time/BoundedTimeline.h" -#include "tools/ProgressBar.h" +#include "tools/progress.h" JoiningBoundedTimeline detectVoiceActivity(const AudioClip& audioClip, int maxThreadCount, ProgressSink& progressSink); diff --git a/rhubarb/src/lib/rhubarbLib.h b/rhubarb/src/lib/rhubarbLib.h index ca40a06..ae841a6 100644 --- a/rhubarb/src/lib/rhubarbLib.h +++ b/rhubarb/src/lib/rhubarbLib.h @@ -3,7 +3,7 @@ #include "core/Shape.h" #include "time/ContinuousTimeline.h" #include "audio/AudioClip.h" -#include "tools/ProgressBar.h" +#include "tools/progress.h" #include #include "animation/targetShapeSet.h" #include "recognition/Recognizer.h" diff --git a/rhubarb/src/recognition/Recognizer.h b/rhubarb/src/recognition/Recognizer.h index 05c445d..0903680 100644 --- a/rhubarb/src/recognition/Recognizer.h +++ b/rhubarb/src/recognition/Recognizer.h @@ -2,7 +2,7 @@ #include "audio/AudioClip.h" #include "core/Phone.h" -#include "tools/ProgressBar.h" +#include "tools/progress.h" #include "time/BoundedTimeline.h" class Recognizer { diff --git a/rhubarb/src/recognition/pocketSphinxTools.h b/rhubarb/src/recognition/pocketSphinxTools.h index 568ccbe..9a72199 100644 --- a/rhubarb/src/recognition/pocketSphinxTools.h +++ b/rhubarb/src/recognition/pocketSphinxTools.h @@ -3,7 +3,7 @@ #include "time/BoundedTimeline.h" #include "core/Phone.h" #include "audio/AudioClip.h" -#include "tools/ProgressBar.h" +#include "tools/progress.h" #include extern "C" { diff --git a/rhubarb/src/tools/ProgressBar.cpp b/rhubarb/src/tools/ProgressBar.cpp index 404a6b8..a436e69 100644 --- a/rhubarb/src/tools/ProgressBar.cpp +++ b/rhubarb/src/tools/ProgressBar.cpp @@ -3,52 +3,11 @@ #include #include #include -#include #include #include using std::string; -ProgressForwarder::ProgressForwarder(std::function callback) : - callback(callback) -{} - -void ProgressForwarder::reportProgress(double value) { - callback(value); -} - -ProgressMerger::ProgressMerger(ProgressSink& sink) : - sink(sink) -{} - -ProgressSink& ProgressMerger::addSink(double weight) { - std::lock_guard lock(mutex); - - totalWeight += weight; - int sinkIndex = weightedValues.size(); - weightedValues.push_back(0); - forwarders.push_back(ProgressForwarder([weight, sinkIndex, this](double progress) { - weightedValues[sinkIndex] = progress * weight; - report(); - })); - return forwarders.back(); -} - -void ProgressMerger::report() { - std::lock_guard lock(mutex); - - if (totalWeight != 0) { - double weightedSum = 0; - for (double weightedValue : weightedValues) { - weightedSum += weightedValue; - } - double progress = weightedSum / totalWeight; - sink.reportProgress(progress); - } else { - sink.reportProgress(0); - } -} - ProgressBar::ProgressBar(std::ostream& stream) : stream(stream) { diff --git a/rhubarb/src/tools/ProgressBar.h b/rhubarb/src/tools/ProgressBar.h index 0c005c7..7e5bd8f 100644 --- a/rhubarb/src/tools/ProgressBar.h +++ b/rhubarb/src/tools/ProgressBar.h @@ -2,45 +2,8 @@ #include #include -#include -#include -#include -#include -#include #include - -class ProgressSink { -public: - virtual ~ProgressSink() {} - virtual void reportProgress(double value) = 0; -}; - -class NullProgressSink : public ProgressSink { -public: - void reportProgress(double) override {} -}; - -class ProgressForwarder : public ProgressSink { -public: - ProgressForwarder(std::function callback); - void reportProgress(double value) override; -private: - std::function callback; -}; - -class ProgressMerger { -public: - ProgressMerger(ProgressSink& sink); - ProgressSink& addSink(double weight); -private: - void report(); - - ProgressSink& sink; - std::mutex mutex; - double totalWeight = 0; - std::list forwarders; - std::vector weightedValues; -}; +#include "progress.h" class ProgressBar : public ProgressSink { public: diff --git a/rhubarb/src/tools/parallel.h b/rhubarb/src/tools/parallel.h index cca4164..de8c5d5 100644 --- a/rhubarb/src/tools/parallel.h +++ b/rhubarb/src/tools/parallel.h @@ -1,7 +1,8 @@ #pragma once #include -#include "ProgressBar.h" +#include +#include "progress.h" #include template diff --git a/rhubarb/src/tools/progress.cpp b/rhubarb/src/tools/progress.cpp new file mode 100644 index 0000000..34c5848 --- /dev/null +++ b/rhubarb/src/tools/progress.cpp @@ -0,0 +1,45 @@ +#include "progress.h" + +#include + +using std::string; + +ProgressForwarder::ProgressForwarder(std::function callback) : + callback(callback) +{} + +void ProgressForwarder::reportProgress(double value) { + callback(value); +} + +ProgressMerger::ProgressMerger(ProgressSink& sink) : + sink(sink) +{} + +ProgressSink& ProgressMerger::addSink(double weight) { + std::lock_guard lock(mutex); + + totalWeight += weight; + int sinkIndex = weightedValues.size(); + weightedValues.push_back(0); + forwarders.push_back(ProgressForwarder([weight, sinkIndex, this](double progress) { + weightedValues[sinkIndex] = progress * weight; + report(); + })); + return forwarders.back(); +} + +void ProgressMerger::report() { + std::lock_guard lock(mutex); + + if (totalWeight != 0) { + double weightedSum = 0; + for (double weightedValue : weightedValues) { + weightedSum += weightedValue; + } + double progress = weightedSum / totalWeight; + sink.reportProgress(progress); + } else { + sink.reportProgress(0); + } +} diff --git a/rhubarb/src/tools/progress.h b/rhubarb/src/tools/progress.h new file mode 100644 index 0000000..23f4965 --- /dev/null +++ b/rhubarb/src/tools/progress.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include + +class ProgressSink { +public: + virtual ~ProgressSink() {} + virtual void reportProgress(double value) = 0; +}; + +class NullProgressSink : public ProgressSink { +public: + void reportProgress(double) override {} +}; + +class ProgressForwarder : public ProgressSink { +public: + ProgressForwarder(std::function callback); + void reportProgress(double value) override; +private: + std::function callback; +}; + +class ProgressMerger { +public: + ProgressMerger(ProgressSink& sink); + ProgressSink& addSink(double weight); +private: + void report(); + + ProgressSink& sink; + std::mutex mutex; + double totalWeight = 0; + std::list forwarders; + std::vector weightedValues; +};