diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-03-06 19:28:55 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-03-06 19:28:55 +0100 |
commit | e0250f0bcca16c14028b00fd9894446d844e2099 (patch) | |
tree | 796d4e31c3b60958746846f9896f0e8470585955 | |
parent | 23df5bd31202c65952fc181c6ad1b611a0c9e26a (diff) |
Improve load state check when freewheeling to also block during load and not just not loaded at all.audiofile_load_state
-rw-r--r-- | src/audiofile.cc | 19 | ||||
-rw-r--r-- | src/audiofile.h | 10 | ||||
-rw-r--r-- | src/drumgizmo.cc | 2 |
3 files changed, 26 insertions, 5 deletions
diff --git a/src/audiofile.cc b/src/audiofile.cc index 1228044..bf27e33 100644 --- a/src/audiofile.cc +++ b/src/audiofile.cc @@ -64,7 +64,7 @@ void AudioFile::unload() // Make sure we don't unload the object while loading it... const std::lock_guard<std::mutex> guard(mutex); - is_loaded.store(false); + load_state.store(LoadState::not_loaded); preloadedsize = 0; size = 0; @@ -84,6 +84,8 @@ void AudioFile::load(const LogFunction& logger, std::size_t sample_limit) return; } + load_state.store(LoadState::loading); + SF_INFO sf_info{}; SNDFILE *file_handle = sf_open(filename.c_str(), SFM_READ, &sf_info); if(file_handle == nullptr) @@ -95,6 +97,7 @@ void AudioFile::load(const LogFunction& logger, std::size_t sample_limit) logger(LogLevel::Warning, "Could not load '" + filename + "': " + sf_strerror(file_handle)); } + load_state.store(LoadState::failed); return; } @@ -106,6 +109,7 @@ void AudioFile::load(const LogFunction& logger, std::size_t sample_limit) logger(LogLevel::Warning, "Could not load '" + filename + "': no audio channels available."); } + load_state.store(LoadState::failed); return; } @@ -168,12 +172,21 @@ void AudioFile::load(const LogFunction& logger, std::size_t sample_limit) this->data = data; this->size = size; this->preloadedsize = preloadedsize; - is_loaded.store(true); + load_state.store(LoadState::loaded); } bool AudioFile::isLoaded() const { - return is_loaded.load(); + return load_state.load() == LoadState::loaded; +} + +bool AudioFile::isLoading() const +{ + auto state = load_state.load(); + return + state == LoadState::loading || // in progress + state == LoadState::not_loaded // not yet started loading + ; } main_state_t AudioFile::mainState() const diff --git a/src/audiofile.h b/src/audiofile.h index cff4f3b..0aae125 100644 --- a/src/audiofile.h +++ b/src/audiofile.h @@ -52,6 +52,7 @@ public: void unload(); bool isLoaded() const; + bool isLoading() const; volatile std::size_t size{0}; // Full size of the file volatile std::size_t preloadedsize{0}; // Number of samples preloaded (in data) @@ -74,6 +75,13 @@ private: friend class InstrumentParserTest; void* magic{}; - std::atomic<bool> is_loaded{false}; + enum class LoadState + { + not_loaded, + loaded, + loading, + failed, + }; + std::atomic<LoadState> load_state{LoadState::not_loaded}; InstrumentChannel* instrument_channel{}; }; diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 093b0bf..2f162c1 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -363,7 +363,7 @@ void DrumGizmo::getSamples(int ch, int pos, sample_t* s, size_t sz) AudioFile& af = *sample_event.file; - while(freewheel && !af.isLoaded()) + while(freewheel && !af.isLoading()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } |