| 1 | use std::f32::consts::PI; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use rustfft::{Fft, FftPlanner, num_complex::Complex}; |
| 5 | |
| 6 | pub const BANDS: usize = 32; |
| 7 | pub const WINDOW: usize = 2048; |
| 8 | const LOW_HZ: f32 = 60.0; |
| 9 | const HIGH_HZ: f32 = 12000.0; |
| 10 | const FLOOR_DB: f32 = -66.0; |
| 11 | const CEIL_DB: f32 = -12.0; |
| 12 | const SPREAD_DB: f32 = 24.0; |
| 13 | |
| 14 | pub struct Analyzer { |
| 15 | fft: Arc<dyn Fft<f32>>, |
| 16 | hann: Vec<f32>, |
| 17 | gain: f32, |
| 18 | edges: Vec<usize>, |
| 19 | } |
| 20 | |
| 21 | impl Analyzer { |
| 22 | pub fn new(sample_rate: u32) -> Self { |
| 23 | let hann: Vec<f32> = (0..WINDOW) |
| 24 | .map(|i| 0.5 - 0.5 * (2.0 * PI * i as f32 / WINDOW as f32).cos()) |
| 25 | .collect(); |
| 26 | let gain = 2.0 / hann.iter().sum::<f32>(); |
| 27 | let bin = |hz: f32| (hz * WINDOW as f32 / sample_rate as f32) as usize; |
| 28 | let mut edges = Vec::with_capacity(BANDS + 1); |
| 29 | for band in 0..=BANDS { |
| 30 | let hz = LOW_HZ * (HIGH_HZ / LOW_HZ).powf(band as f32 / BANDS as f32); |
| 31 | let previous = edges.last().map_or(0, |edge| edge + 1); |
| 32 | edges.push(bin(hz).max(previous).min(WINDOW / 2)); |
| 33 | } |
| 34 | Self { |
| 35 | fft: FftPlanner::new().plan_fft_forward(WINDOW), |
| 36 | hann, |
| 37 | gain, |
| 38 | edges, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub fn bands(&self, samples: &[f32]) -> Vec<f32> { |
| 43 | let mut buffer = vec![Complex::default(); WINDOW]; |
| 44 | let tail = &samples[samples.len().saturating_sub(WINDOW)..]; |
| 45 | let start = WINDOW - tail.len(); |
| 46 | for (index, sample) in tail.iter().enumerate() { |
| 47 | buffer[start + index].re = sample * self.hann[start + index]; |
| 48 | } |
| 49 | self.fft.process(&mut buffer); |
| 50 | let dbs: Vec<f32> = self |
| 51 | .edges |
| 52 | .windows(2) |
| 53 | .map(|edge| { |
| 54 | let peak = buffer[edge[0]..edge[1].max(edge[0] + 1)] |
| 55 | .iter() |
| 56 | .map(|value| value.norm() * self.gain) |
| 57 | .fold(0.0, f32::max); |
| 58 | 20.0 * peak.max(1e-6).log10() |
| 59 | }) |
| 60 | .collect(); |
| 61 | let top = dbs.iter().copied().fold(FLOOR_DB, f32::max); |
| 62 | let loud = ((top - FLOOR_DB) / (CEIL_DB - FLOOR_DB)).clamp(0.0, 1.0); |
| 63 | dbs.into_iter() |
| 64 | .map(|db| ((db - top + SPREAD_DB) / SPREAD_DB).clamp(0.0, 1.0) * loud) |
| 65 | .collect() |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
| 71 | use super::*; |
| 72 | |
| 73 | #[test] |
| 74 | fn tone_lights_its_band() { |
| 75 | let analyzer = Analyzer::new(48000); |
| 76 | let tone: Vec<f32> = (0..WINDOW) |
| 77 | .map(|i| 0.5 * (2.0 * PI * 1000.0 * i as f32 / 48000.0).sin()) |
| 78 | .collect(); |
| 79 | let bands = analyzer.bands(&tone); |
| 80 | let loudest = bands |
| 81 | .iter() |
| 82 | .enumerate() |
| 83 | .max_by(|a, b| a.1.total_cmp(b.1)) |
| 84 | .map(|(index, _)| index) |
| 85 | .unwrap_or_default(); |
| 86 | assert!(bands[loudest] > 0.8); |
| 87 | assert!(bands[0] < 0.2 && bands[BANDS - 1] < 0.2); |
| 88 | } |
| 89 | } |