Voice/share lines draw the spectrum like a bar visualizer: one zigzag point per band (alternating up/down = mirrored bars as one stroke), merged to fit narrow lines, instead of a sine carrier that hid the band shape
Browse files at this commitc23db228125789c2e86a9f59a2d6ab54e2fcbfa6Parent: b97ed6c- style
- voice/share lines draw the spectrum like a bar visualizer: one zigzag point per band (alternating up/down = mirrored bars as one stroke), merged to fit narrow lines, instead of a sine carrier that hid the band shape
1 changed file +19 -20
- crates/client/src/gui/parts.rs +19 -20
crates/client/src/gui/parts.rsmodified+19 -20
| @@ -368,6 +368,8 @@ pub enum Voice { | ||
| 368 | 368 | |
| 369 | 369 | const STILL: f32 = 0.02; |
| 370 | 370 | const EDGE: f32 = 16.; |
| 371 | +const PEAK_SPACING: f32 = 3.; | |
| 372 | +const MIN_PEAKS: usize = 6; | |
| 371 | 373 | |
| 372 | 374 | pub fn voice_line(bands: Option<&VecDeque<f32>>, voice: Voice, t: Tokens) -> AnyElement { |
| 373 | 375 | let levels: Vec<f32> = bands |
| @@ -394,24 +396,21 @@ pub fn voice_line(bands: Option<&VecDeque<f32>>, voice: Voice, t: Tokens) -> Any | ||
| 394 | 396 | let width = f32::from(bounds.size.width); |
| 395 | 397 | let middle = bounds.origin.y + bounds.size.height / 2.; |
| 396 | 398 | let half = f32::from(bounds.size.height) / 2.; |
| 397 | - let steps = (width / 4.).max(8.) as usize; | |
| 399 | + let peaks = merge(&levels, (width / PEAK_SPACING) as usize); | |
| 400 | + let count = peaks.len() as f32; | |
| 398 | 401 | let mut path = PathBuilder::stroke(px(stroke::LINE)); |
| 399 | 402 | if frayed { |
| 400 | 403 | path = path.dash_array(&[px(4.), px(3.)]); |
| 401 | 404 | } |
| 402 | - for step in 0..=steps { | |
| 403 | - let along = step as f32 / steps as f32; | |
| 404 | - let level = sample(&levels, along); | |
| 405 | + path.move_to(point(bounds.origin.x, middle)); | |
| 406 | + for (index, level) in peaks.iter().enumerate() { | |
| 407 | + let along = (index as f32 + 0.5) / count; | |
| 405 | 408 | let edge = (along.min(1. - along) * EDGE).min(1.); |
| 406 | - let swing = (step as f32 * 0.9).sin(); | |
| 409 | + let side = if index % 2 == 0 { -1. } else { 1. }; | |
| 407 | 410 | let x = bounds.origin.x + bounds.size.width * along; |
| 408 | - let y = middle + px(swing * level * edge * half); | |
| 409 | - if step == 0 { | |
| 410 | - path.move_to(point(x, y)); | |
| 411 | - } else { | |
| 412 | - path.line_to(point(x, y)); | |
| 413 | - } | |
| 411 | + path.line_to(point(x, middle + px(side * level * edge * half))); | |
| 414 | 412 | } |
| 413 | + path.line_to(point(bounds.origin.x + bounds.size.width, middle)); | |
| 415 | 414 | if let Ok(path) = path.build() { |
| 416 | 415 | window.paint_path(path, color); |
| 417 | 416 | } |
| @@ -421,15 +420,15 @@ pub fn voice_line(bands: Option<&VecDeque<f32>>, voice: Voice, t: Tokens) -> Any | ||
| 421 | 420 | .into_any_element() |
| 422 | 421 | } |
| 423 | 422 | |
| 424 | -fn sample(levels: &[f32], along: f32) -> f32 { | |
| 425 | - if levels.is_empty() { | |
| 426 | - return 0.; | |
| 427 | - } | |
| 428 | - let at = along * (levels.len() - 1) as f32; | |
| 429 | - let low = at.floor() as usize; | |
| 430 | - let high = (low + 1).min(levels.len() - 1); | |
| 431 | - let mix = at - low as f32; | |
| 432 | - levels[low] * (1. - mix) + levels[high] * mix | |
| 423 | +fn merge(levels: &[f32], count: usize) -> Vec<f32> { | |
| 424 | + let count = count.clamp(MIN_PEAKS, levels.len().max(MIN_PEAKS)); | |
| 425 | + (0..count) | |
| 426 | + .map(|index| { | |
| 427 | + let from = index * levels.len() / count; | |
| 428 | + let to = ((index + 1) * levels.len() / count).max(from + 1).min(levels.len()); | |
| 429 | + levels.get(from..to).map_or(0., |part| part.iter().copied().fold(0., f32::max)) | |
| 430 | + }) | |
| 431 | + .collect() | |
| 433 | 432 | } |
| 434 | 433 | |
| 435 | 434 | pub fn spark(values: Vec<f32>, slots: usize, color: Hsla) -> AnyElement { |