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
7
u/lonkamikaze May 25 '26
I had a raycaster that I profiled and optimised the hell out of, 1% of performance at a time.
After I ran out of ideas on how to improve the functions most time was spent in I did some minor optimization on code that wasn't flagged by the profiler at all.
The result was 6 × the throughout.
I did the change only because I thought it makes the code a little cleaner and easier to reason about. It also eliminated a division from the hot code path.
What I'm trying to say, the profiler usually helps, but it doesn't always let you find the places that matter. Also, divisions are evil.