playerLoop refactor (playerState)
This commit is contained in:
parent
48e0004b34
commit
2c343d98c7
@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#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
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,8 +13,6 @@ extern int playerSkip;
|
||||
|
||||
extern char *stateBuffer;
|
||||
|
||||
void playerSetup();
|
||||
|
||||
void playerLoop();
|
||||
|
||||
#endif
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -15,8 +15,6 @@ Entry playlistNext(int amount);
|
||||
|
||||
Entry playlistNextOrRepeatOneIfEnabled();
|
||||
|
||||
Entry playlistBack();
|
||||
|
||||
size_t playlistGetCount();
|
||||
|
||||
size_t playlistGetIndex();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user