From 4486d1dd80edb54f0cb8517f3cff3c4ca059a5f6 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 17 Jan 2026 18:24:18 -0600 Subject: [PATCH] Fix ALSA device enumeration and Auto backend translation Problem 1: ALSA Direct showed NO devices because aplay -L output doesn't match CPAL device names, causing create_output_stream to fail. Solution: Revert to CPAL enumeration but with blacklist filtering to skip non-playback devices (null, lavrate, samplerate, speexrate, jack, oss, speex, upmix, vdownmix). Problem 2: Auto backend didn't apply fancy names because needsTranslation() was too restrictive and didn't cover all ALSA device name patterns. Solution: Simplify to: if device name starts with 'alsa_output.' it's from PipeWire (already has friendly name), otherwise it's ALSA (needs translation). This covers ALL ALSA naming patterns including front:CARD=, sysdefault:CARD=, hw:, hdmi:, iec958:, etc. Changes: - alsa_backend.rs: Back to CPAL with blacklist filter - SettingsView.svelte: Simplified needsTranslation() logic --- src-tauri/src/audio/alsa_backend.rs | 112 ++++++++----------- src/lib/components/views/SettingsView.svelte | 16 ++- 2 files changed, 54 insertions(+), 74 deletions(-) diff --git a/src-tauri/src/audio/alsa_backend.rs b/src-tauri/src/audio/alsa_backend.rs index 55f44019c..0167e02df 100644 --- a/src-tauri/src/audio/alsa_backend.rs +++ b/src-tauri/src/audio/alsa_backend.rs @@ -44,82 +44,64 @@ impl AlsaBackend { Ok(Self { host }) } - /// Enumerate ALSA devices using aplay -L, filtering only playback devices + /// Enumerate ALSA devices via CPAL fn enumerate_alsa_devices(&self) -> BackendResult> { let mut devices = Vec::new(); - // Use aplay -L to get all ALSA device names - let output = Command::new("aplay") - .arg("-L") - .output() - .map_err(|e| format!("Failed to run aplay: {}", e))?; + // Get all output devices from ALSA host + let output_devices = self.host + .output_devices() + .map_err(|e| format!("Failed to enumerate ALSA devices: {}", e))?; - if !output.status.success() { - return Err("aplay -L command failed".to_string()); - } + for (idx, device) in output_devices.enumerate() { + let name = device.name().unwrap_or_else(|_| format!("ALSA Device {}", idx)); - let stdout = String::from_utf8_lossy(&output.stdout); - let mut current_device: Option = None; - let mut current_description: Option = None; - - // Helper to check if device is valid for playback - let is_valid_device = |id: &str| -> bool { - // Only include actual hardware devices and useful virtual devices - id == "default" - || id == "pipewire" - || id == "pulse" - || id.starts_with("hw:") - || id.starts_with("plughw:") - || id.starts_with("sysdefault:CARD=") - || id.starts_with("default:CARD=") - || id.starts_with("front:CARD=") - || id.starts_with("surround") - || id.starts_with("hdmi:") - || id.starts_with("iec958:") - }; - - for line in stdout.lines() { - if line.is_empty() { - // Empty line marks end of device entry - if let (Some(id), Some(desc)) = (current_device.take(), current_description.take()) { - if is_valid_device(&id) { - let is_default = id == "default"; - devices.push(AudioDevice { - id: id.clone(), - name: id, - description: Some(desc), - is_default, - max_sample_rate: None, // ALSA devices support variable sample rates - }); - } - } - current_description = None; - } else if !line.starts_with(' ') && !line.starts_with('\t') { - // Device name (not indented) - current_device = Some(line.trim().to_string()); - } else { - // Description (indented) - current_description = Some(line.trim().to_string()); + // Skip non-useful devices + if name == "null" + || name.starts_with("lavrate") + || name.starts_with("samplerate") + || name.starts_with("speexrate") + || name == "jack" + || name == "oss" + || name == "speex" + || name == "upmix" + || name == "vdownmix" + { + continue; } - } - // Don't forget last device - if let (Some(id), Some(desc)) = (current_device, current_description) { - if is_valid_device(&id) { - let is_default = id == "default"; - devices.push(AudioDevice { - id: id.clone(), - name: id, - description: Some(desc), - is_default, - max_sample_rate: None, + // ID is the device name + let id = name.clone(); + + // Check if this is the default device + let is_default = self.host + .default_output_device() + .and_then(|d| d.name().ok()) + .map(|default_name| default_name == name) + .unwrap_or(false); + + // Try to get max sample rate from supported configs + let max_sample_rate = device + .supported_output_configs() + .ok() + .and_then(|mut configs| { + configs + .max_by_key(|c| c.max_sample_rate().0) + .map(|c| c.max_sample_rate().0) }); - } + + devices.push(AudioDevice { + id: id.clone(), + name: name.clone(), + description: Some(format!("ALSA: {}", name)), + is_default, + max_sample_rate, + }); } - log::info!("[ALSA Backend] Enumerated {} devices via aplay -L (filtered)", devices.len()); + log::info!("[ALSA Backend] Enumerated {} devices via CPAL", devices.len()); for (idx, dev) in devices.iter().enumerate() { - log::info!(" [{}] {} - {}", idx, dev.name, dev.description.as_deref().unwrap_or("")); + log::info!(" [{}] {} (max_rate: {:?})", idx, dev.name, dev.max_sample_rate); } Ok(devices) diff --git a/src/lib/components/views/SettingsView.svelte b/src/lib/components/views/SettingsView.svelte index 23e5a4bad..e0bc7ea1a 100644 --- a/src/lib/components/views/SettingsView.svelte +++ b/src/lib/components/views/SettingsView.svelte @@ -217,15 +217,13 @@ // Helper to check if a device name looks like raw ALSA (needs translation) function needsTranslation(name: string): boolean { - return name.startsWith('hw:') - || name.startsWith('plughw:') - || name.startsWith('sysdefault:CARD=') - || name.startsWith('default:CARD=') - || name.startsWith('front:CARD=') - || name.startsWith('surround') - || name.startsWith('hdmi:') - || name.startsWith('iec958:') - || name === 'default'; + // PipeWire device names start with "alsa_output." - those already have friendly names + if (name.startsWith('alsa_output.')) { + return false; + } + + // Everything else from ALSA needs translation + return true; } // Device options based on selected backend (derived)