Skip to content

Instantly share code, notes, and snippets.

@biologist79
Last active December 27, 2023 22:17
Show Gist options
  • Save biologist79/72e72e1ca4ed6675c973d4946c324da9 to your computer and use it in GitHub Desktop.
Save biologist79/72e72e1ca4ed6675c973d4946c324da9 to your computer and use it in GitHub Desktop.
Statistics for audio loop
void AudioPlayer_LoopStats(uint16_t statsIterationCount) {
static uint32_t iterationStartTimestamp = 0;
static uint32_t loopCount = 0;
static uint32_t minLoopDuration = 0;
static uint32_t maxLoopDuration = 0;
static uint32_t avgLoopDuration = 0;
static uint32_t startTimestampOfLoop = 0;
static uint32_t currentTimestamp = millis(); // Only get millis once
static uint32_t currentLoopDuration = 0;
currentTimestamp = millis(); // Only execute millis() once
if (!iterationStartTimestamp) {
iterationStartTimestamp = currentTimestamp;
}
if (startTimestampOfLoop > 0) {
currentLoopDuration = currentTimestamp - startTimestampOfLoop; // calc current loop time
if (currentLoopDuration < minLoopDuration || minLoopDuration == 0) { // store minTime
minLoopDuration = currentLoopDuration;
}
if (currentLoopDuration > maxLoopDuration) { // store maxTime
maxLoopDuration = currentLoopDuration;
}
}
if (loopCount % statsIterationCount == 0 && loopCount > 0) { // print stats every n loops
avgLoopDuration = (currentTimestamp - iterationStartTimestamp) / statsIterationCount;
Log_Printf(LOGLEVEL_DEBUG, "LoopStats: min: %u max: %u avg: %u loops: %u", minLoopDuration, maxLoopDuration, avgLoopDuration, loopCount);
minLoopDuration = 0; // Reset min/max stats
maxLoopDuration = 0;
iterationStartTimestamp = currentTimestamp;
}
loopCount++;
startTimestampOfLoop = currentTimestamp; // set new loopStart
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment