use rustfft::{FftPlanner, num_complex::Complex};
#[derive(Clone, Copy, Debug)]
pub enum Window { Hann, Hamming, Blackman, Rectangular }
impl Window {
fn apply(self, buf: &mut [f32]) {
let n = buf.len();
for (i, s) in buf.iter_mut().enumerate() {
let w = match self {
Self::Hann =>
0.5 * (1.0 - (2.0 * std::f32::consts::PI * i as f32 / (n - 1) as f32).cos()),
Self::Hamming =>
0.54 - 0.46 * (2.0 * std::f32::consts::PI * i as f32 / (n - 1) as f32).cos(),
Self::Blackman => {
let t = 2.0 * std::f32::consts::PI * i as f32 / (n - 1) as f32;
0.42 - 0.5 * t.cos() + 0.08 * (2.0 * t).cos()
}
Self::Rectangular => 1.0,
};
*s *= w;
}
}
}
pub struct FftAnalyzer {
fft_size: usize,
sample_rate: u32,
buffer: Vec<f32>,
window: Window,
pub magnitudes: Vec<f32>,
pub smoothed: Vec<f32>,
beat_energy: f32,
beat_avg: f32,
planner: FftPlanner<f32>,
}
impl FftAnalyzer {
pub fn new(fft_size: usize, sample_rate: u32) -> Self {
let bins = fft_size / 2;
Self {
fft_size,
sample_rate,
buffer: vec![0.0; fft_size],
window: Window::Hann,
magnitudes: vec![0.0; bins],
smoothed: vec![0.0; bins],
beat_energy: 0.0,
beat_avg: 0.001,
planner: FftPlanner::new(),
}
}
pub fn set_window(&mut self, w: Window) { self.window = w; }
pub fn push_samples(&mut self, samples: &[f32]) {
let n = samples.len().min(self.fft_size);
let shift = self.fft_size - n;
self.buffer.copy_within(n.., 0);
self.buffer[shift..].copy_from_slice(&samples[..n]);
self.compute();
}
fn compute(&mut self) {
let fft = self.planner.plan_fft_forward(self.fft_size);
let mut buf: Vec<Complex<f32>> = self.buffer.iter()
.cloned()
.map(|s| Complex { re: s, im: 0.0 })
.collect();
{
let mut real: Vec<f32> = buf.iter().map(|c| c.re).collect();
self.window.apply(&mut real);
for (c, &r) in buf.iter_mut().zip(real.iter()) { c.re = r; }
}
fft.process(&mut buf);
let scale = 2.0 / self.fft_size as f32;
let mut energy = 0.0f32;
for (i, mag) in self.magnitudes.iter_mut().enumerate() {
let m = buf[i].norm() * scale;
*mag = m;
energy += m * m;
}
let smooth = 0.82;
for (s, &m) in self.smoothed.iter_mut().zip(self.magnitudes.iter()) {
*s = *s * smooth + m * (1.0 - smooth);
}
self.beat_energy = energy / self.magnitudes.len() as f32;
self.beat_avg = self.beat_avg * 0.992 + self.beat_energy * 0.008;
}
pub fn freq_bands(&self, bands: usize) -> Vec<f32> {
if bands == 0 { return vec![]; }
let nyq = self.sample_rate as f32 / 2.0;
let bins = self.magnitudes.len();
let f_min = 20.0f32;
let f_max = nyq;
let log_lo = f_min.log2();
let log_hi = f_max.log2();
(0..bands).map(|b| {
let lo = 2.0f32.powf(log_lo + (b as f32 / bands as f32) * (log_hi - log_lo));
let hi = 2.0f32.powf(log_lo + ((b + 1) as f32 / bands as f32) * (log_hi - log_lo));
let bin_lo = ((lo / nyq) * bins as f32) as usize;
let bin_hi = ((hi / nyq) * bins as f32) as usize + 1;
let bin_lo = bin_lo.min(bins - 1);
let bin_hi = bin_hi.min(bins);
if bin_hi <= bin_lo {
self.smoothed[bin_lo]
} else {
self.smoothed[bin_lo..bin_hi].iter().cloned().fold(0.0f32, f32::max)
}
}).collect()
}
pub fn dominant_freq(&self) -> f32 {
let nyq = self.sample_rate as f32 / 2.0;
let bins = self.magnitudes.len();
let peak_bin = self.magnitudes.iter()
.enumerate()
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
.map(|(i, _)| i)
.unwrap_or(0);
peak_bin as f32 / bins as f32 * nyq
}
pub fn rms(&self) -> f32 {
(self.buffer.iter().map(|s| s * s).sum::<f32>() / self.fft_size as f32).sqrt()
}
pub fn is_beat(&self) -> bool {
self.beat_energy > self.beat_avg * 1.5
}
pub fn beat_ratio(&self) -> f32 {
self.beat_energy / self.beat_avg.max(1e-6)
}
}
#[derive(Clone, Debug)]
pub struct CosPalette {
pub a: [f32; 3], pub b: [f32; 3], pub c: [f32; 3], pub d: [f32; 3], }
impl CosPalette {
pub fn rainbow() -> Self {
Self {
a: [0.5, 0.5, 0.5],
b: [0.5, 0.5, 0.5],
c: [1.0, 1.0, 1.0],
d: [0.0, 0.333, 0.667],
}
}
pub fn fire() -> Self {
Self {
a: [0.8, 0.4, 0.1],
b: [0.7, 0.3, 0.1],
c: [1.0, 0.5, 0.3],
d: [0.0, 0.5, 0.8],
}
}
pub fn ocean() -> Self {
Self {
a: [0.1, 0.4, 0.7],
b: [0.3, 0.3, 0.4],
c: [0.8, 1.0, 0.5],
d: [0.3, 0.0, 0.6],
}
}
pub fn psychedelic() -> Self {
Self {
a: [0.5, 0.5, 0.5],
b: [0.8, 0.8, 0.8],
c: [1.0, 1.3, 0.7],
d: [0.0, 0.15, 0.3],
}
}
pub fn frequency() -> Self {
Self {
a: [0.5, 0.4, 0.5],
b: [0.5, 0.3, 0.5],
c: [0.5, 0.5, 1.0],
d: [0.0, 0.33, 0.67],
}
}
pub fn eval(&self, t: f32) -> [f32; 3] {
[0, 1, 2].map(|i| {
let v = self.a[i] + self.b[i] * (2.0 * std::f32::consts::PI * (self.c[i] * t + self.d[i])).cos();
v.clamp(0.0, 1.0)
})
}
pub fn eval_animated(&self, t: f32, time: f32, speed: f32) -> [f32; 3] {
self.eval((t + time * speed) % 1.0)
}
pub fn map_bands(&self, magnitudes: &[f32], time: f32, speed: f32) -> Vec<[u8; 4]> {
magnitudes.iter().enumerate().map(|(i, &m)| {
let t = i as f32 / magnitudes.len() as f32;
let [r, g, b] = self.eval_animated(t, time, speed);
let brightness = m.clamp(0.0, 1.0);
[(r * brightness * 255.0) as u8, (g * brightness * 255.0) as u8, (b * brightness * 255.0) as u8, (brightness * 255.0) as u8]
}).collect()
}
}
pub fn freq_texture(bands: &[f32], palette: &CosPalette, width: u32, height: u32, time: f32) -> Vec<u8> {
let w = width as usize;
let h = height as usize;
let mut pixels = vec![0u8; w * h * 4];
for x in 0..w {
let band_idx = (x * bands.len() / w).min(bands.len() - 1);
let mag = bands[band_idx].clamp(0.0, 1.0);
let fill_h = (mag * h as f32) as usize;
let t = x as f32 / w as f32;
let [r, g, b] = palette.eval_animated(t, time, 0.3);
for y in (h - fill_h)..h {
let alpha = (mag * (1.0 - (y as f32 / h as f32)) * 1.5).clamp(0.0, 1.0);
let idx = (y * w + x) * 4;
pixels[idx ] = (r * 255.0) as u8;
pixels[idx + 1] = (g * 255.0) as u8;
pixels[idx + 2] = (b * 255.0) as u8;
pixels[idx + 3] = (alpha * 255.0) as u8;
}
}
pixels
}
pub fn waveform_texture(samples: &[f32], palette: &CosPalette, width: u32, height: u32, time: f32) -> Vec<u8> {
let w = width as usize;
let h = height as usize;
let mut pixels = vec![0u8; w * h * 4];
for (i, &s) in samples.iter().enumerate() {
let x = i * w / samples.len();
let y = ((s * 0.5 + 0.5) * h as f32).clamp(0.0, h as f32 - 1.0) as usize;
let t = i as f32 / samples.len() as f32;
let [r, g, b] = palette.eval_animated(t, time, 0.2);
let idx = (y * w + x) * 4;
pixels[idx ] = (r * 255.0) as u8;
pixels[idx + 1] = (g * 255.0) as u8;
pixels[idx + 2] = (b * 255.0) as u8;
pixels[idx + 3] = 255;
}
pixels
}