voelin-audio: dim everyone else while a priority speaker talks
The server sends virtualserver_priority_speaker_dimm_modificator (-18 dB by default) but nothing read it. ServerDetails carries it now, the session tells the audio thread the priority speakers when they or the value change, and the mixer dims every other queue (watched streams too) while an unmuted priority speaker has one, fading sample by sample over 5 frames. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AVRENKftQqGoEeswiR9TDQ
This commit is contained in:
1 parent
42639fd206
commit
68dd48d607
8 files changed
+364
-20
No files matched your search
@@ -42,6 +42,12 @@ pub(crate) enum AudioIn {
|
||||
},
|
||||
/// The client left; client ids are reused, so forget its volume.
|
||||
ClientLeft(ClientId),
|
||||
/// The priority speakers: while one talks, everyone else (streams too)
|
||||
/// is dimmed by `dimm_db` (the server's setting, e.g. -18).
|
||||
PrioritySpeakers {
|
||||
clients: Vec<ClientId>,
|
||||
dimm_db: f32,
|
||||
},
|
||||
/// An Opus frame (48 kHz) of a watched stream; `time` is its RTP time
|
||||
/// on the 48 kHz clock.
|
||||
StreamAudio {
|
||||
@@ -208,6 +214,9 @@ impl Pipeline {
|
||||
AudioIn::ClientVolume { client, volume } => self.mixer.set_volume(client, volume),
|
||||
AudioIn::ClientMuted { client, muted } => self.mixer.set_muted(client, muted),
|
||||
AudioIn::ClientLeft(client) => self.mixer.forget(client),
|
||||
AudioIn::PrioritySpeakers { clients, dimm_db } => {
|
||||
self.mixer.set_priority(clients, dimm_db);
|
||||
}
|
||||
AudioIn::StreamAudio { stream, time, data } => self.stream_packet(stream, time, &data),
|
||||
AudioIn::StreamVolume { stream, volume } => {
|
||||
if let Some(s) = self.streams.get(&stream) {
|
||||
@@ -547,7 +556,7 @@ mod devices {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use voelin_audio::pcm::{SAMPLE_RATE, energy, sine, white_noise};
|
||||
use voelin_audio::pcm::{SAMPLE_RATE, energy, goertzel_power, sine, white_noise};
|
||||
use voelin_audio::{ProcessingSettings, VadSettings};
|
||||
|
||||
use super::*;
|
||||
@@ -668,6 +677,44 @@ mod tests {
|
||||
assert!(!p.mixer.is_muted(ClientId(3)));
|
||||
}
|
||||
|
||||
/// While a priority speaker talks, another talker and a watched stream
|
||||
/// are dimmed by the server's setting.
|
||||
#[test]
|
||||
fn priority_speaker() {
|
||||
let mut p = pipeline(TransmitMode::PushToTalk);
|
||||
p.handle(AudioIn::PrioritySpeakers { clients: vec![ClientId(2)], dimm_db: -12.0 });
|
||||
let mut voice = VoiceEncoder::new(VoiceCodec::Voice).unwrap();
|
||||
let mut priority = VoiceEncoder::new(VoiceCodec::Voice).unwrap();
|
||||
let mut music = VoiceEncoder::new(VoiceCodec::Music).unwrap();
|
||||
let tone = sine(700.0, 2.0, 0.3);
|
||||
let stream = pcm::from_mono(&sine(1100.0, 2.0, 0.3), 2);
|
||||
let other = sine(1700.0, 1.0, 0.3);
|
||||
let mut out = Vec::new();
|
||||
for (i, frame) in tone.chunks_exact(FRAME_SAMPLES).enumerate() {
|
||||
p.handle(incoming(1, i as u16, voice.encode_to_bytes(frame).unwrap()));
|
||||
let frame = &stream[i * FRAME_SAMPLES * 2..(i + 1) * FRAME_SAMPLES * 2];
|
||||
let data: Arc<[u8]> = music.encode_to_bytes(frame).unwrap().into();
|
||||
let time = i as u64 * OPUS_FRAME;
|
||||
p.handle(AudioIn::StreamAudio { stream: "s".into(), time, data });
|
||||
// The priority speaker talks during the second second.
|
||||
if let Some(j) = i.checked_sub(50) {
|
||||
let frame = &other[j * FRAME_SAMPLES..(j + 1) * FRAME_SAMPLES];
|
||||
p.handle(incoming(2, j as u16, priority.encode_to_bytes(frame).unwrap()));
|
||||
}
|
||||
out.extend(p.playback_frame());
|
||||
}
|
||||
// The second half of each second.
|
||||
let half = SAMPLE_RATE as usize / 2;
|
||||
let level = |second: usize, freq: f32| {
|
||||
let from = second * 2 * half + half;
|
||||
goertzel_power(&out[from..from + half], freq, SAMPLE_RATE)
|
||||
};
|
||||
for freq in [700.0, 1100.0] {
|
||||
let ratio_db = 10.0 * (level(1, freq) / level(0, freq)).log10();
|
||||
assert!((ratio_db + 12.0).abs() < 1.0, "{freq} Hz dimmed by {ratio_db} dB");
|
||||
}
|
||||
}
|
||||
|
||||
/// End to end: a remote talker is played, the speakers leak into the
|
||||
/// microphone 30 ms later, and the echo canceller (fed with what was
|
||||
/// played) removes it from what we would send.
|
||||
|
||||
@@ -60,6 +60,8 @@ fn server_details(server: &data::Server, uid: Option<&str>) -> ServerDetails {
|
||||
platform: server.platform.clone(),
|
||||
version: server.version.clone(),
|
||||
max_clients: server.max_clients,
|
||||
// `as` saturates (and turns NaN into 0); the mixer clamps it.
|
||||
priority_speaker_dimm_db: server.priority_speaker_dimm_modificator.round() as i32,
|
||||
default_server_group: Some(server.default_server_group.0),
|
||||
default_channel_group: Some(server.default_channel_group.0),
|
||||
}
|
||||
@@ -265,5 +267,6 @@ mod tests {
|
||||
assert_eq!(p.server.banner_gfx_interval_s, 60);
|
||||
assert_eq!(p.server.banner_mode, BannerMode::KeepAspect);
|
||||
assert_eq!(BannerMode::from_wire("7"), BannerMode::NoAdjust);
|
||||
assert_eq!(p.server.priority_speaker_dimm_db, -18);
|
||||
}
|
||||
}
|
||||
@@ -234,6 +234,9 @@ struct Session {
|
||||
banner_reload: Option<(String, u64, tokio::task::AbortHandle)>,
|
||||
/// Contact volume and mute applied per client.
|
||||
contact_audio: HashMap<u16, (f32, bool)>,
|
||||
/// Priority speakers and the server's dimming (dB) as told to the
|
||||
/// audio thread.
|
||||
priority_audio: (BTreeSet<u16>, i32),
|
||||
/// Friends' client ids, as last told to the streams.
|
||||
stream_friends: BTreeSet<u16>,
|
||||
/// The server's details and groups as last reported.
|
||||
@@ -266,6 +269,7 @@ impl Session {
|
||||
image_retries: HashMap::new(),
|
||||
banner_reload: None,
|
||||
contact_audio: HashMap::new(),
|
||||
priority_audio: Default::default(),
|
||||
stream_friends: BTreeSet::new(),
|
||||
details: None,
|
||||
groups: None,
|
||||
@@ -824,6 +828,7 @@ impl Session {
|
||||
self.icons.clear();
|
||||
self.pictures.clear();
|
||||
self.contact_audio.clear();
|
||||
self.priority_audio = Default::default();
|
||||
self.stream_friends.clear();
|
||||
self.details = None;
|
||||
self.groups = None;
|
||||
@@ -1139,6 +1144,19 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tell the audio thread who the priority speakers are and how much they
|
||||
/// dim the others, when that changed (voice presence).
|
||||
fn apply_priority(&mut self, p: &Presence) {
|
||||
let Some(a) = &self.audio else { return };
|
||||
let clients = p.clients.values().filter(|c| c.priority_speaker).map(|c| c.id).collect();
|
||||
let wanted = (clients, p.server.priority_speaker_dimm_db);
|
||||
if wanted != self.priority_audio {
|
||||
let clients = wanted.0.iter().map(|c| ClientId(*c)).collect();
|
||||
a.send(AudioIn::PrioritySpeakers { clients, dimm_db: wanted.1 as f32 });
|
||||
self.priority_audio = wanted;
|
||||
}
|
||||
}
|
||||
|
||||
/// Report the server's details and groups when they changed.
|
||||
fn report_details(&mut self, p: &Presence) {
|
||||
if self.details.as_deref() != Some(&p.server) {
|
||||
@@ -1649,6 +1667,7 @@ impl Session {
|
||||
self.report_details(&p);
|
||||
self.fetch_images(&p);
|
||||
self.apply_contacts(&p);
|
||||
self.apply_priority(&p);
|
||||
self.voice_presence = Some(*p);
|
||||
self.publish_presence();
|
||||
}
|
||||
@@ -2273,4 +2292,44 @@ mod banner_tests {
|
||||
tokio::task::yield_now().await;
|
||||
assert!(timer.is_finished());
|
||||
}
|
||||
|
||||
/// The priority speakers and the server's dimming reach the audio thread
|
||||
/// once, and again when either changes.
|
||||
#[tokio::test]
|
||||
async fn priority_speakers_reach_the_audio_thread_when_changed() {
|
||||
let (mut session, _) = session("priority");
|
||||
let (audio, rx) = AudioHandle::channel();
|
||||
session.audio = Some(audio);
|
||||
let mut presence = Presence::default();
|
||||
presence.server.priority_speaker_dimm_db = -18;
|
||||
for (id, priority_speaker) in [(2, false), (5, true)] {
|
||||
let client = voelin_model::ClientInfo { id, priority_speaker, ..Default::default() };
|
||||
presence.clients.insert(id, client);
|
||||
}
|
||||
let sent = || -> Vec<(Vec<u16>, f32)> {
|
||||
rx.try_iter()
|
||||
.filter_map(|msg| match msg {
|
||||
AudioIn::PrioritySpeakers { clients, dimm_db } => {
|
||||
Some((clients.iter().map(|c| c.0).collect(), dimm_db))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
session.apply_priority(&presence);
|
||||
assert_eq!(sent(), [(vec![5], -18.0)]);
|
||||
session.apply_priority(&presence);
|
||||
assert!(sent().is_empty());
|
||||
presence.server.priority_speaker_dimm_db = -30;
|
||||
session.apply_priority(&presence);
|
||||
assert_eq!(sent(), [(vec![5], -30.0)]);
|
||||
presence.clients.get_mut(&5).unwrap().priority_speaker = false;
|
||||
session.apply_priority(&presence);
|
||||
assert_eq!(sent(), [(vec![], -30.0)]);
|
||||
// A new connection has a new audio thread: told again.
|
||||
session.forget_images();
|
||||
session.apply_priority(&presence);
|
||||
assert_eq!(sent(), [(vec![], -30.0)]);
|
||||
session.stop_all();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user