use crate::effects::Effect;
#[derive(Debug)]
pub struct Distortion {
crunch: f32,
drive: f32,
}
impl Distortion {
pub fn new(crunch: f32, drive: f32) -> Self {
let crunch = 1.0 - crunch.max(0.01);
Self { crunch, drive }
}
}
impl Effect for Distortion {
fn apply(&mut self, buffer: &mut [f32], _offset: usize) {
buffer.iter_mut().for_each(|tone| {
let x = *tone * self.drive;
let sign = x.signum();
*tone = (x * sign).powf(self.crunch).min(1.0) * sign;
});
}
}