Repository navigation
refactor(audio/fft): dispatcher for ESP-DSP FFT backend - #2310
Conversation
Refactor the 7 kiss_fftr() call sites in fft_impl.cpp.hpp behind a single fl::audio::fft::fl_fft_real_forward() inline wrapper. The wrapper currently delegates to kiss_fftr 1:1 on every platform — zero behavior change. The wrapper is the insertion point for an ESP-DSP backend (dsps_fft2r on ESP32 / ESP32-S3 / ESP32-P4 via hardware DSP / SIMD instructions), which benchmarks suggest would cut the shared-FFT cost in the audio pipeline from ~54 µs to ~15 µs per call — a 3-5× speedup that directly addresses issue #2254 Issue 3 / #2308. Opt-in scaffold via `FL_FFT_USE_ESP_DSP=1` build flag; unused for now (implementation stubbed out, needs hardware validation of the real-FFT post-processing math vs kiss_fftr's half-spectrum output format). Default off everywhere so this commit cannot regress anything. Host FFT test (fl_audio_fft_fft) passes: 1/1. See issue #2308 for the broader audio-performance plan (ESP-DSP FFT + optional multi-core task affinity for ESP32 dual-core variants). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughA new header introduces a centralized real-to-complex FFT dispatcher Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Caller (FFT users)
participant Dispatcher as fl_fft_real_forward
participant Kiss as kiss_fftr
participant ESP as ESP‑DSP (optional)
Caller->>Dispatcher: fl_fft_real_forward(cfg, in, out)
alt FL_FFT_USE_ESP_DSP && FL_IS_ESP32
Dispatcher->>ESP: try ESP‑DSP real‑forward FFT
ESP-->>Dispatcher: result (currently not used / scaffold)
end
Dispatcher->>Kiss: kiss_fftr(cfg, in, out)
Kiss-->>Dispatcher: half‑spectrum output
Dispatcher-->>Caller: out (DC @ out[0], Nyquist @ out[N/2])
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Captures findings from the research in issue #2308 into fft_backend.h so the next contributor implementing ESP-DSP or CMSIS-DSP backends has the context (output-layout differences, sizes supported, which types of unpacking each vendor requires). Decision recorded: keep kiss_fftr's (N/2+1) kiss_fft_cpx layout as FastLED's internal FFT abstraction — isolate each vendor backend's conversion inside fl_fft_real_forward() rather than rewriting all downstream audio consumers (magnitude, binning, CQ kernels). No code change, only comment expansion. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/fl/audio/fft/fft_backend.h (2)
1-1: Replace raw#pragma oncewith a project-compliant include guard.This header uses a raw pragma in a new C++ file. Prefer a conventional include guard here unless the project has a specific FastLED wrapper for this case. As per coding guidelines, “Use FastLED compiler control macros from fl/stl/compiler_control.h instead of raw
#pragmadirectives.”Proposed fix
-#pragma once +#ifndef FL_AUDIO_FFT_FFT_BACKEND_H +#define FL_AUDIO_FFT_FFT_BACKEND_HAnd add at the end of the file:
} // namespace fft } // namespace audio } // namespace fl + +#endif // FL_AUDIO_FFT_FFT_BACKEND_H🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/fl/audio/fft/fft_backend.h` at line 1, Replace the raw "#pragma once" in fft_backend.h with the project-compliant include guard pattern using the FastLED compiler control macros: include "fl/stl/compiler_control.h" at the top, wrap the header contents between the appropriate begin/end macros (e.g., FL_STL_BEGIN/FL_STL_END or the project's equivalent), and add a conventional unique macro-based include guard (e.g., FFT_BACKEND_H_) around the file so the file no longer relies on a raw pragma; ensure the guard symbol matches the file name and the end of the file closes both the macro guard and the compiler control block.
117-119: Consider keeping the raw-pointer shim narrow and adding a span-based API.The
kiss_fftr-compatible signature makes sense for this refactor, but this is now a project-facing wrapper. Consider adding afl::spanoverload later and treating this raw-pointer form as the backend adapter. As per coding guidelines, “Use fl::span for function parameters instead of fl::vector or raw arrays.”🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/fl/audio/fft/fft_backend.h` around lines 117 - 119, The fl_fft_real_forward raw-pointer signature should be kept as a narrow backend shim and a new span-based public API should be added: implement an overload fl_fft_real_forward(kiss_fftr_cfg cfg, fl::span<const kiss_fft_scalar> in, fl::span<kiss_fft_cpx> out) and have the existing raw-pointer function (the one taking const kiss_fft_scalar* in and kiss_fft_cpx* out) simply delegate to that span overload; ensure the span overload enforces sizes/validation and matches coding guidelines to prefer fl::span over raw arrays or vectors while keeping kiss_fftr_cfg, kiss_fft_scalar and kiss_fft_cpx as the referenced symbols.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/fl/audio/fft/fft_backend.h`:
- Line 1: Replace the raw "#pragma once" in fft_backend.h with the
project-compliant include guard pattern using the FastLED compiler control
macros: include "fl/stl/compiler_control.h" at the top, wrap the header contents
between the appropriate begin/end macros (e.g., FL_STL_BEGIN/FL_STL_END or the
project's equivalent), and add a conventional unique macro-based include guard
(e.g., FFT_BACKEND_H_) around the file so the file no longer relies on a raw
pragma; ensure the guard symbol matches the file name and the end of the file
closes both the macro guard and the compiler control block.
- Around line 117-119: The fl_fft_real_forward raw-pointer signature should be
kept as a narrow backend shim and a new span-based public API should be added:
implement an overload fl_fft_real_forward(kiss_fftr_cfg cfg, fl::span<const
kiss_fft_scalar> in, fl::span<kiss_fft_cpx> out) and have the existing
raw-pointer function (the one taking const kiss_fft_scalar* in and kiss_fft_cpx*
out) simply delegate to that span overload; ensure the span overload enforces
sizes/validation and matches coding guidelines to prefer fl::span over raw
arrays or vectors while keeping kiss_fftr_cfg, kiss_fft_scalar and kiss_fft_cpx
as the referenced symbols.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e80f0cbf-b420-486a-bbfd-1924e048a80e
📒 Files selected for processing (1)
src/fl/audio/fft/fft_backend.h
📊 Header Compilation PerformanceTotal Time: 1198.71ms Top 5 Slowest Headers
Warnings
Full Report |
📊 Header Compilation PerformanceTotal Time: 1179.53ms Top 5 Slowest Headers
Warnings
Full Report |
Summary
Full ESP-DSP real-FFT backend for
fl::audio::fft, gated behind opt-in flag. Zero behavior change by default; opt-in via-DFL_FFT_USE_ESP_DSP=1.Addresses issue #2308 (#2254 Issue 3 follow-up — audio pipeline FPS drop from processing time).
What lands
src/fl/audio/fft/fft_backend.h(new) — inlinefl_fft_real_forward(cfg, N, in, out)dispatcher + full ESP-DSP real-FFT implementation.src/fl/audio/fft/fft_impl.cpp.hpp— 7kiss_fftr()call sites switched to the wrapper with explicitNparameter.Default remains
kiss_fftreverywhere. ESP-DSP path is compiled in only when BOTHFL_FFT_USE_ESP_DSP=1AND the build is on ESP32.Why the
Nparameterkiss_fftr_cfgis opaque — the caller can't extract the size. The ESP-DSP path needs the size to index its own per-size twiddle cache, soNis passed explicitly. kiss path casts it tovoid(no behavior change).ESP-DSP implementation notes
Standard "real FFT via packed N/2-complex FFT" technique (Numerical Recipes ch.12):
dsps_fft2r_fc32+dsps_bit_rev_fc32_ansi.cos(2πk/N)/sin(2πk/N)tables.DC + Nyquist pack into
out[0]andout[N/2]respectively (imag=0 for both). Output layout matcheskiss_fftrexactly so no downstream consumer changes.Twiddle tables cached per-size in a static context; realloc only on size transitions. For N=512 that's 255 cos+sin pairs = 2 KB.
Global ESP-DSP twiddle init (
dsps_fft2r_init_fc32) happens once per process, guarded by a static bool.Hardware-FFT survey (captured inside
fft_backend.h)Researched CMSIS-DSP (
arm_rfft_fast_f32), ESP-DSP, Teensy Audio Lib, Cortex-M0+ Q15 variants, and host FFT libraries. Finding: there is no universal standardized real-FFT layout. CMSIS-DSP packs DC+Nyquist intoout[0]; kiss_fftr separates them intoout[0]andout[N/2]; ESP-DSP produces N-point complex and needs custom unpack. Decision: keep kiss_fftr's layout as FastLED's internal abstraction and convert per-backend in the dispatcher. Minimizes disruption to all downstream detectors, magnitude computation, CQ kernels, etc. Full writeup + table inside the header.Expected performance
Per issue #2308's cached benchmarks: on ESP32-S3, FFT shared cost ~54 µs/call with kiss_fftr. With ESP-DSP enabled, expected ~15 µs (3-5× speedup from hardware MAC / DSP instructions). Detectors that stack FFT get ~35 µs back per audio frame.
Default OFF — why
The ESP-DSP path is math-correct per standard identity but has NOT yet been bit-for-bit validated against kiss_fftr on hardware. Enabling it by default without that validation risks introducing subtle audio-detector regressions (phase errors, wrong magnitudes). Recommended workflow for users wanting to benchmark:
build_flags = -DFL_FFT_USE_ESP_DSP=1in platformio.ini.bash test audiohost + device timing).Future follow-up PR should add a host unit test that compares ESP-DSP output against kiss_fftr output (cross-compiled for host with simulated ESP-DSP if possible, or run the unpack math standalone).
Test plan
bash test audio— 1/1 passed (default kiss path unchanged).bash test fft— 1/1 passed.FL_FFT_USE_ESP_DSP=1— expected cost drop confirmed.Related
FL_HAS_MULTICORE_AFFINITY).🤖 Generated with Claude Code