use {Frame, Sample};
use core;
use peak;
use ring_buffer;
use rms;
#[derive(Clone, Debug)]
pub struct Detector<F, D>
where
F: Frame,
D: Detect<F>,
{
last_env_frame: D::Output,
attack_gain: f32,
release_gain: f32,
detect: D,
}
pub trait Detect<F>
where
F: Frame,
{
type Output: Frame<NumChannels = F::NumChannels>;
fn detect(&mut self, frame: F) -> Self::Output;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Peak<R = peak::FullWave> {
rectifier: R,
}
impl<R> From<R> for Peak<R> {
fn from(rectifier: R) -> Self {
Peak { rectifier: rectifier }
}
}
impl Peak<peak::FullWave> {
pub fn full_wave() -> Self {
peak::FullWave.into()
}
}
impl Peak<peak::PositiveHalfWave> {
pub fn positive_half_wave() -> Self {
peak::PositiveHalfWave.into()
}
}
impl Peak<peak::NegativeHalfWave> {
pub fn negative_half_wave() -> Self {
peak::NegativeHalfWave.into()
}
}
impl<F, R> Detect<F> for Peak<R>
where
F: Frame,
R: peak::Rectifier<F>,
{
type Output = R::Output;
fn detect(&mut self, frame: F) -> Self::Output {
self.rectifier.rectify(frame)
}
}
impl<F, S> Detect<F> for rms::Rms<F, S>
where
F: Frame,
S: ring_buffer::Slice<Element = F::Float>
+ ring_buffer::SliceMut,
{
type Output = F::Float;
fn detect(&mut self, frame: F) -> Self::Output {
self.next(frame)
}
}
fn calc_gain(n_frames: f32) -> f32 {
if n_frames == 0.0 {
0.0
} else {
::ops::f32::powf32(core::f32::consts::E, -1.0 / n_frames)
}
}
impl<F, S> Detector<F, rms::Rms<F, S>>
where
F: Frame,
S: ring_buffer::Slice<Element = F::Float> + ring_buffer::SliceMut,
{
pub fn rms(buffer: ring_buffer::Fixed<S>, attack_frames: f32, release_frames: f32) -> Self {
let rms = rms::Rms::new(buffer);
Self::new(rms, attack_frames, release_frames)
}
}
impl<F, R> Detector<F, Peak<R>>
where
F: Frame,
R: peak::Rectifier<F>,
{
pub fn peak_from_rectifier(rectifier: R, attack_frames: f32, release_frames: f32) -> Self {
let peak = rectifier.into();
Self::new(peak, attack_frames, release_frames)
}
}
impl<F> Detector<F, Peak<peak::FullWave>>
where
F: Frame,
{
pub fn peak(attack_frames: f32, release_frames: f32) -> Self {
let peak = Peak::full_wave();
Self::new(peak, attack_frames, release_frames)
}
}
impl<F> Detector<F, Peak<peak::PositiveHalfWave>>
where
F: Frame,
{
pub fn peak_positive_half_wave(attack_frames: f32, release_frames: f32) -> Self {
let peak = Peak::positive_half_wave();
Self::new(peak, attack_frames, release_frames)
}
}
impl<F> Detector<F, Peak<peak::NegativeHalfWave>>
where
F: Frame,
{
pub fn peak_negative_half_wave(attack_frames: f32, release_frames: f32) -> Self {
let peak = Peak::negative_half_wave();
Self::new(peak, attack_frames, release_frames)
}
}
impl<F, D> Detector<F, D>
where
F: Frame,
D: Detect<F>,
{
fn new(detect: D, attack_frames: f32, release_frames: f32) -> Self {
Detector {
last_env_frame: D::Output::equilibrium(),
attack_gain: calc_gain(attack_frames),
release_gain: calc_gain(release_frames),
detect: detect,
}
}
pub fn set_attack_frames(&mut self, frames: f32) {
self.attack_gain = calc_gain(frames);
}
pub fn set_release_frames(&mut self, frames: f32) {
self.release_gain = calc_gain(frames);
}
pub fn next(&mut self, frame: F) -> D::Output {
let Detector {
attack_gain,
release_gain,
ref mut detect,
ref mut last_env_frame,
} = *self;
let detected_frame = detect.detect(frame);
let new_env_frame = last_env_frame.zip_map(detected_frame, |l, d| {
let gain = if l < d { attack_gain } else { release_gain };
let diff = l.add_amp(-d.to_signed_sample());
d.add_amp(diff.mul_amp(gain.to_sample()).to_sample())
});
*last_env_frame = new_env_frame;
new_env_frame
}
}