From 3b5c95b5e0f581866c389b9bf8c788aeb2292a3a Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sun, 8 Oct 2023 14:10:48 +0100 Subject: [PATCH 1/2] Try adding frame counter to detect duplicate or out of order sync frames --- usermods/audioreactive/audio_reactive.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index 8cd7dbe6b7..c601c0e508 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -966,7 +966,7 @@ class AudioReactive : public Usermod { float sampleRaw; // 04 Bytes - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting float sampleSmth; // 04 Bytes - either "sampleAvg" or "sampleAgc" depending on soundAgc setting uint8_t samplePeak; // 01 Bytes - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude - uint8_t reserved1; // 01 Bytes - for future extensions - not used yet + uint8_t frameCounter; // 01 Bytes - track duplicate/out of order packets uint8_t fftResult[16]; // 16 Bytes float FFT_Magnitude; // 04 Bytes float FFT_MajorPeak; // 04 Bytes @@ -1457,6 +1457,7 @@ class AudioReactive : public Usermod { void transmitAudioData() { if (!udpSyncConnected) return; + static uint8_t frameCounter = 0; //DEBUGSR_PRINTLN("Transmitting UDP Mic Packet"); audioSyncPacket transmitData; @@ -1466,7 +1467,7 @@ class AudioReactive : public Usermod { transmitData.sampleSmth = (soundAgc) ? sampleAgc : sampleAvg; transmitData.samplePeak = udpSamplePeak ? 1:0; udpSamplePeak = false; // Reset udpSamplePeak after we've transmitted it - transmitData.reserved1 = 0; + transmitData.frameCounter = frameCounter; for (int i = 0; i < NUM_GEQ_CHANNELS; i++) { transmitData.fftResult[i] = (uint8_t)constrain(fftResult[i], 0, 254); @@ -1479,7 +1480,8 @@ class AudioReactive : public Usermod { fftUdp.write(reinterpret_cast(&transmitData), sizeof(transmitData)); fftUdp.endPacket(); } - return; + + frameCounter++; } // transmitAudioData() #endif static bool isValidUdpSyncVersion(const char *header) { @@ -1491,6 +1493,16 @@ class AudioReactive : public Usermod { void decodeAudioData(int packetSize, uint8_t *fftBuff) { audioSyncPacket *receivedPacket = reinterpret_cast(fftBuff); + + static uint8_t lastFrameCounter = 0; + if(receivedPacket->frameCounter <= lastFrameCounter && receivedPacket->frameCounter != 0) { // TODO: might need extra checks here + DEBUGSR_PRINTLN("Skipping audio frame out of order or duplicated"); + return; + } + else { + lastFrameCounter = receivedPacket->frameCounter; + } + // update samples for effects volumeSmth = fmaxf(receivedPacket->sampleSmth, 0.0f); volumeRaw = fmaxf(receivedPacket->sampleRaw, 0.0f); From f99856e863f398c9e2a45ef08d5c46cf337a094e Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sun, 8 Oct 2023 19:35:26 +0100 Subject: [PATCH 2/2] Log packet counters --- usermods/audioreactive/audio_reactive.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index c601c0e508..6e0409cd6e 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -1496,7 +1496,7 @@ class AudioReactive : public Usermod { static uint8_t lastFrameCounter = 0; if(receivedPacket->frameCounter <= lastFrameCounter && receivedPacket->frameCounter != 0) { // TODO: might need extra checks here - DEBUGSR_PRINTLN("Skipping audio frame out of order or duplicated"); + DEBUGSR_PRINTF("Skipping audio frame out of order or duplicated - %u vs %u\n", lastFrameCounter, receivedPacket->frameCounter); return; } else {