From 9242b959393708dadd69f4e345a743740992bb77 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 19 Jan 2026 12:13:14 -0600 Subject: [PATCH] Replace 1s polling with event-driven updates on sample rate change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed hardware status polling from continuous (every 1s) to event-driven (only when sample rate changes). Rationale: - Hardware sample rate ONLY changes when playing new song with different rate - Continuous polling was overkill - 3600 unnecessary calls per hour - If system is properly configured, status doesn't change mid-song - Polling was originally added for "instant feedback" but wastes resources Changes: - Removed setInterval polling (lines 162-171) - Added $effect that watches samplingRate prop changes - Only calls get_hardware_audio_status when: * samplingRate changes (new song or format change) * Using DAC Passthrough or ALSA Direct (bit-perfect modes) - Tracks previousSamplingRate to detect actual changes Benefits: - Zero backend calls during playback (unless rate changes) - Still validates bit-perfect when it matters (new sample rate) - Massive reduction in /proc/asound reads - Better battery life, lower CPU usage - Same UX - badge updates when song changes Testing plan: - Play songs with same sample rate → zero backend calls - Play songs with different rates → update on each rate change - Verify badge still shows correct status - If issues found, can revert to 1s polling --- src/lib/components/AudioOutputBadges.svelte | 32 ++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib/components/AudioOutputBadges.svelte b/src/lib/components/AudioOutputBadges.svelte index 133dae754..7b52b87a0 100644 --- a/src/lib/components/AudioOutputBadges.svelte +++ b/src/lib/components/AudioOutputBadges.svelte @@ -44,6 +44,7 @@ let pipewireSinks = $state([]); let hardwareStatus = $state(null); let isHovering = $state(false); + let previousSamplingRate = $state(undefined); // Derived state const currentDevice = $derived(outputStatus?.device_name ?? null); @@ -147,24 +148,23 @@ } } + // Update hardware status when sampling rate changes (new song with different rate) + $effect(() => { + if (samplingRate !== undefined && samplingRate !== previousSamplingRate) { + // Sampling rate changed - new song or format change + previousSamplingRate = samplingRate; + + // Update hardware status to verify bit-perfect playback + if (settings?.dac_passthrough || settings?.backend_type === 'Alsa') { + invoke('get_hardware_audio_status') + .then(status => hardwareStatus = status) + .catch(() => hardwareStatus = null); + } + } + }); + onMount(() => { loadStatus(); - - // Auto-refresh hardware status when DAC passthrough is active - // Fast polling (1s) for instant badge updates without needing hover - const pollInterval = setInterval(async () => { - // Only poll if DAC passthrough is enabled (otherwise wastes resources) - if (settings?.dac_passthrough) { - try { - hardwareStatus = await invoke('get_hardware_audio_status').catch(() => null); - } catch (err) { - // Silently fail - don't spam console - } - } - }, 1000); // 1 second - fast enough for instant feedback, light enough not to impact performance - - // Cleanup on unmount - return () => clearInterval(pollInterval); });