This commit is contained in:
Patrick Haßel 2025-06-05 21:05:41 +02:00
parent f421b77700
commit f7afaa05f7
9 changed files with 120 additions and 16 deletions

View File

@ -11,6 +11,10 @@
#include "SnapClient.h"
#include "wifi.h"
bool audioMute = false;
float audioVolume = 0.05;
AudioBoardStream board(AudioKitEs8388V1);
File file;
@ -188,6 +192,9 @@ void audioSetup() {
}
bool audioLoop() {
audioVolume = max(0.0f, min(audioVolume, 1.0f));
board.setVolume(audioVolume);
board.setMute(audioMute);
if (copier.copy() > 0) {
return true;
}

View File

@ -3,6 +3,10 @@
#include "playlist.h"
extern bool audioMute;
extern float audioVolume;
bool audioPlay(const Entry &entry);
void audioStop();

57
src/http.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "http.h"
#include "audio.h"
#include "ESPAsyncWebServer.h"
#include "player.h"
AsyncWebServer server(80);
void httpSetup() {
Serial.println("Starting HTTP server");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
server.onNotFound([](AsyncWebServerRequest *request) {
const auto path = request->url();
if (path.endsWith("/") && path.length() > 1) {
request->redirect(path.substring(0, path.length() - 1));
} else {
request->send(404, "text/plain", "Not Found");
}
});
server.on("/player/stop", [](AsyncWebServerRequest *request) {
playerState = PLAYER_STOP;
request->send(200);
});
server.on("/player/play", [](AsyncWebServerRequest *request) {
playerState = PLAYER_PLAY;
request->send(200);
});
server.on("/player/pause", [](AsyncWebServerRequest *request) {
playerState = PLAYER_PAUSE;
request->send(200);
});
server.on("/player/mute", [](AsyncWebServerRequest *request) {
audioMute = true;
request->send(200);
});
server.on("/player/unmute", [](AsyncWebServerRequest *request) {
audioMute = false;
request->send(200);
});
server.on("/player/up", [](AsyncWebServerRequest *request) {
audioVolume = max(0.0f, min(audioVolume + 0.05f, 1.0f));
request->send(200);
});
server.on("/player/down", [](AsyncWebServerRequest *request) {
audioVolume = max(0.0f, min(audioVolume - 0.05f, 1.0f));
request->send(200);
});
server.on("/player/next", [](AsyncWebServerRequest *request) {
playerSkip += 1;
request->send(200);
});
server.on("/player/back", [](AsyncWebServerRequest *request) {
playerSkip -= 1;
request->send(200);
});
server.begin();
}

6
src/http.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef HTTP_H
#define HTTP_H
void httpSetup();
#endif

View File

@ -1,5 +1,6 @@
#include <Arduino.h>
#include "http.h"
#include "wifi.h"
#include "player.h"
#include "playlist.h"
@ -13,6 +14,9 @@ void setup() {
playlistClear();
playlistAdd("ICY|http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3|SR1");
playlistAdd("ICY|https://stream.rockantenne.de/rockantenne/stream/mp3|Rockantenne Classic Perlen");
wifiLoop();
httpSetup();
}
void loop() {

View File

@ -12,19 +12,15 @@ unsigned long errorMs = 0;
unsigned long delayMs = 0;
int playerSkip = 0;
PlayerState playerState = PLAYER_STOP;
void playerSetup() {
audioSetup();
}
void playerLoop() {
if (audioLoop()) {
errorMs = 0;
return;
}
if (errorMs > 0 && millis() - errorMs < delayMs) {
return;
}
const auto entry = playlistNextOrRepeatOneIfEnabled();
void playerPlay(const Entry entry) {
if (audioPlay(entry)) {
errorMs = 0;
delayMs = 0;
@ -34,3 +30,20 @@ void playerLoop() {
Serial.printf("retry delay: %d ms\n", delayMs);
}
}
void playerLoop() {
if (playerSkip != 0) {
const auto entry = playlistNext(playerSkip);
playerPlay(entry);
playerSkip = 0;
}
if (audioLoop()) {
errorMs = 0;
return;
}
if (errorMs > 0 && millis() - errorMs < delayMs) {
return;
}
const auto entry = playlistNextOrRepeatOneIfEnabled();
playerPlay(entry);
}

View File

@ -1,6 +1,16 @@
#ifndef PLAYER_H
#define PLAYER_H
enum PlayerState {
PLAYER_STOP,
PLAYER_PLAY,
PLAYER_PAUSE,
};
extern PlayerState playerState;
extern int playerSkip;
void playerSetup();
void playerLoop();

View File

@ -102,21 +102,24 @@ Entry playlistCurrent() {
return playlistSet(playlistIndex);
}
Entry playlistNext() {
if (playlistIndex >= playlistEntries.size() - 1 && !playlistRepeatAll) {
Entry playlistNext(const int amount) {
if (amount < 0 && playlistIndex <= 0 && !playlistRepeatAll) {
return Entry();
}
return playlistSet(playlistIndex + 1);
if (amount > 0 && playlistIndex >= playlistEntries.size() - 1 && !playlistRepeatAll) {
return Entry();
}
return playlistSet(playlistIndex + amount);
}
Entry playlistNextOrRepeatOneIfEnabled() {
if (playlistRepeatOne) {
return playlistCurrent();
}
return playlistNext();
return playlistNext(+1);
}
Entry playlistPrevious() {
Entry playlistBack() {
if (playlistIndex <= 0 && !playlistRepeatAll) {
return Entry();
}

View File

@ -11,10 +11,10 @@ void playlistLoad(const String &path);
Entry playlistCurrent();
Entry playlistNext();
Entry playlistNext(int amount);
Entry playlistNextOrRepeatOneIfEnabled();
Entry playlistPrevious();
Entry playlistBack();
#endif