feat(client): synthesized notification sounds with settings

Short tones for joining and leaving a voice room, other participants
joining and leaving, new messages outside the viewed chat, mentions,
microphone mute and unmute, deafen and undeafen, server connection and
loss, and errors. The tones are generated at runtime (rising motifs for
arrival or enable, falling for leave or disable) with an attack and a
fade-out so they never click; no third-party audio is bundled.

Inside a voice room the sound is mixed into the open playback stream,
otherwise a short one-shot stream is opened on the selected output device
off the UI thread, capped at two at a time. While deafened nothing plays
except the undeafen sound. Settings: master switch, volume and separate
switches for voice room, messages and mentions, stored with the audio
preferences, with a new settings section and a test button.
This commit is contained in:
Loki 2026-10-25 10:01:38 +01:00
parent bf485720f5
commit eed3e28c63
Signed by: boba
GPG key ID: 253067914055423B
19 changed files with 1142 additions and 4 deletions

View file

@ -418,6 +418,11 @@ pub fn sync_settings(window: &MainWindow, state: &UiState, own_pubkey: &str) {
window.set_voice_bitrate(state.voice_bitrate);
window.set_vad_mode(state.vad_mode as i32);
window.set_volume(state.output_volume);
window.set_sounds_enabled(state.sounds.enabled);
window.set_sounds_volume(state.sounds.volume);
window.set_sounds_voice_room(state.sounds.voice_room);
window.set_sounds_messages(state.sounds.messages);
window.set_sounds_mentions(state.sounds.mentions);
window.set_settings_open(state.settings_open);
window.set_input_device_index(state.mic_device as i32);
window.set_output_device_index(state.output_device as i32);

View file

@ -6,6 +6,7 @@ pub mod i18n;
pub mod media;
pub mod preferences;
pub mod slint;
pub mod sounds;
pub mod state;
pub mod update;

View file

@ -4,6 +4,7 @@ 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)]
@ -22,6 +23,8 @@ struct AudioPreferences {
/// stored `mode: 0` for everybody, so an unmarked push-to-talk is not a real choice.
mode_chosen: bool,
volume: Option<f32>,
/// Notification sound switches and volume.
sounds: SoundSettings,
}
fn path() -> PathBuf {
@ -67,6 +70,10 @@ pub fn load(state: &mut UiState) {
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<()> {
@ -83,6 +90,7 @@ pub fn save(state: &UiState) -> anyhow::Result<()> {
mode: 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(())

View file

@ -85,6 +85,13 @@ export component MainWindow inherits Window {
in-out property <float> voice-bitrate: 64.0;
in-out property <int> vad-mode: 1;
in-out property <float> volume: 100.0;
in-out property <bool> sounds-enabled: true;
in-out property <float> sounds-volume: 60;
in-out property <bool> sounds-voice-room: true;
in-out property <bool> sounds-messages: true;
in-out property <bool> sounds-mentions: true;
callback sound-settings-changed(bool, float, bool, bool, bool);
callback preview-sound();
in-out property <string> language-choice: "system";
callback language-selected(string);
in-out property <string> theme-mode: "dark";
@ -331,6 +338,13 @@ export component MainWindow inherits Window {
voice-bitrate <=> root.voice-bitrate;
vad-mode <=> root.vad-mode;
volume <=> root.volume;
sounds-enabled <=> root.sounds-enabled;
sounds-volume <=> root.sounds-volume;
sounds-voice-room <=> root.sounds-voice-room;
sounds-messages <=> root.sounds-messages;
sounds-mentions <=> root.sounds-mentions;
sounds-changed => { root.sound-settings-changed(root.sounds-enabled, root.sounds-volume, root.sounds-voice-room, root.sounds-messages, root.sounds-mentions); }
preview-sound => { root.preview-sound(); }
active-tab <=> root.active-tab;
passphrase-modal-open <=> root.passphrase-modal-open;

View file

@ -1,5 +1,6 @@
import { Theme, ActionButton, Symbol } from "theme.slint";
import { AudioSettings } from "settings/audio.slint";
import { SoundSettings } from "settings/sounds.slint";
import { AppearanceSettings } from "settings/appearance.slint";
import { IdentitySettings } from "settings/identity.slint";
@ -46,6 +47,13 @@ export component SettingsWindow inherits Rectangle {
in-out property <float> voice-bitrate;
in-out property <int> vad-mode;
in-out property <float> volume;
in-out property <bool> sounds-enabled: true;
in-out property <float> sounds-volume: 60;
in-out property <bool> sounds-voice-room: true;
in-out property <bool> sounds-messages: true;
in-out property <bool> sounds-mentions: true;
callback sounds-changed();
callback preview-sound();
in-out property <string> active-tab: "audio";
in-out property <bool> passphrase-modal-open;
in-out property <string> passphrase-input;
@ -132,6 +140,15 @@ export component SettingsWindow inherits Rectangle {
volume <=> root.volume;
audio-changed => { root.audio-changed(); }
}
if root.active-tab == "audio" || (root.active-tab != "appearance" && root.active-tab != "identity") : SoundSettings {
sounds-enabled <=> root.sounds-enabled;
sounds-volume <=> root.sounds-volume;
sounds-voice-room <=> root.sounds-voice-room;
sounds-messages <=> root.sounds-messages;
sounds-mentions <=> root.sounds-mentions;
sounds-changed => { root.sounds-changed(); }
preview-sound => { root.preview-sound(); }
}
if root.active-tab == "appearance" : AppearanceSettings {
language-choice <=> root.language-choice;
language-selected(choice) => { root.language-selected(choice); }

View file

@ -0,0 +1,86 @@
import { Theme, ActionButton } from "../theme.slint";
import { AudioSlider } from "../controls.slint";
component SoundToggle inherits Rectangle {
in property <string> label;
in property <string> detail;
in-out property <bool> checked;
in property <bool> enabled: true;
callback toggled();
height: 46px;
accessible-role: checkbox;
accessible-label: root.label;
accessible-checked: root.checked;
HorizontalLayout {
spacing: 12px;
VerticalLayout {
alignment: center; spacing: 2px; horizontal-stretch: 1;
Text { text: root.label; color: root.enabled ? Theme.text-primary : Theme.text-disabled; font-size: Math.max(Theme.font-size - 2px, 11px); }
Text { text: root.detail; color: Theme.text-muted; font-size: Math.max(Theme.font-size - 4px, 11px); overflow: elide; }
}
ActionButton {
text: root.checked ? "✓" : "○"; width: 34px; height: 30px; quiet: true;
clicked => { if root.enabled { root.checked = !root.checked; root.toggled(); } }
}
}
}
export component SoundSettings inherits Rectangle {
in-out property <bool> sounds-enabled: true;
in-out property <float> sounds-volume: 60;
in-out property <bool> sounds-voice-room: true;
in-out property <bool> sounds-messages: true;
in-out property <bool> sounds-mentions: true;
callback sounds-changed();
callback preview-sound();
VerticalLayout {
spacing: 0;
alignment: start;
Rectangle { height: 1px; background: Theme.border; }
Rectangle { height: 26px; }
Text { text: @tr("Sounds"); color: Theme.text-primary; font-size: Theme.font-size + 2px; font-weight: 600; }
Text { text: @tr("Short tones for voice rooms, messages and connection changes. They stay silent while you are deafened."); color: Theme.text-muted; font-size: Math.max(Theme.font-size - 3px, 11px); wrap: word-wrap; }
Rectangle { height: 14px; }
SoundToggle {
label: @tr("Play sounds");
detail: @tr("Master switch, also covers connection and error tones");
checked <=> root.sounds-enabled;
toggled => { root.sounds-changed(); }
}
SoundToggle {
label: @tr("Voice room");
detail: @tr("Joining, leaving, microphone and sound on or off");
enabled: root.sounds-enabled;
checked <=> root.sounds-voice-room;
toggled => { root.sounds-changed(); }
}
SoundToggle {
label: @tr("Messages");
detail: @tr("New messages in chats you are not looking at");
enabled: root.sounds-enabled;
checked <=> root.sounds-messages;
toggled => { root.sounds-changed(); }
}
SoundToggle {
label: @tr("Mentions");
detail: @tr("Someone mentions you or everyone");
enabled: root.sounds-enabled;
checked <=> root.sounds-mentions;
toggled => { root.sounds-changed(); }
}
Rectangle { height: 10px; }
HorizontalLayout {
spacing: 20px;
Text { text: @tr("Sound volume"); color: Theme.text-primary; font-size: Theme.font-size - 1px; font-weight: 600; horizontal-stretch: 1; }
Text { text: Math.round(root.sounds-volume) + "%"; color: Theme.text-secondary; font-size: Math.max(Theme.font-size - 2px, 11px); horizontal-alignment: right; }
}
Rectangle { height: 10px; }
AudioSlider { minimum: 0; maximum: 100; value <=> root.sounds-volume; changed(value) => { root.sounds-changed(); } }
Rectangle { height: 12px; }
HorizontalLayout {
ActionButton { text: @tr("Play a test sound"); quiet: true; clicked => { root.preview-sound(); } }
Rectangle { horizontal-stretch: 1; }
}
Rectangle { height: 26px; }
}
}

217
src/ui/sounds.rs Normal file
View file

@ -0,0 +1,217 @@
//! Maps application events to notification sounds and plays them.
use std::sync::OnceLock;
use vnox_client::audio::sounds::{self, SoundEvent, SoundPlayer};
use vnox_client::net::NetEvent;
use crate::AppState;
use crate::ui::state::{ConnState, UiState, VoiceUiState};
fn player() -> &'static SoundPlayer {
static PLAYER: OnceLock<SoundPlayer> = OnceLock::new();
PLAYER.get_or_init(SoundPlayer::default)
}
/// Plays `event` if the sound settings and the deafen state allow it.
pub fn play(state: &AppState, event: SoundEvent) {
if let Some(event) = sounds::select(event, &state.ui.sounds, state.ui.deafen) {
player().play(
event,
state.ui.sounds.gain(),
state.audio.as_ref(),
&state.ui.output_device_id,
);
}
}
/// Plays the test sound from the settings page, ignoring the category switches but not the
/// master switch or volume.
pub fn preview(state: &AppState) {
let mut settings = state.ui.sounds;
settings.voice_room = true;
if let Some(event) = sounds::select(SoundEvent::PeerJoined, &settings, false) {
player().play(
event,
settings.gain(),
state.audio.as_ref(),
&state.ui.output_device_id,
);
}
}
/// True when the person is looking at the text channel `channel_id`.
fn viewing_channel(ui: &UiState, channel_id: &str) -> bool {
!ui.showing_voice
&& !ui.browsing_dms
&& ui.active_dm_id.is_none()
&& ui.active_channel.as_deref() == Some(channel_id)
}
/// The sound a network event should make, before settings are applied.
pub fn sound_for_event(
ui: &UiState,
own_id: &str,
own_nickname: &str,
event: &NetEvent,
) -> Option<SoundEvent> {
match event {
NetEvent::Connected { .. } => Some(SoundEvent::Connected),
// Only a connection that was established can be lost; failed attempts stay quiet.
NetEvent::Disconnected { .. } if matches!(ui.conn, ConnState::Connected { .. }) => {
Some(SoundEvent::ConnectionLost)
}
NetEvent::Error { .. } => Some(SoundEvent::Error),
NetEvent::UserJoin {
channel_id,
user_id,
..
} if user_id != own_id && in_current_voice_room(ui, channel_id) => {
Some(SoundEvent::PeerJoined)
}
NetEvent::UserLeave {
channel_id,
user_id,
} if user_id != own_id && in_current_voice_room(ui, channel_id) => {
Some(SoundEvent::PeerLeft)
}
NetEvent::ChatMessage {
channel_id,
sender_id,
content,
..
} => sounds::message_event(
sender_id == own_id,
sounds::is_mention(content, own_nickname),
viewing_channel(ui, channel_id),
),
NetEvent::DmMessage {
dm_id, sender_id, ..
} => sounds::message_event(
sender_id == own_id,
false,
!ui.showing_voice && ui.active_dm_id.as_deref() == Some(dm_id.as_str()),
),
_ => None,
}
}
fn in_current_voice_room(ui: &UiState, channel_id: &str) -> bool {
matches!(&ui.voice, VoiceUiState::Active { channel } if channel == channel_id)
}
#[cfg(test)]
mod tests {
use super::*;
fn connected_ui() -> UiState {
UiState {
conn: ConnState::Connected {
session_id: "s".into(),
node_name: "n".into(),
},
active_channel: Some("general".into()),
..UiState::default()
}
}
fn chat(channel: &str, sender: &str, content: &str) -> NetEvent {
NetEvent::ChatMessage {
message_id: "m".into(),
channel_id: channel.into(),
sender_id: sender.into(),
nickname: sender.into(),
content: content.into(),
timestamp: 0,
reply_to: None,
}
}
#[test]
fn messages_sound_only_outside_the_viewed_channel() {
let ui = connected_ui();
assert_eq!(
sound_for_event(&ui, "me", "me", &chat("general", "bob", "hi")),
None
);
assert_eq!(
sound_for_event(&ui, "me", "me", &chat("random", "bob", "hi")),
Some(SoundEvent::Message)
);
assert_eq!(
sound_for_event(&ui, "me", "me", &chat("random", "me", "hi")),
None
);
}
#[test]
fn viewing_the_voice_page_does_not_count_as_viewing_the_channel() {
let ui = UiState {
showing_voice: true,
..connected_ui()
};
assert_eq!(
sound_for_event(&ui, "me", "me", &chat("general", "bob", "hi")),
Some(SoundEvent::Message)
);
}
#[test]
fn mentions_use_the_mention_sound_even_in_the_viewed_channel() {
let ui = connected_ui();
assert_eq!(
sound_for_event(&ui, "me", "Alice", &chat("general", "bob", "hey @alice")),
Some(SoundEvent::Mention)
);
}
#[test]
fn peers_are_announced_only_in_the_voice_room_you_are_in() {
let ui = UiState {
voice: VoiceUiState::Active {
channel: "lounge".into(),
},
..connected_ui()
};
let join = |channel: &str, user: &str| NetEvent::UserJoin {
channel_id: channel.into(),
user_id: user.into(),
nickname: user.into(),
};
assert_eq!(
sound_for_event(&ui, "me", "me", &join("lounge", "bob")),
Some(SoundEvent::PeerJoined)
);
assert_eq!(
sound_for_event(&ui, "me", "me", &join("lounge", "me")),
None
);
assert_eq!(
sound_for_event(&ui, "me", "me", &join("general", "bob")),
None
);
let leave = NetEvent::UserLeave {
channel_id: "lounge".into(),
user_id: "bob".into(),
};
assert_eq!(
sound_for_event(&ui, "me", "me", &leave),
Some(SoundEvent::PeerLeft)
);
}
#[test]
fn only_an_established_connection_can_be_lost() {
let lost = NetEvent::Disconnected {
reason: "x".into(),
will_retry: true,
};
assert_eq!(
sound_for_event(&connected_ui(), "me", "me", &lost),
Some(SoundEvent::ConnectionLost)
);
assert_eq!(
sound_for_event(&UiState::default(), "me", "me", &lost),
None
);
}
}

View file

@ -43,6 +43,7 @@ impl Default for UiState {
output_device: 0,
output_device_id: String::new(),
output_volume: 90.0,
sounds: Default::default(),
device_lists: Default::default(),
vad_mode_chosen: false,

View file

@ -90,6 +90,7 @@ pub struct UiState {
/// Persisted output device id; empty is the system default.
pub output_device_id: String,
pub output_volume: f32,
pub sounds: vnox_client::audio::sounds::SoundSettings,
/// 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.

View file

@ -9,6 +9,14 @@ use crate::AppState;
pub fn process_net_events(state: &mut AppState) {
while let Some(event) = state.net.try_recv() {
if let Some(sound) = crate::ui::sounds::sound_for_event(
&state.ui,
&state.identity.pubkey_hex,
&state.identity.nickname,
&event,
) {
crate::ui::sounds::play(state, sound);
}
match event {
NetEvent::VoiceRevoked { channel_id } => {
if state.voice_channel.as_deref() == Some(channel_id.as_str())
@ -895,6 +903,8 @@ pub fn start_voice(state: &mut AppState, channel_id: &str) {
match vnox_client::audio::start(cfg.clone()) {
Ok(pipeline) => {
tracing::info!("voice active on channel {channel_id}");
let newly_joined = !matches!(&state.ui.voice,
VoiceUiState::Active { channel } if channel == channel_id);
state.audio = Some(pipeline);
state.voice_channel = Some(channel_id.to_string());
state.last_pipeline_config = Some(cfg);
@ -905,6 +915,9 @@ pub fn start_voice(state: &mut AppState, channel_id: &str) {
state.ui.voice_joined_at = Some(std::time::Instant::now());
}
state.ui.status_msg = None;
if newly_joined {
crate::ui::sounds::play(state, vnox_client::audio::sounds::SoundEvent::VoiceJoined);
}
}
Err(e) => {
tracing::warn!("audio start failed: {e}");
@ -983,6 +996,7 @@ fn report_audio_issues(state: &mut AppState) {
),
};
state.ui.status_msg = Some(message);
crate::ui::sounds::play(state, vnox_client::audio::sounds::SoundEvent::Error);
}
if dead {
let reason = state