Skip to content

Commit 14ab70d

Browse files
authored
AR sound sync - document data offsets
The compiler has added gaps, to align our struct for hardware compatibility. * made the gaps explicit * added `__attribute__ ((packed))`, to ensure that the data layout is the same on all platforms (extensa, risc-v, 8266)
1 parent 27e0bff commit 14ab70d

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

usermods/audioreactive/audio_reactive.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -994,19 +994,21 @@ class AudioReactive : public Usermod {
994994
int8_t mclkPin = MCLK_PIN;
995995
#endif
996996
#endif
997-
// new "V2" audiosync struct - 40 Bytes
998-
struct audioSyncPacket {
999-
char header[6]; // 06 Bytes
1000-
float sampleRaw; // 04 Bytes - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
1001-
float sampleSmth; // 04 Bytes - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
1002-
uint8_t samplePeak; // 01 Bytes - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
1003-
uint8_t frameCounter; // 01 Bytes - track duplicate/out of order packets
1004-
uint8_t fftResult[16]; // 16 Bytes
1005-
float FFT_Magnitude; // 04 Bytes
1006-
float FFT_MajorPeak; // 04 Bytes
997+
// new "V2" audiosync struct - 44 Bytes
998+
struct __attribute__ ((packed)) audioSyncPacket { // WLEDMM "packed" ensures that there are no additional gaps
999+
char header[6]; // 06 Bytes offset 0
1000+
uint8_t gap1[2]; // gap added by compiler: 02 Bytes, offset 6
1001+
float sampleRaw; // 04 Bytes offset 8 - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
1002+
float sampleSmth; // 04 Bytes offset 12 - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
1003+
uint8_t samplePeak; // 01 Bytes offset 16 - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
1004+
uint8_t frameCounter; // 01 Bytes offset 17 - track duplicate/out of order packets
1005+
uint8_t fftResult[16]; // 16 Bytes offset 18
1006+
uint8_t gap2[2]; // gap added by compiler: 02 Bytes, offset 34
1007+
float FFT_Magnitude; // 04 Bytes offset 36
1008+
float FFT_MajorPeak; // 04 Bytes offset 40
10071009
};
10081010

1009-
// old "V1" audiosync struct - 83 Bytes - for backwards compatibility
1011+
// old "V1" audiosync struct - 83 Bytes payload, 88 bytes total - for backwards compatibility
10101012
struct audioSyncPacket_v1 {
10111013
char header[6]; // 06 Bytes
10121014
uint8_t myVals[32]; // 32 Bytes
@@ -1897,6 +1899,26 @@ class AudioReactive : public Usermod {
18971899
DEBUGSR_PRINTLN(enabled ? F("true.") : F("false."));
18981900
USER_FLUSH();
18991901

1902+
// dump audiosync data layout
1903+
#if defined(SR_DEBUG)
1904+
{
1905+
audioSyncPacket data;
1906+
USER_PRINTF("\naudioSyncPacket_v1 size = %d\n", sizeof(audioSyncPacket_v1)); // size 88
1907+
USER_PRINTF("audioSyncPacket size = %d\n", sizeof(audioSyncPacket)); // size 44
1908+
USER_PRINTF("| char header[6] offset = %2d size = %2d\n", offsetof(audioSyncPacket, header[0]), sizeof(data.header)); // offset 0 size 6
1909+
USER_PRINTF("| uint8_t gap1[2] offset = %2d size = %2d\n", offsetof(audioSyncPacket, gap1[0]), sizeof(data.gap1)); // offset 6 size 2
1910+
USER_PRINTF("| float sampleRaw offset = %2d size = %2d\n", offsetof(audioSyncPacket, sampleRaw), sizeof(data.sampleRaw)); // offset 8 size 4
1911+
USER_PRINTF("| float sampleSmth offset = %2d size = %2d\n", offsetof(audioSyncPacket, sampleSmth), sizeof(data.sampleSmth)); // offset 12 size 4
1912+
USER_PRINTF("| uint8_t samplePeak offset = %2d size = %2d\n", offsetof(audioSyncPacket, samplePeak), sizeof(data.samplePeak)); // offset 16 size 1
1913+
USER_PRINTF("| uint8_t frameCounter offset = %2d size = %2d\n", offsetof(audioSyncPacket, frameCounter), sizeof(data.frameCounter)); // offset 17 size 1
1914+
USER_PRINTF("| uint8_t fftResult[16] offset = %2d size = %2d\n", offsetof(audioSyncPacket, fftResult[0]), sizeof(data.fftResult)); // offset 18 size 16
1915+
USER_PRINTF("| uint8_t gap2[2] offset = %2d size = %2d\n", offsetof(audioSyncPacket, gap2[0]), sizeof(data.gap2)); // offset 34 size 2
1916+
USER_PRINTF("| float FFT_Magnitude offset = %2d size = %2d\n", offsetof(audioSyncPacket, FFT_Magnitude), sizeof(data.FFT_Magnitude));// offset 36 size 4
1917+
USER_PRINTF("| float FFT_MajorPeak offset = %2d size = %2d\n", offsetof(audioSyncPacket, FFT_MajorPeak), sizeof(data.FFT_MajorPeak));// offset 40 size 4
1918+
USER_PRINTLN(); USER_FLUSH();
1919+
}
1920+
#endif
1921+
19001922
#if defined(ARDUINO_ARCH_ESP32) && defined(SR_DEBUG)
19011923
DEBUGSR_PRINTF("|| %-9s min free stack %d\n", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); //WLEDMM
19021924
#endif

0 commit comments

Comments
 (0)