forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

wave.rs178 lines, 5.3 KiBHistoryRawDownload
1use std::collections::{HashMap, HashSet, VecDeque};
2
3use common::model::Id;
4use gpui_kit::{Hsla, IntoElement, PathBuilder, Styled, canvas, point, px};
5
6use super::style::stroke;
7
8use crate::net::Levels;
9
10const ATTACK: f32 = 0.6;
11const GRAVITY: f32 = 0.012;
12const HOLD_TICKS: u8 = 3;
13const GONE: f32 = 0.02;
14
15pub struct Wave {
16 bars: VecDeque<f32>,
17 falls: Vec<f32>,
18 target: Vec<f32>,
19 stale: u8,
20}
21
22impl Wave {
23 fn new() -> Self {
24 Self {
25 bars: VecDeque::from(vec![0.0; spectrum::BANDS]),
26 falls: vec![0.0; spectrum::BANDS],
27 target: vec![0.0; spectrum::BANDS],
28 stale: 0,
29 }
30 }
31
32 fn push(&mut self, bands: &[f32]) {
33 if bands.is_empty() {
34 self.stale = self.stale.saturating_add(1);
35 if self.stale > HOLD_TICKS {
36 self.target.fill(0.0);
37 }
38 } else {
39 self.stale = 0;
40 self.target = soften(bands);
41 }
42 for ((bar, fall), target) in self.bars.iter_mut().zip(&mut self.falls).zip(&self.target) {
43 if *target >= *bar {
44 *bar += (target - *bar) * ATTACK;
45 *fall = 0.0;
46 } else {
47 *fall += GRAVITY;
48 *bar = (*bar - *fall).max(*target);
49 }
50 }
51 }
52
53 fn silent(&self) -> bool {
54 self.stale > HOLD_TICKS && self.bars.iter().all(|bar| *bar < GONE)
55 }
56}
57
58fn soften(bands: &[f32]) -> Vec<f32> {
59 (0..spectrum::BANDS)
60 .map(|index| {
61 let at = |offset: isize| {
62 index
63 .checked_add_signed(offset)
64 .and_then(|index| bands.get(index))
65 .copied()
66 };
67 let center = at(0).unwrap_or(0.0);
68 let left = at(-1).unwrap_or(center);
69 let right = at(1).unwrap_or(center);
70 center.max((left + 2.0 * center + right) / 4.0)
71 })
72 .collect()
73}
74
75#[derive(Default)]
76pub struct Waves {
77 pub speaking: HashSet<Id>,
78 pub own_live: bool,
79 voice: HashMap<Id, Wave>,
80 share: HashMap<Id, Wave>,
81}
82
83fn advance(waves: &mut HashMap<Id, Wave>, levels: Vec<(Id, Vec<f32>)>) -> bool {
84 let mut levels: HashMap<Id, Vec<f32>> = levels.into_iter().collect();
85 let ids: HashSet<Id> = waves.keys().chain(levels.keys()).copied().collect();
86 for id in ids {
87 let bands = levels.remove(&id).unwrap_or_default();
88 let wave = waves.entry(id).or_insert_with(Wave::new);
89 wave.push(&bands);
90 if wave.silent() {
91 waves.remove(&id);
92 }
93 }
94 !waves.is_empty()
95}
96
97impl Waves {
98 pub fn update(&mut self, levels: Levels, me: Option<Id>, deafened: bool) -> bool {
99 let mut voice = levels.voice;
100 let mut speaking: HashSet<Id> = voice.iter().map(|(id, _)| *id).collect();
101 let own_live = levels.own_sending && !deafened;
102 if let Some(me) = me
103 && !deafened
104 {
105 voice.push((me, levels.own));
106 if own_live {
107 speaking.insert(me);
108 }
109 }
110 let mut changed = speaking != self.speaking || own_live != self.own_live;
111 self.speaking = speaking;
112 self.own_live = own_live;
113 let had = !self.voice.is_empty() || !self.share.is_empty();
114 changed |= advance(&mut self.voice, voice);
115
116 let mut share = levels.watching;
117 share.extend(me.zip(levels.sharing));
118 changed |= advance(&mut self.share, share);
119 changed || had
120 }
121
122 pub fn clear(&mut self) {
123 *self = Self::default();
124 }
125
126 pub fn voice(&self, id: Id) -> Option<&VecDeque<f32>> {
127 self.voice.get(&id).map(|wave| &wave.bars)
128 }
129
130 pub fn total(&self, ids: &[Id]) -> Option<VecDeque<f32>> {
131 let mut waves = ids.iter().filter_map(|id| self.voice(*id));
132 let first = waves.next()?.clone();
133 Some(waves.fold(first, |mut sum, bars| {
134 sum.iter_mut()
135 .zip(bars)
136 .for_each(|(sum, bar)| *sum = (*sum + bar).min(1.));
137 sum
138 }))
139 }
140
141 pub fn audible(&self, id: Id) -> bool {
142 self.speaking.contains(&id) || self.share_audible(id)
143 }
144
145 pub fn share_audible(&self, id: Id) -> bool {
146 self.share
147 .get(&id)
148 .is_some_and(|wave| wave.bars.iter().any(|bar| *bar >= GONE))
149 }
150
151 pub fn share(&self, id: Id) -> Option<&VecDeque<f32>> {
152 self.share.get(&id).map(|wave| &wave.bars)
153 }
154}
155
156pub fn line_of(levels: Vec<f32>, slots: usize, color: Hsla) -> impl IntoElement {
157 canvas(
158 |_, _, _| {},
159 move |bounds, _, window, _| {
160 let first = slots.saturating_sub(levels.len());
161 let mut path = PathBuilder::stroke(px(stroke::LINE));
162 for (index, &level) in levels.iter().enumerate() {
163 let step = (first + index) as f32 / slots.saturating_sub(1).max(1) as f32;
164 let x = bounds.origin.x + bounds.size.width * step;
165 let y = bounds.origin.y + bounds.size.height * (1. - level);
166 if index == 0 {
167 path.move_to(point(x, y));
168 } else {
169 path.line_to(point(x, y));
170 }
171 }
172 if let Ok(path) = path.build() {
173 window.paint_path(path, color);
174 }
175 },
176 )
177 .size_full()
178}