projectM-visualizer / frontend-sdl2

Standalone application based on SDL2 that turns your desktop audio into awesome visuals. Available on most platforms supported by both libprojectM and libSDL2.
GNU General Public License v3.0
57 stars 32 forks source link

Fix SDL audio capture issues #48

Closed kblaschke closed 1 year ago

kblaschke commented 1 year ago

Two issues with SDL audio capturing are fixed here:

  1. The input buffer size was fixed to the maximum sample count stored by libprojectM (e.g. the internal sample buffer size), which is currently 2048 samples. This caused SDL to collect data until this size was reached, calling the audio callback only with about 21 FPS, which would only update waveforms every third frame making it look jaggy.

    Now, the input buffer size is set to match the target FPS value, and is clamped to a minimum of 300 samples (~144 FPS) and the maximum buffer size projectM can use (21 FPS). This should effctively smooth the waveform display even on high FPS monitors, while still working well with very low FPS settings.

  2. The projectm_pcm_add_float() was only called with 1 or 2 channels, but if the channel count was different, it was never called, not rendering any audio data at all. As SDL guarantees that front left/right channels are always delivered at interleave index 0 and 1, we can simply provide the actual channel count to the library, and it wil pick the correct samples from the audio buffer.

The input buffer size could in theory be matched to the actual FPS, but this would require restarting the SDL capture thread every few frames after requesting a new input buffer format, which isn't really a good idea.