use plughost_core::Sample;
pub(crate) struct DryDelay {
channels: Vec<Vec<f64>>,
write: usize,
delay: usize,
}
impl DryDelay {
pub fn new(channels: usize, capacity: usize) -> DryDelay {
DryDelay {
channels: vec![vec![0.0; capacity]; channels],
write: 0,
delay: 0,
}
}
fn capacity(&self) -> usize {
self.channels.first().map_or(0, Vec::len)
}
pub fn set_delay(&mut self, delay: usize) {
self.delay = delay.min(self.capacity());
}
pub fn push<S: Sample>(&mut self, input: Option<&[&[S]]>, frames: usize) {
let capacity = self.capacity();
if capacity == 0 {
return;
}
for (index, history) in self.channels.iter_mut().enumerate() {
let source = input.and_then(|input| input.get(index));
for frame in 0..frames {
history[(self.write + frame) % capacity] =
source.map_or(0.0, |source| source[frame].to_f64());
}
}
self.write = (self.write + frames) % capacity;
}
pub fn delayed(&self, channel: usize, frame: usize, frames: usize) -> f64 {
let capacity = self.capacity();
let Some(history) = self.channels.get(channel) else {
return 0.0;
};
let back = frames - frame + self.delay;
if back > capacity {
return 0.0;
}
history[(self.write + capacity - back) % capacity]
}
}