Compare commits

...

5 Commits

Author SHA1 Message Date
63eb980c32 TESTING mp3Stream.availableForWrite() 2025-06-06 14:42:52 +02:00
a8837a4598 code clean 2025-06-06 14:42:42 +02:00
97c52fe8f3 logging 2025-06-06 14:41:19 +02:00
e8550cc2a5 RepeatOne,RepeatAll,Random via HTTP 2025-06-06 14:20:59 +02:00
c6134af272 Playlist random 2025-06-06 14:20:40 +02:00
4 changed files with 51 additions and 19 deletions

View File

@ -36,7 +36,7 @@ MP3DecoderHelix mp3Decoder;
EncodedAudioStream mp3Stream(&board, &mp3Decoder);
StreamCopy copier;
StreamCopy mp3Copier;
bool running = false;
@ -46,7 +46,7 @@ void audioStop() {
Serial.println("[AUDIO ] STOP");
}
copyErrors = 0;
copier.end();
mp3Copier.end();
mp3Stream.end();
mp3Decoder.end();
opusDecoder.end();
@ -58,12 +58,12 @@ void audioStop() {
bool audioPlayInit(const Entry &entry) {
audioStop();
Serial.printf("[AUDIO ] [%-4s] Start: %s\n", entry.type.c_str(), entry.url.c_str());
Serial.printf("[AUDIO ] [%s] Opening: %s\n", entry.type.c_str(), entry.url.c_str());
running = true;
if (entry.type == "ICY" || entry.type == "SNAP") {
if (!isWifiConnected()) {
Serial.printf("[AUDIO ] [%-4s] WiFi not connected.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] WiFi not connected.\n", entry.type.c_str());
audioStop();
return false;
}
@ -71,7 +71,7 @@ bool audioPlayInit(const Entry &entry) {
if (entry.type == "SD") {
if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) {
Serial.printf("[AUDIO ] [%-4s] Failed to mount SD-card.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to mount SD-card.\n", entry.type.c_str());
audioStop();
return false;
}
@ -82,25 +82,26 @@ bool audioPlayInit(const Entry &entry) {
bool audioBeginMP3Stream(const Entry &entry, Stream &source) {
if (!mp3Decoder.begin()) {
Serial.printf("[AUDIO ] [%-4s] Failed to start MP3DecoderHelix.", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to start MP3DecoderHelix.", entry.type.c_str());
audioStop();
return false;
}
if (!mp3Stream.begin()) {
Serial.printf("[AUDIO ] [%-4s] Failed to start EncodedAudioStream.", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to start EncodedAudioStream.", entry.type.c_str());
audioStop();
return false;
}
copier.begin(mp3Stream, source);
mp3Copier.begin(mp3Stream, source);
if (copier.copy() == 0) {
Serial.printf("[AUDIO ] [%-4s] Failed to copy initial data.", entry.type.c_str());
if (mp3Copier.copy() == 0) {
Serial.printf("[AUDIO ] [%s] Failed to copy initial data.", entry.type.c_str());
audioStop();
return false;
}
Serial.printf("[AUDIO ] [%s] Stream running.\n", entry.type.c_str());
return true;
}
@ -110,7 +111,7 @@ bool audioPlayICY(const Entry &entry) {
}
if (!icy.begin(entry.url.c_str())) {
Serial.printf("[AUDIO ] [%-4s] Failed to start ICYStream.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to start ICYStream.\n", entry.type.c_str());
audioStop();
return false;
}
@ -125,30 +126,31 @@ bool audioPlaySNAP(const Entry &entry) {
IPAddress ip;
if (!WiFiClass::hostByName(entry.url.c_str(), ip)) {
Serial.printf("[AUDIO ] [%-4s] Failed to resolve host.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to resolve host.\n", entry.type.c_str());
audioStop();
return false;
}
snap.setServerIP(ip);
if (!snap.begin()) {
Serial.printf("[AUDIO ] [%-4s] Failed to connect.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to connect.\n", entry.type.c_str());
audioStop();
return false;
}
if (!opusDecoder.begin()) {
Serial.printf("[AUDIO ] [%-4s] Failed to start OpusAudioDecoder.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to start OpusAudioDecoder.\n", entry.type.c_str());
audioStop();
return false;
}
if (!snap.doLoop()) {
Serial.printf("[AUDIO ] [%-4s] Failed to copy initial data.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to copy initial data.\n", entry.type.c_str());
audioStop();
return false;
}
Serial.printf("[AUDIO ] [%s] Stream running.\n", entry.type.c_str());
return true;
}
@ -158,14 +160,14 @@ bool audioPlaySD(const Entry &entry) {
}
if (!SD.exists(entry.url.c_str())) {
Serial.printf("[AUDIO ] [%-4s] File not found.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] File not found.\n", entry.type.c_str());
audioStop();
return false;
}
file = SD.open(entry.url.c_str(), FILE_READ);
if (!file) {
Serial.printf("[AUDIO ] [%-4s] Failed to open file.\n", entry.type.c_str());
Serial.printf("[AUDIO ] [%s] Failed to open file.\n", entry.type.c_str());
audioStop();
return false;
}
@ -210,12 +212,15 @@ bool audioLoop() {
board.setMute(audioMute);
stateBufferUpdateRequest();
}
if (copier.copy() > 0) {
// TODO check snap too
if (mp3Copier.copy() > 0 || mp3Stream.availableForWrite() > 0) {
copyErrors = 0;
return true;
}
if (running) {
copyErrors++;
copyErrors = 100;
if (copyErrors < 100) {
return true;
}

View File

@ -79,6 +79,24 @@ void httpSetup() {
playerSkip -= 1;
request->send(200);
});
server.on("/repeatOne", [](AsyncWebServerRequest *request) {
Serial.println("[HTTP ] " + request->url());
playlistRepeatOne = !playlistRepeatOne;
stateBufferUpdateRequest();
request->send(200);
});
server.on("/repeatAll", [](AsyncWebServerRequest *request) {
Serial.println("[HTTP ] " + request->url());
playlistRepeatAll = !playlistRepeatAll;
stateBufferUpdateRequest();
request->send(200);
});
server.on("/random", [](AsyncWebServerRequest *request) {
Serial.println("[HTTP ] " + request->url());
playlistRandom = !playlistRandom;
stateBufferUpdateRequest();
request->send(200);
});
ws.onEvent(onWebSocketEvent);
server.addHandler(&ws);
server.begin();

View File

@ -105,6 +105,9 @@ Entry playlistCurrent() {
}
Entry playlistNext(const int amount) {
if (playlistRandom) {
return playlistSet(random(static_cast<long>(playlistEntries.size())));
}
if (amount < 0 && playlistIndex <= 0 && !playlistRepeatAll) {
return Entry();
}

View File

@ -3,6 +3,12 @@
#include "Entry.h"
extern bool playlistRepeatOne;
extern bool playlistRepeatAll;
extern bool playlistRandom;
void playlistClear();
void playlistAdd(String entry);