From 8f3369a5831bec0dc08055094daca43d3e7f1ddf Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 17 Jan 2026 21:02:03 -0600 Subject: [PATCH] Filter ALSA devices, add hw: bit-perfect devices, and fix UI issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ALSA Device Filtering (45 → ~12 devices): - Aggressively filter to show only useful devices for audiophiles - Keep: default, sysdefault:CARD=, front:CARD=, hdmi:CARD=, iec958:CARD= - Blacklist: surround* variants, usbstream:, pipewire, pulse, bare sysdefault - Reduces 45 listed devices to ~12 actually useful ones Add hw:CARD=XXX,DEV=0 Devices for Bit-Perfect Playback: - Auto-generate hw: devices for each detected card - hw: = direct hardware access without ALSA plugins (true bit-perfect) - sysdefault:CARD= has dmix/resampling plugins (NOT bit-perfect) - Description shows "(Direct Hardware - Bit-perfect)" to distinguish - This fixes user's issue: DAC was stuck at 44.1kHz with sysdefault:CARD=C20 Loading Indicator: - Show "Loading devices..." while parsing aplay -L output - Fixes 4-5 second frozen UI on ALSA Direct backend selection - isLoadingDevices state added to SettingsView Stop Playback on Device Change: - Call stop_playback() in handleBackendDeviceChange - Prevents stuck/dead audio streams when changing output device - Fixes: "After any config change, song in player stops working" User can now: 1. Select hw:CARD=C20,DEV=0 for true bit-perfect to DAC 2. See loading indicator instead of frozen UI 3. Change device without breaking playback 4. Only see relevant devices (not 7 surround variants per card) --- src-tauri/src/audio/alsa_backend.rs | 77 +++++++++++++++++++- src/lib/components/views/SettingsView.svelte | 38 ++++++++-- 2 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/audio/alsa_backend.rs b/src-tauri/src/audio/alsa_backend.rs index e1fbd01d1..b443a1f28 100644 --- a/src-tauri/src/audio/alsa_backend.rs +++ b/src-tauri/src/audio/alsa_backend.rs @@ -70,7 +70,13 @@ impl AlsaBackend { let device_id = line.to_string(); // Skip blacklisted devices - if device_id == "null" + // 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") @@ -79,7 +85,13 @@ impl AlsaBackend { || 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(' ') { @@ -134,7 +146,66 @@ impl AlsaBackend { } } - log::info!("[ALSA Backend] Enumerated {} devices via aplay -L", devices.len()); + // Add hw: devices for bit-perfect playback (not listed by aplay -L) + // Extract unique CARD names from devices we found + let mut cards: Vec = Vec::new(); + for device in &devices { + if let Some(card_match) = device.name.strip_prefix("front:CARD=") { + if let Some(card_name) = card_match.split(',').next() { + if !cards.contains(&card_name.to_string()) { + cards.push(card_name.to_string()); + } + } + } else if let Some(card_match) = device.name.strip_prefix("sysdefault:CARD=") { + if !cards.contains(&card_match.to_string()) { + cards.push(card_match.to_string()); + } + } + } + + // For each card, add hw:CARD=XXX,DEV=0 device (bit-perfect, no plugins) + for card in &cards { + let hw_device_id = format!("hw:CARD={},DEV=0", card); + + // 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 front: or sysdefault: device + let base_description = devices.iter() + .find(|d| d.name.starts_with(&format!("front:CARD={}", card)) || + d.name == format!("sysdefault:CARD={}", card)) + .and_then(|d| d.description.as_ref()) + .map(|desc| { + // Extract just the card name (first line before comma) + desc.split(',').next().unwrap_or(desc).trim().to_string() + }) + .unwrap_or_else(|| card.clone()); + + 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, + }); + } + + log::info!("[ALSA Backend] Enumerated {} devices via aplay -L (+ {} hw: devices)", devices.len() - cards.len(), cards.len()); for (idx, dev) in devices.iter().enumerate() { log::info!( " [{}] {} - {} (max_rate: {:?})", diff --git a/src/lib/components/views/SettingsView.svelte b/src/lib/components/views/SettingsView.svelte index 4a5a9d2f7..636a616f0 100644 --- a/src/lib/components/views/SettingsView.svelte +++ b/src/lib/components/views/SettingsView.svelte @@ -211,6 +211,7 @@ let availableBackends = $state([]); let backendDevices = $state([]); let alsaPlugins = $state([]); + let isLoadingDevices = $state(false); // Backend selector options (derived) let backendOptions = $derived(['Auto', ...availableBackends.filter(b => b.is_available).map(b => b.name)]); @@ -755,6 +756,7 @@ } async function loadBackendDevices(backendType: 'PipeWire' | 'Alsa' | 'Pulse') { + isLoadingDevices = true; try { const devices = await invoke('get_devices_for_backend', { backendType }); backendDevices = devices; @@ -762,6 +764,8 @@ } catch (err) { console.error(`Failed to load devices for ${backendType}:`, err); backendDevices = []; + } finally { + isLoadingDevices = false; } } @@ -980,6 +984,14 @@ await invoke('set_audio_output_device', { device: deviceId }); await invoke('reinit_audio_device', { device: deviceId }); console.log('[Audio] Backend device changed:', deviceName, '(id:', deviceId ?? 'default', ')'); + + // Stop current playback to prevent stuck/dead streams + try { + await invoke('stop_playback'); + console.log('[Audio] Stopped playback after device change'); + } catch (err) { + console.log('[Audio] No playback to stop'); + } } catch (err) { console.error('[Audio] Failed to change backend device:', err); } @@ -1296,14 +1308,18 @@ {$t('settings.audio.outputDevice')} - + {#if isLoadingDevices} + Loading devices... + {:else} + + {/if} {#if showAlsaPluginSelector}
@@ -1735,6 +1751,12 @@ background: rgba(255, 255, 255, 0.4); } + .loading-text { + color: rgba(255, 255, 255, 0.6); + font-size: 14px; + font-style: italic; + } + .header { padding-top: 24px; margin-bottom: 32px;