#[derive(Debug)]
pub struct Tuning {
steps: [u32; 128],
sample_period: f32,
}
impl Tuning {
#[must_use]
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "The phase increment is positive and at most 2^31. The nonzero u32 clock ratio fits in f32."
)]
pub const fn new(numerator: u32, denominator: u32) -> Option<Self> {
if numerator == 0 || denominator == 0 {
return None;
}
let mut steps = [0; 128];
let mut step =
8.175_798_915_643_707 * 4_294_967_296.0 * denominator as f64 / numerator as f64;
let mut note: usize = 0;
while note < steps.len() {
if step < 2_147_483_648.0 {
steps[note] = (step + 0.5) as u32;
}
step *= 1.059_463_094_359_295_3;
note = note.wrapping_add(1);
}
Some(Self {
steps,
sample_period: (denominator as f64 / numerator as f64) as f32,
})
}
#[must_use]
pub const fn phase_step(&self, note: u8) -> u32 {
if (note as usize) < self.steps.len() {
self.steps[note as usize]
} else {
0
}
}
#[must_use]
pub const fn sample_period(&self) -> f32 {
self.sample_period
}
}