#![cfg_attr(not(feature = "std"), no_std)]
use dasp_frame::Frame;
use dasp_sample::Sample;
pub trait Rectifier<F>
where
F: Frame,
{
type Output: Frame<NumChannels = F::NumChannels>;
fn rectify(&mut self, frame: F) -> Self::Output;
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct FullWave;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct PositiveHalfWave;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct NegativeHalfWave;
impl<F> Rectifier<F> for FullWave
where
F: Frame,
{
type Output = F::Signed;
fn rectify(&mut self, frame: F) -> Self::Output {
full_wave(frame)
}
}
impl<F> Rectifier<F> for PositiveHalfWave
where
F: Frame,
{
type Output = F;
fn rectify(&mut self, frame: F) -> Self::Output {
positive_half_wave(frame)
}
}
impl<F> Rectifier<F> for NegativeHalfWave
where
F: Frame,
{
type Output = F;
fn rectify(&mut self, frame: F) -> Self::Output {
negative_half_wave(frame)
}
}
pub fn full_wave<F>(frame: F) -> F::Signed
where
F: Frame,
{
frame.map(|s| {
let signed = s.to_signed_sample();
if signed < Sample::EQUILIBRIUM {
-signed
} else {
signed
}
})
}
pub fn positive_half_wave<F>(frame: F) -> F
where
F: Frame,
{
frame.map(|s| {
if s < Sample::EQUILIBRIUM {
Sample::EQUILIBRIUM
} else {
s
}
})
}
pub fn negative_half_wave<F>(frame: F) -> F
where
F: Frame,
{
frame.map(|s| {
if s > Sample::EQUILIBRIUM {
Sample::EQUILIBRIUM
} else {
s
}
})
}