Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maolan-engine"
version = "0.4.23"
version = "0.4.24"
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"
Expand Down
36 changes: 29 additions & 7 deletions src/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,35 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AudioDevicePreferences {
pub default_output_device_id: Option<String>,
pub default_input_device_id: Option<String>,
default_output_device_id: Option<String>,
default_input_device_id: Option<String>,
}

impl AudioDevicePreferences {
pub fn from_toml_str(contents: &str) -> Result<Self, toml::de::Error> {
toml::from_str(contents)
}

pub fn default_output_device_id(&self) -> Option<&str> {
self.default_output_device_id.as_deref()
}

pub fn default_input_device_id(&self) -> Option<&str> {
self.default_input_device_id.as_deref()
}

#[cfg(target_os = "freebsd")]
pub fn set_default_output_device_id(&mut self, id: Option<String>) {
self.default_output_device_id = id;
}

#[cfg(target_os = "freebsd")]
pub fn set_default_input_device_id(&mut self, id: Option<String>) {
self.default_input_device_id = id;
}

/// Replaces these device settings in a TOML document while preserving its other values.
#[cfg(target_os = "freebsd")]
pub fn update_toml_str(&self, contents: &str) -> Result<String, String> {
let mut value = if contents.trim().is_empty() {
toml::Value::Table(toml::map::Map::new())
Expand Down Expand Up @@ -45,6 +64,7 @@ impl AudioDevicePreferences {
.unwrap_or_default()
}

#[cfg(target_os = "freebsd")]
pub fn save_to_path(&self, path: &Path) -> Result<(), String> {
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Expand All @@ -59,6 +79,7 @@ impl AudioDevicePreferences {
}
}

#[cfg(target_os = "freebsd")]
fn update_id(table: &mut toml::map::Map<String, toml::Value>, key: &str, id: &Option<String>) {
if let Some(id) = id
.as_deref()
Expand All @@ -70,16 +91,16 @@ fn update_id(table: &mut toml::map::Map<String, toml::Value>, key: &str, id: &Op
}
}

#[cfg(test)]
#[cfg(all(test, target_os = "freebsd"))]
mod tests {
use super::*;

#[test]
#[cfg(target_os = "freebsd")]
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 mut preferences = AudioDevicePreferences::default();
preferences.set_default_output_device_id(Some(String::from("/dev/dsp5")));
preferences.set_default_input_device_id(Some(String::from("/dev/dsp5")));
let updated = preferences
.update_toml_str("theme = \"dark\"\ndefault_output_device_id = \"out2\"\n")
.unwrap();
Expand All @@ -89,6 +110,7 @@ mod tests {
}

#[test]
#[cfg(target_os = "freebsd")]
fn update_toml_removes_unset_device_ids() {
let updated = AudioDevicePreferences::default()
.update_toml_str(
Expand Down
Loading