Split progress code

This commit is contained in:
Daniel Wolf 2019-01-02 13:22:03 +01:00
parent 44d6cb83de
commit 11a29ddef6
12 changed files with 95 additions and 85 deletions

View File

@ -467,6 +467,8 @@ add_library(rhubarb-tools
src/tools/parallel.h src/tools/parallel.h
src/tools/platformTools.cpp src/tools/platformTools.cpp
src/tools/platformTools.h src/tools/platformTools.h
src/tools/progress.cpp
src/tools/progress.h
src/tools/ProgressBar.cpp src/tools/ProgressBar.cpp
src/tools/ProgressBar.h src/tools/ProgressBar.h
src/tools/stringTools.cpp src/tools/stringTools.cpp

View File

@ -1,4 +1,5 @@
#include "processing.h" #include "processing.h"
#include <algorithm>
using std::function; using std::function;
using std::vector; using std::vector;

View File

@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <functional> #include <functional>
#include "AudioClip.h" #include "AudioClip.h"
#include "tools/ProgressBar.h" #include "tools/progress.h"
void process16bitAudioClip(const AudioClip& audioClip, std::function<void(const std::vector<int16_t>&)> processBuffer, size_t bufferCapacity, ProgressSink& progressSink); void process16bitAudioClip(const AudioClip& audioClip, std::function<void(const std::vector<int16_t>&)> processBuffer, size_t bufferCapacity, ProgressSink& progressSink);
void process16bitAudioClip(const AudioClip& audioClip, std::function<void(const std::vector<int16_t>&)> processBuffer, ProgressSink& progressSink); void process16bitAudioClip(const AudioClip& audioClip, std::function<void(const std::vector<int16_t>&)> processBuffer, ProgressSink& progressSink);

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "AudioClip.h" #include "AudioClip.h"
#include "time/BoundedTimeline.h" #include "time/BoundedTimeline.h"
#include "tools/ProgressBar.h" #include "tools/progress.h"
JoiningBoundedTimeline<void> detectVoiceActivity(const AudioClip& audioClip, int maxThreadCount, ProgressSink& progressSink); JoiningBoundedTimeline<void> detectVoiceActivity(const AudioClip& audioClip, int maxThreadCount, ProgressSink& progressSink);

View File

@ -3,7 +3,7 @@
#include "core/Shape.h" #include "core/Shape.h"
#include "time/ContinuousTimeline.h" #include "time/ContinuousTimeline.h"
#include "audio/AudioClip.h" #include "audio/AudioClip.h"
#include "tools/ProgressBar.h" #include "tools/progress.h"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "animation/targetShapeSet.h" #include "animation/targetShapeSet.h"
#include "recognition/Recognizer.h" #include "recognition/Recognizer.h"

View File

@ -2,7 +2,7 @@
#include "audio/AudioClip.h" #include "audio/AudioClip.h"
#include "core/Phone.h" #include "core/Phone.h"
#include "tools/ProgressBar.h" #include "tools/progress.h"
#include "time/BoundedTimeline.h" #include "time/BoundedTimeline.h"
class Recognizer { class Recognizer {

View File

@ -3,7 +3,7 @@
#include "time/BoundedTimeline.h" #include "time/BoundedTimeline.h"
#include "core/Phone.h" #include "core/Phone.h"
#include "audio/AudioClip.h" #include "audio/AudioClip.h"
#include "tools/ProgressBar.h" #include "tools/progress.h"
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
extern "C" { extern "C" {

View File

@ -3,52 +3,11 @@
#include <future> #include <future>
#include <chrono> #include <chrono>
#include <format.h> #include <format.h>
#include <iostream>
#include <boost/algorithm/clamp.hpp> #include <boost/algorithm/clamp.hpp>
#include <cmath> #include <cmath>
using std::string; using std::string;
ProgressForwarder::ProgressForwarder(std::function<void(double progress)> callback) :
callback(callback)
{}
void ProgressForwarder::reportProgress(double value) {
callback(value);
}
ProgressMerger::ProgressMerger(ProgressSink& sink) :
sink(sink)
{}
ProgressSink& ProgressMerger::addSink(double weight) {
std::lock_guard<std::mutex> 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<std::mutex> 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) : ProgressBar::ProgressBar(std::ostream& stream) :
stream(stream) stream(stream)
{ {

View File

@ -2,45 +2,8 @@
#include <atomic> #include <atomic>
#include <future> #include <future>
#include <functional>
#include <list>
#include <vector>
#include <mutex>
#include <string>
#include <iostream> #include <iostream>
#include "progress.h"
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<void(double progress)> callback);
void reportProgress(double value) override;
private:
std::function<void(double progress)> callback;
};
class ProgressMerger {
public:
ProgressMerger(ProgressSink& sink);
ProgressSink& addSink(double weight);
private:
void report();
ProgressSink& sink;
std::mutex mutex;
double totalWeight = 0;
std::list<ProgressForwarder> forwarders;
std::vector<double> weightedValues;
};
class ProgressBar : public ProgressSink { class ProgressBar : public ProgressSink {
public: public:

View File

@ -1,7 +1,8 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include "ProgressBar.h" #include <future>
#include "progress.h"
#include <gsl_util.h> #include <gsl_util.h>
template<typename TCollection> template<typename TCollection>

View File

@ -0,0 +1,45 @@
#include "progress.h"
#include <mutex>
using std::string;
ProgressForwarder::ProgressForwarder(std::function<void(double progress)> callback) :
callback(callback)
{}
void ProgressForwarder::reportProgress(double value) {
callback(value);
}
ProgressMerger::ProgressMerger(ProgressSink& sink) :
sink(sink)
{}
ProgressSink& ProgressMerger::addSink(double weight) {
std::lock_guard<std::mutex> 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<std::mutex> 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);
}
}

View File

@ -0,0 +1,39 @@
#pragma once
#include <list>
#include <functional>
#include <mutex>
#include <vector>
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<void(double progress)> callback);
void reportProgress(double value) override;
private:
std::function<void(double progress)> callback;
};
class ProgressMerger {
public:
ProgressMerger(ProgressSink& sink);
ProgressSink& addSink(double weight);
private:
void report();
ProgressSink& sink;
std::mutex mutex;
double totalWeight = 0;
std::list<ProgressForwarder> forwarders;
std::vector<double> weightedValues;
};