Skip to content

Commit d115eea

Browse files
authored
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 6ecfec4 commit d115eea

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/host/alsa/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,16 +1061,19 @@ 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+
const NPERIODS: u32 = 2;
10641065
match config.buffer_size {
10651066
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)?;
1067+
hw_params.set_period_size_near(v as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
1068+
hw_params.set_buffer_size((NPERIODS * v) as alsa::pcm::Frames)?;
10681069
}
10691070
BufferSize::Default => {
10701071
// These values together represent a moderate latency and wakeup interval.
10711072
// 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)?;
1073+
const DEFAULT_PERIOD_SIZE: u32 = 1024;
1074+
const DEFAULT_BUFFER_SIZE: u32 = NPERIODS * DEFAULT_PERIOD_SIZE;
1075+
hw_params.set_period_time_near(DEFAULT_PERIOD_SIZE, alsa::ValueOr::Nearest)?;
1076+
hw_params.set_buffer_time_near(DEFAULT_BUFFER_SIZE, alsa::ValueOr::Nearest)?;
10741077
}
10751078
}
10761079

0 commit comments

Comments
 (0)