esmp3/src/playlist.cpp

82 lines
1.8 KiB
C++

#include "playlist.h"
Playlist::Playlist() {}
Playlist::Playlist(String id, PersistedPlaylist* p) {
rfid_id = id;
pp = p;
}
String Playlist::get_rfid_id() {
return rfid_id;
}
void Playlist::add_file(String filename) {
files.push_back(filename);
}
void Playlist::sort() {
std::sort(files.begin(), files.end());
}
void Playlist::set_current_position(uint8_t file, uint32_t bytes) {
log_d("Setting position: File %d, bytes %d.", file, bytes);
current_file = file;
current_time = bytes;
save_current_position();
}
void Playlist::save_current_position(uint32_t position) {
if (position==0) {
position = current_time;
}
log_d("Saving current position: File %d, bytes %d.", current_file, position);
if (pp != NULL) {
pp->file = current_file;
pp->position = position;
}
}
String Playlist::get_current_file_name() {
if (current_file >= files.size()) {
Serial.printf("Requested a file number %d, which is not available in this playlist. Starting over.\n", current_file);
set_current_position(0);
}
return files[current_file];
}
bool Playlist::next_track() {
if (files.size() <= current_file + 1) {
Serial.println("next_track does not exist. Resetting playlist and returning false.");
set_current_position(0, 0);
return false;
}
set_current_position(current_file + 1, 0);
return true;
}
bool Playlist::prev_track() {
log_d("Playlist::prev_track called. current_file is %d", current_file);
if (current_file == 0) {
set_current_position(0, 0);
} else {
set_current_position(current_file - 1, 0);
}
return files.size()>0;
}
void Playlist::restart() {
current_time = 0;
}
void Playlist::set_current_time(uint32_t pos) {
set_current_position(current_file, pos);
}
uint32_t Playlist::get_current_time() {
return current_time;
}
void Playlist::shuffle() {
std::random_shuffle(files.begin(), files.end());
}