Skip to content

Constrain ALSA buffer size and start_threshold in fallback path#1703

Open
honza wants to merge 1 commit intolibrespot-org:devfrom
honza:alsa-fallback-buffer-size
Open

Constrain ALSA buffer size and start_threshold in fallback path#1703
honza wants to merge 1 commit intolibrespot-org:devfrom
honza:alsa-fallback-buffer-size

Conversation

@honza
Copy link
Copy Markdown

@honza honza commented Apr 15, 2026

When buffer/period negotiation fails and we fall back to the device's defaults, some devices (e.g. USB audio via the front: plugin) end up with enormous buffers (1M+ frames at 44.1 kHz = ~23 seconds of audio).

This causes two user-visible problems:

  1. Playback start is delayed because start_threshold is set to buffer_size - period_size, so ALSA waits for ~23 seconds of data to be written before producing any output.

  2. Track changes block because drain() must wait for the entire buffer to play out before the sink can be reopened for the next track.

Fix both by:

  • Calling set_buffer_size_near(MAX_BUFFER) on the fallback hw params so the device picks a buffer close to 500 ms instead of its unconstrained default. This is best-effort (errors are ignored) and does not change behavior for devices that already negotiate successfully.

  • Capping start_threshold to at most SAMPLE_RATE frames (1 second) so that even if the buffer is still large, playback begins promptly.

When buffer/period negotiation fails and we fall back to the device's
defaults, some devices (e.g. USB audio via the `front:` plugin) end up
with enormous buffers (1M+ frames at 44.1 kHz = ~23 seconds of audio).

This causes two user-visible problems:

1. Playback start is delayed because start_threshold is set to
   buffer_size - period_size, so ALSA waits for ~23 seconds of data
   to be written before producing any output.

2. Track changes block because drain() must wait for the entire buffer
   to play out before the sink can be reopened for the next track.

Fix both by:

- Calling set_buffer_size_near(MAX_BUFFER) on the fallback hw params
  so the device picks a buffer close to 500 ms instead of its
  unconstrained default. This is best-effort (errors are ignored) and
  does not change behavior for devices that already negotiate
  successfully.

- Capping start_threshold to at most SAMPLE_RATE frames (1 second) so
  that even if the buffer is still large, playback begins promptly.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
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.

1 participant