use alloc::sync::Arc;
use core::mem;
use crate::{frame, math::Float, swap, Frame, Signal};
pub struct Fader<T> {
progress: f32,
next: swap::Receiver<Option<Command<T>>>,
inner: T,
}
impl<T: Send> Fader<T> {
pub fn new(inner: T) -> (FaderControl<T>, Self) {
let (send, recv) = swap::swap(|| None);
let signal = Self {
progress: 1.0,
next: recv,
inner,
};
let control = FaderControl(send);
(control, signal)
}
}
impl<T: Signal> Signal for Fader<T>
where
T::Frame: Frame,
{
type Frame = T::Frame;
#[allow(clippy::float_cmp)]
fn sample(&mut self, interval: f32, mut out: &mut [T::Frame]) {
if self.progress >= 1.0 {
if self.next.refresh() {
self.progress = 0.0;
} else {
self.inner.sample(interval, out);
return;
}
}
let next = (*self.next.received()).as_mut().unwrap();
let increment = interval / next.duration;
while !out.is_empty() {
let mut buffer = [(); 1024].map(|()| T::Frame::ZERO);
let n = buffer.len().min(out.len());
self.inner.sample(interval, &mut buffer);
next.fade_to.sample(interval, out);
for (o, x) in out.iter_mut().zip(&buffer) {
let fade_out = (1.0 - self.progress).sqrt();
let fade_in = self.progress.sqrt();
*o = frame::mix(&frame::scale(x, fade_out), &frame::scale(o, fade_in));
self.progress = (self.progress + increment).min(1.0);
}
out = &mut out[n..];
}
if self.progress >= 1.0 {
mem::swap(&mut self.inner, &mut next.fade_to);
}
}
#[inline]
fn is_finished(&self) -> bool {
false
}
}
pub struct FaderControl<T>(swap::Sender<Option<Command<T>>>);
impl<T> FaderControl<T> {
pub fn fade_to(&mut self, signal: T, duration: f32) {
*self.0.pending() = Some(Command {
fade_to: signal,
duration,
});
self.0.flush()
}
}
struct Command<T> {
fade_to: T,
duration: f32,
}
#[cfg(test)]
mod tests {
use crate::Constant;
use super::*;
#[test]
fn smoke() {
let (mut c, mut s) = Fader::new(Constant(1.0));
let mut buf = [42.0; 12];
s.sample(0.1, &mut buf);
assert_eq!(buf, [1.0; 12]);
c.fade_to(Constant(0.0), 1.0);
s.sample(0.1, &mut buf);
assert_eq!(buf[0], 1.0);
assert_eq!(buf[11], 0.0);
assert!((buf[5] - 0.5f32.sqrt()).abs() < 1e-6);
}
}