From 774815a97901fe9d5ce2767a8c479c52a5ad0ce8 Mon Sep 17 00:00:00 2001 From: - Date: Thu, 9 May 2013 17:54:45 +0200 Subject: Lazy load of audio. Unloads when there's nothing else to do. --- src/drumkitloader.cc | 126 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 39 deletions(-) (limited to 'src/drumkitloader.cc') diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 79d000a..6516c7d 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -82,14 +82,40 @@ void DrumKitLoader::loadKit(DrumKit *kit) semaphore.post(); } +void DrumKitLoader::prepare(AudioFile* af) { + printf("Preparing audiofile %p (%d in queue)\n", af, load_queue.size()); + mutex.lock(); + af->ref_count++; + load_queue.push_back(af); +// if(ref_count.find(af) == ref_count.end()) { +// ref_count[af]++; +// } +// else { +// ref_count[af] = 0; +// } + mutex.unlock(); + semaphore.post(); +} + +void DrumKitLoader::reset(AudioFile* af) { + mutex.lock(); + af->ref_count--; + reset_queue.push_back(af); + mutex.unlock(); + semaphore.post(); +} + void DrumKitLoader::thread_main() { while(1) { DEBUG(loader, "before sem\n"); + semaphore.wait(); + DEBUG(loader, "after sem\n"); fflush(stdout); + if(quitit) return; if(skipit) { @@ -98,66 +124,88 @@ void DrumKitLoader::thread_main() continue; } - unsigned int count = 0; + if(!load_queue.empty()) { + printf("Loading remaining of audio file\n"); + AudioFile* af = load_queue.front(); + mutex.lock(); + load_queue.pop_front(); + mutex.unlock(); + af->loadNext(); + } + else if(!reset_queue.empty()) { + AudioFile* af = reset_queue.front(); + mutex.lock(); + if(af->ref_count <= 0) { + af->reset(); + af->ref_count = 0; + } + reset_queue.pop_front(); + mutex.unlock(); + } + else { // Initialize drum kit + printf("Initializing drum kit\n"); + unsigned int count = 0; - if(kit && !kit->isValid()) goto finish; + if(kit && !kit->isValid()) goto finish; - { // Count total number of files that need loading: - Instruments::iterator i = kit->instruments.begin(); - while(i != kit->instruments.end()) { - Instrument *instr = *i; - if(instr && !instr->isValid()) goto finish; + { // Count total number of files that need loading: + Instruments::iterator i = kit->instruments.begin(); + while(i != kit->instruments.end()) { + Instrument *instr = *i; + if(instr && !instr->isValid()) goto finish; - count += instr->audiofiles.size(); - i++; + count += instr->audiofiles.size(); + i++; + } } - } - { // Now actually load them: - unsigned int loaded = 0; - Instruments::iterator i = kit->instruments.begin(); - while(i != kit->instruments.end()) { - Instrument *instr = *i; + { // Now actually load them: + unsigned int loaded = 0; + Instruments::iterator i = kit->instruments.begin(); + while(i != kit->instruments.end()) { + Instrument *instr = *i; - if(instr && !instr->isValid()) goto finish; + if(instr && !instr->isValid()) goto finish; - std::vector::iterator a = instr->audiofiles.begin(); - while(a != instr->audiofiles.end()) { + std::vector::iterator a = instr->audiofiles.begin(); + while(a != instr->audiofiles.end()) { #if 0 #ifdef WIN32 - SleepEx(5000, FALSE); + SleepEx(5000, FALSE); #else - usleep(5000); + usleep(5000); #endif/*WIN32*/ #endif - AudioFile *af = *a; + AudioFile *af = *a; - if(af && !af->isValid()) goto finish; + if(af && !af->isValid()) goto finish; - af->load(); - loaded++; + af->load(); + + loaded++; - LoadStatusMessage *ls = new LoadStatusMessage(); - ls->number_of_files = count; - ls->numer_of_files_loaded = loaded; - ls->current_file = af->filename; - drumgizmo->sendGUIMessage(ls); + LoadStatusMessage *ls = new LoadStatusMessage(); + ls->number_of_files = count; + ls->numer_of_files_loaded = loaded; + ls->current_file = af->filename; + drumgizmo->sendGUIMessage(ls); - a++; + a++; - if(skipit) goto finish; - } + if(skipit) goto finish; + } - i++; + i++; + } } - } - mutex.lock(); - is_done = true; - mutex.unlock(); + mutex.lock(); + is_done = true; + mutex.unlock(); - finish: - continue; + finish: + continue; + } } } -- cgit v1.2.3