From 3c11c9ec626aa3f81fb67f3fe5ca75d5406395d8 Mon Sep 17 00:00:00 2001 From: ainyan03 <205502311+ainyan03@users.noreply.github.com> Date: Thu, 15 Oct 2026 13:43:28 +0000 Subject: [PATCH] examples/Mic_FFT: pace drawing by captured audio, build on PC loop() advanced one drawing step whenever two recording requests were queued, whether or not new audio had arrived. The history row scrolls once per five steps, so its speed depended on how fast the display draws: on the PC build it scrolled hundreds of rows per second over the same data. A drawing cycle now starts only after a new block has been queued, which limits the history to the capture rate; a display slower than that is unaffected. Also use decltype instead of the GNU typeof extension, and malloc / free instead of heap_caps_* outside ESP_PLATFORM, so the example builds as is on the PC (SDL) build. --- examples/Advanced/Mic_FFT/Mic_FFT.ino | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/Advanced/Mic_FFT/Mic_FFT.ino b/examples/Advanced/Mic_FFT/Mic_FFT.ino index e0f6963c..b2b05f9d 100644 --- a/examples/Advanced/Mic_FFT/Mic_FFT.ino +++ b/examples/Advanced/Mic_FFT/Mic_FFT.ino @@ -9,15 +9,24 @@ #include #include +#include static void* memory_alloc(size_t size) { +#if defined ( ESP_PLATFORM ) return heap_caps_malloc(size, MALLOC_CAP_8BIT); +#else + return malloc(size); +#endif } static void memory_free(void* ptr) { +#if defined ( ESP_PLATFORM ) heap_caps_free(ptr); +#else + free(ptr); +#endif } struct wav_data_t { @@ -719,10 +728,10 @@ void setup(void) fft_data.sample_rate = SAMPLE_RATE; fft_data.wav_data = &wav_data; fft_data.length = (1 << (fft_data.fft_size_bits - 1)) + 1; - fft_data.fdata = (typeof(fft_data.fdata))memory_alloc(fft_data.length * sizeof(fft_data.fdata[0])); + fft_data.fdata = (decltype(fft_data.fdata))memory_alloc(fft_data.length * sizeof(fft_data.fdata[0])); wav_data.length = WAVE_TOTAL_SIZE; - wav_data.wav = (typeof(wav_data.wav))memory_alloc(WAVE_TOTAL_SIZE * sizeof(wav_data.wav[0])); + wav_data.wav = (decltype(wav_data.wav))memory_alloc(WAVE_TOTAL_SIZE * sizeof(wav_data.wav[0])); memset(wav_data.wav, 0 , WAVE_TOTAL_SIZE * sizeof(int16_t)); } @@ -732,14 +741,27 @@ void loop(void) if (M5.Mic.isEnabled()) { + static bool has_new_block = false; int wav_idx = wav_data.latest_index; while (M5.Mic.isRecording() < 2) { M5.Mic.record(&(wav_data.wav[wav_idx]), WAVE_BLOCK_SIZE, SAMPLE_RATE, false); wav_idx += WAVE_BLOCK_SIZE; if (wav_idx >= WAVE_TOTAL_SIZE) { wav_idx = 0; } wav_data.latest_index = wav_idx; + has_new_block = true; }; + // Start a new drawing cycle only after a new block has been captured, + // so the history scrolls at the capture rate even on a fast display. + if (step < 0 || step >= 4) + { + if (!has_new_block) { + M5.delay(1); + return; + } + has_new_block = false; + } + { switch (++step) { default: