diff --git a/src/tangara/audio/playlist.cpp b/src/tangara/audio/playlist.cpp index db712252..f57f078d 100644 --- a/src/tangara/audio/playlist.cpp +++ b/src/tangara/audio/playlist.cpp @@ -33,6 +33,10 @@ Playlist::Playlist(const std::string& playlistFilepath) auto Playlist::open() -> bool { std::unique_lock lock(mutex_); + if (file_open_) { + return true; + } + FRESULT res = f_open(&file_, filepath_.c_str(), FA_READ | FA_WRITE | FA_OPEN_ALWAYS); if (res != FR_OK) { @@ -194,6 +198,14 @@ auto Playlist::deserialiseCache() -> bool { return true; } +auto Playlist::close() -> void { + if (file_open_) { + f_close(&file_); + file_open_ = false; + file_error_ = false; + } +} + auto Playlist::skipToLocked(size_t position) -> void { if (!file_open_ || file_error_) { return; @@ -306,6 +318,10 @@ MutablePlaylist::MutablePlaylist(const std::string& playlistFilepath) auto MutablePlaylist::open() -> bool { std::unique_lock lock(mutex_); + + if (file_open_) { + return true; + } FRESULT res = f_open(&file_, filepath_.c_str(), FA_READ | FA_WRITE | FA_OPEN_ALWAYS); diff --git a/src/tangara/audio/playlist.hpp b/src/tangara/audio/playlist.hpp index b794e444..1e05e9c4 100644 --- a/src/tangara/audio/playlist.hpp +++ b/src/tangara/audio/playlist.hpp @@ -47,6 +47,7 @@ class Playlist { auto serialiseCache() -> bool; auto deserialiseCache() -> bool; + auto close() -> void; protected: const std::string filepath_; diff --git a/src/tangara/audio/track_queue.cpp b/src/tangara/audio/track_queue.cpp index 35c1403f..05ac0b95 100644 --- a/src/tangara/audio/track_queue.cpp +++ b/src/tangara/audio/track_queue.cpp @@ -159,6 +159,13 @@ auto TrackQueue::open() -> bool { return playlist_.open(); } +auto TrackQueue::close() -> void { + playlist_.close(); + if (opened_playlist_) { + opened_playlist_->close(); + } +} + auto TrackQueue::openPlaylist(const std::string& playlist_file, bool notify) -> bool { opened_playlist_.emplace(playlist_file); diff --git a/src/tangara/audio/track_queue.hpp b/src/tangara/audio/track_queue.hpp index 727b4be0..383c204e 100644 --- a/src/tangara/audio/track_queue.hpp +++ b/src/tangara/audio/track_queue.hpp @@ -76,8 +76,9 @@ class TrackQueue { auto currentPosition(size_t position) -> bool; auto totalSize() const -> size_t; auto open() -> bool; - auto openPlaylist(const std::string& playlist_file, bool notify = true) - -> bool; + auto close() -> void; + auto openPlaylist(const std::string& playlist_file, + bool notify = true) -> bool; auto playFromPosition(const std::string& filepath, uint32_t position) -> void; using Item = diff --git a/src/tangara/system_fsm/running.cpp b/src/tangara/system_fsm/running.cpp index f065737b..227eac2c 100644 --- a/src/tangara/system_fsm/running.cpp +++ b/src/tangara/system_fsm/running.cpp @@ -214,6 +214,7 @@ void Running::react(const internal::Mount& ev) { auto Running::unmountStorage() -> void { ESP_LOGW(kTag, "unmounting storage"); + sServices->track_queue().close(); sServices->database({}); sStorage.reset(); updateSdState(drivers::SdState::kNotMounted);