Fix ALSA device enumeration and UI translation logic
Problem 1: ALSA backend only showed CPAL hardware devices, missing virtual devices like 'default', 'plughw', USB DACs, etc. Solution: Use 'aplay -L' to enumerate ALL ALSA devices including: - Hardware devices (hw:X,Y) - Virtual devices (default, plughw, dmix) - USB audio devices - Network audio devices Problem 2: UI was applying getDevicePrettyName() to all backends, causing PipeWire's already-friendly names to be translated incorrectly. Solution: Only apply translator for ALSA/Auto backends. PipeWire names pass through unchanged since pactl already provides friendly descriptions. Changes: - ALSA backend: Use aplay -L instead of CPAL enumeration - Frontend: Conditional translation based on selected backend - Map deviceByDisplayName for correct reverse lookup - Load settings with correct translation logic This fixes both issues: ALSA now shows all devices including DACs, and PipeWire names are no longer mangled.
This commit is contained in:
1 parent
d530b13497
commit
f0c0e00e7d
2 files changed
+74
-45
No files matched your search
@@ -15,6 +15,7 @@ use rodio::{
|
|||||||
},
|
},
|
||||||
OutputStream, OutputStreamHandle,
|
OutputStream, OutputStreamHandle,
|
||||||
};
|
};
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
pub struct AlsaBackend {
|
pub struct AlsaBackend {
|
||||||
host: rodio::cpal::Host,
|
host: rodio::cpal::Host,
|
||||||
@@ -43,50 +44,67 @@ impl AlsaBackend {
|
|||||||
Ok(Self { host })
|
Ok(Self { host })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enumerate ALSA devices with hardware IDs
|
/// Enumerate ALSA devices using aplay -L for complete device list
|
||||||
fn enumerate_alsa_devices(&self) -> BackendResult<Vec<AudioDevice>> {
|
fn enumerate_alsa_devices(&self) -> BackendResult<Vec<AudioDevice>> {
|
||||||
let mut devices = Vec::new();
|
let mut devices = Vec::new();
|
||||||
|
|
||||||
// Get all output devices from ALSA host
|
// Use aplay -L to get all ALSA device names (including virtual devices)
|
||||||
let output_devices = self.host
|
let output = Command::new("aplay")
|
||||||
.output_devices()
|
.arg("-L")
|
||||||
.map_err(|e| format!("Failed to enumerate ALSA devices: {}", e))?;
|
.output()
|
||||||
|
.map_err(|e| format!("Failed to run aplay: {}", e))?;
|
||||||
|
|
||||||
for (idx, device) in output_devices.enumerate() {
|
if !output.status.success() {
|
||||||
let name = device.name().unwrap_or_else(|_| format!("ALSA Device {}", idx));
|
return Err("aplay -L command failed".to_string());
|
||||||
|
|
||||||
// Try to get device ID (hardware 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 device: {}", name)),
|
|
||||||
is_default,
|
|
||||||
max_sample_rate,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!("[ALSA Backend] Enumerated {} devices", devices.len());
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let mut current_device: Option<String> = None;
|
||||||
|
let mut current_description: Option<String> = None;
|
||||||
|
|
||||||
|
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()) {
|
||||||
|
// Skip null device
|
||||||
|
if id != "null" {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't forget last device
|
||||||
|
if let (Some(id), Some(desc)) = (current_device, current_description) {
|
||||||
|
if id != "null" {
|
||||||
|
let is_default = id == "default";
|
||||||
|
devices.push(AudioDevice {
|
||||||
|
id: id.clone(),
|
||||||
|
name: id,
|
||||||
|
description: Some(desc),
|
||||||
|
is_default,
|
||||||
|
max_sample_rate: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log::info!("[ALSA Backend] Enumerated {} devices via aplay -L", devices.len());
|
||||||
for (idx, dev) in devices.iter().enumerate() {
|
for (idx, dev) in devices.iter().enumerate() {
|
||||||
log::info!(" [{}] {} (max_rate: {:?})", idx, dev.name, dev.max_sample_rate);
|
log::info!(" [{}] {} - {}", idx, dev.name, dev.description.as_deref().unwrap_or(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(devices)
|
Ok(devices)
|
||||||
|
|||||||
@@ -216,14 +216,20 @@
|
|||||||
let backendOptions = $derived(['Auto', ...availableBackends.filter(b => b.is_available).map(b => b.name)]);
|
let backendOptions = $derived(['Auto', ...availableBackends.filter(b => b.is_available).map(b => b.name)]);
|
||||||
|
|
||||||
// Device options based on selected backend (derived)
|
// Device options based on selected backend (derived)
|
||||||
// Apply pretty names for ALSA devices (PipeWire names pass through unchanged)
|
// Apply pretty names for ALSA devices only (PipeWire already has friendly names)
|
||||||
let deviceOptions = $derived(['System Default', ...backendDevices.map(d => getDevicePrettyName(d.name))]);
|
let deviceOptions = $derived.by(() => {
|
||||||
|
const shouldTranslate = selectedBackend === 'ALSA Direct' || selectedBackend === 'Auto';
|
||||||
|
const names = backendDevices.map(d => shouldTranslate ? getDevicePrettyName(d.name) : d.name);
|
||||||
|
return ['System Default', ...names];
|
||||||
|
});
|
||||||
|
|
||||||
// Map pretty name -> device for lookup when user selects
|
// Map display name -> device for lookup when user selects
|
||||||
let deviceByPrettyName = $derived.by(() => {
|
let deviceByDisplayName = $derived.by(() => {
|
||||||
const map = new Map<string, AudioDevice>();
|
const map = new Map<string, AudioDevice>();
|
||||||
|
const shouldTranslate = selectedBackend === 'ALSA Direct' || selectedBackend === 'Auto';
|
||||||
for (const device of backendDevices) {
|
for (const device of backendDevices) {
|
||||||
map.set(getDevicePrettyName(device.name), device);
|
const displayName = shouldTranslate ? getDevicePrettyName(device.name) : device.name;
|
||||||
|
map.set(displayName, device);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
});
|
});
|
||||||
@@ -759,7 +765,12 @@
|
|||||||
// Set selected device from backend devices
|
// Set selected device from backend devices
|
||||||
if (settings.output_device) {
|
if (settings.output_device) {
|
||||||
const device = backendDevices.find(d => d.id === settings.output_device);
|
const device = backendDevices.find(d => d.id === settings.output_device);
|
||||||
outputDevice = device ? getDevicePrettyName(device.name) : 'System Default';
|
if (device) {
|
||||||
|
const shouldTranslate = settings.backend_type === 'Alsa' || !settings.backend_type;
|
||||||
|
outputDevice = shouldTranslate ? getDevicePrettyName(device.name) : device.name;
|
||||||
|
} else {
|
||||||
|
outputDevice = 'System Default';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -907,8 +918,8 @@
|
|||||||
async function handleBackendDeviceChange(deviceName: string) {
|
async function handleBackendDeviceChange(deviceName: string) {
|
||||||
outputDevice = deviceName;
|
outputDevice = deviceName;
|
||||||
|
|
||||||
// Get device ID from backendDevices using pretty name mapping
|
// Get device ID from backendDevices using display name mapping
|
||||||
const device = deviceByPrettyName.get(deviceName);
|
const device = deviceByDisplayName.get(deviceName);
|
||||||
const deviceId = deviceName === 'System Default' ? null : device?.id ?? null;
|
const deviceId = deviceName === 'System Default' ? null : device?.id ?? null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in new issue
Block a user