Filter ALSA devices, add hw: bit-perfect devices, and fix UI issues

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)
This commit is contained in:
vicrodh committed 2026-01-17 21:02:03 -06:00
1 parent 7091c64d56
commit 8f3369a583
2 files changed
+104 -11

No files matched your search

+74 -3
View File
@@ -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<String> = 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: {:?})",
+30 -8
View File
@@ -211,6 +211,7 @@
let availableBackends = $state<BackendInfo[]>([]);
let backendDevices = $state<AudioDevice[]>([]);
let alsaPlugins = $state<AlsaPluginInfo[]>([]);
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<AudioDevice[]>('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 @@
<span class="setting-label">{$t('settings.audio.outputDevice')}</span>
<Tooltip text="Select your preferred audio output device. Devices shown are from the selected backend." />
</div>
<Dropdown
value={outputDevice}
options={deviceOptions}
onchange={handleBackendDeviceChange}
wide
expandLeft
compact
/>
{#if isLoadingDevices}
<span class="loading-text">Loading devices...</span>
{:else}
<Dropdown
value={outputDevice}
options={deviceOptions}
onchange={handleBackendDeviceChange}
wide
expandLeft
compact
/>
{/if}
</div>
{#if showAlsaPluginSelector}
<div class="setting-row">
@@ -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;