Skip to content

Commit 919d45f

Browse files
committed
ALSA: fix buffer size / period size configuration
With ALSA, the buffer size is `nperiod * period_size`. We want 2 periods. The default was far too large, 1024 samples is a much more reasonable default (still too large for real-time audio). Fixes RustAudio#913
1 parent 4a8c9b8 commit 919d45f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/host/alsa/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,18 +1075,19 @@ fn set_hw_params_from_format(
10751075
hw_params.set_rate(config.sample_rate.0, alsa::ValueOr::Nearest)?;
10761076
hw_params.set_channels(config.channels as u32)?;
10771077

1078-
match config.buffer_size {
1079-
BufferSize::Fixed(v) => {
1080-
hw_params.set_period_size_near((v / 4) as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
1081-
hw_params.set_buffer_size(v as alsa::pcm::Frames)?;
1082-
}
1078+
let period_size = match config.buffer_size {
1079+
BufferSize::Fixed(v) => v as alsa::pcm::Frames,
10831080
BufferSize::Default => {
1084-
// These values together represent a moderate latency and wakeup interval.
1085-
// Without them, we are at the mercy of the device
1086-
hw_params.set_period_time_near(25_000, alsa::ValueOr::Nearest)?;
1087-
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?;
1081+
// This value represent a moderate latency and wakeup interval.
1082+
1024 as alsa::pcm::Frames
10881083
}
1089-
}
1084+
};
1085+
hw_params.set_period_size_near(period_size, alsa::ValueOr::Nearest)?;
1086+
1087+
// We shouldn't fail if the driver isn't happy here.
1088+
// `default` pcm sometimes fails here, but there no reason to as we
1089+
// provide a direction and 2 is strictly the minimum number of periods.
1090+
let _ = hw_params.set_periods(2, alsa::ValueOr::Greater);
10901091

10911092
pcm_handle.hw_params(&hw_params)?;
10921093

0 commit comments

Comments
 (0)