summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tangara/audio/playlist.cpp44
-rw-r--r--src/tangara/audio/playlist.hpp3
2 files changed, 41 insertions, 6 deletions
diff --git a/src/tangara/audio/playlist.cpp b/src/tangara/audio/playlist.cpp
index 6b87b618..630b0097 100644
--- a/src/tangara/audio/playlist.cpp
+++ b/src/tangara/audio/playlist.cpp
@@ -135,10 +135,10 @@ auto Playlist::serialiseCache() -> bool {
header[7] = q_file_size;
// Next 4 bytes = number of tracks in this queue
- header[4] = total_size_ >> 24;
- header[5] = total_size_ >> 16;
- header[6] = total_size_ >> 8;
- header[7] = total_size_;
+ header[8] = total_size_ >> 24;
+ header[9] = total_size_ >> 16;
+ header[10] = total_size_ >> 8;
+ header[11] = total_size_;
UINT bytes_written = 0;
f_write(&file, header, 12, &bytes_written);
@@ -198,7 +198,7 @@ auto Playlist::deserialiseCache() -> bool {
}
uint32_t size =
- header[4] << 24 | header[5] << 16 | header[6] << 8 | header[7];
+ header[8] << 24 | header[9] << 16 | header[10] << 8 | header[11];
total_size_ = size;
// Read in the cache
@@ -329,6 +329,40 @@ auto Playlist::nextItem(std::span<TCHAR> buf)
MutablePlaylist::MutablePlaylist(const std::string& playlistFilepath)
: Playlist(playlistFilepath) {}
+auto MutablePlaylist::open() -> bool {
+ std::unique_lock<std::mutex> 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) {
+ ESP_LOGE(kTag, "failed to open file! res: %i", res);
+ return false;
+ }
+ file_open_ = true;
+ file_error_ = false;
+
+ auto queue_filesize = f_size(&file_);
+
+ if (!deserialiseCache()) {
+ // If there's no cache (or deserialising failed) and the queue is
+ // sufficiently large, abort and clear the queue
+ if (queue_filesize > 50000) {
+ clear();
+ } else {
+ // Otherwise, read in the existing entries
+ countItems();
+ // Advance to the first item.
+ skipToWithoutCache(0);
+ }
+ }
+
+ return !file_error_;
+ return false;
+}
+
auto MutablePlaylist::clear() -> bool {
std::unique_lock<std::mutex> lock(mutex_);
diff --git a/src/tangara/audio/playlist.hpp b/src/tangara/audio/playlist.hpp
index 421b28fa..7cf99953 100644
--- a/src/tangara/audio/playlist.hpp
+++ b/src/tangara/audio/playlist.hpp
@@ -71,7 +71,7 @@ class Playlist {
*/
const uint32_t sample_size_;
- private:
+ protected:
auto skipToLocked(size_t position) -> void;
auto countItems() -> void;
auto advanceBy(ssize_t amt) -> bool;
@@ -82,6 +82,7 @@ class Playlist {
class MutablePlaylist : public Playlist {
public:
MutablePlaylist(const std::string& playlistFilepath);
+ auto open() -> bool;
auto clear() -> bool;
auto append(Item i) -> void;