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:
Loki 2026-10-24 23:59:27 +02:00
parent 82dd9a324d
commit 3b95df9e9c
Signed by: boba
GPG key ID: 253067914055423B
17 changed files with 780 additions and 210 deletions

69
src/ui/devices.rs Normal file
View file

@ -0,0 +1,69 @@
//! Bridges the persisted device ids and the positions of the settings pickers.
use vnox_client::audio::devices::{self, filter};
use super::state::UiState;
/// Recomputes the picker positions from the persisted ids and the last enumeration.
pub fn sync_indexes(ui: &mut UiState) {
ui.mic_device = filter::index_for_id(&ui.device_lists.input, &ui.mic_device_id);
ui.output_device = filter::index_for_id(&ui.device_lists.output, &ui.output_device_id);
}
/// Enumerates devices again (hardware may have changed) and keeps the saved choices.
pub fn refresh(ui: &mut UiState) {
match devices::enumerate() {
Ok(lists) => ui.device_lists = lists,
Err(error) => tracing::warn!(%error, "cannot enumerate audio devices"),
}
sync_indexes(ui);
}
/// Applies picker positions chosen by the person, storing the matching stable ids.
pub fn select(ui: &mut UiState, input_index: usize, output_index: usize) {
ui.mic_device_id = filter::id_for_index(&ui.device_lists.input, input_index);
ui.output_device_id = filter::id_for_index(&ui.device_lists.output, output_index);
sync_indexes(ui);
}
#[cfg(test)]
mod tests {
use super::*;
use vnox_client::audio::devices::{DeviceEntry, DeviceLists};
fn entry(id: &str, label: &str) -> DeviceEntry {
DeviceEntry {
id: id.into(),
label: label.into(),
}
}
fn state() -> UiState {
UiState {
device_lists: DeviceLists {
input: vec![entry("a", "Mic A"), entry("b", "Mic B")],
output: vec![entry("x", "Out X")],
},
..Default::default()
}
}
#[test]
fn selection_is_stored_by_id_and_survives_list_changes() {
let mut ui = state();
select(&mut ui, 2, 1);
assert_eq!((ui.mic_device_id.as_str(), ui.mic_device), ("b", 2));
assert_eq!((ui.output_device_id.as_str(), ui.output_device), ("x", 1));
// A new device appears before the saved one; the choice follows the id.
ui.device_lists.input.insert(0, entry("new", "Aaa"));
sync_indexes(&mut ui);
assert_eq!(ui.mic_device, 3);
}
#[test]
fn missing_device_falls_back_to_default_position() {
let mut ui = state();
ui.mic_device_id = "unplugged".into();
sync_indexes(&mut ui);
assert_eq!(ui.mic_device, 0);
}
}

View file

@ -1,6 +1,7 @@
pub mod appearance;
pub mod avatar;
pub mod bridge;
pub mod devices;
pub mod i18n;
pub mod media;
pub mod preferences;

View file

@ -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));
}
}

View file

@ -82,7 +82,7 @@ export component MainWindow inherits Window {
in-out property <string> voice-input-device: "";
in-out property <string> voice-output-device: "";
in-out property <float> voice-bitrate: 64.0;
in-out property <int> vad-mode: 0;
in-out property <int> vad-mode: 1;
in-out property <float> volume: 100.0;
in-out property <string> language-choice: "system";
callback language-selected(string);

View file

@ -33,13 +33,17 @@ impl Default for UiState {
mic_enabled: true,
ptt_held: false,
deafen: false,
vad_mode: 0,
vad_mode: crate::ui::state::DEFAULT_VOICE_MODE,
voice_bitrate: 64.0,
packet_interval: 1,
mic_device: 0,
mic_device_id: String::new(),
output_device: 0,
output_device_id: String::new(),
output_volume: 90.0,
device_lists: Default::default(),
vad_mode_chosen: false,
auto_reconnect: true,

View file

@ -8,6 +8,9 @@ pub use types::{ConnState, TrustPrompt, TrustPromptKind, UiState, VoiceUiState};
use models::bookmark_label_from_address;
/// Voice input mode of a fresh profile: 0 push to talk, 1 voice activation, 2 open microphone.
pub const DEFAULT_VOICE_MODE: usize = 1;
impl UiState {
pub fn remove_bookmark(&mut self, index: usize) {
if index < self.bookmarks.len() {

View file

@ -77,10 +77,20 @@ pub struct UiState {
pub vad_mode: usize,
pub voice_bitrate: f32,
pub packet_interval: usize,
/// Picker position of the microphone (0 is the system default).
pub mic_device: usize,
/// Persisted microphone id; empty is the system default.
pub mic_device_id: String,
/// Picker position of the output device (0 is the system default).
pub output_device: usize,
/// Persisted output device id; empty is the system default.
pub output_device_id: String,
pub output_volume: f32,
/// Devices behind the picker positions, as of the last enumeration.
pub device_lists: vnox_client::audio::DeviceLists,
/// True once the person picked a voice input mode themselves.
pub vad_mode_chosen: bool,
pub auto_reconnect: bool,

View file

@ -865,8 +865,8 @@ fn status_handle(ui: &mut UiState, _net: &NetHandle, event: NetEvent) {
pub fn pipeline_config(channel_id: &str, state: &AppState) -> PipelineConfig {
PipelineConfig {
channel_key: vnox_client::net::voice::channel_key(channel_id),
mic_device_index: state.ui.mic_device,
output_device_index: state.ui.output_device,
mic_device_id: state.ui.mic_device_id.clone(),
output_device_id: state.ui.output_device_id.clone(),
voice_bitrate_kbps: state.ui.voice_bitrate,
packet_interval: state.ui.packet_interval,
}