diff --git a/Cargo.lock b/Cargo.lock index e659db8..1cc45a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,6 +174,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + [[package]] name = "errno" version = "0.3.14" @@ -248,12 +254,28 @@ dependencies = [ "r-efi", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "hermit-abi" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.18" @@ -346,7 +368,7 @@ dependencies = [ [[package]] name = "maolan-engine" -version = "0.4.22" +version = "0.4.23" dependencies = [ "alsa", "arc-swap", @@ -378,6 +400,7 @@ dependencies = [ "symphonia", "timestretch", "tokio", + "toml", "tracing", "tracing-subscriber", "uuid", @@ -760,6 +783,15 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_spanned" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c187e0a369471c550af028cba56d4f4fa2a489ad28151ae95a4d340a2a8b782" +dependencies = [ + "serde_core", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -1103,6 +1135,45 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "toml" +version = "1.2.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5755c51863cdac711f1956baf8eb9588e060755f7d3c4c979e70ea1e1f32cd7" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.2.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a" + [[package]] name = "tracing" version = "0.1.44" @@ -1404,6 +1475,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "winnow" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" + [[package]] name = "zmij" version = "1.0.23" diff --git a/Cargo.toml b/Cargo.toml index a0090d3..57aac41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-engine" -version = "0.4.22" +version = "0.4.23" edition = "2024" description = "Audio engine for the Maolan DAW with audio/MIDI tracks, routing, export, and out-of-process CLAP/VST3/LV2 plugin support" license = "BSD-2-Clause" @@ -24,6 +24,7 @@ tracing = { version = "0.1.44", features = ["release_max_level_debug"] } midly = { version = "0.5.3", optional = true } serde = { version = "1.0.229", features = ["derive"] } serde_json = "1.0.154" +toml = "1.2.1" maolan-plugin-protocol = { version = "0.0.21", optional = true } uuid = { version = "1.29.0", features = ["v4"] } fearless_simd = { version = "1.2.0", features = ["force_support_fallback"] } diff --git a/src/lib.rs b/src/lib.rs index d7bc43a..a1e9486 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod audio; #[cfg(feature = "audio")] pub mod audio_codec; +pub mod preferences; pub mod wav_stream; #[cfg(target_os = "macos")] pub mod audio_devices { diff --git a/src/preferences.rs b/src/preferences.rs new file mode 100644 index 0000000..f4d913f --- /dev/null +++ b/src/preferences.rs @@ -0,0 +1,101 @@ +use std::path::Path; + +use serde::{Deserialize, Serialize}; + +/// Shared default audio device selections stored by Maolan applications. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub struct AudioDevicePreferences { + pub default_output_device_id: Option, + pub default_input_device_id: Option, +} + +impl AudioDevicePreferences { + pub fn from_toml_str(contents: &str) -> Result { + toml::from_str(contents) + } + + /// Replaces these device settings in a TOML document while preserving its other values. + pub fn update_toml_str(&self, contents: &str) -> Result { + let mut value = if contents.trim().is_empty() { + toml::Value::Table(toml::map::Map::new()) + } else { + toml::from_str::(contents).map_err(|error| error.to_string())? + }; + let table = value + .as_table_mut() + .ok_or_else(|| String::from("Audio device settings require a TOML table."))?; + update_id( + table, + "default_output_device_id", + &self.default_output_device_id, + ); + update_id( + table, + "default_input_device_id", + &self.default_input_device_id, + ); + toml::to_string_pretty(&value).map_err(|error| error.to_string()) + } + + pub fn load_from_path(path: &Path) -> Self { + std::fs::read_to_string(path) + .ok() + .and_then(|contents| Self::from_toml_str(&contents).ok()) + .unwrap_or_default() + } + + pub fn save_to_path(&self, path: &Path) -> Result<(), String> { + let contents = match std::fs::read_to_string(path) { + Ok(contents) => contents, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(), + Err(error) => return Err(error.to_string()), + }; + let updated = self.update_toml_str(&contents)?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|error| error.to_string())?; + } + std::fs::write(path, updated).map_err(|error| error.to_string()) + } +} + +fn update_id(table: &mut toml::map::Map, key: &str, id: &Option) { + if let Some(id) = id + .as_deref() + .filter(|id| !id.is_empty() && *id != "__auto__") + { + table.insert(key.to_string(), toml::Value::String(id.to_string())); + } else { + table.remove(key); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn update_toml_preserves_other_preferences() { + let preferences = AudioDevicePreferences { + default_output_device_id: Some(String::from("/dev/dsp5")), + default_input_device_id: Some(String::from("/dev/dsp5")), + }; + let updated = preferences + .update_toml_str("theme = \"dark\"\ndefault_output_device_id = \"out2\"\n") + .unwrap(); + let parsed = AudioDevicePreferences::from_toml_str(&updated).unwrap(); + assert_eq!(parsed, preferences); + assert!(updated.contains("theme = \"dark\"")); + } + + #[test] + fn update_toml_removes_unset_device_ids() { + let updated = AudioDevicePreferences::default() + .update_toml_str( + "default_output_device_id = \"old\"\ndefault_input_device_id = \"old\"\n", + ) + .unwrap(); + assert!(!updated.contains("default_output_device_id")); + assert!(!updated.contains("default_input_device_id")); + } +}