108 lines
2.3 KiB
C++
108 lines
2.3 KiB
C++
|
#include <playlist.h>
|
||
|
#include "spi_master.h"
|
||
|
#include "config.h"
|
||
|
#include <SD.h>
|
||
|
#include <algorithm>
|
||
|
|
||
|
Playlist::Playlist(String path) {
|
||
|
// Add files to _files
|
||
|
SPIMaster::select_sd();
|
||
|
TRACE("Examining folder %s...\n", path.c_str());
|
||
|
if (!path.startsWith("/")) path = String("/") + path;
|
||
|
if (!SD.exists(path)) {
|
||
|
DEBUG("Could not open path '%s'.\n", path.c_str());
|
||
|
SPIMaster::select_sd(false);
|
||
|
return;
|
||
|
}
|
||
|
File dir = SD.open(path);
|
||
|
File entry;
|
||
|
while (entry = dir.openNextFile()) {
|
||
|
String filename = entry.name();
|
||
|
filename = filename.substring(path.length() + 1);
|
||
|
String ext = filename.substring(filename.length() - 4);
|
||
|
if (!entry.isDirectory() &&
|
||
|
!filename.startsWith(".") &&
|
||
|
( ext.equals(".mp3") ||
|
||
|
ext.equals(".ogg") ||
|
||
|
ext.equals(".wma") ||
|
||
|
ext.equals(".mp4") ||
|
||
|
ext.equals(".mpa"))) {
|
||
|
TRACE(" Adding entry %s\n", entry.name());
|
||
|
_files.push_back(entry.name());
|
||
|
} else {
|
||
|
TRACE(" Ignoring entry %s\n", filename.c_str());
|
||
|
}
|
||
|
entry.close();
|
||
|
}
|
||
|
dir.close();
|
||
|
SPIMaster::select_sd(false);
|
||
|
std::sort(_files.begin(), _files.end());
|
||
|
}
|
||
|
|
||
|
bool Playlist::has_track_prev() {
|
||
|
return _current_track > 0;
|
||
|
}
|
||
|
|
||
|
bool Playlist::has_track_next() {
|
||
|
return _current_track < _files.size()-1;
|
||
|
}
|
||
|
|
||
|
bool Playlist::track_prev() {
|
||
|
if (_current_track > 0) {
|
||
|
_current_track--;
|
||
|
_position = 0;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool Playlist::track_next() {
|
||
|
if (_current_track < _files.size()-1) {
|
||
|
_current_track++;
|
||
|
_position = 0;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
void Playlist::track_restart() {
|
||
|
_position = 0;
|
||
|
}
|
||
|
|
||
|
void Playlist::shuffle(uint8_t random_offset) {
|
||
|
DEBUG("Shuffling the playlist with an offset of %d...\n", random_offset);
|
||
|
for (int i=random_offset; i<_files.size(); i++) {
|
||
|
int j = random(random_offset, _files.size()-1);
|
||
|
if (i!=j) {
|
||
|
TRACE(" Swapping elements %d and %d.\n", i, j);
|
||
|
String temp = _files[i];
|
||
|
_files[i] = _files[j];
|
||
|
_files[j] = temp;
|
||
|
}
|
||
|
}
|
||
|
_shuffled = true;
|
||
|
TRACE("Done.\n");
|
||
|
}
|
||
|
|
||
|
void Playlist::reset() {
|
||
|
std::sort(_files.begin(), _files.end());
|
||
|
_current_track = 0;
|
||
|
_position = 0;
|
||
|
_shuffled = false;
|
||
|
}
|
||
|
|
||
|
String Playlist::get_current_file() {
|
||
|
return _files[_current_track];
|
||
|
}
|
||
|
|
||
|
uint32_t Playlist::get_position() {
|
||
|
return _position;
|
||
|
}
|
||
|
|
||
|
void Playlist::set_position(uint32_t p) {
|
||
|
_position = p;
|
||
|
}
|
||
|
|
||
|
bool Playlist::is_fresh() {
|
||
|
return !_shuffled && _position==0 && _current_track==0;
|
||
|
}
|