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:
parent
bf485720f5
commit
eed3e28c63
19 changed files with 1142 additions and 4 deletions
217
src/ui/sounds.rs
Normal file
217
src/ui/sounds.rs
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue