diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-05-01 21:36:04 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-05-08 12:16:21 +0200 |
commit | 42953944da1261584d56405128c6eb3c0e9e7da0 (patch) | |
tree | 1f150ce606457a0bea5647374e0719ecd9646f01 /src/drumkitloader.cc | |
parent | f8feb953c6d86bee12747679e5e4dac7795a65d0 (diff) |
The last loader thread stuff.
Diffstat (limited to 'src/drumkitloader.cc')
-rw-r--r-- | src/drumkitloader.cc | 147 |
1 files changed, 103 insertions, 44 deletions
diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 3a0e096..ec86e77 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -32,11 +32,16 @@ #include "drumkitparser.h" #include "drumgizmo.h" +#include "audioinputenginemidi.h" -DrumKitLoader::DrumKitLoader(Settings& settings) - : framesize(0) - , settings(settings) +DrumKitLoader::DrumKitLoader(Settings& settings, DrumKit& kit, + AudioInputEngine& ie, + std::array<CHResampler, 64>& resampler) + : settings(settings) , getter(settings) + , kit(kit) + , ie(ie) + , resampler(resampler) { run(); run_semaphore.wait(); // Wait for the thread to actually start. @@ -55,73 +60,112 @@ DrumKitLoader::~DrumKitLoader() DEBUG(loader, "~DrumKitLoader() post\n"); } -void DrumKitLoader::stop() +bool DrumKitLoader::loadkit(const std::string& file) { + settings.drumkit_load_status.store(LoadStatus::Idle); + + if(file == "") { - std::lock_guard<std::mutex> guard(mutex); - load_queue.clear(); + settings.drumkit_load_status.store(LoadStatus::Error); + return false; } - running = false; - semaphore.post(); - wait_stop(); -} + DEBUG(drumgizmo, "loadkit(%s)\n", file.c_str()); -void DrumKitLoader::skip() -{ - std::lock_guard<std::mutex> guard(mutex); - load_queue.clear(); -} + // Remove all queue AudioFiles from loader before we actually delete them. + skip(); -void DrumKitLoader::setFrameSize(size_t framesize) -{ - std::lock_guard<std::mutex> guard(mutex); - this->framesize = framesize; - framesize_semaphore.post(); // Signal that the framesize has been set. -} + // Delete all Channels, Instruments, Samples and AudioFiles. + kit.clear(); -bool DrumKitLoader::isDone() -{ - std::lock_guard<std::mutex> guard(mutex); - return load_queue.size() == 0; + settings.drumkit_load_status.store(LoadStatus::Loading); + + DrumKitParser parser(settings, kit); + if(parser.parseFile(file)) + { + ERR(drumgizmo, "Drumkit parser failed: %s\n", file.c_str()); + settings.drumkit_load_status.store(LoadStatus::Error); + return false; + } + + // Check if there is enough free RAM to load the drumkit. + if(!memchecker.enoughFreeMemory(kit)) + { + printf("WARNING: " + "There doesn't seem to be enough RAM available to load the kit.\n" + "Trying to load it anyway...\n"); + } + + loadKit(&kit); + +#ifdef WITH_RESAMPLER + for(auto& chresampler: resampler) + { + chresampler.setup(kit.getSamplerate(), settings.samplerate.load()); + } +#endif/*WITH_RESAMPLER*/ + + + DEBUG(loadkit, "loadkit: Success\n"); + + return true; } void DrumKitLoader::loadKit(DrumKit *kit) { - std::lock_guard<std::mutex> guard(mutex); +// std::lock_guard<std::mutex> guard(mutex); DEBUG(loader, "Create AudioFile queue from DrumKit\n"); - std::size_t total_num_audiofiles = 0;// For UI Progress Messages + settings.number_of_files_loaded.store(0); // Count total number of files that need loading: + settings.number_of_files.store(0); for(auto instr : kit->instruments) { - total_num_audiofiles += instr->audiofiles.size(); + settings.number_of_files.fetch_add(instr->audiofiles.size()); } - settings.number_of_files.store(total_num_audiofiles); - // Now actually queue them for loading: for(auto instr : kit->instruments) { - std::vector<AudioFile*>::iterator af = instr->audiofiles.begin(); - while(af != instr->audiofiles.end()) + for(auto audiofile : instr->audiofiles) { - AudioFile *audiofile = *af; load_queue.push_back(audiofile); - af++; } } - loaded = 0; // For UI Progress Messages - DEBUG(loader, "Queued %d (size: %d) AudioFiles for loading.\n", - (int)total_num_audiofiles, (int)load_queue.size()); + (int)settings.number_of_files.load(), (int)load_queue.size()); semaphore.post(); // Start loader loop. } +void DrumKitLoader::stop() +{ + { + std::lock_guard<std::mutex> guard(mutex); + load_queue.clear(); + } + + running = false; + semaphore.post(); + wait_stop(); +} + +void DrumKitLoader::skip() +{ + std::lock_guard<std::mutex> guard(mutex); + load_queue.clear(); +} + +void DrumKitLoader::setFrameSize(size_t framesize) +{ + std::lock_guard<std::mutex> guard(mutex); + this->framesize = framesize; + framesize_semaphore.post(); // Signal that the framesize has been set. +} + void DrumKitLoader::thread_main() { running = true; @@ -144,14 +188,31 @@ void DrumKitLoader::thread_main() semaphore.wait(std::chrono::milliseconds(1000)); } + bool newKit = false; if(getter.drumkit_file.hasChanged()) { - //std::cout << "RELOAD DRUMKIT!" << std::endl; + loadkit(getter.drumkit_file.getValue()); + newKit = true; } - if(getter.midimap_file.hasChanged()) + if(getter.midimap_file.hasChanged() || newKit) { - //std::cout << "RELOAD MIDIMAP!" << std::endl; + auto ie_midi = dynamic_cast<AudioInputEngineMidi*>(&ie); + std::cout << "ie_midi: " << (void*)ie_midi << std::endl; + if(ie_midi) + { + settings.midimap_load_status.store(LoadStatus::Loading); + bool ret = ie_midi->loadMidiMap(getter.midimap_file.getValue(), + kit.instruments); + if(ret) + { + settings.midimap_load_status.store(LoadStatus::Done); + } + else + { + settings.midimap_load_status.store(LoadStatus::Error); + } + } } std::string filename; @@ -178,11 +239,9 @@ void DrumKitLoader::thread_main() audiofile->load(preload_size); } - ++loaded; - - settings.number_of_files_loaded.store(loaded); + settings.number_of_files_loaded.fetch_add(1); - if(settings.number_of_files.load() == loaded) + if(settings.number_of_files.load() == settings.number_of_files_loaded.load()) { settings.drumkit_load_status.store(LoadStatus::Done); } |