From 35e804b984c28131fe13d229c5a0867762c6e8cf Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 16 Apr 2016 13:23:11 +0200 Subject: Some DrumKitLoader refactoring. --- plugingui/progressbar.cc | 4 +-- plugingui/progressbar.h | 10 +++--- src/drumkitloader.cc | 86 ++++++++++++++++++++++-------------------------- src/drumkitloader.h | 12 +++---- src/settings.h | 12 +++---- 5 files changed, 59 insertions(+), 65 deletions(-) diff --git a/plugingui/progressbar.cc b/plugingui/progressbar.cc index f934664..8a382f5 100644 --- a/plugingui/progressbar.cc +++ b/plugingui/progressbar.cc @@ -78,7 +78,7 @@ void ProgressBar::setState(ProgressBarState state) } } -void ProgressBar::setTotal(int total) +void ProgressBar::setTotal(std::size_t total) { if(this->total != total) { @@ -87,7 +87,7 @@ void ProgressBar::setTotal(int total) } } -void ProgressBar::setValue(int value) +void ProgressBar::setValue(std::size_t value) { if(this->value != value) { diff --git a/plugingui/progressbar.h b/plugingui/progressbar.h index d48a8b5..7485e40 100644 --- a/plugingui/progressbar.h +++ b/plugingui/progressbar.h @@ -43,11 +43,11 @@ enum class ProgressBarState class ProgressBar : public Widget { public: - ProgressBar(Widget *parent); + ProgressBar(Widget* parent); ~ProgressBar(); - void setTotal(int total); - void setValue(int value); + void setTotal(std::size_t total); + void setValue(std::size_t value); void setState(ProgressBarState state); @@ -64,8 +64,8 @@ private: Painter::Bar bar_blue; Painter::Bar bar_red; - int total{0}; - int value{0}; + std::size_t total{0}; + std::size_t value{0}; }; } // GUI:: diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index d785bc4..3a0e096 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -26,6 +26,8 @@ */ #include "drumkitloader.h" +#include + #include #include "drumkitparser.h" @@ -34,6 +36,7 @@ DrumKitLoader::DrumKitLoader(Settings& settings) : framesize(0) , settings(settings) + , getter(settings) { run(); run_semaphore.wait(); // Wait for the thread to actually start. @@ -55,7 +58,7 @@ DrumKitLoader::~DrumKitLoader() void DrumKitLoader::stop() { { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); load_queue.clear(); } @@ -66,68 +69,48 @@ void DrumKitLoader::stop() void DrumKitLoader::skip() { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); load_queue.clear(); } void DrumKitLoader::setFrameSize(size_t framesize) { - DEBUG(loader, "%s pre\n", __PRETTY_FUNCTION__); - - { - MutexAutolock l(mutex); - this->framesize = framesize; - framesize_semaphore.post(); // Signal that the framesize has been set. - } - - DEBUG(loader, "%s post\n", __PRETTY_FUNCTION__); + std::lock_guard guard(mutex); + this->framesize = framesize; + framesize_semaphore.post(); // Signal that the framesize has been set. } bool DrumKitLoader::isDone() { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); return load_queue.size() == 0; } void DrumKitLoader::loadKit(DrumKit *kit) { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); DEBUG(loader, "Create AudioFile queue from DrumKit\n"); - total_num_audiofiles = 0;// For UI Progress Messages - - { // Count total number of files that need loading: - Instruments::iterator i = kit->instruments.begin(); - while(i != kit->instruments.end()) - { - Instrument *instr = *i; - total_num_audiofiles += instr->audiofiles.size(); - ++i; - } - } + std::size_t total_num_audiofiles = 0;// For UI Progress Messages - fraction = total_num_audiofiles / 200; - if(fraction == 0) + // Count total number of files that need loading: + for(auto instr : kit->instruments) { - fraction = 1; + total_num_audiofiles += instr->audiofiles.size(); } - { // Now actually queue them for loading: - Instruments::iterator i = kit->instruments.begin(); - while(i != kit->instruments.end()) - { - Instrument *instr = *i; + settings.number_of_files.store(total_num_audiofiles); - std::vector::iterator af = instr->audiofiles.begin(); - while(af != instr->audiofiles.end()) - { - AudioFile *audiofile = *af; - load_queue.push_back(audiofile); - af++; - } - - ++i; + // Now actually queue them for loading: + for(auto instr : kit->instruments) + { + std::vector::iterator af = instr->audiofiles.begin(); + while(af != instr->audiofiles.end()) + { + AudioFile *audiofile = *af; + load_queue.push_back(audiofile); + af++; } } @@ -151,23 +134,35 @@ void DrumKitLoader::thread_main() { size_t size; { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); size = load_queue.size(); } // Only sleep if queue is empty. if(size == 0) { - semaphore.wait(); + semaphore.wait(std::chrono::milliseconds(1000)); + } + + if(getter.drumkit_file.hasChanged()) + { + //std::cout << "RELOAD DRUMKIT!" << std::endl; + } + + if(getter.midimap_file.hasChanged()) + { + //std::cout << "RELOAD MIDIMAP!" << std::endl; } std::string filename; { - MutexAutolock l(mutex); + std::lock_guard guard(mutex); + if(load_queue.size() == 0) { continue; } + AudioFile *audiofile = load_queue.front(); load_queue.pop_front(); filename = audiofile->filename; @@ -185,10 +180,9 @@ void DrumKitLoader::thread_main() ++loaded; - settings.number_of_files.store(total_num_audiofiles); settings.number_of_files_loaded.store(loaded); - if(total_num_audiofiles == loaded) + if(settings.number_of_files.load() == loaded) { settings.drumkit_load_status.store(LoadStatus::Done); } diff --git a/src/drumkitloader.h b/src/drumkitloader.h index 2410074..3fd7ec1 100644 --- a/src/drumkitloader.h +++ b/src/drumkitloader.h @@ -28,10 +28,11 @@ #include #include +#include +#include "mutex.h" #include "thread.h" #include "semaphore.h" -#include "mutex.h" #include "drumkit.h" #include "settings.h" @@ -76,12 +77,11 @@ protected: Semaphore run_semaphore; Semaphore semaphore; Semaphore framesize_semaphore; - Mutex mutex; + std::mutex mutex; volatile bool running{false}; std::list load_queue; - size_t total_num_audiofiles{0}; - size_t fraction{1}; - size_t loaded{0}; - size_t framesize{0}; + std::size_t loaded{0}; + std::size_t framesize{0}; Settings& settings; + SettingsGetter getter; }; diff --git a/src/settings.h b/src/settings.h index c79e4e5..6b4e882 100644 --- a/src/settings.h +++ b/src/settings.h @@ -61,8 +61,8 @@ struct Settings Atomic enable_resampling{true}; - Atomic number_of_files; - Atomic number_of_files_loaded; + Atomic number_of_files; + Atomic number_of_files_loaded; Atomic current_file; }; @@ -86,8 +86,8 @@ struct SettingsGetter SettingRef enable_resampling; - SettingRef number_of_files; - SettingRef number_of_files_loaded; + SettingRef number_of_files; + SettingRef number_of_files_loaded; SettingRef current_file; SettingsGetter(Settings& settings) @@ -130,8 +130,8 @@ public: Notifier enable_resampling; - Notifier number_of_files; - Notifier number_of_files_loaded; + Notifier number_of_files; + Notifier number_of_files_loaded; Notifier current_file; void evaluate() -- cgit v1.2.3