use std::sync::Arc;
use realfft::num_complex::Complex;
use realfft::{RealFftPlanner, RealToComplex};
use crate::picture::{self, BINS, Shown};
use crate::thresholds::{MeasureThresholds, ReadingThresholds};
use crate::totals::Totals;
pub(crate) const FFT_SIZE: usize = 4096;
const HOP: usize = FFT_SIZE / 2;
const SMOOTH_HZ: f64 = 50.0;
const FLOOR_DB: f64 = -200.0;
const ENVELOPE_BLOCK: usize = 64;
const CODEC_PERIODS: [usize; 3] = [
1024 / ENVELOPE_BLOCK,
1152 / ENVELOPE_BLOCK,
2048 / ENVELOPE_BLOCK,
];
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Step {
pub hz: f64,
pub drop: f64,
pub clearance: f64,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Window {
pub start: f64,
pub seconds: f64,
pub rms_dbfs: f64,
pub cutoff_hz: Option<f64>,
pub shelf_db: Option<f64>,
pub holes: Option<f64>,
pub inner_shelf: Option<Step>,
pub frame_score: Option<f64>,
pub upper_power: f64,
pub power: f64,
pub shown: Shown,
}
impl Window {
pub(crate) fn sharp(&self, thresholds: &ReadingThresholds) -> bool {
self
.shelf_db
.is_some_and(|shelf| shelf >= thresholds.sharp_shelf_db)
}
fn silent(&self, thresholds: &MeasureThresholds) -> bool {
self.rms_dbfs < thresholds.silence_dbfs
}
}
pub(crate) struct Analyzer<'a> {
fft: Arc<dyn RealToComplex<f32>>,
hann: Vec<f32>,
input: Vec<f32>,
output: Vec<Complex<f32>>,
scratch: Vec<Complex<f32>>,
rate: f64,
bin_hz: f64,
frames: Vec<Vec<f64>>,
quantization_db: Option<f64>,
thresholds: &'a MeasureThresholds,
}
impl<'a> Analyzer<'a> {
pub fn new(rate: u32, bits: Option<u32>, thresholds: &'a MeasureThresholds) -> Self {
let fft = RealFftPlanner::<f32>::new().plan_fft_forward(FFT_SIZE);
let hann = (0..FFT_SIZE)
.map(|i| {
let phase = 2.0 * std::f64::consts::PI * i as f64 / FFT_SIZE as f64;
(0.5 - 0.5 * phase.cos()) as f32
})
.collect();
let rate = f64::from(rate);
let bin_hz = rate / FFT_SIZE as f64;
Self {
input: fft.make_input_vec(),
output: fft.make_output_vec(),
scratch: vec![Complex::default(); fft.get_scratch_len()],
fft,
hann,
rate,
bin_hz,
frames: Vec::new(),
quantization_db: bits.map(quantization_db),
thresholds,
}
}
pub fn measure(&mut self, start: f64, channels: &[Vec<f32>], totals: &mut Totals) -> Window {
let length = channels[0].len();
totals.add(channels);
let squares: f64 = channels
.iter()
.flat_map(|c| c.iter())
.map(|s| f64::from(*s) * f64::from(*s))
.sum();
let count = (length * channels.len()).max(1) as f64;
let rms_dbfs = (10.0 * (squares / count).log10()).max(FLOOR_DB);
let frame_score = frame_score(channels);
let spectrum = self.spectrum(channels);
let shown = self.shown(channels, spectrum.as_deref());
let mut window = Window {
start,
seconds: length as f64 / self.rate,
rms_dbfs,
cutoff_hz: None,
shelf_db: None,
holes: None,
inner_shelf: None,
frame_score: None,
upper_power: 0.0,
power: 0.0,
shown,
};
let Some(power) = spectrum else {
return window;
};
let half = (self.rate / 4.0 / self.bin_hz).ceil() as usize;
window.upper_power = power[half..].iter().sum();
window.power = power[1..].iter().sum();
let t = self.thresholds;
if window.silent(t) {
return window;
}
let smoothed = smooth_db(&power, (SMOOTH_HZ / self.bin_hz).round() as usize);
let peak = smoothed[1..].iter().copied().fold(FLOOR_DB, f64::max);
let line = self
.quantization_db
.map_or(peak - t.cutoff_range_db, |floor| {
(peak - t.cutoff_range_db).max(floor + t.floor_margin_db)
});
let top = smoothed
.iter()
.rposition(|level| *level >= line)
.unwrap_or(0);
let cutoff = top as f64 * self.bin_hz;
window.cutoff_hz = Some(cutoff);
window.shelf_db = self
.step(&smoothed, cutoff)
.map(|(below, above)| below - above);
window.holes = self.holes(cutoff);
window.frame_score = frame_score;
if cutoff >= t.full_band * self.rate / 2.0 {
window.inner_shelf = self.inner_shelf(&smoothed, cutoff, line);
}
window
}
fn spectrum(&mut self, channels: &[Vec<f32>]) -> Option<Vec<f64>> {
let length = channels[0].len();
if length < FFT_SIZE {
self.frames.clear();
return None;
}
let count = (length - FFT_SIZE) / HOP + 1;
let bins = FFT_SIZE / 2 + 1;
let norm = (FFT_SIZE as f64 / 4.0).powi(2);
self.frames.resize_with(count, Vec::new);
let mut total = vec![0.0; bins];
for (index, frame) in self.frames.iter_mut().enumerate() {
frame.clear();
frame.resize(bins, 0.0);
let start = index * HOP;
for channel in channels {
let samples = &channel[start..start + FFT_SIZE];
for ((input, sample), weight) in self.input.iter_mut().zip(samples).zip(&self.hann) {
*input = sample * weight;
}
self
.fft
.process_with_scratch(&mut self.input, &mut self.output, &mut self.scratch)
.expect("the buffers are the ones the plan made");
for (power, value) in frame.iter_mut().zip(&self.output) {
*power += f64::from(value.norm_sqr()) / norm;
}
}
for (sum, power) in total.iter_mut().zip(frame.iter()) {
*sum += power;
}
}
for sum in &mut total {
*sum /= count as f64;
}
Some(total)
}
fn shown(&self, channels: &[Vec<f32>], spectrum: Option<&[f64]>) -> Shown {
let length = channels[0].len();
let rate = self.rate as usize;
Shown {
spectrum: spectrum.map_or_else(|| vec![0; BINS], picture::spectrum_bytes),
columns: picture::columns(&self.frames, length, rate, FFT_SIZE, HOP),
peaks: picture::peaks(channels, rate),
}
}
fn bins(&self, low: f64, high: f64) -> std::ops::RangeInclusive<usize> {
let last = FFT_SIZE / 2;
let first = ((low / self.bin_hz).ceil().max(1.0) as usize).min(last);
let end = ((high / self.bin_hz).floor().max(0.0) as usize).min(last);
first..=end
}
fn level(&self, smoothed: &[f64], low: f64, high: f64) -> Option<f64> {
let range = self.bins(low, high);
let count = range.clone().count();
(count > 0).then(|| smoothed[range].iter().sum::<f64>() / count as f64)
}
fn step(&self, smoothed: &[f64], hz: f64) -> Option<(f64, f64)> {
let nyquist = self.rate / 2.0;
let (gap, span) = (self.thresholds.shelf_gap_hz, self.thresholds.shelf_span_hz);
if hz + gap + span > nyquist || hz < gap + span {
return None;
}
let below = self.level(smoothed, hz - gap - span, hz - gap)?;
let above = self.level(smoothed, hz + gap, hz + gap + span)?;
Some((below, above))
}
fn inner_shelf(&self, smoothed: &[f64], cutoff: f64, line: f64) -> Option<Step> {
let mut best: Option<Step> = None;
let t = self.thresholds;
let mut hz = t.inner_shelf_from_hz.max(0.0);
while hz <= self.rate / 2.0 && hz + t.shelf_gap_hz + t.shelf_span_hz <= cutoff {
if let Some((below, above)) = self.step(smoothed, hz)
&& best.is_none_or(|deepest| below - above > deepest.drop)
{
best = Some(Step {
hz,
drop: below - above,
clearance: above - line,
});
}
hz += 100.0;
}
best
}
fn holes(&self, cutoff: f64) -> Option<f64> {
let t = self.thresholds;
let high = t.holes_to_hz.min(cutoff - t.shelf_gap_hz * 2.0);
if high - t.holes_from_hz < 1000.0 || self.frames.is_empty() {
return None;
}
let range = self.bins(t.holes_from_hz, high);
let radius = (SMOOTH_HZ * 2.0 / self.bin_hz).round() as usize;
let mut ratios = 0.0;
for frame in &self.frames {
let smoothed = smooth_db(frame, radius);
let (mut holes, mut populated) = (0u32, 0u32);
for bin in range.clone() {
if db(frame[bin]) < smoothed[bin] - t.hole_depth_db {
holes += 1;
} else {
populated += 1;
}
}
ratios += f64::from(holes) / f64::from(populated.max(1));
}
Some(ratios / self.frames.len() as f64)
}
}
fn quantization_db(bits: u32) -> f64 {
let step = 2f64.powi(1 - bits.clamp(2, 32) as i32);
10.0 * (step * step / 4.0 * 6.0 / FFT_SIZE as f64).log10()
}
fn db(power: f64) -> f64 {
if power > 0.0 {
(10.0 * power.log10()).max(FLOOR_DB)
} else {
FLOOR_DB
}
}
fn smooth_db(power: &[f64], radius: usize) -> Vec<f64> {
let mut prefix = Vec::with_capacity(power.len() + 1);
prefix.push(0.0);
for value in power {
prefix.push(prefix.last().copied().unwrap_or(0.0) + value);
}
(0..power.len())
.map(|bin| {
let first = bin.saturating_sub(radius);
let last = (bin + radius).min(power.len() - 1);
db((prefix[last + 1] - prefix[first]) / (last - first + 1) as f64)
})
.collect()
}
fn frame_score(channels: &[Vec<f32>]) -> Option<f64> {
let length = channels[0].len();
let blocks = length / ENVELOPE_BLOCK;
let longest = CODEC_PERIODS.iter().max().copied().unwrap_or(0) + 3;
if blocks < longest * 4 {
return None;
}
let mut envelope = vec![0.0f64; blocks];
for channel in channels {
for (block, energy) in envelope.iter_mut().enumerate() {
let start = (block * ENVELOPE_BLOCK).max(1);
let end = (block + 1) * ENVELOPE_BLOCK;
*energy += (start..end)
.map(|i| {
let difference = f64::from(channel[i]) - f64::from(channel[i - 1]);
difference * difference
})
.sum::<f64>();
}
}
let mean = envelope.iter().sum::<f64>() / blocks as f64;
for energy in &mut envelope {
*energy -= mean;
}
let lag = |k: usize| -> f64 { (0..blocks - k).map(|i| envelope[i] * envelope[i + k]).sum() };
let zero = lag(0);
if zero <= 0.0 {
return Some(0.0);
}
let correlation = |k: usize| lag(k) / zero;
let excess = CODEC_PERIODS
.iter()
.map(|&period| {
let around = (period - 3..=period + 3)
.filter(|k| *k != period)
.map(correlation)
.fold(f64::MIN, f64::max);
correlation(period) - around
})
.fold(0.0, f64::max);
Some(excess.clamp(0.0, 1.0))
}