Clear queue if loading cache fails and file is large, also fix indices oops

custom
ailurux 7 months ago
parent 1d2153f69d
commit ea71fa5d2b
  1. 44
      src/tangara/audio/playlist.cpp
  2. 3
      src/tangara/audio/playlist.hpp

@ -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_);

@ -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;

Loading…
Cancel
Save