From e0250f0bcca16c14028b00fd9894446d844e2099 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 6 Mar 2025 19:28:55 +0100 Subject: Improve load state check when freewheeling to also block during load and not just not loaded at all. --- src/audiofile.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/audiofile.cc') 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 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 -- cgit v1.2.3