From 1e304979c57a08686a35ab5d0c7d1494aba972a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 6 Jun 2025 12:52:17 +0200 Subject: [PATCH] stateBufferUpdateRequest --- src/audio.cpp | 16 ++++++++++++---- src/player.cpp | 42 ++++++++++++++++++++++++++---------------- src/player.h | 2 ++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index b8ad0e5..fc128ac 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -7,12 +7,15 @@ #include #include +#include -#include "SnapClient.h" +#include "player.h" #include "wifi.h" bool audioMute = false; +bool audioMute2 = true; + float audioVolume = 0.1; int copyErrors = 0; @@ -195,12 +198,17 @@ void audioSetup() { } bool audioLoop() { - audioVolume = max(0.0f, min(audioVolume, 1.0f)); if (abs(board.volume() - audioVolume) >= 0.01f) { - Serial.printf("[AUDIO ] writing volume = %.2f\n", audioVolume); + Serial.printf("[AUDIO ] volume = %.2f\n", audioVolume); board.setVolume(audioVolume); + stateBufferUpdateRequest(); + } + if (audioMute2 != audioMute) { + audioMute2 = audioMute; + Serial.printf("[AUDIO ] mute = %s\n", audioMute ? "MUTED" : "no"); + board.setMute(audioMute); + stateBufferUpdateRequest(); } - board.setMute(audioMute); if (copier.copy() > 0) { copyErrors = 0; return true; diff --git a/src/player.cpp b/src/player.cpp index 0884123..a6ddb68 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -23,6 +23,8 @@ char stateBuffer1[200]; char *stateBuffer = stateBuffer0; +bool stateBufferUpdateNeeded = true; + const char *getPlayerStateString() { switch (playerState) { case PLAYER_STOP: @@ -36,7 +38,15 @@ const char *getPlayerStateString() { } -void updateStateBuffer() { +void stateBufferUpdateRequest() { + stateBufferUpdateNeeded = true; +} + +void updateStateBufferIfNeeded() { + if (!stateBufferUpdateNeeded) { + return; + } + stateBufferUpdateNeeded = false; auto json = JsonDocument(); const auto entry = playlistCurrent(); json["state"] = getPlayerStateString(); @@ -69,6 +79,7 @@ void playerStart(const Entry &entry) { delayMs = min(DELAY_MS_MAX, delayMs + DELAY_MS_ADD); Serial.printf("[PLAYER ] Retry delay: %d ms\n", delayMs); } + stateBufferUpdateRequest(); } void playerLoop() { @@ -76,20 +87,19 @@ void playerLoop() { 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(); + } else { + const auto playing = audioLoop(); + if (playing) { + errorMs = 0; + } + if (playing && playerState == PLAYER_STOP) { + errorMs = 0; + audioStop(); + stateBufferUpdateRequest(); + } + if (!playing && playerState == PLAYER_PLAY && (errorMs == 0 || millis() - errorMs >= delayMs)) { + playerStart(playlistNextOrRepeatOneIfEnabled()); + } } + updateStateBufferIfNeeded(); } diff --git a/src/player.h b/src/player.h index 24d811c..d4626d5 100644 --- a/src/player.h +++ b/src/player.h @@ -15,4 +15,6 @@ extern char *stateBuffer; void playerLoop(); +void stateBufferUpdateRequest(); + #endif