diff --git a/GLOSSARY.md b/GLOSSARY.md index 5023bff..034dd31 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -37,7 +37,7 @@ Sending Plays straight to the Scrobbling account instead of to Navidrome, which _Avoid_: Last.fm only **Device config**: -A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. +A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. Launches can be set to begin on one config, or on the system default, whichever was active when Reverb closed; otherwise a launch picks up the config last active. _Avoid_: Output profile, audio preset, zone ### Stats diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 570eb45..d00c74a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -58,6 +58,20 @@ pub fn run() { } } + // A launch can be set to begin on one device config (or the + // system default), whatever was active at the last quit. Switch + // before the engine is built from the preferences, so it opens + // that output first. + { + let prefs = settings.get().prefs; + let launch = prefs.launch_device_config(); + if launch != prefs.active_device_config { + if let Err(e) = settings.update(|s| s.prefs.active_device_config = launch) { + log::warn!("could not switch to the startup device config: {e}"); + } + } + } + let keychain = Keychain::new(config_dir); let subsonic = Arc::new(SubsonicClient::new()); diff --git a/src-tauri/src/persistence/settings.rs b/src-tauri/src/persistence/settings.rs index 18c0835..cdba015 100644 --- a/src-tauri/src/persistence/settings.rs +++ b/src-tauri/src/persistence/settings.rs @@ -22,6 +22,10 @@ use serde::{Deserialize, Serialize}; use crate::error::ReverbError; +/// [`Preferences::startup_device_config`] value that starts every launch on +/// the system default. Config ids are nanoids or UUIDs, so none can be this. +pub const STARTUP_SYSTEM_DEFAULT: &str = "system-default"; + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct EqPreset { @@ -95,6 +99,10 @@ pub struct Preferences { /// The device config playing now, by id. `None` — or an id no config has — /// plays on the system default, shared, at the device's own rate. pub active_device_config: Option, + /// The device config every launch starts on, by id, whatever was active + /// when Reverb closed — or [`STARTUP_SYSTEM_DEFAULT`] for none. `None` — + /// or an id no config has — starts on the one last active. + pub startup_device_config: Option, /// "raw" for the original file, or "mp3-320" / "mp3-192" / "mp3-128" /// for server-side transcoding. pub streaming_format: String, @@ -176,6 +184,7 @@ impl Default for Preferences { playlist_layout: "grid".into(), device_configs: Vec::new(), active_device_config: None, + startup_device_config: None, streaming_format: "raw".into(), replay_gain: ReplayGainSettings::default(), crossfade_secs: 0, @@ -208,6 +217,19 @@ impl Preferences { self.device_configs.iter().find(|config| config.id == id) } + /// The config a launch starts on, as `active_device_config` should then + /// read (`None` = system default): the startup choice, or the one last + /// active when there is none or it names a config that no longer exists. + pub fn launch_device_config(&self) -> Option { + match self.startup_device_config.as_deref() { + Some(STARTUP_SYSTEM_DEFAULT) => None, + Some(id) if self.device_configs.iter().any(|config| config.id == id) => { + Some(id.to_owned()) + } + _ => self.active_device_config.clone(), + } + } + /// What the local engine should be playing through. An HQPlayer config /// has no device here, so with one active this is the system default — /// which the engine, in standby, does not open. @@ -706,6 +728,42 @@ mod tests { assert_eq!(back["deviceConfigs"][1]["lockDevice"], serde_json::json!(false)); } + #[test] + fn a_launch_switches_to_the_startup_config() { + let raw = r#"{ + "deviceConfigs": [ + {"id": "a", "name": "Config A", "device": "coreaudio:dac"}, + {"id": "b", "name": "Config B", "device": null} + ], + "activeDeviceConfig": "b", + "startupDeviceConfig": "a" + }"#; + let mut prefs: Preferences = serde_json::from_str(raw).unwrap(); + assert_eq!(prefs.launch_device_config().as_deref(), Some("a")); + + // From the system default too. + prefs.active_device_config = None; + assert_eq!(prefs.launch_device_config().as_deref(), Some("a")); + + // The system default can be the startup choice as well. + prefs.active_device_config = Some("b".into()); + prefs.startup_device_config = Some(STARTUP_SYSTEM_DEFAULT.into()); + assert_eq!(prefs.launch_device_config(), None); + + // No startup config, or a deleted one: the last one active stays. + prefs.startup_device_config = None; + assert_eq!(prefs.launch_device_config().as_deref(), Some("b")); + prefs.startup_device_config = Some("gone".into()); + assert_eq!(prefs.launch_device_config().as_deref(), Some("b")); + prefs.active_device_config = None; + assert_eq!(prefs.launch_device_config(), None); + + // And the field round-trips under its name. + prefs.startup_device_config = Some("a".into()); + let back = serde_json::to_value(&prefs).unwrap(); + assert_eq!(back["startupDeviceConfig"], serde_json::json!("a")); + } + #[test] fn old_settings_files_parse_with_pref_defaults() { // A settings.json written before the preferences existed. @@ -728,6 +786,7 @@ mod tests { assert!(!s.prefs.direct_scrobble_reset_on_start); assert!(!s.prefs.direct_scrobble_pinned); assert!(s.prefs.device_configs.is_empty()); + assert_eq!(s.prefs.startup_device_config, None, "no startup config: start on the last one"); assert_eq!(s.prefs.output(), OutputSettings::default()); assert!(s.prefs.check_for_updates, "update checks default to on"); assert!(!s.prefs.discord_presence, "Discord presence defaults to off"); diff --git a/src/lib/deviceConfigs.test.ts b/src/lib/deviceConfigs.test.ts index 0c8ab63..e2390b2 100644 --- a/src/lib/deviceConfigs.test.ts +++ b/src/lib/deviceConfigs.test.ts @@ -9,6 +9,8 @@ import { isConnected, parsePort, saveDeviceConfig, + STARTUP_SYSTEM_DEFAULT, + startupDeviceConfig, } from "./deviceConfigs"; import type { DeviceConfig, OutputDevice } from "./subsonic/types"; @@ -30,7 +32,11 @@ const speakers: DeviceConfig = { lockDevice: false, hqplayer: null, }; -const prefs = { deviceConfigs: [desk, speakers], activeDeviceConfig: "desk" }; +const prefs = { + deviceConfigs: [desk, speakers], + activeDeviceConfig: "desk", + startupDeviceConfig: null, +}; describe("activeDeviceConfig", () => { it("finds the config by id", () => { @@ -49,6 +55,7 @@ describe("saveDeviceConfig", () => { expect(saveDeviceConfig(prefs, fresh)).toEqual({ deviceConfigs: [desk, speakers, fresh], activeDeviceConfig: "new", + startupDeviceConfig: null, }); }); @@ -57,8 +64,16 @@ describe("saveDeviceConfig", () => { expect(saveDeviceConfig(prefs, renamed)).toEqual({ deviceConfigs: [desk, renamed], activeDeviceConfig: "desk", + startupDeviceConfig: null, }); }); + + it("keeps the startup config", () => { + const withStartup = { ...prefs, startupDeviceConfig: "speakers" }; + const fresh = { ...speakers, id: "new", name: "Headphones" }; + expect(saveDeviceConfig(withStartup, fresh).startupDeviceConfig).toBe("speakers"); + expect(saveDeviceConfig(withStartup, desk).startupDeviceConfig).toBe("speakers"); + }); }); describe("deleteDeviceConfig", () => { @@ -66,6 +81,7 @@ describe("deleteDeviceConfig", () => { expect(deleteDeviceConfig(prefs, "speakers")).toEqual({ deviceConfigs: [desk], activeDeviceConfig: "desk", + startupDeviceConfig: null, }); }); @@ -73,8 +89,28 @@ describe("deleteDeviceConfig", () => { expect(deleteDeviceConfig(prefs, "desk")).toEqual({ deviceConfigs: [speakers], activeDeviceConfig: null, + startupDeviceConfig: null, }); }); + + it("drops the startup config when it goes, and only then", () => { + const withStartup = { ...prefs, startupDeviceConfig: "speakers" }; + expect(deleteDeviceConfig(withStartup, "speakers").startupDeviceConfig).toBeNull(); + expect(deleteDeviceConfig(withStartup, "desk").startupDeviceConfig).toBe("speakers"); + }); +}); + +describe("startupDeviceConfig", () => { + it("is a config's id, or the system default", () => { + expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "speakers" })).toBe("speakers"); + const systemDefault = { ...prefs, startupDeviceConfig: STARTUP_SYSTEM_DEFAULT }; + expect(startupDeviceConfig(systemDefault)).toBe(STARTUP_SYSTEM_DEFAULT); + }); + + it("is none without one, or with one that was deleted", () => { + expect(startupDeviceConfig(prefs)).toBeNull(); + expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "gone" })).toBeNull(); + }); }); describe("afterDeviceLost", () => { diff --git a/src/lib/deviceConfigs.ts b/src/lib/deviceConfigs.ts index fd78511..f83a14d 100644 --- a/src/lib/deviceConfigs.ts +++ b/src/lib/deviceConfigs.ts @@ -4,10 +4,19 @@ import type { DeviceConfig, HqPlayerEndpoint, OutputDevice, Preferences } from "./subsonic/types"; +/** + * `startupDeviceConfig` value that starts every launch on the system default. + * Config ids are nanoids or UUIDs, so none can be this. + */ +export const STARTUP_SYSTEM_DEFAULT = "system-default"; + /** The port HQPlayer listens for control on unless told otherwise. */ export const HQPLAYER_DEFAULT_PORT = 4321; -type ConfigPrefs = Pick; +type ConfigPrefs = Pick< + Preferences, + "deviceConfigs" | "activeDeviceConfig" | "startupDeviceConfig" +>; /** The device config playing now; `null` means the system default. */ export function activeDeviceConfig(prefs: ConfigPrefs): DeviceConfig | null { @@ -24,19 +33,38 @@ export function saveDeviceConfig(prefs: ConfigPrefs, config: DeviceConfig): Conf return { deviceConfigs: prefs.deviceConfigs.map((c) => (c.id === config.id ? config : c)), activeDeviceConfig: prefs.activeDeviceConfig, + startupDeviceConfig: prefs.startupDeviceConfig, }; } - return { deviceConfigs: [...prefs.deviceConfigs, config], activeDeviceConfig: config.id }; + return { + deviceConfigs: [...prefs.deviceConfigs, config], + activeDeviceConfig: config.id, + startupDeviceConfig: prefs.startupDeviceConfig, + }; } -/** Delete a config. Deleting the active one moves playback to the system default. */ +/** + * Delete a config. Deleting the active one moves playback to the system + * default; deleting the startup one leaves launches on the one last active. + */ export function deleteDeviceConfig(prefs: ConfigPrefs, id: string): ConfigPrefs { return { deviceConfigs: prefs.deviceConfigs.filter((c) => c.id !== id), activeDeviceConfig: prefs.activeDeviceConfig === id ? null : prefs.activeDeviceConfig, + startupDeviceConfig: prefs.startupDeviceConfig === id ? null : prefs.startupDeviceConfig, }; } +/** + * What launches start on: a config's id, `STARTUP_SYSTEM_DEFAULT`, or `null` + * for the one last active — which is also what an id no config has means. + */ +export function startupDeviceConfig(prefs: ConfigPrefs): string | null { + const id = prefs.startupDeviceConfig; + if (id === STARTUP_SYSTEM_DEFAULT) return id; + return prefs.deviceConfigs.some((c) => c.id === id) ? id : null; +} + /** * Mirror the backend after `device` was unplugged: playback fell back to the * system default, so that is what is active now. A config switched to since diff --git a/src/lib/i18n/messages/de.ts b/src/lib/i18n/messages/de.ts index baa2bd6..fb0feae 100644 --- a/src/lib/i18n/messages/de.ts +++ b/src/lib/i18n/messages/de.ts @@ -466,6 +466,10 @@ export const de: Messages = { "settings.deleteDeviceConfigTitle": "„{name}“ löschen?", "settings.deleteActiveDeviceConfigBody": "Sie ist gerade aktiv, daher wechselt die Wiedergabe auf den Systemstandard.", + "settings.startupDeviceConfig": "Konfiguration beim Start", + "settings.startupDeviceConfigHint": + "Die Gerätekonfiguration, mit der Reverb startet, egal welche beim Beenden aktiv war. „Zuletzt verwendet“ macht dort weiter, wo du aufgehört hast.", + "settings.startupDeviceConfigLastUsed": "Zuletzt verwendet", "settings.deviceConfigName": "Name", "settings.deviceConfigNameHint": "Leer lassen, um den Namen des Geräts zu verwenden.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/en.ts b/src/lib/i18n/messages/en.ts index 201d1f4..22ad7aa 100644 --- a/src/lib/i18n/messages/en.ts +++ b/src/lib/i18n/messages/en.ts @@ -491,6 +491,10 @@ export const en = { "settings.deleteDeviceConfig": "Delete {name}", "settings.deleteDeviceConfigTitle": "Delete “{name}”?", "settings.deleteActiveDeviceConfigBody": "It is in use, so playback moves to the system default.", + "settings.startupDeviceConfig": "Config at startup", + "settings.startupDeviceConfigHint": + "Reverb starts on this config every time, whichever one was in use when it closed. “Last used” picks up where you left off.", + "settings.startupDeviceConfigLastUsed": "Last used", "settings.deviceConfigName": "Name", "settings.deviceConfigNameHint": "Leave empty to use the device's name.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/es.ts b/src/lib/i18n/messages/es.ts index 84a5557..d250f1c 100644 --- a/src/lib/i18n/messages/es.ts +++ b/src/lib/i18n/messages/es.ts @@ -466,6 +466,10 @@ export const es: Messages = { "settings.deleteDeviceConfigTitle": "¿Eliminar “{name}”?", "settings.deleteActiveDeviceConfigBody": "Está en uso, así que la reproducción pasa al predeterminado del sistema.", + "settings.startupDeviceConfig": "Configuración al iniciar", + "settings.startupDeviceConfigHint": + "La configuración de dispositivo con la que arranca Reverb, sea cual sea la que estaba en uso al cerrarlo. “Última usada” continúa donde lo dejaste.", + "settings.startupDeviceConfigLastUsed": "Última usada", "settings.deviceConfigName": "Nombre", "settings.deviceConfigNameHint": "Déjalo vacío para usar el nombre del dispositivo.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/fr.ts b/src/lib/i18n/messages/fr.ts index 5cfea46..0b41880 100644 --- a/src/lib/i18n/messages/fr.ts +++ b/src/lib/i18n/messages/fr.ts @@ -467,6 +467,10 @@ export const fr: Messages = { "settings.deleteDeviceConfigTitle": "Supprimer « {name} » ?", "settings.deleteActiveDeviceConfigBody": "Elle est en cours d'utilisation : la lecture passe sur le périphérique par défaut.", + "settings.startupDeviceConfig": "Configuration au démarrage", + "settings.startupDeviceConfigHint": + "La configuration de périphérique avec laquelle Reverb démarre, quelle que soit celle utilisée à sa fermeture. « Dernière utilisée » reprend là où vous vous étiez arrêté.", + "settings.startupDeviceConfigLastUsed": "Dernière utilisée", "settings.deviceConfigName": "Nom", "settings.deviceConfigNameHint": "Laissez vide pour utiliser le nom du périphérique.", "settings.systemDefaultConfigHint": diff --git a/src/lib/subsonic/types.ts b/src/lib/subsonic/types.ts index df01c4e..2624334 100644 --- a/src/lib/subsonic/types.ts +++ b/src/lib/subsonic/types.ts @@ -426,6 +426,13 @@ export interface Preferences { * plays on the system default, shared, at the device's own rate. */ activeDeviceConfig: string | null; + /** + * The device config every launch starts on, by id, whatever was active when + * Reverb closed — or `STARTUP_SYSTEM_DEFAULT` for none. `null` — or an id no + * config has — starts on the one last active. The backend switches at + * launch, before the engine opens. + */ + startupDeviceConfig: string | null; streamingFormat: StreamingFormat; replayGain: ReplayGainSettings; /** Crossfade between tracks in seconds (0 = gapless). */ diff --git a/src/stores/prefsStore.ts b/src/stores/prefsStore.ts index 06614e1..2bb143c 100644 --- a/src/stores/prefsStore.ts +++ b/src/stores/prefsStore.ts @@ -146,6 +146,7 @@ const DEFAULT_PREFS: Preferences = { playlistLayout: "grid", deviceConfigs: [], activeDeviceConfig: null, + startupDeviceConfig: null, streamingFormat: "raw", replayGain: { mode: "off", preampDb: 0, clipProtection: true, fallbackDb: 0 }, crossfadeSecs: 0, diff --git a/src/views/settings/SettingsView.tsx b/src/views/settings/SettingsView.tsx index 4ccffe8..ef61a3e 100644 --- a/src/views/settings/SettingsView.tsx +++ b/src/views/settings/SettingsView.tsx @@ -46,6 +46,8 @@ import { isConnected, parsePort, saveDeviceConfig, + STARTUP_SYSTEM_DEFAULT, + startupDeviceConfig, } from "../../lib/deviceConfigs"; import { formatSampleRate } from "../../lib/format"; import { LANGUAGE_NAMES, LOCALES, useT, type MessageKey, type T } from "../../lib/i18n"; @@ -479,6 +481,30 @@ function AudioSection() { })} + {/* Only worth asking once there is a config: until then every launch + is on the system default anyway. Reads the resolved choice, so a + config deleted elsewhere shows as "Last used". */} + {prefs.deviceConfigs.length > 0 && ( + + + + )} + {/* Reverb's own stream; with HQPlayer playing there is none, and the signal path shows HQPlayer's rate instead. */} {!active?.hqplayer && (