Skip to content

perf(audio): optimize audio processing pipeline to restore LED FPS #2308

Closed

Description

@zackees

Summary

Follow-up to #2254 Issue 3 ("FastLED Audio driver reduces FPS 100 → 60"). The streaming/yield fixes in commit 25873cf32c and PR #2300 did NOT resolve this — those targeted I2S and SPI drivers. Audio is a separate code path and its bottleneck is processing time per call, not scheduler starvation.

From profiling (baseline 16 kHz, 1000 iterations, cached from earlier work):

Stage Time/call Notes
EnergyAnalyzer (baseline) 17.6 µs Minimum overhead
FrequencyBands 72 µs = 54 µs FFT + 18 µs analysis
VibeDetector 69 µs = 54 µs FFT + 15 µs analysis
VocalDetector 51 µs FFT user
BeatDetector 25 µs No FFT
TempoAnalyzer 23 µs No FFT
TransientDetector 24 µs No FFT
PitchDetector 19 µs Clever FFT reuse
All detectors combined ~165 µs FFT is shared once (~54 µs), analyses stack

The shared FFT path through src/fl/audio/fft/fft_impl.cpp.hpp uses kiss_fftr (third_party/cq_kernel), a portable pure-C FFT. On Xtensa / RISC-V it doesn't use SIMD / DSP acceleration.

Two complementary optimizations

(A) Replace FFT backend with ESP-DSP on ESP32 — IN PROGRESS

ESP-DSP is already bundled with the Arduino ESP32 framework (verified at framework-arduinoespressif32-libs/esp32*/include/espressif__esp-dsp/modules/fft/). It provides dsps_fft2r/dsps_fft4r with hardware-optimized radix-2/radix-4 using Xtensa DSP instructions on ESP32/S2/S3 and RISC-V DSP extensions on C3/C6. Typical 3-5× speedup on Xtensa, 2-3× on RISC-V → expected FFT cost drop from ~54 µs to ~10-20 µs. Detectors stacking FFT get ~35 µs back per frame.

Scope: add a platform-gated FFT backend in src/fl/audio/fft/. Conditional on ESP_PLATFORM or similar; kiss_fftr stays the universal fallback. Does NOT change the public FFT API.

(B) Audio task on separate core (ESP32 multi-core only) — QUEUED

Pin audio processing to the idle second core on ESP32 variants that have one, so audio never competes with LED render for CPU.

Key research findings:

  • ESP32 FastLED coroutine layer already has CoroutineConfig::core_id (src/fl/task/task.h:135) — but it's currently ignored in the ESP32 backend (src/platforms/esp/32/coroutine_esp32.impl.hpp:202-210 hardcodes tskNO_AFFINITY).
  • Dual-core platforms where this actually helps: ESP32 (original), ESP32-S3, ESP32-P4. Everything else is effectively single-core from an RTOS-task perspective.
  • Single-core ESP32 variants (S2, C2, C3, C5, C6, H2): xTaskCreatePinnedToCore(core=1) returns NULL. Must validate before calling.
  • Non-ESP32 platforms with multiple cores don't map to this model:
    • RP2040/RP2350 (dual Cortex-M0+ / M33) — no SMP RTOS; uses multicore_launch_core1() bare-metal.
    • STM32 H747 (M7+M4) — asymmetric multiprocessing, partitioned firmware, no task affinity.
    • Teensy 4.x — single-core; FreeRTOS optional but no SMP.

Proposed feature-flag additions (matches existing feature_flags/ convention):

// src/platforms/esp/32/feature_flags/enabled.h
#if defined(FL_IS_ESP_32DEV) || defined(FL_IS_ESP_32S3) || defined(FL_IS_ESP_32P4)
#define FL_HAS_MULTICORE_AFFINITY 1
#define FL_CPU_CORES 2
#else
#define FL_HAS_MULTICORE_AFFINITY 0
#define FL_CPU_CORES 1
#endif

Default behavior: status quo — audio runs on whatever core the OS schedules it on. Only opt-in consumers get the pinning benefit. Matches user direction: "(1) should be gated by a setting, default is status quo".

Implementation sketch for (B):

  1. Wire CoroutineConfig::core_id through createTaskCoroutine() in coroutine_esp32.impl.hpp — respect the value, validate against FL_CPU_CORES, fall back to tskNO_AFFINITY if out of range.
  2. Add fl::audio::Processor::setCoreAffinity(int core) API surface — stores the core ID and passes it to task::every_ms(1) when constructing the auto-update coroutine.
  3. Document: "call setCoreAffinity(1) on ESP32/S3/P4 before .start() to offload audio onto the idle core".

Test plan

For (A):

  • Host unit test (bash test) — kiss_fftr path unchanged, regression-free.
  • ESP32-S3 hardware — measure FFT cost before/after with the same 1000-iteration benchmark. Expected: 54 µs → ~15 µs.
  • Validate FFT output matches kiss_fftr within rounding tolerance (test harness in tests/fl/audio/fft/).

For (B):

  • Host unit test — CoroutineConfig::core_id honored by a mock backend, out-of-range rejected.
  • ESP32-S3 hardware — verify audio task pinned to core 1 (read uxTaskGetCoreID at runtime), confirm LED FPS under audio + FastLED.show recovers to ~100 FPS.
  • ESP32-C3 single-core — confirm out-of-range core_id gracefully falls back.

Related

Activity

  1. added
    platform: esp32
    on Apr 18, 2026
  2. zackees commented on Jun 5, 2026

    @zackees
    MemberAuthor

    Closing — the two proposals in this issue landed or were intentionally parked:

    Closing because the actionable scope is delivered. Re-open with measured before/after FPS numbers if Option A turns out to be insufficient on a specific board — that's the signal #2359 is waiting for.

  3. moved this from Triage to Done in FastLED Trackeron Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform: esp32

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions