From 52eb604013f1ec5a143b472c6aeed24d0bcfde64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Sat, 28 Nov 2026 01:30:52 +0100 Subject: [PATCH] Move alsa discovery to engine --- Cargo.lock | 21 +++-- Cargo.toml | 6 +- src/devices.rs | 235 ++++--------------------------------------------- 3 files changed, 29 insertions(+), 233 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a1ce9a..19a73fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -625,9 +625,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.11.3" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69" +checksum = "95e45cf2980b2d467be49a9ed942c9179b701063a7ca3d037de7ee9bd67f8d66" dependencies = [ "find-msvc-tools", "jobserver", @@ -1174,9 +1174,9 @@ dependencies = [ [[package]] name = "find-msvc-tools" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +checksum = "3e44f110d1777dc6559a51e7d45083945ded4f1bf3305494350f693b6d20b9c8" [[package]] name = "flate2" @@ -1691,9 +1691,9 @@ dependencies = [ [[package]] name = "iced_drop" -version = "0.2.59" +version = "0.2.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf898bb059c076ca03c26cd4e70ef52303e9321a60062f7d12dd14a30859038" +checksum = "13dbf1505e032a8bbd86cf3b7a627314ebd9be8563a3ed1495facb8d0298b8ff" dependencies = [ "iced_core", "iced_runtime", @@ -2291,7 +2291,6 @@ dependencies = [ name = "maolan-editor" version = "0.1.7" dependencies = [ - "alsa", "maolan-engine", "maolan-widgets", "rfd", @@ -2304,9 +2303,9 @@ dependencies = [ [[package]] name = "maolan-engine" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4d719514d35d0c442ddf44fda37e074c594821d4e2d56c52523fe5794c805cb" +checksum = "8e6fefa4429c3116f1bb5a93608a28be91b12e7d586d25998e7d8fdfd26cea6f" dependencies = [ "alsa", "arc-swap", @@ -2357,9 +2356,9 @@ dependencies = [ [[package]] name = "maolan-widgets" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8f1fa0d6a52285842226e8578631157471a485a003b8c73aeca3533f2eb2c0" +checksum = "5f08e81dc23ef2499b6a6fd03a64399bc4762909507b893c0c8ea6298650073c" dependencies = [ "bytemuck", "iced", diff --git a/Cargo.toml b/Cargo.toml index 2e9d054..702fd60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ debug = true [features] default = ["standalone"] standalone = [ - "dep:alsa", "dep:maolan-engine", "maolan-engine?/audio", "maolan-engine?/plugins", @@ -26,7 +25,7 @@ standalone = [ embedded = [] [dependencies] -maolan-engine = { version = "0.4.27", optional = true } +maolan-engine = { version = "0.4.28", optional = true } maolan-widgets = "0.1.6" rfd = "0.18.0" serde = { version = "1.0", features = ["derive"] } @@ -35,9 +34,6 @@ serde_json = "1.0.154" toml = "1.2.1" tokio = { version = "1.54.1", features = ["sync", "time"] } -[target.'cfg(target_os = "linux")'.dependencies] -alsa = { version = "0.12.1", optional = true } - [dev-dependencies] tokio = { version = "1.54.1", features = ["rt", "macros"] } diff --git a/src/devices.rs b/src/devices.rs index 4877118..d52fb3a 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -96,6 +96,20 @@ impl AudioDeviceOption { } } +#[cfg(all(feature = "standalone", target_os = "linux"))] +impl From for AudioDeviceOption { + fn from(device: maolan_engine::audio_devices::AudioDeviceDescriptor) -> Self { + Self::with_supported_direction_caps( + device.id, + device.label, + device.supported_bits, + device.supported_sample_rates, + device.supports_input, + device.supports_output, + ) + } +} + #[cfg(all(feature = "standalone", target_os = "freebsd"))] impl From for AudioDeviceOption { fn from(device: maolan_engine::audio_devices::AudioDeviceDescriptor) -> Self { @@ -348,12 +362,10 @@ fn platform_audio_devices() -> Vec { } #[cfg(target_os = "linux")] { - let mut output_devices = platform_linux::discover_alsa_output_devices(); - let mut input_devices = platform_linux::discover_alsa_input_devices(); - output_devices.append(&mut input_devices); - output_devices.sort_by_key(|device| device.label.to_lowercase()); - output_devices.dedup_by(|a, b| a.id == b.id); - output_devices + maolan_engine::audio_devices::discover_alsa_audio_devices() + .into_iter() + .map(AudioDeviceOption::from) + .collect() } #[cfg(target_os = "openbsd")] { @@ -379,217 +391,6 @@ fn platform_audio_devices() -> Vec { vec![simple_audio_device("default")] } -#[cfg(all(feature = "standalone", target_os = "linux"))] -mod platform_linux { - use alsa::{ - Direction, - pcm::{Access, Format, HwParams, PCM}, - }; - - const SAMPLE_RATE_CANDIDATES: [u32; 12] = [ - 8_000, 11_025, 16_000, 22_050, 32_000, 44_100, 48_000, 88_200, 96_000, 176_400, 192_000, - 384_000, - ]; - - fn read_alsa_card_labels() -> std::collections::HashMap { - let mut labels = std::collections::HashMap::new(); - let Ok(contents) = std::fs::read_to_string("/proc/asound/cards") else { - return labels; - }; - for line in contents.lines() { - let line = line.trim_start(); - let Some((num_str, rest)) = line.split_once(' ') else { - continue; - }; - let Ok(card) = num_str.parse::() else { - continue; - }; - let Some((_, desc)) = rest.split_once("]:") else { - continue; - }; - let desc = desc.trim(); - if !desc.is_empty() { - labels.insert(card, desc.to_string()); - } - } - labels - } - - fn probe_alsa_supported_bits(device: &str, direction: Direction) -> Vec { - let Ok(pcm) = PCM::new(device, direction, false) else { - return Vec::new(); - }; - let Ok(hwp) = HwParams::any(&pcm) else { - return Vec::new(); - }; - if hwp.set_access(Access::RWInterleaved).is_err() { - return Vec::new(); - } - - fn supports(hwp: &HwParams<'_>, fmt: Format) -> bool { - hwp.test_format(fmt).is_ok() - } - - let candidates: Vec<(usize, Vec)> = vec![ - (32, vec![native_s32(), foreign_s32()]), - (24, vec![native_s24(), foreign_s24()]), - (16, vec![native_s16(), foreign_s16()]), - (8, vec![Format::S8]), - ]; - - let mut supported = Vec::new(); - for (bits, formats) in candidates { - if formats.iter().any(|f| supports(&hwp, *f)) { - supported.push(bits); - } - } - supported - } - - fn probe_alsa_supported_sample_rates(device: &str, direction: Direction) -> Vec { - let Ok(pcm) = PCM::new(device, direction, false) else { - return Vec::new(); - }; - let Ok(hwp) = HwParams::any(&pcm) else { - return Vec::new(); - }; - if hwp.set_access(Access::RWInterleaved).is_err() { - return Vec::new(); - } - - let mut supported = Vec::new(); - for rate in SAMPLE_RATE_CANDIDATES { - if hwp.test_rate(rate).is_ok() { - supported.push(rate as i32); - } - } - supported - } - - #[cfg(target_endian = "little")] - fn native_s16() -> Format { - Format::S16LE - } - #[cfg(target_endian = "big")] - fn native_s16() -> Format { - Format::S16BE - } - #[cfg(target_endian = "little")] - fn foreign_s16() -> Format { - Format::S16BE - } - #[cfg(target_endian = "big")] - fn foreign_s16() -> Format { - Format::S16LE - } - - #[cfg(target_endian = "little")] - fn native_s24() -> Format { - Format::S24LE - } - #[cfg(target_endian = "big")] - fn native_s24() -> Format { - Format::S24BE - } - #[cfg(target_endian = "little")] - fn foreign_s24() -> Format { - Format::S24BE - } - #[cfg(target_endian = "big")] - fn foreign_s24() -> Format { - Format::S24LE - } - - #[cfg(target_endian = "little")] - fn native_s32() -> Format { - Format::S32LE - } - #[cfg(target_endian = "big")] - fn native_s32() -> Format { - Format::S32BE - } - #[cfg(target_endian = "little")] - fn foreign_s32() -> Format { - Format::S32BE - } - #[cfg(target_endian = "big")] - fn foreign_s32() -> Format { - Format::S32LE - } - - fn discover_alsa_devices( - direction_marker: &str, - direction: Direction, - ) -> Vec { - let mut devices = Vec::new(); - let card_labels = read_alsa_card_labels(); - if let Ok(contents) = std::fs::read_to_string("/proc/asound/pcm") { - for line in contents.lines() { - let Some((card_dev, rest)) = line.split_once(':') else { - continue; - }; - if !rest.contains(direction_marker) { - continue; - } - let mut parts = card_dev.trim().split('-'); - let (Some(card), Some(dev)) = (parts.next(), parts.next()) else { - continue; - }; - let Ok(card) = card.parse::() else { - continue; - }; - let Ok(dev) = dev.parse::() else { - continue; - }; - let device_name = rest.split(':').next().unwrap_or("").trim(); - let card_label = card_labels - .get(&card) - .cloned() - .unwrap_or_else(|| format!("Card {card}")); - let base_label = if device_name.is_empty() { - card_label - } else { - format!("{card_label} - {device_name}") - }; - let id = format!("hw:{card},{dev}"); - let label = format!("{base_label} (hw:{card},{dev})"); - let supported_bits = probe_alsa_supported_bits(&id, direction); - let supported_sample_rates = { - let rates = probe_alsa_supported_sample_rates(&id, direction); - if rates.is_empty() { - super::fallback_sample_rates() - } else { - rates - } - }; - let (supports_input, supports_output) = match direction { - Direction::Playback => (false, true), - Direction::Capture => (true, false), - }; - devices.push(super::AudioDeviceOption::with_supported_direction_caps( - id, - label, - supported_bits, - supported_sample_rates, - supports_input, - supports_output, - )); - } - } - devices.sort_by_key(|a| a.label.to_lowercase()); - devices.dedup_by(|a, b| a.id == b.id); - devices - } - - pub(crate) fn discover_alsa_output_devices() -> Vec { - discover_alsa_devices("playback", Direction::Playback) - } - - pub(crate) fn discover_alsa_input_devices() -> Vec { - discover_alsa_devices("capture", Direction::Capture) - } -} - fn simple_audio_device(id: impl Into) -> AudioDeviceOption { let id = id.into(); AudioDeviceOption::with_supported_caps(