fix(client): list only real audio devices and persist the choice by id
The device pickers showed every ALSA PCM plugin (Discard all samples, Rate Converter, JACK, Open Sound System, surround/dmix aliases). Enumeration now goes through a pure filter that keeps the system default, the PipeWire and PulseAudio servers and one entry per hardware endpoint, with technical tails removed and a stable sort. Selections are stored by device id instead of list position, so the filter and hotplug cannot move a saved choice. Indexes saved by older versions are migrated by device name; a missing device falls back to the default. The same preferences change records whether the voice mode was chosen by the person: a profile that only carries the old push-to-talk default now starts in voice activation, explicit choices are kept.
This commit is contained in:
parent
82dd9a324d
commit
3b95df9e9c
17 changed files with 780 additions and 210 deletions
|
|
@ -2,15 +2,25 @@ use std::path::PathBuf;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::state::UiState;
|
||||
use super::state::{DEFAULT_VOICE_MODE, UiState};
|
||||
use vnox_client::audio::devices::{self, filter};
|
||||
|
||||
#[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>,
|
||||
mode: usize,
|
||||
/// True once the person picked the voice input mode. Files from older versions lack it and
|
||||
/// stored `mode: 0` for everybody, so an unmarked push-to-talk is not a real choice.
|
||||
mode_chosen: bool,
|
||||
volume: Option<f32>,
|
||||
}
|
||||
|
||||
|
|
@ -20,17 +30,42 @@ fn path() -> PathBuf {
|
|||
.join("vnox/audio.json")
|
||||
}
|
||||
|
||||
/// Resolves the voice mode to use: an explicit choice is kept, and so is any non-default mode
|
||||
/// written by older versions (those could only come from the person), but a legacy file that
|
||||
/// merely records the old push-to-talk default becomes voice activation.
|
||||
fn resolve_mode(prefs: &AudioPreferences) -> (usize, bool) {
|
||||
let stored = prefs.mode.min(2);
|
||||
if prefs.mode_chosen || stored != 0 {
|
||||
(stored, true)
|
||||
} else {
|
||||
(DEFAULT_VOICE_MODE, false)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(state: &mut UiState) {
|
||||
let Ok(text) = std::fs::read_to_string(path()) else {
|
||||
return;
|
||||
};
|
||||
let Ok(prefs) = serde_json::from_str::<AudioPreferences>(&text) else {
|
||||
return;
|
||||
};
|
||||
state.mic_device = prefs.input_device;
|
||||
state.output_device = prefs.output_device;
|
||||
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 = prefs.mode.min(2);
|
||||
(state.vad_mode, state.vad_mode_chosen) = resolve_mode(&prefs);
|
||||
state.output_volume = prefs.volume.unwrap_or(90.0).clamp(0.0, 100.0);
|
||||
}
|
||||
|
||||
|
|
@ -40,12 +75,52 @@ pub fn save(state: &UiState) -> anyhow::Result<()> {
|
|||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
let preferences = AudioPreferences {
|
||||
input_device: state.mic_device,
|
||||
output_device: state.output_device,
|
||||
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: state.vad_mode,
|
||||
mode_chosen: state.vad_mode_chosen,
|
||||
volume: Some(state.output_volume),
|
||||
};
|
||||
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_default_becomes_voice_activation() {
|
||||
assert_eq!(
|
||||
resolve_mode(&parse(r#"{"mode":0,"volume":50.0}"#)),
|
||||
(1, false)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_push_to_talk_is_kept() {
|
||||
assert_eq!(
|
||||
resolve_mode(&parse(r#"{"mode":0,"mode_chosen":true}"#)),
|
||||
(0, true)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_non_default_modes_are_kept() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue