Skip to content

Commit f63864e

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 582e93c commit f63864e

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
@@ -1062,18 +1062,19 @@ fn set_hw_params_from_format(
10621062
hw_params.set_rate(config.sample_rate.0, alsa::ValueOr::Nearest)?;
10631063
hw_params.set_channels(config.channels as u32)?;
10641064

1065-
match config.buffer_size {
1066-
BufferSize::Fixed(v) => {
1067-
hw_params.set_period_size_near((v / 4) as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
1068-
hw_params.set_buffer_size(v as alsa::pcm::Frames)?;
1069-
}
1065+
let period_size = match config.buffer_size {
1066+
BufferSize::Fixed(v) => v as alsa::pcm::Frames,
10701067
BufferSize::Default => {
1071-
// These values together represent a moderate latency and wakeup interval.
1072-
// Without them, we are at the mercy of the device
1073-
hw_params.set_period_time_near(25_000, alsa::ValueOr::Nearest)?;
1074-
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?;
1068+
// This value represent a moderate latency and wakeup interval.
1069+
1024 as alsa::pcm::Frames
10751070
}
1076-
}
1071+
};
1072+
hw_params.set_period_size_near(period_size, alsa::ValueOr::Nearest)?;
1073+
1074+
// We shouldn't fail if the driver isn't happy here.
1075+
// `default` pcm sometimes fails here, but there no reason to as we
1076+
// provide a direction and 2 is strictly the minimum number of periods.
1077+
let _ = hw_params.set_periods(2, alsa::ValueOr::Greater);
10771078

10781079
pcm_handle.hw_params(&hw_params)?;
10791080

0 commit comments

Comments
 (0)