Skip to content

Clamp the default buffer size to what the device supports - #914

Open
benface wants to merge 3 commits into
RustAudio:masterfrom
benface:fix/clamp-buffer-size-to-device-range
Open

benface wants to merge 3 commits into
RustAudio:masterfrom
benface:fix/clamp-buffer-size-to-device-range

Conversation

@benface

@benface benface commented Aug 6, 2026 •

Copy link
Copy Markdown

from_device picks a 50 ms buffer without checking what the device accepts. My AirPods only take 15–960 frames, so it asks for 2048, and cpal 0.18 refuses a fixed size outside the supported range — the stream never opens.

open_default_sink then walks the other devices and takes the first that opens. On my machine that's a virtual output from a screen recorder, so the audio plays somewhere nobody can hear. That part surprised me more than the failure itself.

The range is already in default_config, so when the device refuses the 50 ms size as an unsupported config, this retries with it clamped to that range. It doesn't clamp up front, since the range isn't always a limit: WASAPI in shared mode reports its period as a one-value range and takes any size.

Might be #900 as well — same "worked on crates.io, unsupported device on master" shape, on Windows.

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_device() now treats the WASAPI shared-mode scheduling period as a buffer limit. For software endpoints, CPAL 0.18.1 and 0.18.2 report that period as a one-value range. The stream builder passes the requested duration to IAudioClient::Initialize. With a custom CPAL device at 48 kHz, the base requests 2048 frames and this PR requests 480, even though the device accepts 2048. Can we preserve the preferred request when the backend accepts it? The actual buffer allocation still needs a Windows check.

In the capped-device test, the PR opens the stream with 960 frames. It would be worth keeping regression tests for both cases. The default macOS suite passed and Clippy had no new warnings. The stream docs could also use the 50 ms target and express buffer duration as frames divided by sample rate.

The reported range isn't always a limit. WASAPI in shared mode reports
the device period as a one-value range for software endpoints, and
takes any size, so clamping to it asked for 480 frames at 48 kHz where
2048 opened fine.

Keep asking for 50 ms first, and fall back to the nearest size in the
range only if building the stream fails. A size set with
`with_buffer_size` or `with_config` is still never replaced.

Tests cover a capped device, which now opens with 960 frames after
2048 is refused, and one that takes the preferred size despite its
range, using cpal's custom devices. The `stream` docs now state the
50 ms target and give a buffer's duration as frames over the sample
rate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@benface

benface commented Nov 25, 2026

Copy link
Copy Markdown
Author

Thanks @kapok-4o! Good catch on WASAPI, and cpal's code agrees: for software endpoints, the range is the device period, and Initialize takes any duration in shared mode.

  • Keep the preferred size when the device takes it. from_device asks for 50 ms again. Only if building the stream fails does it retry with the size clamped to the reported range. A size set with with_buffer_size or with_config is never replaced. (0284e05)
  • Tests for both cases, with cpal's custom devices (I added the custom feature to the dev-dependency). A device capped at 15–960 frames opens with 960 once 2048 is refused, and one that reports 480–480 but takes anything keeps 2048. A third test checks that a size you chose isn't replaced.
  • Docs. The stream docs now state the 50 ms target (they said 100 ms), and with_buffer_size gives a buffer's duration as frames over the sample rate.

What I couldn't check is the buffer WASAPI really allocates, since I don't have a Windows machine. The tests only show what rodio asks for. Also, the speakers API's OutputConfig::from clamps to the reported range up front, so it probably asks WASAPI for its period too. I left it alone here, since it's outside this PR.

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Both earlier points are addressed: with 0284e05 a WASAPI-style one-value range keeps the 2048-frame request, and the capped case retries with 960. The stream docs now give the 50 ms target and frames divided by sample rate.

I checked whether the new tests catch the bug. On the base stream.rs, capped_device_opens_with_the_largest_size_it_takes fails. On 427acec, both it and device_keeps_the_preferred_size_when_it_takes_it fail. chosen_buffer_size_is_never_replaced passes on all three, so it guards against a future regression rather than this change.

One small, optional point: src/stream.rs:447 retries on any BuildError. With a custom device returning DeviceBusy, rodio requests 2048 and then 960, and still returns the first error, so nothing is masked. It only costs one pointless attempt. CPAL 0.18.1 reports an out-of-range size as ErrorKind::UnsupportedConfig on CoreAudio and ALSA, so checking e.kind() == UnsupportedConfig would limit the retry to that case.

Locally on macOS (stable 1.99): cargo fmt --check and cargo clippy --all-features -- -D warnings are clean. cargo test --all-targets passed 113 (1 ignored), --all-features passed 153 (1 ignored), and --doc passed 71 (2 ignored). The Windows allocation is still untested on my side. Otherwise this looks good to me.

Any build error led to a second attempt with the clamped buffer size,
so a busy device was tried twice for nothing. Hosts report a buffer
size outside the device's range as `UnsupportedConfig`, so retry only
on that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@benface

benface commented Nov 26, 2026

Copy link
Copy Markdown
Author

Thanks @kapok-4o, and for checking which tests catch what!

Done in 2868c19: the retry now only happens when the build fails with ErrorKind::UnsupportedConfig. Beyond CoreAudio and ALSA, PulseAudio, PipeWire, JACK, ASIO, iOS and Web Audio also report an out-of-range buffer size that way in cpal 0.18.1. A new test, busy_device_is_not_tried_again_with_another_size, checks that a device refusing with DeviceBusy gets a single attempt and returns that error.

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.

2 participants