use audio_visualizer::live::{LiveVisualizer, Transform};
use spectrum_analyzer::scaling::divide_by_N;
use spectrum_analyzer::windows::hann_window;
use spectrum_analyzer::{FrequencyLimit, samples_fft_to_spectrum};
mod common;
fn main() {
let mut smoothed: Vec<(f64, f64)> = vec![];
let to_spectrum = move |samples: &[f32], sample_rate: f32| {
let latest = &samples[samples.len() - 2048..];
let hann_window = hann_window(latest);
let spectrum = samples_fft_to_spectrum(
&hann_window,
sample_rate as u32,
FrequencyLimit::All,
Some(÷_by_N),
)
.unwrap();
let current = spectrum
.data()
.iter()
.map(|(f, v)| (f.val() as f64, (v.val() * 5000.0) as f64))
.collect::<Vec<_>>();
if smoothed.len() != current.len() {
smoothed = current;
} else {
for ((_, old), (_, new)) in smoothed.iter_mut().zip(¤t) {
*old = (*old * 0.84).max(*new);
}
}
smoothed.clone()
};
let input = common::select_input();
LiveVisualizer::new(Transform::points(to_spectrum))
.title("Live Spectrum View")
.axis_labels("frequency (Hz)", "magnitude")
.x_range(0.0..22050.0)
.y_range(0.0..500.0)
.input(input)
.open()
.unwrap();
}