Skip to content

Commit 2222ff1

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 #913
1 parent 6ecfec4 commit 2222ff1

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/host/alsa/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1061,18 +1061,15 @@ fn set_hw_params_from_format(
10611061
hw_params.set_rate(config.sample_rate.0, alsa::ValueOr::Nearest)?;
10621062
hw_params.set_channels(config.channels as u32)?;
10631063

1064-
match config.buffer_size {
1065-
BufferSize::Fixed(v) => {
1066-
hw_params.set_period_size_near((v / 4) as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
1067-
hw_params.set_buffer_size(v as alsa::pcm::Frames)?;
1068-
}
1064+
let period_size = match config.buffer_size {
1065+
BufferSize::Fixed(v) => v as alsa::pcm::Frames,
10691066
BufferSize::Default => {
1070-
// These values together represent a moderate latency and wakeup interval.
1071-
// Without them, we are at the mercy of the device
1072-
hw_params.set_period_time_near(25_000, alsa::ValueOr::Nearest)?;
1073-
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?;
1067+
// This value represent a moderate latency and wakeup interval.
1068+
1024 as alsa::pcm::Frames
10741069
}
1075-
}
1070+
};
1071+
hw_params.set_period_size_near(period_size, alsa::ValueOr::Nearest)?;
1072+
hw_params.set_periods(2, alsa::ValueOr::Greater);
10761073

10771074
pcm_handle.hw_params(&hw_params)?;
10781075

0 commit comments

Comments
 (0)