use {Compressor, Detector};
use envelope_detector::{Sample, Frame};
pub trait EvenGainFunction: Sized {
fn next_gain<F, D>(&mut Compressor<F, D, Self>, frame: F) -> <F::Sample as Sample>::Float
where F: Frame,
D: Detector<F>;
}
#[derive(Copy, Clone, Debug)]
pub enum Average {}
impl EvenGainFunction for Average {
#[inline]
fn next_gain<F, D>(compressor: &mut Compressor<F, D, Self>, frame: F) -> <F::Sample as Sample>::Float
where F: Frame,
D: Detector<F>,
{
let next_frame = compressor.next_gain_per_channel(frame);
let sum: <F::Sample as Sample>::Float =
next_frame.channels().fold(Sample::equilibrium(), |s, ch_gain| s + ch_gain);
sum / (F::n_channels() as f32).to_sample()
}
}
#[derive(Copy, Clone, Debug)]
pub enum Minimum {}
impl EvenGainFunction for Minimum {
#[inline]
fn next_gain<F, D>(compressor: &mut Compressor<F, D, Self>, frame: F) -> <F::Sample as Sample>::Float
where F: Frame,
D: Detector<F>,
{
let next_frame = compressor.next_gain_per_channel(frame);
let one = <F::Sample as Sample>::identity();
next_frame.channels().fold(one, |min, ch_gain| if ch_gain < min { ch_gain } else { min })
}
}