use crate::prelude::*;
#[derive(Clone, Debug)]
pub struct Delay<
S: Signal<Sample = B::Item>,
B: buf::BufferMut,
F: Map<Input = B::Item, Output = B::Item>,
> {
pub sgn: S,
pub loop_gen: gen::LoopBuf<B>,
pub feedback: F,
}
impl<S: Signal<Sample = B::Item>, B: buf::BufferMut, F: Map<Input = B::Item, Output = B::Item>>
Delay<S, B, F>
{
pub const fn new(sgn: S, buffer: B, feedback: F) -> Self {
Self {
sgn,
loop_gen: gen::LoopBuf::new(buffer),
feedback,
}
}
pub const fn buffer(&self) -> &B {
&self.loop_gen.buffer
}
pub fn buffer_mut(&mut self) -> &mut B {
&mut self.loop_gen.buffer
}
pub fn clear(&mut self) {
self.buffer_mut().clear();
}
pub fn read_sgn(&mut self) {
let sgn = self.sgn.get();
let buf = self.loop_gen._get();
let new_sgn = sgn + self.feedback.eval(buf);
let idx = self.loop_gen.index();
self.buffer_mut()[idx] = new_sgn;
self.loop_gen.advance();
}
}
impl<S: Signal, F: Map<Input = S::Sample, Output = S::Sample>> Delay<S, buf::Dyn<S::Sample>, F>
where
S::Sample: Audio,
{
pub fn new_owned(sgn: S, delay: unt::Time, feedback: F) -> Self {
Self::new(sgn, buf::Dyn::new_time(delay), feedback)
}
}
impl<S: Signal<Sample = B::Item>, B: buf::BufferMut, F: Map<Input = B::Item, Output = B::Item>>
Signal for Delay<S, B, F>
{
type Sample = S::Sample;
fn get(&self) -> Self::Sample {
self.loop_gen._get()
}
}
impl<
S: SignalMut<Sample = B::Item>,
B: buf::BufferMut,
F: Map<Input = B::Item, Output = B::Item>,
> SignalMut for Delay<S, B, F>
{
fn advance(&mut self) {
self.read_sgn();
self.sgn.advance();
}
fn retrigger(&mut self) {
self.sgn.retrigger();
self.loop_gen.retrigger();
self.clear();
}
}
impl<
S: Frequency<Sample = B::Item>,
B: buf::BufferMut,
F: Map<Input = B::Item, Output = B::Item>,
> Frequency for Delay<S, B, F>
{
fn freq(&self) -> unt::Freq {
self.sgn.freq()
}
fn freq_mut(&mut self) -> &mut unt::Freq {
self.sgn.freq_mut()
}
}
impl<S: Base<Sample = B::Item>, B: buf::BufferMut, F: Map<Input = B::Item, Output = B::Item>> Base
for Delay<S, B, F>
{
type Base = S::Base;
fn base(&self) -> &S::Base {
self.sgn.base()
}
fn base_mut(&mut self) -> &mut S::Base {
self.sgn.base_mut()
}
}
impl<S: Stop<Sample = B::Item>, B: buf::BufferMut, F: Map<Input = B::Item, Output = B::Item>> Stop
for Delay<S, B, F>
{
fn stop(&mut self) {
self.sgn.stop();
}
}
impl<S: Panic<Sample = B::Item>, B: buf::BufferMut, F: Map<Input = B::Item, Output = B::Item>> Panic
for Delay<S, B, F>
{
fn panic(&mut self) {
self.sgn.panic();
self.clear();
}
}
pub type Pure<S, B> = Delay<S, B, map::Zero<<S as Signal>::Sample, <S as Signal>::Sample>>;
impl<S: Signal<Sample = B::Item>, B: buf::BufferMut> Pure<S, B> {
pub const fn new_pure(sgn: S, buffer: B) -> Self {
Self::new(sgn, buffer, map::Zero::new())
}
}
impl<S: Signal> Pure<S, buf::Dyn<S::Sample>>
where
S::Sample: Audio,
{
pub fn new_pure_owned(sgn: S, delay: unt::Time) -> Self {
Self::new_owned(sgn, delay, map::Zero::new())
}
}
pub type Exp<S, B> = Delay<S, B, map::Pw<<S as Signal>::Sample, unt::Vol>>;
impl<S: Signal<Sample = B::Item>, B: buf::BufferMut> Exp<S, B> {
pub const fn new_exp(sgn: S, buffer: B, vol: unt::Vol) -> Self {
Self::new(sgn, buffer, map::Pw::new(vol))
}
pub const fn vol(&self) -> unt::Vol {
self.feedback.func
}
pub fn vol_mut(&mut self) -> &mut unt::Vol {
&mut self.feedback.func
}
}
impl<S: Signal> Exp<S, buf::Dyn<S::Sample>>
where
S::Sample: Audio,
{
pub fn new_exp_owned(sgn: S, delay: unt::Time, vol: unt::Vol) -> Self {
Self::new_owned(sgn, delay, map::Pw::new(vol))
}
}
pub type Flip<S, B> = Delay<S, B, map::Comp<map::Pw<smp::Stereo, unt::Vol>, map::Flip>>;
const fn comp_flip(vol: unt::Vol) -> map::Comp<map::Pw<smp::Stereo, unt::Vol>, map::Flip> {
map::Comp::new(map::Pw::new(vol), map::Flip)
}
impl<S: Signal<Sample = smp::Stereo>, B: buf::BufferMut<Item = smp::Stereo>> Flip<S, B> {
pub const fn new_flip(sgn: S, buffer: B, vol: unt::Vol) -> Self {
Self::new(sgn, buffer, comp_flip(vol))
}
pub const fn vol(&self) -> unt::Vol {
self.feedback.inner.func
}
pub fn vol_mut(&mut self) -> &mut unt::Vol {
&mut self.feedback.inner.func
}
}
impl<S: Signal<Sample = smp::Stereo>> Flip<S, buf::Dyn<S::Sample>> {
pub fn new_flip_owned(sgn: S, delay: unt::Time, vol: unt::Vol) -> Self {
Self::new_owned(sgn, delay, comp_flip(vol))
}
}