use crate::{SignalNext, word};
#[doc = crate::_tags!(num signal wave primitive)]
#[repr(transparent)]
#[doc = crate::_doc_meta!{
location("num/signal", struct Phase),
test_size_of(Phase = 4|32; niche !Option),
}]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Phase(pub u32);
word! { impl Phase(u32); }
#[rustfmt::skip]
impl Phase {
pub const ZERO: Self = Self(0);
pub const QUARTER: Self = Self(0x4000_0000);
pub const HALF: Self = Self(0x8000_0000);
pub const THREE_QUARTERS: Self = Self(0xC000_0000);
pub const fn new(raw: u32) -> Self { Self(raw) }
pub const fn advance(self, step: PhaseStep) -> Self {
Self(self.0.wrapping_add(step.0))
}
pub const fn as_f32(self) -> f32 { self.0 as f32 * (1.0 / (u32::MAX as f32 + 1.0)) }
pub const fn as_f64(self) -> f64 { self.0 as f64 * (1.0 / (u32::MAX as f64 + 1.0)) }
}
#[doc = crate::_tags!(num signal wave primitive)]
#[doc = crate::_doc_meta!{
location("num/signal", struct PhaseStep),
test_size_of(PhaseStep = 4|32; niche !Option),
}]
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PhaseStep(pub u32);
word! { impl PhaseStep(u32); }
#[rustfmt::skip]
impl PhaseStep {
pub const ZERO: Self = Self(0);
pub const fn new(raw: u32) -> Self {
Self(raw)
}
}
#[doc = crate::_tags!(num signal wave)]
#[doc = crate::_doc_meta!{
location("num/signal", struct PhaseAccum),
test_size_of(PhaseAccum = 8|64; niche !Option),
}]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PhaseAccum {
pub phase: Phase,
pub step: PhaseStep,
}
impl PhaseAccum {
pub const fn new(phase: Phase, step: PhaseStep) -> Self {
Self { phase, step }
}
pub const fn next_phase(&mut self) -> Phase {
let phase = self.phase;
self.phase = self.phase.advance(self.step);
phase
}
pub const fn advance_phase(&mut self) -> Phase {
self.phase = self.phase.advance(self.step);
self.phase
}
}
impl SignalNext for PhaseAccum {
type Sample = Phase;
fn next(&mut self) -> Phase {
self.next_phase()
}
}