diff --git a/Cargo.lock b/Cargo.lock index f147d86..3c13758 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -450,7 +450,7 @@ dependencies = [ [[package]] name = "maolan-engine" -version = "0.4.28" +version = "0.4.29" dependencies = [ "alsa", "arc-swap", diff --git a/Cargo.toml b/Cargo.toml index ae25996..3fa27e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-engine" -version = "0.4.28" +version = "0.4.29" edition = "2024" description = "Audio engine for the Maolan DAW with audio/MIDI tracks, routing, export, and out-of-process CLAP/VST3/LV2 plugin support" license = "BSD-2-Clause" diff --git a/src/hw/alsa.rs b/src/hw/alsa.rs index 908cd49..c9f9a71 100644 --- a/src/hw/alsa.rs +++ b/src/hw/alsa.rs @@ -1578,6 +1578,84 @@ fn discover_alsa_devices( devices } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AlsaDirection { + Capture, + Playback, +} + +pub fn probe_alsa_period_range( + device: &str, + direction: AlsaDirection, + channels: usize, + sample_rate: usize, + bits: usize, +) -> Result<(usize, usize), String> { + let direction = match direction { + AlsaDirection::Capture => Direction::Capture, + AlsaDirection::Playback => Direction::Playback, + }; + let pcm = PCM::new(device, direction, false) + .map_err(|error| format!("cannot open ALSA {direction:?} device {device}: {error}"))?; + let params = HwParams::any(&pcm).map_err(|error| error.to_string())?; + if params.set_access(Access::MMapInterleaved).is_err() { + params + .set_access(Access::RWInterleaved) + .map_err(|error| error.to_string())?; + } + let formats = match bits { + 32 => vec![ + native_s32_format(), + foreign_s32_format(), + native_s24_format(), + foreign_s24_format(), + native_s16_format(), + foreign_s16_format(), + Format::S8, + ], + 24 => vec![ + native_s24_format(), + foreign_s24_format(), + native_s16_format(), + foreign_s16_format(), + Format::S8, + ], + 16 => vec![native_s16_format(), foreign_s16_format(), Format::S8], + _ => vec![Format::S8], + }; + if !formats + .into_iter() + .any(|format| params.set_format(format).is_ok()) + { + return Err(format!( + "ALSA {direction:?} device {device} has no supported integer format" + )); + } + let actual_channels = params + .set_channels_near(channels.max(1) as u32) + .map_err(|error| error.to_string())?; + if actual_channels < channels as u32 { + return Err(format!( + "ALSA {direction:?} device {device} supports {actual_channels} channels, but channel {channels} was requested" + )); + } + params + .set_rate(sample_rate as u32, ValueOr::Nearest) + .map_err(|error| error.to_string())?; + let min = params + .get_period_size_min() + .map_err(|error| error.to_string())? as usize; + let max = params + .get_period_size_max() + .map_err(|error| error.to_string())? as usize; + if min == 0 || max < min { + return Err(format!( + "ALSA {direction:?} device {device} reported an invalid period range" + )); + } + Ok((min, max)) +} + #[cfg(test)] mod mmap_tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 4914a30..8cd192f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,9 @@ pub mod preferences; pub mod wav_stream; #[cfg(target_os = "linux")] pub mod audio_devices { - pub use crate::hw::alsa::{AudioDeviceDescriptor, discover_alsa_audio_devices}; + pub use crate::hw::alsa::{ + AlsaDirection, AudioDeviceDescriptor, discover_alsa_audio_devices, probe_alsa_period_range, + }; } #[cfg(target_os = "macos")] pub mod audio_devices {