From df26430e42f83cc045536f6edfc703d645194f34 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 25 May 2023 21:45:46 +0200 Subject: [PATCH] audioreactive: replace dynamic buffer with static buffer using a dynamic array for newSamples[num_samples] was never needed. It was a risks as the buffer was allcated/deallocated with every call of getSamples(). --- usermods/audioreactive/audio_source.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 71d121a4..c774a5be 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -106,6 +106,9 @@ #endif +// max number of samples for a single i2s_read --> size of global buffer. +#define I2S_SAMPLES_MAX 512 // same as samplesFFT + /* Interface class AudioSource serves as base class for all microphone types This enables accessing all microphones with one single interface @@ -162,6 +165,7 @@ class AudioSource { bool _initialized; // Gets set to true if initialization is successful bool _i2sMaster; // when false, ESP32 will be in I2S SLAVE mode (for devices that only operate in MASTER mode). Only workds in newer IDF >= 4.4.x float _sampleScale; // pre-scaling factor for I2S samples + I2S_datatype newSampleBuffer[I2S_SAMPLES_MAX+4] = { 0 }; // global buffer for i2s_read }; /* Basic I2S microphone source @@ -350,17 +354,20 @@ class I2SSource : public AudioSource { if (_initialized) { esp_err_t err; size_t bytes_read = 0; /* Counter variable to check if we actually got enough data */ - I2S_datatype newSamples[num_samples]; /* Intermediary sample storage */ - err = i2s_read(I2S_NUM_0, (void *)newSamples, sizeof(newSamples), &bytes_read, portMAX_DELAY); + memset(buffer, 0, sizeof(float) * num_samples); // clear output buffer + I2S_datatype *newSamples = newSampleBuffer; // use global input buffer + if (num_samples > I2S_SAMPLES_MAX) num_samples = I2S_SAMPLES_MAX; // protect the buffer from overflow + + err = i2s_read(I2S_NUM_0, (void *)newSamples, num_samples * sizeof(I2S_datatype), &bytes_read, portMAX_DELAY); if (err != ESP_OK) { DEBUGSR_PRINTF("Failed to get samples: %d\n", err); return; } // For correct operation, we need to read exactly sizeof(samples) bytes from i2s - if (bytes_read != sizeof(newSamples)) { - DEBUGSR_PRINTF("Failed to get enough samples: wanted: %d read: %d\n", sizeof(newSamples), bytes_read); + if (bytes_read != (num_samples * sizeof(I2S_datatype))) { + DEBUGSR_PRINTF("Failed to get enough samples: wanted: %d read: %d\n", num_samples * sizeof(I2S_datatype), bytes_read); return; }