Skip to content

Commit

Permalink
Try adding frame counter to detect duplicate or out of order sync frames
Browse files Browse the repository at this point in the history
  • Loading branch information
netmindz committed Oct 8, 2023
1 parent 81e57ba commit 3b5c95b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions usermods/audioreactive/audio_reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -1479,7 +1480,8 @@ class AudioReactive : public Usermod {
fftUdp.write(reinterpret_cast<uint8_t *>(&transmitData), sizeof(transmitData));
fftUdp.endPacket();
}
return;

frameCounter++;
} // transmitAudioData()
#endif
static bool isValidUdpSyncVersion(const char *header) {
Expand All @@ -1491,6 +1493,16 @@ class AudioReactive : public Usermod {

void decodeAudioData(int packetSize, uint8_t *fftBuff) {
audioSyncPacket *receivedPacket = reinterpret_cast<audioSyncPacket*>(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);
Expand Down

0 comments on commit 3b5c95b

Please sign in to comment.