use crate::prelude::*;
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Vol {
pub gain: f64,
}
impl Vol {
pub const ZERO: Self = Self::new(0.0);
pub const HALF: Self = Self::new(0.5);
pub const FULL: Self = Self::new(1.0);
pub const TWICE: Self = Self::new(2.0);
pub const MDB3: Self = Self::new(0.707_945_784_384_137_9);
pub const MDB6: Self = Self::new(0.501_187_233_627_272_2);
pub const MDB10: Self = Self::new(0.316_227_766_016_837_94);
pub const DB3: Self = Self::new(1.412_537_544_622_754_4);
pub const DB6: Self = Self::new(1.995_262_314_968_879_5);
pub const DB10: Self = Self::new(3.162_277_660_168_379_5);
#[must_use]
pub const fn new(gain: f64) -> Self {
Self { gain }
}
#[must_use]
pub fn from_db(db: f64) -> Self {
Self::new(10f64.powf(db / 20.0))
}
#[cfg(feature = "midly")]
#[must_use]
pub fn from_vel(vel: midly::num::u7) -> Self {
Self::new(f64::from(vel.as_int()) / 127.0)
}
#[must_use]
pub fn db(&self) -> f64 {
20.0 * self.gain.log10()
}
}
impl Default for Vol {
fn default() -> Self {
Self::new(1.0)
}
}
impl Map for Vol {
type Input = f64;
type Output = f64;
fn eval(&self, x: f64) -> f64 {
x * self.gain
}
}