31 lines
653 B
Rust
31 lines
653 B
Rust
pub struct DelayLine<const LEN: usize> {
|
|
buffer: [f32; LEN],
|
|
index: usize,
|
|
}
|
|
|
|
impl<const LEN: usize> DelayLine<LEN> {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
buffer: [0.0; LEN],
|
|
index: 0,
|
|
}
|
|
}
|
|
|
|
pub fn read_slice(&self, slice: &mut [f32]) {
|
|
// Copy values in order
|
|
for i in 0..LEN {
|
|
slice[i] = self.buffer[(self.index + i - LEN) % LEN];
|
|
}
|
|
}
|
|
|
|
pub fn write_and_advance(&mut self, value: f32) {
|
|
self.buffer[self.index] = value;
|
|
|
|
if self.index == LEN - 1 {
|
|
self.index = 0;
|
|
} else {
|
|
self.index += 1;
|
|
}
|
|
}
|
|
}
|