Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
use alloc::sync::Arc;
use core::cell::Cell;

use crate::{frame, math::Float, Frame, Frames, Seek, Signal};
use crate::{frame, frame::lerp, math::Float, Frame, Frames, Seek, Signal};

/// Loops [`Frames`] end-to-end to construct a repeating signal
///
/// To avoid glitching at the loop point, the underlying `Frames` *must* be specially prepared to
/// provide a smooth transition.
#[derive(Clone)]
pub struct Cycle<T> {
/// Current playback time, in samples
cursor: Cell<f64>,
frames: Arc<Frames<T>>,
}

impl<T> Cycle<T> {
/// Construct cycle from `frames` played at `rate` Hz, smoothing the loop point over
/// `crossfade_size` seconds
///
/// Suitable for use with arbitrary audio,
pub fn with_crossfade(crossfade_size: f32, rate: u32, frames: &[T]) -> Self
where
T: Frame + Copy,
{
let mut frames = frames.iter().copied().collect::<alloc::vec::Vec<_>>();
let frames = apply_crossfade((crossfade_size * rate as f32) as usize, &mut frames);
Self::new(Frames::from_slice(rate, frames))
}

/// Construct cycle from `frames`
// TODO: Crossfade
///
/// `frames` *must* be specially constructed to loop seamlessly. For arbitrary audio, use
/// [`with_crossfade`](Self::with_crossfade) instead.
pub fn new(frames: Arc<Frames<T>>) -> Self {
Self {
cursor: Cell::new(0.0),
Expand Down Expand Up @@ -62,6 +81,18 @@ impl<T: Frame + Copy> Seek for Cycle<T> {
}
}

/// Prepare arbitrary frames for glitch-free use in `Cycle`
fn apply_crossfade<T: Frame + Copy>(size: usize, frames: &mut [T]) -> &mut [T] {
let end = frames.len() - size;
for i in 0..size {
let a = frames[end + i];
let b = frames[i];
let t = (i + 1) as f32 / (size + 1) as f32;
frames[i] = lerp(&a, &b, t);
}
&mut frames[..end]
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -122,4 +153,14 @@ mod tests {
s.sample(10.0, &mut buf[2..]);
assert_eq!(buf, [1.0, 2.0, 3.0]);
}

#[test]
fn crossfade() {
let mut frames = [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
let faded = apply_crossfade(4, &mut frames);
assert_eq!(faded.len(), 5);
for i in 0..5 {
assert!((faded[i] - (5 - (i + 1)) as f32 / 5.0).abs() < 1e-3);
}
}
}
28 changes: 25 additions & 3 deletions src/fader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::{
cell::{Cell, UnsafeCell},
mem,
time::Duration,
};
use std::time::Instant;

use crate::{frame, math::Float, Controlled, Filter, Frame, Signal, Swap};

Expand Down Expand Up @@ -36,11 +38,17 @@ where
fn sample(&self, interval: f32, mut out: &mut [T::Frame]) {
let inner = unsafe { &mut *self.inner.get() };

if self.progress.get() >= 1.0 {
if self.progress.get() >= 1.0 || self.progress.get() < 0.0 {
// A fade must complete before a new one begins
if self.next.refresh() {
self.progress.set(0.0);
} else {
let time_diff = unsafe { (*self.next.received()).as_mut().unwrap() }
.begin
.map(|i| i.saturating_duration_since(Instant::now()).as_secs_f32())
.unwrap_or(0.0);
self.progress.set(-time_diff);
}

if !self.next.refresh() || self.progress.get() < 0.0 {
// Fast path
inner.sample(interval, out);
return;
Expand Down Expand Up @@ -112,6 +120,19 @@ impl<'a, T> FaderControl<'a, T> {
*self.0.pending() = Some(Command {
fade_to: signal,
duration,
begin: None,
});
}
self.0.flush()
}

/// Crossfade to `signal` over `duration`, after `seconds_from_now`.
pub fn deferred_fade_to(&mut self, signal: T, duration: f32, seconds_from_now: f32) {
unsafe {
*self.0.pending() = Some(Command {
fade_to: signal,
duration,
begin: Some(Instant::now() + Duration::from_secs_f32(seconds_from_now)),
});
}
self.0.flush()
Expand All @@ -121,6 +142,7 @@ impl<'a, T> FaderControl<'a, T> {
struct Command<T> {
fade_to: T,
duration: f32,
begin: Option<Instant>,
}

#[cfg(test)]
Expand Down
35 changes: 29 additions & 6 deletions src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T> Sender<T> {
/// Append a prefix of `data` to the channel
///
/// Returns the number of items sent.
pub fn send_from_slice(&mut self, data: &[T]) -> usize
pub fn send_from_slice(&mut self, data: &[T]) -> SendSliceResult
where
T: Copy,
{
Expand Down Expand Up @@ -63,7 +63,12 @@ impl<T> Sender<T> {
.header
.write
.store((write + n) % size, Ordering::Release);
n

if n < data.len() {
SendSliceResult::PushedElements(n)
} else {
SendSliceResult::RemainingSlots(free.0.len() + free.1.len() - n)
}
}
}

Expand Down Expand Up @@ -97,6 +102,12 @@ impl<T> Sender<T> {
}
}

#[derive(PartialEq, Eq, Debug)]
pub enum SendSliceResult {
RemainingSlots(usize),
PushedElements(usize),
}

pub struct Receiver<T> {
shared: Arc<Shared<T>>,
len: usize,
Expand Down Expand Up @@ -296,7 +307,10 @@ mod tests {
#[test]
fn send_excess() {
let (mut send, mut recv) = channel::<u32>(4);
assert_eq!(send.send_from_slice(&[1, 2, 3, 4, 5]), 4);
assert_eq!(
send.send_from_slice(&[1, 2, 3, 4, 5]),
SendSliceResult::PushedElements(4)
);
recv.update();
assert_eq!(recv.len(), 4);
assert_eq!(recv[0], 1);
Expand All @@ -308,10 +322,19 @@ mod tests {
#[test]
fn fill_release_fill() {
let (mut send, mut recv) = channel::<u32>(4);
assert_eq!(send.send_from_slice(&[1, 2, 3, 4]), 4);
assert_eq!(
send.send_from_slice(&[1, 2, 3, 4]),
SendSliceResult::RemainingSlots(0)
);
recv.update();
recv.release(2);
assert_eq!(send.send_from_slice(&[5, 6, 7]), 2);
assert_eq!(send.send_from_slice(&[7]), 0);
assert_eq!(
send.send_from_slice(&[5, 6, 7]),
SendSliceResult::PushedElements(2)
);
assert_eq!(
send.send_from_slice(&[7]),
SendSliceResult::PushedElements(0)
);
}
}
42 changes: 30 additions & 12 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ impl<T: Frame + Copy> Signal for Stream<T> {
self.advance(interval * out.len() as f32);
}

#[allow(clippy::float_cmp)]
fn is_finished(&self) -> bool {
if !self.closed.get() {
return false;
}
self.t.get() == self.inner.borrow().len() as f32
self.closed.get()
}

fn handle_dropped(&self) {
Expand All @@ -116,15 +112,25 @@ where
}

impl<'a, T> StreamControl<'a, T> {
/// Add more samples. Returns the number of samples read.
pub fn write(&mut self, samples: &[T]) -> usize
/// Add more frames. Returns either the number of frames read if they couldn't all fit inside
/// the buffer, or the remaining number of frames that can be written in the buffer.
pub fn write(&mut self, samples: &[T]) -> StreamWriteResult
where
T: Copy,
{
self.0.send.borrow_mut().send_from_slice(samples)
match self.0.send.borrow_mut().send_from_slice(samples) {
spsc::SendSliceResult::RemainingSlots(n) => StreamWriteResult::AvailableSpace(n),
spsc::SendSliceResult::PushedElements(n) => StreamWriteResult::FramesRead(n),
}
}
}

#[derive(PartialEq, Eq, Debug)]
pub enum StreamWriteResult {
FramesRead(usize),
AvailableSpace(usize),
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -139,10 +145,19 @@ mod tests {
#[test]
fn smoke() {
let s = Stream::<f32>::new(1, 3);
assert_eq!(StreamControl(&s).write(&[1.0, 2.0]), 2);
assert_eq!(StreamControl(&s).write(&[3.0, 4.0]), 1);
assert_eq!(
StreamControl(&s).write(&[1.0, 2.0]),
StreamWriteResult::AvailableSpace(1)
);
assert_eq!(
StreamControl(&s).write(&[3.0, 4.0]),
StreamWriteResult::FramesRead(1)
);
assert_out(&s, &[1.0, 2.0, 3.0, 0.0, 0.0]);
assert_eq!(StreamControl(&s).write(&[5.0, 6.0, 7.0, 8.0]), 3);
assert_eq!(
StreamControl(&s).write(&[5.0, 6.0, 7.0, 8.0]),
StreamWriteResult::FramesRead(3)
);
assert_out(&s, &[5.0]);
assert_out(&s, &[6.0, 7.0, 0.0, 0.0]);
assert_out(&s, &[0.0, 0.0]);
Expand All @@ -151,7 +166,10 @@ mod tests {
#[test]
fn cleanup() {
let s = Stream::<f32>::new(1, 4);
assert_eq!(StreamControl(&s).write(&[1.0, 2.0]), 2);
assert_eq!(
StreamControl(&s).write(&[1.0, 2.0]),
StreamWriteResult::AvailableSpace(2)
);
assert!(!s.is_finished());
s.handle_dropped();
assert!(!s.is_finished());
Expand Down