Skip to content

Commit

Permalink
Save some cpu time in audio task by only updating the playtime statis…
Browse files Browse the repository at this point in the history
…tics every 250ms
  • Loading branch information
tueddy committed Nov 25, 2023
1 parent 4c9c555 commit 556d1f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## DEV-branch

* 25.11.2023: Save some cpu time in audio task by only updating the playtime statistics every 250ms
* 22.11.2023: Web-UI: Search for files feature #268
* 21.11.2023: New command CMD_TOGGLE_MODE to switch Normal => BT-Sink => BT-Source
* 21.11.2023: Bugfix: Some commands e.g. Play/Pause did not work in BT-Source mode
Expand Down
35 changes: 21 additions & 14 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void AudioPlayer_Init(void) {
"mp3play", /* Name of the task */
6000, /* Stack size in words */
NULL, /* Task input parameter */
3 | portPRIVILEGE_BIT, /* Priority of the task */
2 | portPRIVILEGE_BIT, /* Priority of the task */
&AudioTaskHandle, /* Task handle. */
1 /* Core where the task should run */
);
Expand Down Expand Up @@ -364,6 +364,7 @@ void AudioPlayer_Task(void *parameter) {
bool audioReturnCode;
AudioPlayer_CurrentTime = 0;
AudioPlayer_FileDuration = 0;
static uint32_t AudioPlayer_LastPlaytimeStatsTimestamp = 0u;

for (;;) {
/*
Expand All @@ -384,9 +385,23 @@ void AudioPlayer_Task(void *parameter) {
Log_Printf(LOGLEVEL_INFO, newCntrlReceivedQueue, trackCommand);
}

// update current playtime and duration
AudioPlayer_CurrentTime = audio->getAudioCurrentTime();
AudioPlayer_FileDuration = audio->getAudioFileDuration();
// Update playtime stats every 250 ms
if ((millis() - AudioPlayer_LastPlaytimeStatsTimestamp) > 250) {
AudioPlayer_LastPlaytimeStatsTimestamp = millis();
// Update current playtime and duration
AudioPlayer_CurrentTime = audio->getAudioCurrentTime();
AudioPlayer_FileDuration = audio->getAudioFileDuration();
// Calculate relative position in file (for trackprogress neopixel & web-ui)
if (!gPlayProperties.playlistFinished && !gPlayProperties.isWebstream) {
if (!gPlayProperties.pausePlay && (gPlayProperties.seekmode != SEEK_POS_PERCENT) && (audio->getFileSize() > 0)) { // To progress necessary when paused
gPlayProperties.currentRelPos = ((double) (audio->getFilePos() - audio->inBufferFilled()) / (double) audio->getFileSize()) * 100;
}
} else {
gPlayProperties.currentRelPos = 0;
}
}


trackQStatus = xQueueReceive(gTrackQueue, &gPlayProperties.playlist, 0);
if (trackQStatus == pdPASS || gPlayProperties.trackFinished || trackCommand != NO_ACTION) {
if (trackQStatus == pdPASS) {
Expand Down Expand Up @@ -421,6 +436,8 @@ void AudioPlayer_Task(void *parameter) {
}
}
if (gPlayProperties.sleepAfterCurrentTrack) { // Go to sleep if "sleep after track" was requested
gPlayProperties.playlistFinished = true;
gPlayProperties.playMode = NO_PLAYLIST;
System_RequestSleep();
break;
}
Expand Down Expand Up @@ -803,16 +820,6 @@ void AudioPlayer_Task(void *parameter) {
}
}

// Calculate relative position in file (for neopixel) for SD-card-mode
if (!gPlayProperties.playlistFinished && !gPlayProperties.isWebstream) {
if (millis() % 20 == 0) { // Keep it simple
if (!gPlayProperties.pausePlay && (audio->getFileSize() > 0) && (gPlayProperties.seekmode != SEEK_POS_PERCENT)) { // To progress necessary when paused
gPlayProperties.currentRelPos = ((double) (audio->getFilePos() - audio->inBufferFilled()) / (double) audio->getFileSize()) * 100;
}
}
} else {
gPlayProperties.currentRelPos = 0;
}

audio->loop();
if (gPlayProperties.playlistFinished || gPlayProperties.pausePlay) {
Expand Down
6 changes: 4 additions & 2 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1743,8 +1743,10 @@ static void handleDeleteRFIDRequest(AsyncWebServerRequest *request) {
return;
}
if (gPrefsRfid.isKey(tagId.c_str())) {
// stop playback, tag to delete might be in use
Cmd_Action(CMD_STOP);
if (tagId.equals(gCurrentRfidTagId) {
// stop playback, tag to delete is in use
Cmd_Action(CMD_STOP);
}
if (gPrefsRfid.remove(tagId.c_str())) {
Log_Printf(LOGLEVEL_INFO, "/rfid (DELETE): tag %s removed successfuly", tagId);
request->send(200, "text/plain; charset=utf-8", tagId + " removed successfuly");
Expand Down
2 changes: 1 addition & 1 deletion src/revision.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once

#include "gitrevision.h"
constexpr const char softwareRevision[] = "Software-revision: 20231124-1-DEV";
constexpr const char softwareRevision[] = "Software-revision: 20231125-1-DEV";

0 comments on commit 556d1f4

Please sign in to comment.