forge
Explore
Log in

Don't you dare go Hollow

Draw voice waves from per frame levels scaled to each speaker

Browse files at this commit

ebf01d591ac996f2b8a4fc7754e4f521d7b4efccAhmet Kaan GümüşParent: 0906d1e
  • ui
    • draw voice waves from per frame levels scaled to each speaker
Show full message
💄 ui: draw voice waves from per frame levels scaled to each speaker
crates/client/src/gui/chat.rsmodified+1 -1
@@ -256,7 +256,7 @@ pub struct Chat {
256 const RING_LIMIT: usize = 15;256 const RING_LIMIT: usize = 15;
257 const RING_INTERVAL: Duration = Duration::from_secs(2);257 const RING_INTERVAL: Duration = Duration::from_secs(2);
258 const VOICE_SAMPLE_INTERVAL: Duration = Duration::from_secs(1);258 const VOICE_SAMPLE_INTERVAL: Duration = Duration::from_secs(1);
259-const WAVE_INTERVAL: Duration = Duration::from_millis(100);259+const WAVE_INTERVAL: Duration = Duration::from_millis(50);
260 const DEFAULT_USER_VOLUME: f32 = 100.0;260 const DEFAULT_USER_VOLUME: f32 = 100.0;
261 261
262 pub struct UserVolume {262 pub struct UserVolume {
crates/client/src/gui/wave.rsmodified+54 -27
@@ -4,39 +4,64 @@ use gpui_kit::{Div, Hsla, ParentElement, Styled, div, px};
4 4
5 use crate::network::Levels;5 use crate::network::Levels;
6 6
7-pub const BARS: usize = 10;7+pub const BARS: usize = 16;
8-const FLOOR_DB: f32 = -50.0;8+const SILENT_BARS: usize = 2;
9+const DECAY: f32 = 0.985;
10+const PEAK_FLOOR: f32 = 0.02;
11+const CURVE: f32 = 0.7;
9 const AUDIBLE: f32 = 0.1;12 const AUDIBLE: f32 = 0.1;
13+const BAR_WIDTH: f32 = 2.;
14+const MIN_BAR: f32 = 1.5;
15+
16+pub struct Wave {
17+ bars: VecDeque<f32>,
18+ peak: f32,
19+}
20+
21+impl Wave {
22+ fn new() -> Self {
23+ Self {
24+ bars: VecDeque::from(vec![0.0; BARS]),
25+ peak: PEAK_FLOOR,
26+ }
27+ }
28+
29+ fn push(&mut self, level: f32) {
30+ self.peak = (self.peak * DECAY).max(level).max(PEAK_FLOOR);
31+ self.bars.pop_front();
32+ self.bars
33+ .push_back((level / self.peak).clamp(0.0, 1.0).powf(CURVE));
34+ }
35+
36+ fn silent(&self) -> bool {
37+ self.bars.iter().all(|bar| *bar == 0.0)
38+ }
39+}
10 40
11 #[derive(Default)]41 #[derive(Default)]
12 pub struct Waves {42 pub struct Waves {
13 pub speaking: HashSet<String>,43 pub speaking: HashSet<String>,
14- voice: HashMap<String, VecDeque<f32>>,44+ voice: HashMap<String, Wave>,
15- share: HashMap<String, VecDeque<f32>>,45+ share: HashMap<String, Wave>,
16 }46 }
17 47
18-fn height(level: f32) -> f32 {48+fn advance(waves: &mut HashMap<String, Wave>, levels: Vec<(String, Vec<f32>)>) -> bool {
19- ((20.0 * level.max(f32::MIN_POSITIVE).log10() - FLOOR_DB) / -FLOOR_DB).clamp(0.0, 1.0)49+ let mut levels: HashMap<String, Vec<f32>> = levels.into_iter().collect();
20-}
21-
22-fn advance(waves: &mut HashMap<String, VecDeque<f32>>, levels: Vec<(String, f32)>) -> bool {
23- let mut levels: HashMap<String, f32> = levels.into_iter().collect();
24 let ids: HashSet<String> = waves.keys().chain(levels.keys()).cloned().collect();50 let ids: HashSet<String> = waves.keys().chain(levels.keys()).cloned().collect();
25- let mut changed = false;
26 for id in ids {51 for id in ids {
27- let level = levels.remove(&id).map_or(0.0, height);52+ let incoming = levels
28- let history = waves53+ .remove(&id)
29- .entry(id.clone())54+ .filter(|incoming| !incoming.is_empty())
30- .or_insert_with(|| VecDeque::from(vec![0.0; BARS]));55+ .unwrap_or_else(|| vec![0.0; SILENT_BARS]);
31- history.pop_front();56+ let wave = waves.entry(id.clone()).or_insert_with(Wave::new);
32- history.push_back(level);57+ for level in incoming {
33- changed |= history.iter().any(|bar| *bar > 0.0) || level != 0.0;58+ wave.push(level);
34- if history.iter().all(|bar| *bar == 0.0) {59+ }
60+ if wave.silent() {
35 waves.remove(&id);61 waves.remove(&id);
36- changed = true;
37 }62 }
38 }63 }
39- changed64+ !waves.is_empty()
40 }65 }
41 66
42 impl Waves {67 impl Waves {
@@ -48,15 +73,16 @@ impl Waves {
48 let speaking: HashSet<String> = voice.iter().map(|(id, _)| id.clone()).collect();73 let speaking: HashSet<String> = voice.iter().map(|(id, _)| id.clone()).collect();
49 let mut changed = speaking != self.speaking;74 let mut changed = speaking != self.speaking;
50 self.speaking = speaking;75 self.speaking = speaking;
76+ let had = !self.voice.is_empty() || !self.share.is_empty();
51 changed |= advance(&mut self.voice, voice);77 changed |= advance(&mut self.voice, voice);
52 78
53 let mut share = levels.watching;79 let mut share = levels.watching;
54 share.extend(80 share.extend(
55 me.zip(levels.sharing)81 me.zip(levels.sharing)
56- .map(|(id, level)| (id.to_string(), level)),82+ .map(|(id, levels)| (id.to_string(), levels)),
57 );83 );
58 changed |= advance(&mut self.share, share);84 changed |= advance(&mut self.share, share);
59- changed85+ changed || had
60 }86 }
61 87
62 pub fn clear(&mut self) {88 pub fn clear(&mut self) {
@@ -64,13 +90,14 @@ impl Waves {
64 }90 }
65 91
66 pub fn voice(&self, id: &str) -> Option<&VecDeque<f32>> {92 pub fn voice(&self, id: &str) -> Option<&VecDeque<f32>> {
67- self.voice.get(id)93+ self.voice.get(id).map(|wave| &wave.bars)
68 }94 }
69 95
70 pub fn share(&self, id: &str) -> Option<&VecDeque<f32>> {96 pub fn share(&self, id: &str) -> Option<&VecDeque<f32>> {
71 self.share97 self.share
72 .get(id)98 .get(id)
73- .filter(|history| history.iter().any(|bar| *bar >= AUDIBLE))99+ .filter(|wave| wave.peak > PEAK_FLOOR || wave.bars.iter().any(|bar| *bar >= AUDIBLE))
100+ .map(|wave| &wave.bars)
74 }101 }
75 }102 }
76 103
@@ -82,8 +109,8 @@ pub fn wave(history: &VecDeque<f32>, size: f32, color: Hsla) -> Div {
82 .h(px(size))109 .h(px(size))
83 .children(history.iter().map(|bar| {110 .children(history.iter().map(|bar| {
84 div()111 div()
85- .w(px(2.))112+ .w(px(BAR_WIDTH))
86- .h(px((size * bar).max(2.)))113+ .h(px((size * bar).max(MIN_BAR)))
87 .rounded(px(1.))114 .rounded(px(1.))
88 .bg(color)115 .bg(color)
89 }))116 }))
crates/client/src/media/meter.rsmodified+15 -8
@@ -1,7 +1,6 @@
1-use std::sync::{1+use std::sync::{Arc, Mutex, PoisonError};
2- Arc,2+
3- atomic::{AtomicU32, Ordering},3+pub const LEVEL_CAPACITY: usize = 32;
4-};
5 4
6 pub fn rms(samples: &[f32]) -> f32 {5 pub fn rms(samples: &[f32]) -> f32 {
7 if samples.is_empty() {6 if samples.is_empty() {
@@ -10,15 +9,23 @@ pub fn rms(samples: &[f32]) -> f32 {
10 (samples.iter().map(|sample| sample * sample).sum::<f32>() / samples.len() as f32).sqrt()9 (samples.iter().map(|sample| sample * sample).sum::<f32>() / samples.len() as f32).sqrt()
11 }10 }
12 11
12+pub fn push_level(levels: &mut Vec<f32>, level: f32) {
13+ if levels.len() >= LEVEL_CAPACITY {
14+ levels.remove(0);
15+ }
16+ levels.push(level);
17+}
18+
13 #[derive(Debug, Clone, Default)]19 #[derive(Debug, Clone, Default)]
14-pub struct Meter(Arc<AtomicU32>);20+pub struct Meter(Arc<Mutex<Vec<f32>>>);
15 21
16 impl Meter {22 impl Meter {
17 pub fn record(&self, samples: &[f32]) {23 pub fn record(&self, samples: &[f32]) {
18- self.0.fetch_max(rms(samples).to_bits(), Ordering::Relaxed);24+ let mut levels = self.0.lock().unwrap_or_else(PoisonError::into_inner);
25+ push_level(&mut levels, rms(samples));
19 }26 }
20 27
21- pub fn take(&self) -> f32 {28+ pub fn take(&self) -> Vec<f32> {
22- f32::from_bits(self.0.swap(0, Ordering::Relaxed))29+ std::mem::take(&mut *self.0.lock().unwrap_or_else(PoisonError::into_inner))
23 }30 }
24 }31 }
crates/client/src/network.rsmodified+3 -3
@@ -85,9 +85,9 @@ type SharedShareSettings = Arc<Mutex<ShareSettings>>;
85 85
86 #[derive(Default)]86 #[derive(Default)]
87 pub struct Levels {87 pub struct Levels {
88- pub voice: Vec<(String, f32)>,88+ pub voice: Vec<(String, Vec<f32>)>,
89- pub watching: Vec<(String, f32)>,89+ pub watching: Vec<(String, Vec<f32>)>,
90- pub sharing: Option<f32>,90+ pub sharing: Option<Vec<f32>>,
91 }91 }
92 92
93 #[derive(Default)]93 #[derive(Default)]
crates/client/src/share/session.rsmodified+4 -4
@@ -194,8 +194,8 @@ impl Sharing {
194 Vec::new()194 Vec::new()
195 }195 }
196 196
197- pub fn level(&self) -> f32 {197+ pub fn level(&self) -> Vec<f32> {
198- 0.0198+ Vec::new()
199 }199 }
200 }200 }
201 201
@@ -325,7 +325,7 @@ impl Sharing {
325 lock(&self.sent).take()325 lock(&self.sent).take()
326 }326 }
327 327
328- pub fn level(&self) -> f32 {328+ pub fn level(&self) -> Vec<f32> {
329 self.meter.take()329 self.meter.take()
330 }330 }
331 }331 }
@@ -601,7 +601,7 @@ impl Watching {
601 self.emitted = None;601 self.emitted = None;
602 }602 }
603 603
604- pub fn level(&self) -> f32 {604+ pub fn level(&self) -> Vec<f32> {
605 self.meter.take()605 self.meter.take()
606 }606 }
607 607
crates/client/src/voice/engine.rsmodified+12 -10
@@ -12,7 +12,7 @@ use common::{
12 use ec_opus::{Application, Bandwidth, Decoder, Encoder};12 use ec_opus::{Application, Bandwidth, Decoder, Encoder};
13 13
14 use crate::{14 use crate::{
15- media::meter::rms,15+ media::meter::{push_level, rms},
16 settings::{Activation, Profile},16 settings::{Activation, Profile},
17 };17 };
18 18
@@ -78,7 +78,7 @@ struct Speaker {
78 previous_transit: Option<f32>,78 previous_transit: Option<f32>,
79 jitter_ms: f32,79 jitter_ms: f32,
80 highest: u32,80 highest: u32,
81- peak: f32,81+ levels: Vec<f32>,
82 }82 }
83 83
84 #[derive(Debug, Clone, Copy, Default)]84 #[derive(Debug, Clone, Copy, Default)]
@@ -105,7 +105,7 @@ impl Speaker {
105 previous_transit: None,105 previous_transit: None,
106 jitter_ms: 0.0,106 jitter_ms: 0.0,
107 highest: sequence.wrapping_sub(1),107 highest: sequence.wrapping_sub(1),
108- peak: 0.0,108+ levels: Vec::new(),
109 }109 }
110 }110 }
111 111
@@ -157,7 +157,7 @@ impl Speaker {
157 self.previous_transit = None;157 self.previous_transit = None;
158 return;158 return;
159 };159 };
160- self.peak = self.peak.max(rms(&frame));160+ push_level(&mut self.levels, rms(&frame));
161 self.pending.extend(frame);161 self.pending.extend(frame);
162 }162 }
163 *sample += self.pending.pop_front().unwrap_or(0.0) * gain;163 *sample += self.pending.pop_front().unwrap_or(0.0) * gain;
@@ -180,7 +180,7 @@ pub struct Engine {
180 hangover: u32,180 hangover: u32,
181 sending: bool,181 sending: bool,
182 level_db: f32,182 level_db: f32,
183- sent_peak: f32,183+ sent_levels: Vec<f32>,
184 volumes: HashMap<String, f32>,184 volumes: HashMap<String, f32>,
185 speakers: HashMap<String, Speaker>,185 speakers: HashMap<String, Speaker>,
186 sent_bytes: usize,186 sent_bytes: usize,
@@ -213,7 +213,7 @@ impl Engine {
213 hangover: 0,213 hangover: 0,
214 sending: false,214 sending: false,
215 level_db: SILENT_DB,215 level_db: SILENT_DB,
216- sent_peak: 0.0,216+ sent_levels: Vec::new(),
217 volumes: HashMap::new(),217 volumes: HashMap::new(),
218 speakers: HashMap::new(),218 speakers: HashMap::new(),
219 sent_bytes: 0,219 sent_bytes: 0,
@@ -253,7 +253,7 @@ impl Engine {
253 }253 }
254 254
255 if speaking {255 if speaking {
256- self.sent_peak = self.sent_peak.max(loud);256+ push_level(&mut self.sent_levels, loud);
257 }257 }
258 let mut samples = *frame;258 let mut samples = *frame;
259 if speaking && !was_sending {259 if speaking && !was_sending {
@@ -304,7 +304,9 @@ impl Engine {
304 }304 }
305 speaker.last_heard = now;305 speaker.last_heard = now;
306 if payload.is_empty() {306 if payload.is_empty() {
307- speaker.peak = speaker.peak.max(std::mem::take(&mut self.sent_peak));307+ for level in std::mem::take(&mut self.sent_levels) {
308+ push_level(&mut speaker.levels, level);
309+ }
308 return;310 return;
309 }311 }
310 if sequence < speaker.next {312 if sequence < speaker.next {
@@ -349,13 +351,13 @@ impl Engine {
349 }351 }
350 }352 }
351 353
352- pub fn levels(&mut self) -> Vec<(String, f32)> {354+ pub fn levels(&mut self) -> Vec<(String, Vec<f32>)> {
353 let now = Instant::now();355 let now = Instant::now();
354 356
355 self.speakers357 self.speakers
356 .iter_mut()358 .iter_mut()
357 .filter(|(_, speaker)| now.duration_since(speaker.last_heard) <= SPEAKING_WINDOW)359 .filter(|(_, speaker)| now.duration_since(speaker.last_heard) <= SPEAKING_WINDOW)
358- .map(|(user_id, speaker)| (user_id.clone(), std::mem::take(&mut speaker.peak)))360+ .map(|(user_id, speaker)| (user_id.clone(), std::mem::take(&mut speaker.levels)))
359 .collect()361 .collect()
360 }362 }
361 363
crates/client/src/voice/mod.rsmodified+1 -1
@@ -303,7 +303,7 @@ impl Voice {
303 self.with_engine((), |engine| engine.receive(datagram));303 self.with_engine((), |engine| engine.receive(datagram));
304 }304 }
305 305
306- pub fn levels(&self) -> Vec<(String, f32)> {306+ pub fn levels(&self) -> Vec<(String, Vec<f32>)> {
307 self.with_engine(Vec::new(), Engine::levels)307 self.with_engine(Vec::new(), Engine::levels)
308 }308 }
309 309