use ::anyhow::Result;
use crate::dsp::{db_to_gain, ramp};
use crate::param::Param;
use crate::stage::{Frame, Stage};
pub struct Volume {
volume: Param,
gain: f32,
}
pub const VOLUME_DB: f32 = 0.;
pub fn param() -> Param {
Param::new("volume", "Volume", "dB", (-40., 0., 1.), VOLUME_DB)
}
impl Volume {
pub fn new(volume: Param) -> Self {
let gain = db_to_gain(volume.get());
Self { volume, gain }
}
}
impl Stage for Volume {
fn name(&self) -> &'static str {
"volume"
}
fn latency(&self) -> usize {
0
}
fn params(&self) -> Vec<Param> {
vec![self.volume.clone()]
}
fn process(&mut self, frame: &mut Frame) -> Result<()> {
let (from, to) = (self.gain, db_to_gain(self.volume.get()));
self.gain = to;
for (sample, gain) in frame.iter_mut().zip(ramp(from, to)) {
*sample *= gain;
}
Ok(())
}
}