Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
78 changes: 78 additions & 0 deletions src/hw/alsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading