Add audio output badges and pretty device names

- Add AudioOutputBadges component showing DAC/Exclusive mode status
- Badges are reactive to actual player state, not just settings
- Add device tooltip on hover showing current output device
- Create audioDeviceNames helper for ALSA name to friendly name mapping
- Use pretty names in Settings dropdown while storing raw names
- Add get_audio_output_status backend command
- Store current device name in player SharedState
This commit is contained in:
vicrodh committed 2026-01-11 11:00:31 -06:00
1 parent 4b07618ed0
commit dd9d5558fa
7 files changed
+384 -6

No files matched your search

+201
View File
@@ -0,0 +1,201 @@
<script lang="ts">
import { invoke } from '@tauri-apps/api/core';
import { onMount } from 'svelte';
import { getDevicePrettyName, isExternalDevice } from '$lib/utils/audioDeviceNames';
interface AudioSettings {
output_device: string | null;
exclusive_mode: boolean;
dac_passthrough: boolean;
preferred_sample_rate: number | null;
}
interface AudioOutputStatus {
device_name: string | null;
is_playing: boolean;
}
// Props
interface Props {
showTooltips?: boolean;
}
let { showTooltips = true }: Props = $props();
// State
let settings = $state<AudioSettings | null>(null);
let outputStatus = $state<AudioOutputStatus | null>(null);
let isHovering = $state(false);
// Derived state
const currentDevice = $derived(outputStatus?.device_name ?? null);
const prettyDeviceName = $derived(currentDevice ? getDevicePrettyName(currentDevice) : 'No device');
const isExternal = $derived(currentDevice ? isExternalDevice(currentDevice) : false);
// Badge states - based on settings AND actual device capability
const dacPassthroughActive = $derived(
settings?.dac_passthrough === true && isExternal
);
const exclusiveModeActive = $derived(
settings?.exclusive_mode === true
);
// Whether to show badges at all (only if at least one setting is enabled or device is external)
const shouldShowBadges = $derived(
settings?.dac_passthrough || settings?.exclusive_mode || isExternal
);
async function loadStatus() {
try {
const [settingsResult, statusResult] = await Promise.all([
invoke<AudioSettings>('get_audio_settings'),
invoke<AudioOutputStatus>('get_audio_output_status')
]);
settings = settingsResult;
outputStatus = statusResult;
} catch (err) {
console.error('Failed to load audio status:', err);
}
}
onMount(() => {
loadStatus();
// Poll for updates while playing
const interval = setInterval(loadStatus, 5000);
return () => clearInterval(interval);
});
</script>
{#if shouldShowBadges}
<div
class="audio-badges"
onmouseenter={() => isHovering = true}
onmouseleave={() => isHovering = false}
>
<!-- DAC Badge -->
<div
class="badge"
class:active={dacPassthroughActive}
class:external={isExternal}
title={showTooltips ? (dacPassthroughActive ? 'DAC Passthrough active' : 'DAC Passthrough inactive') : undefined}
>
<span class="badge-label">DAC</span>
</div>
<!-- Exclusive Mode Badge -->
<div
class="badge"
class:active={exclusiveModeActive}
title={showTooltips ? (exclusiveModeActive ? 'Exclusive Mode active' : 'Exclusive Mode inactive') : undefined}
>
<span class="badge-label">EXC</span>
</div>
<!-- Device Tooltip on hover -->
{#if isHovering && showTooltips}
<div class="device-tooltip">
<div class="tooltip-label">Output Device</div>
<div class="tooltip-device">{prettyDeviceName}</div>
{#if currentDevice && currentDevice !== prettyDeviceName}
<div class="tooltip-raw">{currentDevice}</div>
{/if}
</div>
{/if}
</div>
{/if}
<style>
.audio-badges {
display: flex;
align-items: center;
gap: 4px;
position: relative;
margin-left: 8px;
}
.badge {
display: flex;
align-items: center;
justify-content: center;
padding: 2px 6px;
border-radius: 3px;
font-size: 8px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
background: rgba(255, 255, 255, 0.05);
color: rgba(255, 255, 255, 0.25);
border: 1px solid rgba(255, 255, 255, 0.08);
transition: all 200ms ease;
}
.badge.active {
background: rgba(34, 197, 94, 0.15);
color: #22c55e;
border-color: rgba(34, 197, 94, 0.3);
}
.badge.external:not(.active) {
background: rgba(99, 102, 241, 0.1);
color: rgba(99, 102, 241, 0.5);
border-color: rgba(99, 102, 241, 0.2);
}
.badge-label {
line-height: 1;
}
.device-tooltip {
position: absolute;
bottom: calc(100% + 8px);
right: 0;
background: rgba(24, 24, 28, 0.98);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 6px;
padding: 8px 12px;
min-width: 180px;
max-width: 300px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
z-index: 200;
animation: tooltip-appear 150ms ease;
}
@keyframes tooltip-appear {
from {
opacity: 0;
transform: translateY(4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.tooltip-label {
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.5px;
color: rgba(255, 255, 255, 0.4);
margin-bottom: 4px;
}
.tooltip-device {
font-size: 12px;
font-weight: 500;
color: white;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.tooltip-raw {
font-size: 9px;
color: rgba(255, 255, 255, 0.3);
font-family: var(--font-mono, monospace);
margin-top: 4px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
+5
View File
@@ -18,6 +18,7 @@
Maximize2
} from 'lucide-svelte';
import QualityBadge from './QualityBadge.svelte';
import AudioOutputBadges from './AudioOutputBadges.svelte';
interface Props {
artwork?: string;
@@ -277,6 +278,7 @@
<div class="quality-indicator">
<QualityBadge {quality} {bitDepth} {samplingRate} />
<AudioOutputBadges />
</div>
</div>
{:else}
@@ -633,6 +635,9 @@
.quality-indicator {
flex-shrink: 0;
display: flex;
align-items: center;
gap: 2px;
}
.empty-state {
+27 -6
View File
@@ -14,6 +14,7 @@
type DownloadCacheStats
} from '$lib/stores/downloadState';
import { clearCache as clearLyricsCache } from '$lib/stores/lyricsStore';
import { getDevicePrettyName } from '$lib/utils/audioDeviceNames';
interface Props {
onBack?: () => void;
@@ -51,7 +52,20 @@
// Audio device state
let audioDevices = $state<AudioDevice[]>([]);
let audioDeviceOptions = $derived(['System Default', ...audioDevices.map(d => d.name)]);
// Map of pretty name -> raw name
const deviceNameMap = $derived.by(() => {
const map = new Map<string, string>();
map.set('System Default', 'System Default');
for (const d of audioDevices) {
const pretty = getDevicePrettyName(d.name);
map.set(pretty, d.name);
}
return map;
});
// Options for dropdown (pretty names)
let audioDeviceOptions = $derived(['System Default', ...audioDevices.map(d => getDevicePrettyName(d.name))]);
// Theme mapping: display name -> data-theme value
const themeMap: Record<string, string> = {
@@ -295,7 +309,12 @@
async function loadAudioSettings() {
try {
const settings = await invoke<AudioSettings>('get_audio_settings');
outputDevice = settings.output_device ?? 'System Default';
// Convert raw name to pretty name for display
if (settings.output_device) {
outputDevice = getDevicePrettyName(settings.output_device);
} else {
outputDevice = 'System Default';
}
exclusiveMode = settings.exclusive_mode;
dacPassthrough = settings.dac_passthrough;
} catch (err) {
@@ -303,13 +322,15 @@
}
}
async function handleOutputDeviceChange(device: string) {
outputDevice = device;
async function handleOutputDeviceChange(prettyName: string) {
outputDevice = prettyName;
// Convert pretty name back to raw name for saving
const rawName = deviceNameMap.get(prettyName) ?? prettyName;
try {
await invoke('set_audio_output_device', {
device: device === 'System Default' ? null : device
device: rawName === 'System Default' ? null : rawName
});
console.log('Audio output device saved:', device);
console.log('Audio output device saved:', rawName, '(displayed as:', prettyName, ')');
} catch (err) {
console.error('Failed to save audio output device:', err);
}
+114
View File
@@ -0,0 +1,114 @@
/**
* Helper to convert ALSA device names to more user-friendly display names.
* This is a best-effort mapping - some names may not be recognized.
*/
// Common device name patterns and their friendly names
const DEVICE_PATTERNS: [RegExp, string | ((match: RegExpMatchArray) => string)][] = [
// PipeWire virtual devices
[/^default$/, 'System Default'],
[/^pipewire$/i, 'PipeWire'],
// USB Audio devices - extract product name
[/alsa_output\.usb-([^.]+)\.([^.]+)/i, (m) => cleanProductName(m[1], m[2])],
[/usb-([^-]+)-([^-]+)/i, (m) => cleanProductName(m[1], m[2])],
// PCI Audio (Intel HD Audio, etc)
[/alsa_output\.pci-.*\.hdmi-stereo/i, 'HDMI Audio Output'],
[/alsa_output\.pci-.*\.analog-stereo/i, 'Analog Audio Output'],
[/alsa_output\.pci-.*\.iec958-stereo/i, 'S/PDIF Digital Output'],
// HDMI patterns
[/hdmi[:\-_]?(\d+)/i, (m) => `HDMI Output ${parseInt(m[1]) + 1}`],
[/DisplayPort/i, 'DisplayPort Audio'],
// Common audio interfaces
[/Focusrite/i, (m) => 'Focusrite Scarlett'],
[/Steinberg/i, 'Steinberg Interface'],
[/MOTU/i, 'MOTU Interface'],
[/PreSonus/i, 'PreSonus Interface'],
// DACs
[/DAC|Topping|SMSL|FiiO|iFi|Schiit|Dragonfly|Modi|Magni/i, (m) => `USB DAC (${m[0]})`],
// Hardware device patterns (hw:X,Y)
[/^hw:(\d+),(\d+)$/, (m) => `Hardware Device ${m[1]}:${m[2]}`],
[/^plughw:(\d+),(\d+)$/, (m) => `Hardware Device ${m[1]}:${m[2]} (Plugin)`],
// Intel/AMD audio
[/Intel.*HDA|HDA Intel/i, 'Intel HD Audio'],
[/AMD.*Audio|Audio.*AMD/i, 'AMD Audio'],
[/Realtek/i, 'Realtek Audio'],
// Headphones/Headsets
[/headphone|headset/i, 'Headphones'],
[/speaker/i, 'Speakers'],
// Bluetooth (if visible through ALSA)
[/bluez|bluetooth/i, 'Bluetooth Audio'],
];
/**
* Clean up USB product names by removing underscores and fixing case
*/
function cleanProductName(vendor: string, product: string): string {
const clean = (s: string) => s
.replace(/_/g, ' ')
.replace(/([a-z])([A-Z])/g, '$1 $2')
.trim();
const v = clean(vendor);
const p = clean(product);
// If product contains vendor name, just use product
if (p.toLowerCase().includes(v.toLowerCase())) {
return p;
}
return `${v} ${p}`.trim();
}
/**
* Get a user-friendly name for an ALSA device name.
* Falls back to the original name if no pattern matches.
*/
export function getDevicePrettyName(alsaName: string): string {
if (!alsaName) return 'Unknown Device';
for (const [pattern, replacement] of DEVICE_PATTERNS) {
const match = alsaName.match(pattern);
if (match) {
if (typeof replacement === 'function') {
return replacement(match);
}
return replacement;
}
}
// Fallback: clean up the raw name a bit
return alsaName
.replace(/alsa_output\./g, '')
.replace(/\./g, ' ')
.replace(/_/g, ' ')
.replace(/-/g, ' ')
.replace(/\s+/g, ' ')
.trim() || alsaName;
}
/**
* Check if a device name appears to be a DAC or external audio interface
*/
export function isExternalDevice(deviceName: string): boolean {
if (!deviceName) return false;
const lower = deviceName.toLowerCase();
return (
lower.includes('usb') ||
lower.includes('dac') ||
lower.includes('interface') ||
lower.includes('focusrite') ||
lower.includes('steinberg') ||
lower.includes('motu') ||
lower.includes('presonus') ||
/topping|smsl|fiio|ifi|schiit|dragonfly/i.test(lower)
);
}