From 3a19563278e17a310377ec9a5de5de2c4667e7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 5 Jun 2025 14:33:16 +0200 Subject: [PATCH] playlist --- src/main.cpp | 3 ++ src/player.cpp | 13 +++---- src/player.h | 6 ---- src/playlist.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ src/playlist.h | 20 +++++++++++ 5 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/playlist.cpp create mode 100644 src/playlist.h diff --git a/src/main.cpp b/src/main.cpp index c172687..6acbfdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,14 @@ #include "wifi.h" #include "player.h" +#include "playlist.h" void setup() { delay(500); Serial.begin(115200); playerSetup(); + playlistClear(); + playlistAdd("http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3"); } void loop() { diff --git a/src/player.cpp b/src/player.cpp index 4c45674..4962ed3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -3,6 +3,7 @@ #include #include "audio.h" +#include "playlist.h" #define DELAY_MS_ADD 250UL #define DELAY_MS_MAX 5000UL @@ -23,14 +24,10 @@ void playerLoop() { if (errorMs > 0 && millis() - errorMs < delayMs) { return; } - playerNext(); -} - -void playerNext() { - playerPlay("http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3"); -} - -void playerPlay(const String &url) { + const auto url = playlistNextOrRepeatOneIfEnabled(); + if (url.isEmpty()) { + return; + } if (audioPlay(url)) { errorMs = 0; delayMs = 0; diff --git a/src/player.h b/src/player.h index cb73716..c280aa6 100644 --- a/src/player.h +++ b/src/player.h @@ -1,12 +1,6 @@ #ifndef PLAYER_H #define PLAYER_H -#include - -void playerNext(); - -void playerPlay(const String &url); - void playerSetup(); void playerLoop(); diff --git a/src/playlist.cpp b/src/playlist.cpp new file mode 100644 index 0000000..881d025 --- /dev/null +++ b/src/playlist.cpp @@ -0,0 +1,91 @@ +#include "playlist.h" + +#include + +#include "AudioTools/AudioLibs/I2SCodecStream.h" +#include + +bool playlistRepeatOne = false; + +bool playlistRepeatAll = false; + +bool playlistRandom = false; + +String playlistTitle = ""; + +size_t playlistIndex = 0; + +std::vector playlistEntries; + +void playlistClear() { + playlistTitle = ""; + playlistIndex = 0; + playlistEntries.clear(); + std::vector().swap(playlistEntries); +} + +void playlistAdd(String url) { + url.trim(); + if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("sd://") || url.startsWith("snap://")) { + playlistEntries.push_back(url); + } else if (url.startsWith("title://")) { + playlistTitle = url.substring(8); + } +} + +void playlistLoad(const String &path) { + playlistClear(); + Serial.println("[PLAYLIST] Loading playlist: " + path); + if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) { + Serial.println("[PLAYLIST] Failed to initialize SD card."); + return; + } + if (!SD.exists(path)) { + Serial.println("[PLAYLIST] File not found."); + return; + } + auto file = SD.open(path, FILE_READ); + if (!file) { + Serial.println("[PLAYLIST] Failed to open file."); + } + + while (file.available() > 0) { + String entry = file.readStringUntil('\n'); + playlistAdd(entry); + } + file.close(); +} + +String playlistSet(const size_t index) { + const auto size = playlistEntries.size(); + if (size == 0) { + return ""; + } + playlistIndex = (index % size + size) % size; + return playlistEntries[playlistIndex]; +} + +String playlistCurrent() { + return playlistSet(playlistIndex); +} + +String playlistNext() { + if (playlistIndex >= playlistEntries.size() - 1 && !playlistRepeatAll) { + return ""; + } + return playlistSet(playlistIndex + 1); +} + +String playlistNextOrRepeatOneIfEnabled() { + if (playlistRepeatOne) { + return playlistCurrent(); + } + return playlistNext(); +} + +String playlistPrevious() { + if (playlistIndex <= 0 && !playlistRepeatAll) { + return ""; + } + return playlistSet(playlistIndex - 1); +} diff --git a/src/playlist.h b/src/playlist.h new file mode 100644 index 0000000..8e37571 --- /dev/null +++ b/src/playlist.h @@ -0,0 +1,20 @@ +#ifndef PLAYLIST_H +#define PLAYLIST_H + +#include + +void playlistClear(); + +void playlistAdd(String url); + +void playlistLoad(const String &path); + +String playlistCurrent(); + +String playlistNext(); + +String playlistNextOrRepeatOneIfEnabled(); + +String playlistPrevious(); + +#endif