Merge pull request #2020 from blazoncek/endless-playlist-fix

Endless playlist fix.
This commit is contained in:
Christian Schwinne 2021-06-07 22:29:17 +02:00 committed by GitHub
commit 73d6cc1e54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,30 +10,32 @@ typedef struct PlaylistEntry {
uint16_t tr; uint16_t tr;
} ple; } ple;
int8_t playlistRepeat = 1; bool playlistEndless = false;
byte playlistEndPreset = 0; int8_t playlistRepeat = 1;
byte *playlistEntries = nullptr; byte playlistEndPreset = 0;
byte playlistLen; PlaylistEntry *playlistEntries = nullptr;
int8_t playlistIndex = -1; byte playlistLen;
uint16_t playlistEntryDur = 0; int8_t playlistIndex = -1;
uint16_t playlistEntryDur = 0;
void shufflePlaylist() { void shufflePlaylist() {
int currentIndex = playlistLen; int currentIndex = playlistLen;
PlaylistEntry temporaryValue;
PlaylistEntry temporaryValue, *entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
// While there remain elements to shuffle... // While there remain elements to shuffle...
while (currentIndex--) { while (currentIndex--) {
// Pick a random element... // Pick a random element...
int randomIndex = random(0, currentIndex); int randomIndex = random(0, currentIndex);
// And swap it with the current element. // And swap it with the current element.
temporaryValue = entries[currentIndex]; temporaryValue = playlistEntries[currentIndex];
entries[currentIndex] = entries[randomIndex]; playlistEntries[currentIndex] = playlistEntries[randomIndex];
entries[randomIndex] = temporaryValue; playlistEntries[randomIndex] = temporaryValue;
} }
DEBUG_PRINTLN(F("Playlist shuffle."));
} }
void unloadPlaylist() { void unloadPlaylist() {
if (playlistEntries != nullptr) { if (playlistEntries != nullptr) {
delete[] playlistEntries; delete[] playlistEntries;
@ -41,8 +43,10 @@ void unloadPlaylist() {
} }
currentPlaylist = playlistIndex = -1; currentPlaylist = playlistIndex = -1;
playlistLen = playlistEntryDur = 0; playlistLen = playlistEntryDur = 0;
DEBUG_PRINTLN(F("Playlist unloaded."));
} }
void loadPlaylist(JsonObject playlistObj) { void loadPlaylist(JsonObject playlistObj) {
unloadPlaylist(); unloadPlaylist();
@ -50,49 +54,52 @@ void loadPlaylist(JsonObject playlistObj) {
playlistLen = presets.size(); playlistLen = presets.size();
if (playlistLen == 0) return; if (playlistLen == 0) return;
if (playlistLen > 100) playlistLen = 100; if (playlistLen > 100) playlistLen = 100;
uint16_t dataSize = sizeof(ple) * playlistLen;
playlistEntries = new byte[dataSize]; playlistEntries = new PlaylistEntry[playlistLen];
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries); if (playlistEntries == nullptr) return;
byte it = 0; byte it = 0;
for (int ps : presets) { for (int ps : presets) {
if (it >= playlistLen) break; if (it >= playlistLen) break;
entries[it].preset = ps; playlistEntries[it].preset = ps;
it++; it++;
} }
it = 0; it = 0;
JsonArray durations = playlistObj["dur"]; JsonArray durations = playlistObj["dur"];
if (durations.isNull()) { if (durations.isNull()) {
entries[0].dur = playlistObj["dur"] | 100; playlistEntries[0].dur = playlistObj["dur"] | 100;
it = 1; it = 1;
} else { } else {
for (int dur : durations) { for (int dur : durations) {
if (it >= playlistLen) break; if (it >= playlistLen) break;
entries[it].dur = dur; playlistEntries[it].dur = (dur > 0) ? dur : presetCycleTime;
it++; it++;
} }
} }
for (int i = it; i < playlistLen; i++) entries[i].dur = entries[it -1].dur; for (int i = it; i < playlistLen; i++) playlistEntries[i].dur = playlistEntries[it -1].dur;
it = 0; it = 0;
JsonArray tr = playlistObj["transition"]; JsonArray tr = playlistObj[F("transition")];
if (tr.isNull()) { if (tr.isNull()) {
entries[0].tr = playlistObj["transition"] | (transitionDelay / 100); playlistEntries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
it = 1; it = 1;
} else { } else {
for (int transition : tr) { for (int transition : tr) {
if (it >= playlistLen) break; if (it >= playlistLen) break;
entries[it].tr = transition; playlistEntries[it].tr = transition;
it++; it++;
} }
} }
for (int i = it; i < playlistLen; i++) entries[i].tr = entries[it -1].tr; for (int i = it; i < playlistLen; i++) playlistEntries[i].tr = playlistEntries[it -1].tr;
playlistRepeat = playlistObj[F("repeat")] | 0; playlistRepeat = playlistObj[F("repeat")] | 0;
playlistEndPreset = playlistObj[F("end")] | 0; playlistEndPreset = playlistObj[F("end")] | 0;
if (playlistRepeat <= 0) playlistRepeat--; // make it endless (-2 == endless & random)
currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved
DEBUG_PRINTLN(F("Playlist loaded."));
} }
@ -112,20 +119,14 @@ void handlePlaylist() {
} }
// playlist roll-over // playlist roll-over
if (!playlistIndex) { if (!playlistIndex) {
if (playlistRepeat > 0) {// playlistRepeat < 0 => endless loop with shuffling presets // playlistRepeat < 0 => endless loop
playlistRepeat--; // decrease repeat count on each index reset if (playlistRepeat > 0) playlistRepeat--; // decrease repeat count on each index reset if not an endless playlist
} else { if (playlistRepeat < -1) shufflePlaylist(); // shuffle playlist and start over
shufflePlaylist(); // shuffle playlist and start over
}
} }
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
jsonTransitionOnce = true; jsonTransitionOnce = true;
transitionDelayTemp = entries[playlistIndex].tr * 100; transitionDelayTemp = playlistEntries[playlistIndex].tr * 100;
playlistEntryDur = playlistEntries[playlistIndex].dur;
applyPreset(entries[playlistIndex].preset); applyPreset(playlistEntries[playlistIndex].preset);
playlistEntryDur = entries[playlistIndex].dur;
if (playlistEntryDur == 0) playlistEntryDur = 10;
} }
} }