Skip to content

ALSA: fix buffer size / period size configuration #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,18 +1061,19 @@ 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)?;
}
let period_size = match config.buffer_size {
BufferSize::Fixed(v) => 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)?;
// This value represent a moderate latency and wakeup interval.
Comment on lines +1065 to +1067
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Fixed buffer size case is treating the buffer size as period size directly, but according to the PR description and ALSA documentation, buffer size should be nperiod * period_size. For 2 periods, the period size should be v / 2, not v.

Copilot uses AI. Check for mistakes.

1024 as alsa::pcm::Frames
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The explicit cast as alsa::pcm::Frames is unnecessary here since 1024 can be directly assigned to the alsa::pcm::Frames type. Consider using just 1024.

Suggested change
1024 as alsa::pcm::Frames
1024

Copilot uses AI. Check for mistakes.

}
}
};
hw_params.set_period_size_near(period_size, alsa::ValueOr::Nearest)?;

// We shouldn't fail if the driver isn't happy here.
// `default` pcm sometimes fails here, but there no reason to as we
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar error: 'there no reason' should be 'there is no reason' or 'there's no reason'.

Suggested change
// `default` pcm sometimes fails here, but there no reason to as we
// `default` pcm sometimes fails here, but there's no reason to as we

Copilot uses AI. Check for mistakes.

// provide a direction and 2 is strictly the minimum number of periods.
let _ = hw_params.set_periods(2, alsa::ValueOr::Greater);

pcm_handle.hw_params(&hw_params)?;

Expand Down