use crate::common::diagnostics::NamErrorCode;
use crate::math::common::AlignedVec;
use super::super::sinc_kernel::TAPS_PER_PHASE;
const DELAY_LINE_LEN: usize = TAPS_PER_PHASE * 2;
pub(crate) struct DelayLine {
pub buf: AlignedVec<f32>,
pub pos: usize,
}
impl DelayLine {
pub fn new() -> Result<Self, NamErrorCode> {
Ok(Self {
buf: AlignedVec::new(DELAY_LINE_LEN, 0.0f32)?,
pos: 0,
})
}
#[inline(always)]
pub fn push(&mut self, sample: f32) {
let pos = self.pos;
debug_assert!(pos < TAPS_PER_PHASE);
unsafe {
*self.buf.get_unchecked_mut(pos) = sample;
*self.buf.get_unchecked_mut(pos + TAPS_PER_PHASE) = sample;
}
self.pos += 1;
if self.pos >= TAPS_PER_PHASE {
self.pos = 0;
}
}
#[inline(always)]
pub fn window_ptr(&self) -> *const f32 {
debug_assert!(self.pos < TAPS_PER_PHASE);
debug_assert!(self.pos + TAPS_PER_PHASE <= DELAY_LINE_LEN);
unsafe { self.buf.as_ptr().add(self.pos) }
}
}