diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index c1cfc4971..3e625ff76 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -13,7 +13,8 @@ use qbz_models::{Album, Artist, Playlist, Quality, QueueState, RepeatMode, Searc use crate::artist_blacklist::BlacklistState; use crate::cache::AudioCache; -use crate::config::audio_settings::AudioSettingsState; +use crate::audio::{AlsaPlugin, AudioBackendType}; +use crate::config::audio_settings::{AudioSettings, AudioSettingsState}; use crate::core_bridge::CoreBridgeState; use crate::offline_cache::OfflineCacheState; use crate::queue::QueueManager; @@ -1346,3 +1347,265 @@ pub async fn v2_remove_tracks_from_playlist( Err("Either playlistTrackIds or trackIds must be provided".to_string()) } + +// ==================== Audio Settings Commands (V2) ==================== + +/// Get current audio settings (V2) +#[tauri::command] +pub fn v2_get_audio_settings( + state: State<'_, AudioSettingsState>, +) -> Result { + log::info!("[V2] get_audio_settings"); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.get_settings() +} + +/// Set audio output device (V2) +#[tauri::command] +pub fn v2_set_audio_output_device( + device: Option, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + let normalized_device = device + .as_ref() + .map(|d| crate::audio::normalize_device_id_to_stable(d)); + log::info!( + "[V2] set_audio_output_device {:?} -> {:?} (normalized)", + device, normalized_device + ); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_output_device(normalized_device.as_deref()) +} + +/// Set audio exclusive mode (V2) +#[tauri::command] +pub fn v2_set_audio_exclusive_mode( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_exclusive_mode: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_exclusive_mode(enabled) +} + +/// Set DAC passthrough mode (V2) +#[tauri::command] +pub fn v2_set_audio_dac_passthrough( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_dac_passthrough: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_dac_passthrough(enabled) +} + +/// Set preferred sample rate (V2) +#[tauri::command] +pub fn v2_set_audio_sample_rate( + rate: Option, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_sample_rate: {:?}", rate); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_sample_rate(rate) +} + +/// Set audio backend type (V2) +#[tauri::command] +#[allow(non_snake_case)] +pub fn v2_set_audio_backend_type( + backendType: Option, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_backend_type: {:?}", backendType); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_backend_type(backendType) +} + +/// Set ALSA plugin (V2) +#[tauri::command] +pub fn v2_set_audio_alsa_plugin( + plugin: Option, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_alsa_plugin: {:?}", plugin); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_alsa_plugin(plugin) +} + +/// Set gapless playback enabled (V2) +#[tauri::command] +pub fn v2_set_audio_gapless_enabled( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_gapless_enabled: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_gapless_enabled(enabled) +} + +/// Set normalization enabled (V2) +#[tauri::command] +pub fn v2_set_audio_normalization_enabled( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_normalization_enabled: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_normalization_enabled(enabled) +} + +/// Set normalization target LUFS (V2) +#[tauri::command] +pub fn v2_set_audio_normalization_target( + target: f32, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_normalization_target: {}", target); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_normalization_target_lufs(target) +} + +/// Set device max sample rate (V2) +#[tauri::command] +pub fn v2_set_audio_device_max_sample_rate( + rate: Option, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_device_max_sample_rate: {:?}", rate); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_device_max_sample_rate(rate) +} + +/// Set limit quality to device capability (V2) +#[tauri::command] +pub fn v2_set_audio_limit_quality_to_device( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_limit_quality_to_device: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_limit_quality_to_device(enabled) +} + +/// Set streaming only mode (V2) +#[tauri::command] +pub fn v2_set_audio_streaming_only( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_streaming_only: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_streaming_only(enabled) +} + +/// Reset audio settings to defaults (V2) +#[tauri::command] +pub fn v2_reset_audio_settings( + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] reset_audio_settings"); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.reset_all().map(|_| ()) +} + +/// Set stream first track enabled (V2) +#[tauri::command] +pub fn v2_set_audio_stream_first_track( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_stream_first_track: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_stream_first_track(enabled) +} + +/// Set stream buffer seconds (V2) +#[tauri::command] +pub fn v2_set_audio_stream_buffer_seconds( + seconds: u8, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_stream_buffer_seconds: {}", seconds); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_stream_buffer_seconds(seconds) +} + +/// Set ALSA hardware volume control (V2) +#[tauri::command] +pub fn v2_set_audio_alsa_hardware_volume( + enabled: bool, + state: State<'_, AudioSettingsState>, +) -> Result<(), String> { + log::info!("[V2] set_audio_alsa_hardware_volume: {}", enabled); + let guard = state + .store + .lock() + .map_err(|e| format!("Lock error: {}", e))?; + let store = guard.as_ref().ok_or("No active session - please log in")?; + store.set_alsa_hardware_volume(enabled) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index dc9842088..e0cf817b9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1150,6 +1150,24 @@ pub fn run() { commands_v2::v2_prefetch_track, // Audio device commands_v2::v2_reinit_audio_device, + // Audio settings + commands_v2::v2_get_audio_settings, + commands_v2::v2_set_audio_output_device, + commands_v2::v2_set_audio_exclusive_mode, + commands_v2::v2_set_audio_dac_passthrough, + commands_v2::v2_set_audio_sample_rate, + commands_v2::v2_set_audio_backend_type, + commands_v2::v2_set_audio_alsa_plugin, + commands_v2::v2_set_audio_gapless_enabled, + commands_v2::v2_set_audio_normalization_enabled, + commands_v2::v2_set_audio_normalization_target, + commands_v2::v2_set_audio_device_max_sample_rate, + commands_v2::v2_set_audio_limit_quality_to_device, + commands_v2::v2_set_audio_streaming_only, + commands_v2::v2_reset_audio_settings, + commands_v2::v2_set_audio_stream_first_track, + commands_v2::v2_set_audio_stream_buffer_seconds, + commands_v2::v2_set_audio_alsa_hardware_volume, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src/lib/components/views/SettingsView.svelte b/src/lib/components/views/SettingsView.svelte index e311b3bb3..201d6b3a8 100644 --- a/src/lib/components/views/SettingsView.svelte +++ b/src/lib/components/views/SettingsView.svelte @@ -2119,7 +2119,7 @@ console.warn(`[Audio] Saved device '${settings.output_device}' not found in current enumeration. Resetting to System Default.`); outputDevice = 'System Default'; try { - await invoke('set_audio_output_device', { device: null }); + await invoke('v2_set_audio_output_device', { device: null }); console.log('[Audio] Cleared stale device from database'); } catch (err) { console.error('[Audio] Failed to clear stale device:', err); @@ -2172,9 +2172,9 @@ try { // Save the preference - await invoke('set_audio_output_device', { device: deviceToStore }); + await invoke('v2_set_audio_output_device', { device: deviceToStore }); // Store device's max sample rate for quality limiting - await invoke('set_audio_device_max_sample_rate', { rate: maxSampleRate }); + await invoke('v2_set_audio_device_max_sample_rate', { rate: maxSampleRate }); // Reinitialize audio with the selected device // CRITICAL: Pass the actual CPAL device name, not null @@ -2190,7 +2190,7 @@ async function handleExclusiveModeChange(enabled: boolean) { exclusiveMode = enabled; try { - await invoke('set_audio_exclusive_mode', { enabled }); + await invoke('v2_set_audio_exclusive_mode', { enabled }); // Reinitialize audio with currently selected device const deviceName = getCurrentDeviceSinkName(); @@ -2207,12 +2207,12 @@ // Gapless not compatible with DAC Passthrough if (enabled && gaplessPlayback) { gaplessPlayback = false; - await invoke('set_audio_gapless_enabled', { enabled: false }); + await invoke('v2_set_audio_gapless_enabled', { enabled: false }); console.log('[Audio] Disabled gapless playback (not compatible with DAC Passthrough)'); } try { - await invoke('set_audio_dac_passthrough', { enabled }); + await invoke('v2_set_audio_dac_passthrough', { enabled }); // Reinitialize audio with currently selected device const deviceName = getCurrentDeviceSinkName(); @@ -2235,7 +2235,7 @@ if (backendName !== 'PipeWire') { if (dacPassthrough) { dacPassthrough = false; - await invoke('set_audio_dac_passthrough', { enabled: false }); + await invoke('v2_set_audio_dac_passthrough', { enabled: false }); console.log('[Audio] Disabled DAC Passthrough (only compatible with PipeWire)'); } } @@ -2244,7 +2244,7 @@ if (backendName !== 'ALSA Direct') { if (exclusiveMode) { exclusiveMode = false; - await invoke('set_audio_exclusive_mode', { enabled: false }); + await invoke('v2_set_audio_exclusive_mode', { enabled: false }); console.log('[Audio] Disabled exclusive mode (only compatible with ALSA Direct)'); } } @@ -2253,14 +2253,14 @@ if (backendName === 'ALSA Direct') { if (gaplessPlayback) { gaplessPlayback = false; - await invoke('set_audio_gapless_enabled', { enabled: false }); + await invoke('v2_set_audio_gapless_enabled', { enabled: false }); console.log('[Audio] Disabled gapless playback (not compatible with ALSA Direct)'); } } try { // Save backend preference - await invoke('set_audio_backend_type', { backendType }); + await invoke('v2_set_audio_backend_type', { backendType }); console.log('[Audio] Backend changed:', backendName, '(type:', backendType ?? 'auto', ')'); // Load devices for new backend @@ -2274,7 +2274,7 @@ // Reset to default device when switching backends (always) outputDevice = 'System Default'; - await invoke('set_audio_output_device', { device: null }); + await invoke('v2_set_audio_output_device', { device: null }); // Reinitialize audio - recreates stream with new backend. // Position and audio data are preserved so the user can resume. @@ -2292,7 +2292,7 @@ const plugin = pluginInfo?.plugin ?? null; try { - await invoke('set_audio_alsa_plugin', { plugin }); + await invoke('v2_set_audio_alsa_plugin', { plugin }); console.log('[Audio] ALSA plugin changed:', pluginName, '(type:', plugin ?? 'none', ')'); // Reinitialize audio if ALSA backend is active @@ -2308,7 +2308,7 @@ async function handleAlsaHardwareVolumeChange(enabled: boolean) { alsaHardwareVolume = enabled; try { - await invoke('set_audio_alsa_hardware_volume', { enabled }); + await invoke('v2_set_audio_alsa_hardware_volume', { enabled }); console.log('[Audio] ALSA hardware volume changed:', enabled); } catch (err) { console.error('[Audio] Failed to change ALSA hardware volume:', err); @@ -2318,7 +2318,7 @@ async function handleStreamFirstTrackChange(enabled: boolean) { streamFirstTrack = enabled; try { - await invoke('set_audio_stream_first_track', { enabled }); + await invoke('v2_set_audio_stream_first_track', { enabled }); console.log('[Audio] Stream first track changed:', enabled); } catch (err) { console.error('[Audio] Failed to change stream first track:', err); @@ -2330,7 +2330,7 @@ const clamped = Math.max(1, Math.min(10, Math.round(seconds))); streamBufferSeconds = clamped; try { - await invoke('set_audio_stream_buffer_seconds', { seconds: clamped }); + await invoke('v2_set_audio_stream_buffer_seconds', { seconds: clamped }); console.log('[Audio] Stream buffer seconds changed:', clamped); } catch (err) { console.error('[Audio] Failed to change stream buffer seconds:', err); @@ -2343,12 +2343,12 @@ // Gapless not compatible with streaming-only if (enabled && gaplessPlayback) { gaplessPlayback = false; - await invoke('set_audio_gapless_enabled', { enabled: false }); + await invoke('v2_set_audio_gapless_enabled', { enabled: false }); console.log('[Audio] Disabled gapless playback (not compatible with streaming-only)'); } try { - await invoke('set_audio_streaming_only', { enabled }); + await invoke('v2_set_audio_streaming_only', { enabled }); console.log('[Audio] Streaming-only mode changed:', enabled); } catch (err) { console.error('[Audio] Failed to change streaming-only mode:', err); @@ -2358,7 +2358,7 @@ async function handleLimitQualityToDeviceChange(enabled: boolean) { limitQualityToDevice = enabled; try { - await invoke('set_audio_limit_quality_to_device', { enabled }); + await invoke('v2_set_audio_limit_quality_to_device', { enabled }); console.log('[Audio] Limit quality to device changed:', enabled); } catch (err) { console.error('[Audio] Failed to change limit quality to device:', err); @@ -2404,9 +2404,9 @@ } try { - await invoke('set_audio_output_device', { device: deviceId }); + await invoke('v2_set_audio_output_device', { device: deviceId }); // Store device's max sample rate for quality limiting - await invoke('set_audio_device_max_sample_rate', { rate: maxSampleRate }); + await invoke('v2_set_audio_device_max_sample_rate', { rate: maxSampleRate }); // Load per-device sample rate limit for the new device await loadDeviceSampleRateLimit(deviceId ?? 'default'); // Reinitialize audio - position and audio data preserved for resume. @@ -2420,7 +2420,7 @@ async function handleGaplessPlaybackChange(enabled: boolean) { gaplessPlayback = enabled; try { - await invoke('set_audio_gapless_enabled', { enabled }); + await invoke('v2_set_audio_gapless_enabled', { enabled }); console.log('[Audio] Gapless playback changed:', enabled); } catch (err) { console.error('[Audio] Failed to change gapless playback:', err); @@ -2435,7 +2435,7 @@ dacPassthrough = false; console.log('[Audio] Crossfade enabled: disabled DAC passthrough'); try { - await invoke('set_audio_dac_passthrough', { enabled: false }); + await invoke('v2_set_audio_dac_passthrough', { enabled: false }); // Reinitialize audio with currently selected device const deviceName = getCurrentDeviceSinkName(); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index b2c1e9644..e2865b26c 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1433,7 +1433,7 @@ async function toggleNormalization() { const newState = !normalizationEnabled; try { - await invoke('set_audio_normalization_enabled', { enabled: newState }); + await invoke('v2_set_audio_normalization_enabled', { enabled: newState }); normalizationEnabled = newState; } catch (err) { console.error('Failed to toggle normalization:', err);