use crate::scale8::{scale8, scale16};
use crate::trig8::{sin8, sin16};
use crate::{Accum88, Fract8, Fract16};
#[inline]
pub const fn beat88(bpm88: Accum88, timebase: u32, now_millis: u32) -> u16 {
let elapsed = now_millis.wrapping_sub(timebase);
let scaled = elapsed.wrapping_mul(bpm88.0 as u32).wrapping_mul(280);
(scaled >> 16) as u16
}
#[inline]
pub const fn beat16(bpm: u16, timebase: u32, now_millis: u32) -> u16 {
let bpm88 = if bpm < 256 { bpm << 8 } else { bpm };
beat88(Accum88(bpm88), timebase, now_millis)
}
#[inline]
pub const fn beat8(bpm: u16, timebase: u32, now_millis: u32) -> u8 {
(beat16(bpm, timebase, now_millis) >> 8) as u8
}
#[inline]
pub const fn beatsin88(
bpm88: Accum88,
lowest: u16,
highest: u16,
timebase: u32,
phase_offset: u16,
now_millis: u32,
) -> u16 {
let beat = beat88(bpm88, timebase, now_millis);
let beatsin = (sin16(beat.wrapping_add(phase_offset)) as i32 + 32768) as u16;
let rangewidth = highest.wrapping_sub(lowest);
let scaledbeat = scale16(beatsin, Fract16(rangewidth));
lowest.wrapping_add(scaledbeat)
}
#[inline]
pub const fn beatsin16(
bpm: u16,
lowest: u16,
highest: u16,
timebase: u32,
phase_offset: u16,
now_millis: u32,
) -> u16 {
let beat = beat16(bpm, timebase, now_millis);
let beatsin = (sin16(beat.wrapping_add(phase_offset)) as i32 + 32768) as u16;
let rangewidth = highest.wrapping_sub(lowest);
let scaledbeat = scale16(beatsin, Fract16(rangewidth));
lowest.wrapping_add(scaledbeat)
}
#[inline]
pub const fn beatsin8(
bpm: u16,
lowest: u8,
highest: u8,
timebase: u32,
phase_offset: u8,
now_millis: u32,
) -> u8 {
let beat = beat8(bpm, timebase, now_millis);
let beatsin = sin8(beat.wrapping_add(phase_offset));
let rangewidth = highest.wrapping_sub(lowest);
let scaledbeat = scale8(beatsin, Fract8(rangewidth));
lowest.wrapping_add(scaledbeat)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BeatSin8 {
bpm: u16,
lowest: u8,
highest: u8,
timebase: u32,
phase_offset: u8,
}
impl BeatSin8 {
#[inline]
pub const fn new(bpm: u16) -> Self {
Self {
bpm,
lowest: 0,
highest: 255,
timebase: 0,
phase_offset: 0,
}
}
#[inline]
pub const fn range(mut self, lowest: u8, highest: u8) -> Self {
self.lowest = lowest;
self.highest = highest;
self
}
#[inline]
pub const fn timebase(mut self, timebase: u32) -> Self {
self.timebase = timebase;
self
}
#[inline]
pub const fn phase_offset(mut self, phase_offset: u8) -> Self {
self.phase_offset = phase_offset;
self
}
#[inline]
pub const fn at(&self, now_millis: u32) -> u8 {
beatsin8(
self.bpm,
self.lowest,
self.highest,
self.timebase,
self.phase_offset,
now_millis,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BeatSin16 {
bpm: u16,
lowest: u16,
highest: u16,
timebase: u32,
phase_offset: u16,
}
impl BeatSin16 {
#[inline]
pub const fn new(bpm: u16) -> Self {
Self {
bpm,
lowest: 0,
highest: 65535,
timebase: 0,
phase_offset: 0,
}
}
#[inline]
pub const fn range(mut self, lowest: u16, highest: u16) -> Self {
self.lowest = lowest;
self.highest = highest;
self
}
#[inline]
pub const fn timebase(mut self, timebase: u32) -> Self {
self.timebase = timebase;
self
}
#[inline]
pub const fn phase_offset(mut self, phase_offset: u16) -> Self {
self.phase_offset = phase_offset;
self
}
#[inline]
pub const fn at(&self, now_millis: u32) -> u16 {
beatsin16(
self.bpm,
self.lowest,
self.highest,
self.timebase,
self.phase_offset,
now_millis,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BeatSin88 {
bpm88: Accum88,
lowest: u16,
highest: u16,
timebase: u32,
phase_offset: u16,
}
impl BeatSin88 {
#[inline]
pub const fn new(bpm88: Accum88) -> Self {
Self {
bpm88,
lowest: 0,
highest: 65535,
timebase: 0,
phase_offset: 0,
}
}
#[inline]
pub const fn range(mut self, lowest: u16, highest: u16) -> Self {
self.lowest = lowest;
self.highest = highest;
self
}
#[inline]
pub const fn timebase(mut self, timebase: u32) -> Self {
self.timebase = timebase;
self
}
#[inline]
pub const fn phase_offset(mut self, phase_offset: u16) -> Self {
self.phase_offset = phase_offset;
self
}
#[inline]
pub const fn at(&self, now_millis: u32) -> u16 {
beatsin88(
self.bpm88,
self.lowest,
self.highest,
self.timebase,
self.phase_offset,
now_millis,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn beat88_ramps_and_wraps_with_time() {
assert_eq!(beat88(Accum88(120 << 8), 0, 0), 0);
assert_eq!(beat88(Accum88(120 << 8), 0, 250), 32812);
assert_eq!(beat88(Accum88(120 << 8), 0, 500), 89);
}
#[test]
fn beat16_promotes_plain_bpm_to_q88() {
assert_eq!(beat16(120, 0, 250), beat16(120 << 8, 0, 250));
assert_eq!(beat16(120, 0, 250), beat88(Accum88(120 << 8), 0, 250));
}
#[test]
fn beat8_is_high_byte_of_beat16() {
for ms in (0..=2000u32).step_by(37) {
assert_eq!(beat8(120, 0, ms), (beat16(120, 0, ms) >> 8) as u8);
}
}
#[test]
fn timebase_shifts_phase() {
assert_eq!(
beat88(Accum88(120 << 8), 1000, 1500),
beat88(Accum88(120 << 8), 0, 500)
);
}
#[test]
fn beatsin_outputs_stay_within_their_range() {
for ms in (0..=10_000u32).step_by(73) {
let v8 = beatsin8(120, 10, 200, 0, 0, ms);
assert!((10..=200).contains(&v8), "beatsin8 out of range: {v8}");
let v16 = beatsin16(120, 1000, 60000, 0, 0, ms);
assert!(
(1000..=60000).contains(&v16),
"beatsin16 out of range: {v16}"
);
let v88 = beatsin88(Accum88(120 << 8), 1000, 60000, 0, 0, ms);
assert!(
(1000..=60000).contains(&v88),
"beatsin88 out of range: {v88}"
);
}
}
#[test]
fn beatsin_full_range_hits_both_ends() {
let mut min8 = u8::MAX;
let mut max8 = 0u8;
for ms in (0..=2000u32).step_by(5) {
let v = beatsin8(120, 0, 255, 0, 0, ms);
min8 = min8.min(v);
max8 = max8.max(v);
}
assert!(min8 < 10, "min8={min8}");
assert!(max8 > 245, "max8={max8}");
}
#[test]
fn phase_offset_shifts_the_wave() {
let ms = 1234;
let unshifted = beatsin8(120, 0, 255, 0, 0, ms);
let shifted = beatsin8(120, 0, 255, 0, 128, ms);
assert_ne!(unshifted, shifted);
}
#[test]
fn builders_match_their_function_counterparts() {
for ms in (0..=2000u32).step_by(37) {
assert_eq!(
BeatSin8::new(120)
.range(10, 200)
.timebase(50)
.phase_offset(64)
.at(ms),
beatsin8(120, 10, 200, 50, 64, ms)
);
assert_eq!(
BeatSin16::new(120)
.range(1000, 60000)
.timebase(50)
.phase_offset(1234)
.at(ms),
beatsin16(120, 1000, 60000, 50, 1234, ms)
);
assert_eq!(
BeatSin88::new(Accum88(120 << 8))
.range(1000, 60000)
.timebase(50)
.phase_offset(1234)
.at(ms),
beatsin88(Accum88(120 << 8), 1000, 60000, 50, 1234, ms)
);
}
}
#[test]
fn builder_defaults_cover_the_full_output_range() {
assert_eq!(BeatSin8::new(120).at(0), beatsin8(120, 0, 255, 0, 0, 0));
assert_eq!(BeatSin16::new(120).at(0), beatsin16(120, 0, 65535, 0, 0, 0));
assert_eq!(
BeatSin88::new(Accum88(120 << 8)).at(0),
beatsin88(Accum88(120 << 8), 0, 65535, 0, 0, 0)
);
}
}