You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* fixed a few typo's in comments
* fixed 8266 specific warning about 'comparison of integer expressions of different signedness'
based on recommendations made by @willmmiles:
* make sure that audioSyncPacket is the same size (44bytes) on all platforms
* use static buffer for receiving (avoids heap fragmentation)
* copy receive buffer to local audioSyncPacket struct - avoids alignment problems
* esp32 only: to stay in sync with UDP, Udp.flush() is needed when Udp.parsePacket() is _not_ followed by Udp.read()
Copy file name to clipboardExpand all lines: usermods/audioreactive/audio_reactive.h
+36-28Lines changed: 36 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,7 @@ static uint8_t soundAgc = 0; // Automagic gain control: 0 - n
76
76
staticfloat FFT_MajorPeak = 1.0f; // FFT: strongest (peak) frequency
77
77
staticfloat FFT_Magnitude = 0.0f; // FFT: volume (magnitude) of peak frequency
78
78
staticbool samplePeak = false; // Boolean flag for peak - used in effects. Responding routine may reset this flag. Auto-reset after strip.getMinShowDelay()
79
-
staticbool udpSamplePeak = false; // Boolean flag for peak. Set at the same tiem as samplePeak, but reset by transmitAudioData
79
+
staticbool udpSamplePeak = false; // Boolean flag for peak. Set at the same time as samplePeak, but reset by transmitAudioData
80
80
staticunsignedlong timeOfPeak = 0; // time of last sample peak detection.
81
81
staticuint8_t fftResult[NUM_GEQ_CHANNELS]= {0};// Our calculated freq. channel result table to be used by effects
82
82
@@ -94,7 +94,7 @@ static uint16_t decayTime = 1400; // int: decay time in milliseconds
94
94
95
95
// peak detection
96
96
#ifdef ARDUINO_ARCH_ESP32
97
-
staticvoiddetectSamplePeak(void); // peak detection function (needs scaled FFT reasults in vReal[]) - no used for 8266 receive-only mode
97
+
staticvoiddetectSamplePeak(void); // peak detection function (needs scaled FFT results in vReal[]) - no used for 8266 receive-only mode
98
98
#endif
99
99
staticvoidautoResetPeak(void); // peak auto-reset function
100
100
staticuint8_t maxVol = 31; // (was 10) Reasonable value for constant volume for 'peak detector', as it won't always trigger (deprecated)
@@ -587,19 +587,21 @@ class AudioReactive : public Usermod {
587
587
#endif
588
588
#endif
589
589
590
-
// new "V2" audiosync struct - 40 Bytes
591
-
structaudioSyncPacket {
592
-
char header[6]; // 06 Bytes
593
-
float sampleRaw; // 04 Bytes - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
594
-
float sampleSmth; // 04 Bytes - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
595
-
uint8_t samplePeak; // 01 Bytes - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
596
-
uint8_t reserved1; // 01 Bytes - for future extensions - not used yet
597
-
uint8_t fftResult[16]; // 16 Bytes
598
-
float FFT_Magnitude; // 04 Bytes
599
-
float FFT_MajorPeak; // 04 Bytes
590
+
// new "V2" audiosync struct - 44 Bytes
591
+
struct__attribute__ ((packed)) audioSyncPacket { // "packed" ensures that there are no additional gaps
592
+
char header[6]; // 06 Bytes offset 0
593
+
uint8_t reserved1[2]; // 02 Bytes, offset 6 - gap required by the compiler - not used yet
594
+
float sampleRaw; // 04 Bytes offset 8 - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
595
+
float sampleSmth; // 04 Bytes offset 12 - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
596
+
uint8_t samplePeak; // 01 Bytes offset 16 - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
597
+
uint8_t reserved2; // 01 Bytes offset 17 - for future extensions - not used yet
598
+
uint8_t fftResult[16]; // 16 Bytes offset 18
599
+
uint16_t reserved3; // 02 Bytes, offset 34 - gap required by the compiler - not used yet
600
+
float FFT_Magnitude; // 04 Bytes offset 36
601
+
float FFT_MajorPeak; // 04 Bytes offset 40
600
602
};
601
603
602
-
// old "V1" audiosync struct - 83 Bytes - for backwards compatibility
604
+
// old "V1" audiosync struct - 83 Bytes payload, 88 bytes total (with padding added by compiler) - for backwards compatibility
603
605
structaudioSyncPacket_v1 {
604
606
char header[6]; // 06 Bytes
605
607
uint8_t myVals[32]; // 32 Bytes
@@ -612,6 +614,8 @@ class AudioReactive : public Usermod {
612
614
double FFT_MajorPeak; // 08 Bytes
613
615
};
614
616
617
+
#defineUDPSOUND_MAX_PACKET88// max packet size for audiosync
618
+
615
619
// set your config variables to their boot default value (this can also be done in readFromConfig() or a constructor if you prefer)
616
620
#ifdef UM_AUDIOREACTIVE_ENABLE
617
621
bool enabled = true;
@@ -997,7 +1001,6 @@ class AudioReactive : public Usermod {
@@ -1084,9 +1090,12 @@ class AudioReactive : public Usermod {
1084
1090
bool haveFreshData = false;
1085
1091
1086
1092
size_t packetSize = fftUdp.parsePacket();
1087
-
if (packetSize > 5) {
1093
+
#ifdef ARDUINO_ARCH_ESP32
1094
+
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) fftUdp.flush(); // discard invalid packets (too small or too big) - only works on esp32
1095
+
#endif
1096
+
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
1088
1097
//DEBUGSR_PRINTLN("Received UDP Sync Packet");
1089
-
uint8_t fftBuff[packetSize];
1098
+
staticuint8_t fftBuff[UDPSOUND_MAX_PACKET+1] = { 0 }; // static buffer for receiving, to reuse the same memory and avoid heap fragmentation
1090
1099
fftUdp.read(fftBuff, packetSize);
1091
1100
1092
1101
// VERIFY THAT THIS IS A COMPATIBLE PACKET
@@ -1229,7 +1238,7 @@ class AudioReactive : public Usermod {
1229
1238
1230
1239
if (!audioSource) enabled = false; // audio failed to initialise
1231
1240
#endif
1232
-
if (enabled) onUpdateBegin(false); // create FFT task, and initailize network
1241
+
if (enabled) onUpdateBegin(false); // create FFT task, and initialize network
1233
1242
1234
1243
1235
1244
#ifdef ARDUINO_ARCH_ESP32
@@ -1243,7 +1252,7 @@ class AudioReactive : public Usermod {
1243
1252
disableSoundProcessing = true;
1244
1253
}
1245
1254
#endif
1246
-
if (enabled) disableSoundProcessing = false; // all good - enable audio processing
1255
+
if (enabled) disableSoundProcessing = false; // all good - enable audio processing
1247
1256
if (enabled) connectUDPSoundSync();
1248
1257
if (enabled && addPalettes) createAudioPalettes();
1249
1258
initDone = true;
@@ -1803,7 +1812,6 @@ class AudioReactive : public Usermod {
1803
1812
dynLim[F("rise")] = attackTime;
1804
1813
dynLim[F("fall")] = decayTime;
1805
1814
1806
-
1807
1815
JsonObject sync = top.createNestedObject("sync");
1808
1816
sync["port"] = audioSyncPort;
1809
1817
sync["mode"] = audioSyncEnabled;
@@ -2008,8 +2016,8 @@ CRGB AudioReactive::getCRGBForBand(int x, int pal) {
0 commit comments