133 lines
4.4 KiB
Rust
133 lines
4.4 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use super::state::{DEFAULT_VOICE_MODE, UiState};
|
|
use vnox_client::audio::devices::{self, filter};
|
|
use vnox_client::audio::sounds::SoundSettings;
|
|
|
|
#[derive(Default, Serialize, Deserialize)]
|
|
#[serde(default)]
|
|
struct AudioPreferences {
|
|
/// Legacy position in the old, unfiltered device list. Read only to migrate.
|
|
input_device: usize,
|
|
/// Legacy position in the old, unfiltered device list. Read only to migrate.
|
|
output_device: usize,
|
|
/// Stable id of the chosen microphone; empty or missing is the system default.
|
|
input_device_id: Option<String>,
|
|
/// Stable id of the chosen output; empty or missing is the system default.
|
|
output_device_id: Option<String>,
|
|
bitrate: Option<f32>,
|
|
/// Voice input mode stored by the app. Missing only on a profile that never saved one.
|
|
mode: Option<usize>,
|
|
/// True once the person picked the voice input mode explicitly.
|
|
mode_chosen: bool,
|
|
volume: Option<f32>,
|
|
/// Notification sound switches and volume.
|
|
sounds: SoundSettings,
|
|
}
|
|
|
|
fn path() -> PathBuf {
|
|
dirs::config_dir()
|
|
.unwrap_or_else(|| PathBuf::from("."))
|
|
.join("vnox/audio.json")
|
|
}
|
|
|
|
/// Resolves the voice mode to use. Voice activation is the default only for a profile that has
|
|
/// no stored mode yet. Any mode already stored, including push-to-talk written by older
|
|
/// versions, is kept: switching an existing profile to an open microphone would start
|
|
/// transmitting without the person agreeing to it.
|
|
fn resolve_mode(prefs: &AudioPreferences) -> (usize, bool) {
|
|
match prefs.mode {
|
|
Some(mode) => (mode.min(2), true),
|
|
None => (DEFAULT_VOICE_MODE, false),
|
|
}
|
|
}
|
|
|
|
pub fn load(state: &mut UiState) {
|
|
let prefs = std::fs::read_to_string(path())
|
|
.ok()
|
|
.and_then(|text| serde_json::from_str::<AudioPreferences>(&text).ok())
|
|
.unwrap_or_default();
|
|
let lists = devices::enumerate().unwrap_or_default();
|
|
state.mic_device_id = prefs.input_device_id.clone().unwrap_or_else(|| {
|
|
filter::migrate_legacy_index(
|
|
&devices::legacy_names(true),
|
|
prefs.input_device,
|
|
&lists.input,
|
|
)
|
|
});
|
|
state.output_device_id = prefs.output_device_id.clone().unwrap_or_else(|| {
|
|
filter::migrate_legacy_index(
|
|
&devices::legacy_names(false),
|
|
prefs.output_device,
|
|
&lists.output,
|
|
)
|
|
});
|
|
state.device_lists = lists;
|
|
super::devices::sync_indexes(state);
|
|
state.voice_bitrate = prefs.bitrate.unwrap_or(64.0).clamp(8.0, 128.0);
|
|
(state.vad_mode, state.vad_mode_chosen) = resolve_mode(&prefs);
|
|
state.output_volume = prefs.volume.unwrap_or(90.0).clamp(0.0, 100.0);
|
|
state.sounds = SoundSettings {
|
|
volume: prefs.sounds.volume.clamp(0.0, 100.0),
|
|
..prefs.sounds
|
|
};
|
|
}
|
|
|
|
pub fn save(state: &UiState) -> anyhow::Result<()> {
|
|
let path = path();
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
let preferences = AudioPreferences {
|
|
input_device: 0,
|
|
output_device: 0,
|
|
input_device_id: Some(state.mic_device_id.clone()),
|
|
output_device_id: Some(state.output_device_id.clone()),
|
|
bitrate: Some(state.voice_bitrate),
|
|
mode: Some(state.vad_mode),
|
|
mode_chosen: state.vad_mode_chosen,
|
|
volume: Some(state.output_volume),
|
|
sounds: state.sounds,
|
|
};
|
|
std::fs::write(path, serde_json::to_vec_pretty(&preferences)?)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn parse(text: &str) -> AudioPreferences {
|
|
serde_json::from_str(text).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn fresh_profile_uses_voice_activation() {
|
|
assert_eq!(resolve_mode(&parse("{}")), (1, false));
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_push_to_talk_is_kept_not_silently_opened() {
|
|
assert_eq!(
|
|
resolve_mode(&parse(r#"{"mode":0,"volume":50.0}"#)),
|
|
(0, true)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_push_to_talk_is_kept() {
|
|
assert_eq!(
|
|
resolve_mode(&parse(r#"{"mode":0,"mode_chosen":true}"#)),
|
|
(0, true)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn stored_modes_are_kept_and_clamped() {
|
|
assert_eq!(resolve_mode(&parse(r#"{"mode":1}"#)), (1, true));
|
|
assert_eq!(resolve_mode(&parse(r#"{"mode":2}"#)), (2, true));
|
|
assert_eq!(resolve_mode(&parse(r#"{"mode":9}"#)), (2, true));
|
|
}
|
|
}
|