diff --git a/src/audio.rs b/src/audio.rs index 74761d3..480932a 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -248,14 +248,28 @@ impl AudioVisualizer { thread::sleep(std::time::Duration::from_millis(16)); let samples = { - let buf = match audio_buffer.lock() { + let mut buf = match audio_buffer.lock() { Ok(b) => b, Err(_) => continue, }; + + // decay heights smoothly if buf.len() < chunk_size { + for val in prev_heights.iter_mut() { + *val = (*val - falloff).max(0.0); + } + + if let Ok(mut bars) = bar_data.lock() { + for (i, val) in prev_heights.iter().enumerate() { + bars[i] = ((*val * 10.0).clamp(0.0, 100.0)) as u64; + } + } + continue; } - buf[buf.len() - chunk_size..].to_vec() + + // drain the consumed samples + buf.drain(0..chunk_size).collect::>() }; Self::apply_hann_window(&samples, &mut windowed_buffer);