diff --git a/src/main.cpp b/src/main.cpp index 5b4195f..b910656 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include +#include "audio.h" #include "http.h" #include "wifi.h" #include "player.h" @@ -10,7 +11,8 @@ void setup() { Serial.begin(115200); Serial.println("\n\n\nStartup!"); - playerSetup(); + audioSetup(); + playlistClear(); playlistAdd("ICY|http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3|SR1"); // 48000 Hz playlistAdd("ICY|http://uk3.internet-radio.com:8082/live|1940s Radio"); // 44100 Hz TODO stuttering diff --git a/src/player.cpp b/src/player.cpp index c00deb8..0884123 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -15,43 +15,7 @@ unsigned long delayMs = 0; int playerSkip = 0; -PlayerState playerState = PLAYER_STOP; - -void updateStateBuffer(); - -void playerSetup() { - audioSetup(); -} - -void playerPlay(const Entry &entry) { - if (audioPlay(entry)) { - errorMs = 0; - delayMs = 0; - } else { - errorMs = max(1UL, millis()); - delayMs = min(DELAY_MS_MAX, delayMs + DELAY_MS_ADD); - Serial.printf("retry delay: %d ms\n", delayMs); - } -} - -void playerLoop() { - if (playerSkip != 0) { - const auto entry = playlistNext(playerSkip); - playerPlay(entry); - playerSkip = 0; - updateStateBuffer(); - } - if (audioLoop()) { - errorMs = 0; - return; - } - if (errorMs > 0 && millis() - errorMs < delayMs) { - return; - } - const auto entry = playlistNextOrRepeatOneIfEnabled(); - playerPlay(entry); - updateStateBuffer(); -} +PlayerState playerState = PLAYER_PLAY; char stateBuffer0[200] = "{}"; @@ -92,3 +56,40 @@ void updateStateBuffer() { } websocketSendAll(); } + +void playerStart(const Entry &entry) { + if (entry.type == "") { + Serial.println("[PLAYER ] Not playing empty Entry => STOP"); + playerState = PLAYER_STOP; + } else if (audioPlay(entry)) { + errorMs = 0; + delayMs = 0; + } else { + errorMs = max(1UL, millis()); + delayMs = min(DELAY_MS_MAX, delayMs + DELAY_MS_ADD); + Serial.printf("[PLAYER ] Retry delay: %d ms\n", delayMs); + } +} + +void playerLoop() { + if (playerSkip != 0) { + playerState = PLAYER_PLAY; + playerStart(playlistNext(playerSkip)); + playerSkip = 0; + return; + } + + const auto playing = audioLoop(); + if (playing) { + errorMs = 0; + } + if (playing && playerState == PLAYER_STOP) { + errorMs = 0; + audioStop(); + updateStateBuffer(); + } + if (!playing && playerState == PLAYER_PLAY && (errorMs == 0 || millis() - errorMs >= delayMs)) { + playerStart(playlistNextOrRepeatOneIfEnabled()); + updateStateBuffer(); + } +} diff --git a/src/player.h b/src/player.h index bc6673a..24d811c 100644 --- a/src/player.h +++ b/src/player.h @@ -13,8 +13,6 @@ extern int playerSkip; extern char *stateBuffer; -void playerSetup(); - void playerLoop(); #endif diff --git a/src/playlist.cpp b/src/playlist.cpp index 5fe2702..f457345 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -121,13 +121,6 @@ Entry playlistNextOrRepeatOneIfEnabled() { return playlistNext(+1); } -Entry playlistBack() { - if (playlistIndex <= 0 && !playlistRepeatAll) { - return Entry(); - } - return playlistSet(playlistIndex - 1); -} - size_t playlistGetCount() { return playlistEntries.size(); } diff --git a/src/playlist.h b/src/playlist.h index a12d62f..ed213a7 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -15,8 +15,6 @@ Entry playlistNext(int amount); Entry playlistNextOrRepeatOneIfEnabled(); -Entry playlistBack(); - size_t playlistGetCount(); size_t playlistGetIndex();