Revert to CPAL enumeration + aplay -L for descriptions only

Critical Fix - Use CPAL Device Names:
- CPAL enumerates devices → these IDs actually work for playback
- aplay -L provides friendly descriptions → update CPAL device descriptions
- Stop creating hw:X,Y devices that CPAL doesn't recognize
- Fixes: "Device not found → fallback to default → audio from laptop"

Flow:
1. enumerate_via_cpal() returns devices CPAL can actually use
2. enumerate_via_aplay() calls #1, then parses aplay -L for descriptions
3. Updates device.description with friendly names from aplay -L
4. Frontend shows: "Cambridge Audio USB Audio 2.0" not "front:CARD=C20,DEV=0"

Loading Overlay:
- Full-screen semi-transparent overlay during device enumeration
- Animated spinner (Loader2 icon)
- "Loading audio devices... / Parsing hardware information"
- Prevents user thinking app is frozen during 4-5 second aplay parse

For Bit-Perfect Playback:
- User should select devices like front:CARD=C20,DEV=0 (CPAL knows these)
- These are more direct than sysdefault:CARD= (which has dmix/plugins)
- Not as direct as hw:4,0 but CPAL can actually find them

PipeWire and PulseAudio backends unchanged - still working as before.
This commit is contained in:
vicrodh committed 2026-01-17 21:12:18 -06:00
1 parent 63b6809259
commit 845d53c672
2 files changed
+78 -142

No files matched your search

+20 -141
View File
@@ -44,23 +44,31 @@ impl AlsaBackend {
Ok(Self { host }) Ok(Self { host })
} }
/// Enumerate ALSA devices via aplay -L (preferred - has real descriptions) /// Enumerate ALSA devices via CPAL with descriptions from aplay -L
fn enumerate_via_aplay(&self) -> BackendResult<Vec<AudioDevice>> { fn enumerate_via_aplay(&self) -> BackendResult<Vec<AudioDevice>> {
let mut devices = Vec::new(); // First: Get devices from CPAL (these are the device IDs that actually work)
let cpal_devices_result = self.enumerate_via_cpal();
if cpal_devices_result.is_err() {
return cpal_devices_result;
}
let mut devices = cpal_devices_result.unwrap();
// Run aplay -L to get device list with descriptions // Second: Build description map from aplay -L
let output = Command::new("aplay") let output = Command::new("aplay")
.arg("-L") .arg("-L")
.output() .output()
.map_err(|e| format!("Failed to run aplay -L: {}", e))?; .map_err(|e| format!("Failed to run aplay -L: {}", e))?;
if !output.status.success() { if !output.status.success() {
return Err("aplay -L command failed".to_string()); log::warn!("[ALSA Backend] aplay -L failed, using CPAL names only");
return Ok(devices);
} }
let stdout = String::from_utf8_lossy(&output.stdout); let stdout = String::from_utf8_lossy(&output.stdout);
let lines: Vec<&str> = stdout.lines().collect(); let lines: Vec<&str> = stdout.lines().collect();
// Build map: device_id -> friendly description
let mut description_map = std::collections::HashMap::new();
let mut i = 0; let mut i = 0;
while i < lines.len() { while i < lines.len() {
let line = lines[i].trim_end(); let line = lines[i].trim_end();
@@ -69,37 +77,6 @@ impl AlsaBackend {
if !line.starts_with(' ') && !line.is_empty() { if !line.starts_with(' ') && !line.is_empty() {
let device_id = line.to_string(); let device_id = line.to_string();
// Skip blacklisted devices
// Only keep useful devices for audiophile playback:
// - default, sysdefault:CARD= (system defaults)
// - front:CARD= (stereo/front output - most common for DACs)
// - hdmi:CARD= (HDMI outputs)
// - iec958:CARD= (S/PDIF digital)
// Skip surround variants, usbstream, plugins, etc.
let is_blacklisted = device_id == "null"
|| device_id.starts_with("lavrate")
|| device_id.starts_with("samplerate")
|| device_id.starts_with("speexrate")
|| device_id == "jack"
|| device_id == "oss"
|| device_id == "speex"
|| device_id == "upmix"
|| device_id == "vdownmix"
|| device_id.starts_with("surround") // Skip surround21, surround40, surround51, etc.
|| device_id.starts_with("usbstream:") // Skip USB stream devices
|| device_id == "pipewire" // Skip - use PipeWire backend instead
|| device_id == "pulse" // Skip - use PulseAudio backend instead
|| device_id == "sysdefault"; // Skip bare sysdefault (keep sysdefault:CARD= only)
if is_blacklisted {
// Skip this device and its description lines
i += 1;
while i < lines.len() && lines[i].starts_with(' ') {
i += 1;
}
continue;
}
// Get description from next line (if it exists and is indented) // Get description from next line (if it exists and is indented)
let description = if i + 1 < lines.len() && lines[i + 1].starts_with(' ') { let description = if i + 1 < lines.len() && lines[i + 1].starts_with(' ') {
Some(lines[i + 1].trim().to_string()) Some(lines[i + 1].trim().to_string())
@@ -107,34 +84,9 @@ impl AlsaBackend {
None None
}; };
// Check if this is the default device if let Some(desc) = description {
let is_default = device_id == "default" || device_id.starts_with("default:"); description_map.insert(device_id, desc);
}
// Try to get max sample rate by testing with CPAL
let max_sample_rate = self.host
.output_devices()
.ok()
.and_then(|mut devs| {
devs.find(|d| d.name().ok().as_deref() == Some(&device_id))
})
.and_then(|device| {
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: device_id.clone(),
name: device_id.clone(),
description,
is_default,
max_sample_rate,
});
// Skip description lines // Skip description lines
i += 1; i += 1;
@@ -146,87 +98,14 @@ impl AlsaBackend {
} }
} }
// Add hw:X,Y devices for bit-perfect playback // Third: Update device descriptions from aplay -L map
// Parse aplay -l to get card numbers (CPAL requires hw:0,0 format, not hw:CARD=name) for device in &mut devices {
let aplay_l_output = Command::new("aplay") if let Some(desc) = description_map.get(&device.name) {
.arg("-l") device.description = Some(desc.clone());
.output()
.ok()
.and_then(|o| if o.status.success() { Some(o) } else { None });
if let Some(output) = aplay_l_output {
let stdout = String::from_utf8_lossy(&output.stdout);
// Parse card numbers and names from "card N: NAME [DESCRIPTION]" lines
// Example: card 4: C20 [Cambridge Audio USB Audio 2.0], device 0: USB Audio [USB Audio]
for line in stdout.lines() {
if let Some(card_info) = line.strip_prefix("card ") {
// Extract card number and name
let parts: Vec<&str> = card_info.splitn(2, ':').collect();
if parts.len() == 2 {
let card_num_str = parts[0].trim();
let rest = parts[1].trim();
// Extract card name (before space or bracket)
let card_name = rest.split_whitespace().next().unwrap_or("");
// Extract device number if present
if let Some(device_info) = line.find(", device ") {
if let Some(dev_start) = line[device_info..].find("device ") {
let dev_part = &line[device_info + dev_start + 7..];
if let Some(dev_num_str) = dev_part.split(':').next() {
// Create hw:X,Y device
let hw_device_id = format!("hw:{},{}", card_num_str, dev_num_str.trim());
// Skip if we already added this device
if devices.iter().any(|d| d.name == hw_device_id) {
continue;
}
// Get max sample rate from CPAL
let max_sample_rate = self.host
.output_devices()
.ok()
.and_then(|mut devs| {
devs.find(|d| d.name().ok().as_deref() == Some(&hw_device_id))
})
.and_then(|device| {
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)
})
});
// Find the friendly description from aplay -L for this card
let base_description = devices.iter()
.find(|d| d.name.contains(&format!("CARD={}", card_name)))
.and_then(|d| d.description.as_ref())
.map(|desc| {
desc.split(',').next().unwrap_or(desc).trim().to_string()
})
.unwrap_or_else(|| card_name.to_string());
devices.push(AudioDevice {
id: hw_device_id.clone(),
name: hw_device_id.clone(),
description: Some(format!("{} (Direct Hardware - Bit-perfect)", base_description)),
is_default: false,
max_sample_rate,
});
}
}
}
}
}
} }
} }
let hw_device_count = devices.iter().filter(|d| d.name.starts_with("hw:")).count(); log::info!("[ALSA Backend] Enumerated {} ALSA devices", devices.len());
log::info!("[ALSA Backend] Enumerated {} devices via aplay -L (+ {} hw: devices)", devices.len() - hw_device_count, hw_device_count);
for (idx, dev) in devices.iter().enumerate() { for (idx, dev) in devices.iter().enumerate() {
log::info!( log::info!(
" [{}] {} - {} (max_rate: {:?})", " [{}] {} - {} (max_rate: {:?})",
+58 -1
View File
@@ -2,7 +2,7 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { invoke } from '@tauri-apps/api/core'; import { invoke } from '@tauri-apps/api/core';
import { getCurrentWebview } from '@tauri-apps/api/webview'; import { getCurrentWebview } from '@tauri-apps/api/webview';
import { ArrowLeft, FolderOpen, ChevronDown, ChevronRight } from 'lucide-svelte'; import { ArrowLeft, FolderOpen, ChevronDown, ChevronRight, Loader2 } from 'lucide-svelte';
import Toggle from '../Toggle.svelte'; import Toggle from '../Toggle.svelte';
import Dropdown from '../Dropdown.svelte'; import Dropdown from '../Dropdown.svelte';
import VolumeSlider from '../VolumeSlider.svelte'; import VolumeSlider from '../VolumeSlider.svelte';
@@ -1234,6 +1234,17 @@
</script> </script>
<div class="settings-view" bind:this={settingsViewEl}> <div class="settings-view" bind:this={settingsViewEl}>
<!-- Loading Overlay for Device Enumeration -->
{#if isLoadingDevices}
<div class="loading-overlay">
<div class="loading-content">
<Loader2 size={48} class="spinner" />
<p>Loading audio devices...</p>
<p class="loading-subtitle">Parsing hardware information</p>
</div>
</div>
{/if}
<!-- Header --> <!-- Header -->
<div class="header"> <div class="header">
{#if onBack} {#if onBack}
@@ -1757,6 +1768,52 @@
font-style: italic; font-style: italic;
} }
.loading-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.7);
backdrop-filter: blur(4px);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
}
.loading-content {
text-align: center;
color: white;
}
.loading-content :global(.spinner) {
animation: spin 1s linear infinite;
margin: 0 auto 16px auto;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.loading-content p {
margin: 0;
font-size: 18px;
font-weight: 500;
}
.loading-subtitle {
margin-top: 8px !important;
font-size: 14px !important;
opacity: 0.7;
font-weight: 400 !important;
}
.header { .header {
padding-top: 24px; padding-top: 24px;
margin-bottom: 32px; margin-bottom: 32px;