r/cpp • u/Jolly-Addendum-7199 • May 25 '26
Optimizing a real-time C++17 terminal audio visualizer, what am I missing?
I've been building spectrum, a terminal audio visualizer that hooks into WASAPI and runs FFT analysis via FFTW3. Took heavy inspiration from Winamp's spectral analyzer for the peak physics and decay behavior.
Current pipeline:
- 2400-sample Hann-windowed FFT with 95% window overlap (120-sample hop at 48kHz)
- Producer-consumer architecture, mutex-guarded shared buffers between capture and render thread
- AGC with rolling normalization + gamma contrast for dynamic range
- Logarithmic frequency binning (20Hz–16kHz) with perceptual tilt
It runs at 60 FPS with <5% CPU.
What would you optimize next?
I'm hitting a point of diminishing returns (especially with the bar height logic, and what frequencies should and should not be displayed) and would love some architectural feedback.
Considering:
- Lock-free ring buffer to replace the mutex
- WASAPI exclusive mode for lower latency capture
GitHub: github.com/majockbim/spectrum
View all comments
Show parent comment
6
u/ack_error May 25 '26
Nah. Checked the profile, and the FFT is taking <10% of the total process time. An FFT every 120 samples at 48KHz is 400 transforms/sec. My homegrown FFT does a 4K point r2c transform in ~1.6us with AVX2; FFTW has to deal with double precision and a few small prime steps for a 2400 point transform here, but it also has faster algorithms. CPUs love FFTs that are well vectorized and fit well into L1 cache. The calls to log10() for each bin actually take longer than the FFT.
The real problem is system calls. It's spending ~40% of the CPU in the kernel, between the console writes and the calls to Sleep(). Beyond that, the console implementation (Windows Terminal) is also taking 4x the CPU of spectrum.exe. There's a lot that can be done here algorithmically, but this is way on the far side of diminishing returns with the console output dominating CPU usage.