Skip to content

Fix PulseAudio FFT Hann window application and sizing (#533) - #663

Open
psieg wants to merge 2 commits into
masterfrom
fix/533-pulseaudio-fft-window
Open

psieg wants to merge 2 commits into
masterfrom
fix/533-pulseaudio-fft-window

Conversation

@psieg

@psieg psieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Fixes #533

Summary

Fixes three bugs in PulseAudioSoundManager that caused Linux sound visualization to lose low and high frequencies, suffer spectral leakage, and drop/corrupt buffered leftover samples, and addresses Round-1 review feedback:

  1. Hann window sizing (PulseAudioSoundManager::init_buffers):

    • Resize m_weights to m_buffer_samples (fftSize() * 2 = 2048) instead of fftSize() + 1 (1025), and initialize with normalized WINDOW_HANNING.
    • Ensure m_buffer_samples = fftSize() * 2 and channel defaults are consistently initialized.
  2. Time-domain windowing, multi-channel downmix & FFT amplitude scaling (PulseAudioSoundManager::process_fft):

    • Apply m_weights[i] in the time domain prior to fftwf_execute(m_plan) instead of multiplying FFT magnitudes by m_weights[i] in the frequency domain (which previously zeroed out low and high frequency bins).
    • Downmix interleaved channels by averaging across all channels when m_ss.channels > 1 while keeping a direct fast path for mono (channels == 1).
    • Compute magnitudes with std::hypotf and scale AC bins (i > 0) by 2.0f (1.0f for DC i == 0), matching the normalized Hann window gain so a unit-amplitude sine wave yields magnitude ~1.0 (consistent with Windows BASSWASAPI / macOS vDSP).
  3. Stream buffer leftover sample tracking (PulseAudioSoundManager::stream_read_cb / consumeAudioData):

    • Extract consumeAudioData() to loop over all complete frames in padata, invoking process_fft() whenever a full m_buffer_samples frame completes and accurately preserving any remaining sample count in m_pa_samples_read (instead of resetting m_pa_samples_read = 0 or discarding data across oversized chunks).
    • Avoid calling pa_stream_drop when pa_stream_peek returns padata == nullptr && nbytes == 0.

Testing

  • Added PulseAudioSoundManagerTest (Software/tests/PulseAudioSoundManagerTest.hpp / .cpp), guarded under unix:!macx:contains(DEFINES, PULSEAUDIO_SUPPORT) in Software/tests/tests.pro (supporting PULSEAUDIO_INC_DIR/FFTW3_INC_DIR/PULSEAUDIO_LIB_DIR/FFTW3_LIB_DIR, using packagesExist(libpulse fftw3f) for PKGCONFIG += libpulse fftw3f with LIBS += -lpulse -lfftw3f fallback) and #if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) && defined(PULSEAUDIO_SUPPORT) in Software/tests/TestsMain.cpp.
  • Verified qmake6 -r && make -j2 and QT_QPA_PLATFORM=offscreen ./bin/LightpackTests on Linux:
    • With PULSEAUDIO_SUPPORT enabled: all 9 test suites passed (0 failed, 0 skipped), including PulseAudioSoundManagerTest (12 passed, 0 failed, covering Hann weights, low/mid/high frequencies, fractional-bin sidelobe suppression, DC/amplitude scaling, leftover sample tracking, stereo and 4-channel downmix, and end-to-end SoundVisualizer LED output with qScopeGuard settings cleanup).
    • With PULSEAUDIO_SUPPORT disabled: clean build and all 8 remaining test suites passed (0 failed, 0 skipped).
    • With libpulse.pc/fftw3f.pc absent from PKG_CONFIG_LIBDIR and custom *_INC_DIR/*_LIB_DIR paths: qmake6 cleanly fell back to -L... -lpulse -lfftw3f.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux prismatik sound visualisation very lacking in comparison to windows

1 participant