1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{cell::Cell, sync::Arc};
use crate::{frame, Frame, Frames, Signal};
pub struct Cycle<T> {
cursor: Cell<f32>,
frames: Arc<Frames<T>>,
}
impl<T> Cycle<T> {
pub fn new(frames: Arc<Frames<T>>) -> Self {
Self {
cursor: Cell::new(0.0),
frames,
}
}
fn interpolate(&self, sample: f32) -> T
where
T: Frame,
{
let a = sample as usize;
let b = (a + 1) % self.frames.len();
frame::lerp(&self.frames[a], &self.frames[b], sample.fract())
}
}
impl<T: Frame + Copy> Signal for Cycle<T> {
type Frame = T;
fn sample(&self, interval: f32, out: &mut [T]) {
let ds = interval * self.frames.rate() as f32;
for x in out {
*x = self.interpolate(self.cursor.get());
self.cursor
.set((self.cursor.get() + ds) % self.frames.len() as f32);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const FRAMES: &[f32] = &[1.0, 2.0, 3.0];
#[test]
fn wrap_single() {
let s = Cycle::new(Frames::from_slice(1, FRAMES));
let mut buf = [0.0; 5];
s.sample(1.0, &mut buf);
assert_eq!(buf, [1.0, 2.0, 3.0, 1.0, 2.0]);
}
#[test]
fn wrap_multi() {
let s = Cycle::new(Frames::from_slice(1, FRAMES));
let mut buf = [0.0; 5];
s.sample(1.0, &mut buf[..2]);
s.sample(1.0, &mut buf[2..]);
assert_eq!(buf, [1.0, 2.0, 3.0, 1.0, 2.0]);
}
}