use std::collections::{HashMap, HashSet}; use common::model::Id; use gpui_kit::{Hsla, IntoElement, PathBuilder, Styled, canvas, point, px}; use super::style::stroke; use crate::net::Levels; const ATTACK: f32 = 0.6; const GRAVITY: f32 = 0.012; const HOLD_TICKS: u8 = 3; const GONE: f32 = 0.02; pub struct Wave { bars: Vec, falls: Vec, target: Vec, stale: u8, } impl Wave { fn new() -> Self { Self { bars: vec![0.0; spectrum::BANDS], falls: vec![0.0; spectrum::BANDS], target: vec![0.0; spectrum::BANDS], stale: 0, } } fn push(&mut self, bands: &[f32]) { if bands.is_empty() { self.stale = self.stale.saturating_add(1); if self.stale > HOLD_TICKS { self.target.fill(0.0); } } else { self.stale = 0; self.target = soften(bands); } for ((bar, fall), target) in self.bars.iter_mut().zip(&mut self.falls).zip(&self.target) { if *target >= *bar { *bar += (target - *bar) * ATTACK; *fall = 0.0; } else { *fall += GRAVITY; *bar = (*bar - *fall).max(*target); } } } fn silent(&self) -> bool { self.stale > HOLD_TICKS && self.bars.iter().all(|bar| *bar < GONE) } } fn soften(bands: &[f32]) -> Vec { (0..spectrum::BANDS) .map(|index| { let at = |offset: isize| { index .checked_add_signed(offset) .and_then(|index| bands.get(index)) .copied() }; let center = at(0).unwrap_or(0.0); let left = at(-1).unwrap_or(center); let right = at(1).unwrap_or(center); center.max((left + 2.0 * center + right) / 4.0) }) .collect() } #[derive(Default)] pub struct Waves { pub speaking: HashSet, voice: HashMap, share: HashMap, } fn advance(waves: &mut HashMap, levels: Vec<(Id, Vec)>) -> bool { let mut levels: HashMap> = levels.into_iter().collect(); let ids: HashSet = waves.keys().chain(levels.keys()).copied().collect(); for id in ids { let bands = levels.remove(&id).unwrap_or_default(); let wave = waves.entry(id).or_insert_with(Wave::new); wave.push(&bands); if wave.silent() { waves.remove(&id); } } !waves.is_empty() } impl Waves { pub fn update(&mut self, levels: Levels, me: Option, deafened: bool) -> bool { let mut voice = levels.voice; let mut speaking: HashSet = voice.iter().map(|(id, _)| *id).collect(); if let Some(me) = me && !deafened { voice.push((me, levels.own)); if levels.own_sending { speaking.insert(me); } } let mut changed = speaking != self.speaking; self.speaking = speaking; let had = !self.voice.is_empty() || !self.share.is_empty(); changed |= advance(&mut self.voice, voice); let mut share = levels.watching; share.extend(me.zip(levels.sharing)); changed |= advance(&mut self.share, share); changed || had } pub fn clear(&mut self) { *self = Self::default(); } pub fn voice(&self, id: Id) -> Option<&[f32]> { self.voice.get(&id).map(|wave| wave.bars.as_slice()) } pub fn total(&self, ids: &[Id]) -> Option> { let mut waves = ids.iter().filter_map(|id| self.voice(*id)); let first = waves.next()?.to_vec(); Some(waves.fold(first, |mut sum, bars| { sum.iter_mut() .zip(bars) .for_each(|(sum, bar)| *sum = (*sum + bar).min(1.)); sum })) } pub fn audible(&self, id: Id) -> bool { self.speaking.contains(&id) || self.share_audible(id) } pub fn share_audible(&self, id: Id) -> bool { self.share .get(&id) .is_some_and(|wave| wave.bars.iter().any(|bar| *bar >= GONE)) } pub fn share(&self, id: Id) -> Option<&[f32]> { self.share.get(&id).map(|wave| wave.bars.as_slice()) } } pub fn line_of(levels: Vec, slots: usize, color: Hsla) -> impl IntoElement { canvas( |_, _, _| {}, move |bounds, _, window, _| { let first = slots.saturating_sub(levels.len()); let mut path = PathBuilder::stroke(px(stroke::LINE)); for (index, &level) in levels.iter().enumerate() { let step = (first + index) as f32 / slots.saturating_sub(1).max(1) as f32; let x = bounds.origin.x + bounds.size.width * step; let y = bounds.origin.y + bounds.size.height * (1. - level); if index == 0 { path.move_to(point(x, y)); } else { path.line_to(point(x, y)); } } if let Ok(path) = path.build() { window.paint_path(path, color); } }, ) .size_full() }