Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- ALSA(process_output): pass `silent=true` to `PCM.try_recover`, so it doesn't write to stderr
- ALSA: Fix buffer size selection. (error 22)
- WASAPI: Expose IMMDevice from WASAPI host Device.

# Version 0.16.0 (2025-06-07)
Expand Down
28 changes: 18 additions & 10 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,19 +1104,27 @@ fn set_hw_params_from_format(
hw_params.set_rate(config.sample_rate.0, alsa::ValueOr::Nearest)?;
hw_params.set_channels(config.channels as u32)?;

match config.buffer_size {
BufferSize::Fixed(v) => {
hw_params.set_period_size_near((v / 4) as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
hw_params.set_buffer_size(v as alsa::pcm::Frames)?;
}
BufferSize::Default => {
// These values together represent a moderate latency and wakeup interval.
// Without them, we are at the mercy of the device
hw_params.set_period_time_near(25_000, alsa::ValueOr::Nearest)?;
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?;
let mut buffer_size = config.buffer_size;

if let BufferSize::Fixed(v) = buffer_size {
if hw_params
.set_period_size_near((v / 4) as alsa::pcm::Frames, alsa::ValueOr::Nearest)
.is_err()
|| hw_params
.set_buffer_size_near(v as alsa::pcm::Frames)
.is_err()
{
buffer_size = BufferSize::Default;
}
}

if let BufferSize::Default = buffer_size {
// These values together represent a moderate latency and wakeup interval.
// Without them, we are at the mercy of the device
hw_params.set_period_time_near(25_000, alsa::ValueOr::Nearest)?;
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?;
}

pcm_handle.hw_params(&hw_params)?;

Ok(hw_params.can_pause())
Expand Down
Loading