Skip to content

Commit 5a06280

Browse files
committed
receiveAudioData() process last packet in buffer now to account for slower refreshes
1 parent a244910 commit 5a06280

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

usermods/audioreactive/audio_reactive.h

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,54 +1836,62 @@ class AudioReactive : public Usermod {
18361836
agcSensitivity = 128.0f; // substitute - V1 format does not include this value
18371837
}
18381838

1839-
bool receiveAudioData() // check & process new data. return TRUE in case that new audio data was received.
1840-
{
1839+
bool receiveAudioData() {
18411840
if (!udpSyncConnected) return false;
18421841
bool haveFreshData = false;
1843-
18441842
size_t packetSize = 0;
1845-
// WLEDMM use exception handler to catch out-of-memory errors
1846-
#if __cpp_exceptions
1847-
try{
1843+
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET + 1] = {0};
1844+
size_t lastValidPacketSize = 0;
1845+
1846+
// Loop to read all available packets
1847+
while (true) {
1848+
#if __cpp_exceptions
1849+
try {
18481850
packetSize = fftUdp.parsePacket();
1849-
} catch(...) {
1850-
packetSize = 0; // low heap memory -> discard packet.
1851-
#ifdef ARDUINO_ARCH_ESP32
1852-
fftUdp.flush(); // this does not work on 8266
1853-
#endif
1851+
} catch (...) {
1852+
packetSize = 0;
1853+
#ifdef ARDUINO_ARCH_ESP32
1854+
fftUdp.flush();
1855+
#endif
18541856
DEBUG_PRINTLN(F("receiveAudioData: parsePacket out of memory exception caught!"));
18551857
USER_FLUSH();
1858+
continue; // Skip to next iteration
18561859
}
1857-
#else
1860+
#else
18581861
packetSize = fftUdp.parsePacket();
1859-
#endif
1862+
#endif
18601863

1861-
#ifdef ARDUINO_ARCH_ESP32
1862-
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) fftUdp.flush(); // discard invalid packets (too small or too big)
1863-
#endif
1864-
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
1865-
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET+1] = { 0 }; // static buffer for receiving, to reuse the same memory and avoid heap fragmentation
1866-
//DEBUGSR_PRINTLN("Received UDP Sync Packet");
1867-
fftUdp.read(fftUdpBuffer, packetSize);
1864+
#ifdef ARDUINO_ARCH_ESP32
1865+
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) {
1866+
fftUdp.flush();
1867+
continue; // Skip invalid packets
1868+
}
1869+
#endif
1870+
1871+
if (packetSize == 0) break; // No more packets available
18681872

1869-
// VERIFY THAT THIS IS A COMPATIBLE PACKET
1870-
if (packetSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
1873+
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
1874+
fftUdp.read(fftUdpBuffer, packetSize);
1875+
lastValidPacketSize = packetSize;
1876+
}
1877+
}
1878+
1879+
// Process only the last valid packet
1880+
if (lastValidPacketSize > 0) {
1881+
if (lastValidPacketSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
18711882
receivedFormat = 2;
1872-
haveFreshData = decodeAudioData(packetSize, fftUdpBuffer);
1873-
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v2");
1883+
haveFreshData = decodeAudioData(lastValidPacketSize, fftUdpBuffer);
1884+
} else if (lastValidPacketSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
1885+
decodeAudioData_v1(lastValidPacketSize, fftUdpBuffer);
1886+
receivedFormat = 1;
1887+
haveFreshData = true;
18741888
} else {
1875-
if (packetSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
1876-
decodeAudioData_v1(packetSize, fftUdpBuffer);
1877-
receivedFormat = 1;
1878-
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v1");
1879-
haveFreshData = true;
1880-
} else receivedFormat = 0; // unknown format
1889+
receivedFormat = 0; // unknown format
18811890
}
18821891
}
18831892
return haveFreshData;
18841893
}
18851894

1886-
18871895
//////////////////////
18881896
// usermod functions//
18891897
//////////////////////
@@ -2319,6 +2327,7 @@ class AudioReactive : public Usermod {
23192327
static float syncVolumeSmth = 0;
23202328
bool have_new_sample = false;
23212329
if (millis() - lastTime > delayMs) {
2330+
// DEBUG_PRINTF(F("AR reading at %d compared to %d max\n"), millis() - lastTime, delayMs); // TroyHacks
23222331
have_new_sample = receiveAudioData();
23232332
if (have_new_sample) {
23242333
last_UDPTime = millis();

0 commit comments

Comments
 (0)