use ::std::f64::consts::PI;
use ::anyhow::Result;
use crate::dsp::{db_to_gain, smoothing};
use crate::param::Param;
use crate::stage::{Frame, Stage};
use crate::{FRAME_LEN, SAMPLE_RATE};
pub struct DeEsser {
taps: [f32; TAPS],
freq: Param,
threshold: Param,
applied_freq: f32,
history: [f32; TAPS - 1],
power: f32,
band_power: f32,
reduction_db: f32,
}
pub const FREQ: f32 = 5000.;
const TAPS: usize = 97;
pub const DELAY: usize = TAPS / 2;
pub const THRESHOLD_DB: f32 = -7.;
pub const RATIO: f32 = 4.;
pub const MAX_REDUCTION_DB: f32 = 12.;
const DETECTOR_TAU: f32 = 0.002;
const ATTACK_TAU: f32 = 0.001;
const RELEASE_TAU: f32 = 0.05;
pub fn reduction_db(share_db: f32, threshold_db: f32) -> f32 {
let over = (share_db - threshold_db).max(0.);
-(over * (1. - 1. / RATIO)).min(MAX_REDUCTION_DB)
}
fn low_pass(freq: f32) -> [f32; TAPS] {
let cutoff = freq as f64 / SAMPLE_RATE as f64;
let taps: [f64; TAPS] = ::std::array::from_fn(|i| {
let t = i as f64 - DELAY as f64;
let sinc = if t == 0. {
2. * cutoff
} else {
(2. * PI * cutoff * t).sin() / (PI * t)
};
let phase = 2. * PI * i as f64 / (TAPS - 1) as f64;
sinc * (0.42 - 0.5 * phase.cos() + 0.08 * (2. * phase).cos())
});
let sum: f64 = taps.iter().sum();
taps.map(|tap| (tap / sum) as f32)
}
impl DeEsser {
pub fn new() -> Self {
Self {
taps: low_pass(FREQ),
freq: Param::new("freq", "Frequency", "Hz", (3000., 8000., 100.), FREQ),
threshold: Param::new(
"threshold",
"Threshold",
"dB",
(-20., 0., 0.5),
THRESHOLD_DB,
),
applied_freq: FREQ,
history: [0.; TAPS - 1],
power: 0.,
band_power: 0.,
reduction_db: 0.,
}
}
pub fn reduction(&self) -> f32 {
self.reduction_db
}
}
impl Default for DeEsser {
fn default() -> Self {
Self::new()
}
}
impl Stage for DeEsser {
fn name(&self) -> &'static str {
"deesser"
}
fn latency(&self) -> usize {
DELAY
}
fn params(&self) -> Vec<Param> {
vec![self.threshold.clone(), self.freq.clone()]
}
fn process(&mut self, frame: &mut Frame) -> Result<()> {
let freq = self.freq.get();
if freq != self.applied_freq {
self.taps = low_pass(freq);
self.applied_freq = freq;
}
let threshold = self.threshold.get();
let threshold_share = 10f32.powf(threshold / 10.);
let detector = smoothing(DETECTOR_TAU);
let attack = smoothing(ATTACK_TAU);
let release = smoothing(RELEASE_TAU);
let mut work = [0.; TAPS - 1 + FRAME_LEN];
work[..TAPS - 1].copy_from_slice(&self.history);
work[TAPS - 1..].copy_from_slice(frame);
let mut low = [0.; FRAME_LEN];
for (k, tap) in self.taps.iter().enumerate() {
for (low, x) in low.iter_mut().zip(&work[k..k + FRAME_LEN]) {
*low += x * tap;
}
}
for (i, (out, low)) in frame.iter_mut().zip(low).enumerate() {
let delayed = work[i + DELAY];
let band = delayed - low;
self.power += (delayed * delayed - self.power) * detector;
self.band_power += (band * band - self.band_power) * detector;
let (band_power, power) = (self.band_power + 1e-12, self.power + 1e-12);
let wanted = if band_power > threshold_share * power {
reduction_db(10. * (band_power / power).log10(), threshold)
} else {
0.
};
let speed = if wanted < self.reduction_db {
attack
} else {
release
};
self.reduction_db += (wanted - self.reduction_db) * speed;
if wanted == 0. && self.reduction_db > -0.01 {
self.reduction_db = 0.;
}
*out = if self.reduction_db < 0. {
delayed + (db_to_gain(self.reduction_db) - 1.) * band
} else {
delayed
};
}
self.history.copy_from_slice(&work[FRAME_LEN..]);
Ok(())
}
}