use crate::{FRAME_LEN, SAMPLE_RATE};
pub fn db_to_gain(db: f32) -> f32 {
(db * (::std::f32::consts::LN_10 / 20.)).exp()
}
pub fn gain_to_db(gain: f32) -> f32 {
20. * gain.log10()
}
pub fn power_db(power: f32) -> f32 {
10. * (power + 1e-12).log10()
}
pub fn without_denormals<T>(f: impl FnOnce() -> T) -> T {
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
let result = unsafe { ::no_denormals::no_denormals(f) };
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
let result = f();
result
}
pub fn smoothing(tau: f32) -> f32 {
1. - (-1. / (tau * SAMPLE_RATE as f32)).exp()
}
#[inline]
pub fn follow(value: f32, target: f32, attack: f32, release: f32) -> f32 {
let factor = if target < value { attack } else { release };
value + (target - value) * factor
}
pub fn slew(value: f32, target: f32, rise: f32, fall: f32) -> f32 {
if target > value {
target.min(value + rise)
} else {
target.max(value - fall)
}
}
pub fn ramp(from: f32, to: f32) -> impl Iterator<Item = f32> {
(0..FRAME_LEN).map(move |i| {
let t = (i + 1) as f32 / FRAME_LEN as f32;
from + (to - from) * t
})
}