#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeatClock {
beat_ticks: u64,
beats_per_bar: u32,
markers: Vec<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct BeatPosition {
pub bar: u32,
pub beat: u32,
pub beat_phase: f32,
pub bar_phase: f32,
pub section: Option<usize>,
}
impl BeatClock {
pub fn new(
beat_ticks: u64,
beats_per_bar: u32,
markers: impl IntoIterator<Item = u64>,
) -> Self {
let mut markers: Vec<u64> = markers.into_iter().collect();
markers.sort_unstable();
markers.dedup();
BeatClock {
beat_ticks: beat_ticks.max(1),
beats_per_bar: beats_per_bar.max(1),
markers,
}
}
pub fn beat_ticks(&self) -> u64 {
self.beat_ticks
}
pub fn beats_per_bar(&self) -> u32 {
self.beats_per_bar
}
pub fn sections(&self) -> usize {
self.markers.len()
}
pub fn at(&self, tick: u64) -> BeatPosition {
let total_beat = tick / self.beat_ticks;
let bpb = self.beats_per_bar as u64;
let beat = (total_beat % bpb) as u32;
let beat_phase = (tick % self.beat_ticks) as f32 / self.beat_ticks as f32;
BeatPosition {
bar: u32::try_from(total_beat / bpb).unwrap_or(u32::MAX),
beat,
beat_phase,
bar_phase: (beat as f32 + beat_phase) / self.beats_per_bar as f32,
section: self.markers.partition_point(|&m| m <= tick).checked_sub(1),
}
}
pub fn ticks_to_next_section(&self, tick: u64) -> Option<u64> {
let i = self.markers.partition_point(|&m| m <= tick);
self.markers.get(i).map(|&m| m - tick)
}
fn total_beat(&self, tick: u64) -> u64 {
tick / self.beat_ticks
}
}
const BEAT_FLASH_SECS: f32 = 0.18;
const BAR_FLASH_SECS: f32 = 0.35;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct BeatFrame {
pub position: BeatPosition,
pub beat_started: bool,
pub bar_started: bool,
pub section_changed: bool,
pub jumped: bool,
pub beat_flash: f32,
pub bar_flash: f32,
}
#[derive(Debug, Clone)]
pub struct BeatTracker {
clock: BeatClock,
last_tick: Option<u64>,
beat_flash: f32,
bar_flash: f32,
}
impl BeatTracker {
pub fn new(clock: BeatClock) -> Self {
BeatTracker {
clock,
last_tick: None,
beat_flash: 0.0,
bar_flash: 0.0,
}
}
pub fn clock(&self) -> &BeatClock {
&self.clock
}
pub fn set_clock(&mut self, clock: BeatClock) {
*self = BeatTracker::new(clock);
}
pub fn reset(&mut self) {
self.last_tick = None;
self.beat_flash = 0.0;
self.bar_flash = 0.0;
}
pub fn update(&mut self, tick: u64, dt: f32) -> BeatFrame {
let position = self.clock.at(tick);
if dt.is_finite() && dt > 0.0 {
self.beat_flash *= (-dt / BEAT_FLASH_SECS).exp();
self.bar_flash *= (-dt / BAR_FLASH_SECS).exp();
if self.beat_flash < 0.002 {
self.beat_flash = 0.0;
}
if self.bar_flash < 0.002 {
self.bar_flash = 0.0;
}
}
let mut frame = BeatFrame {
position,
..BeatFrame::default()
};
if let Some(last) = self.last_tick {
if tick < last {
frame.jumped = true;
} else {
let (a, b) = (self.clock.total_beat(last), self.clock.total_beat(tick));
if b != a {
frame.beat_started = true;
self.beat_flash = 1.0;
let bpb = self.clock.beats_per_bar as u64;
if b / bpb != a / bpb {
frame.bar_started = true;
self.bar_flash = 1.0;
}
}
}
frame.section_changed = self.clock.at(last).section != position.section;
}
self.last_tick = Some(tick);
frame.beat_flash = self.beat_flash;
frame.bar_flash = self.bar_flash;
frame
}
}
#[cfg(test)]
mod tests {
use super::*;
fn clock() -> BeatClock {
BeatClock::new(100, 4, [1200, 800])
}
#[test]
fn position_splits_ticks_into_bar_beat_and_phase() {
let c = clock();
let p = c.at(0);
assert_eq!((p.bar, p.beat), (0, 0));
assert_eq!((p.beat_phase, p.bar_phase), (0.0, 0.0));
assert_eq!(p.section, None, "最初のマーカーより前");
let p = c.at(650);
assert_eq!((p.bar, p.beat), (1, 2));
assert!((p.beat_phase - 0.5).abs() < 1e-6);
assert!((p.bar_phase - (2.5 / 4.0)).abs() < 1e-6);
assert_eq!(c.at(799).section, None);
assert_eq!(c.at(800).section, Some(0), "マーカーの位置ちょうどで入る");
assert_eq!(c.at(1199).section, Some(0));
assert_eq!(c.at(5000).section, Some(1));
assert_eq!(c.sections(), 2, "順不同の入力は昇順にそろう");
}
#[test]
fn degenerate_inputs_do_not_panic() {
let c = BeatClock::new(0, 0, [5, 5]);
assert_eq!((c.beat_ticks(), c.beats_per_bar(), c.sections()), (1, 1, 1));
let p = c.at(u64::MAX);
assert_eq!(p.bar, u32::MAX, "桁あふれは上限で止める");
assert!(p.beat_phase.is_finite() && p.bar_phase.is_finite());
}
#[test]
fn the_distance_to_the_next_section_counts_down() {
let c = BeatClock::new(100, 4, [400, 800]);
assert_eq!(c.ticks_to_next_section(0), Some(400));
assert_eq!(c.ticks_to_next_section(390), Some(10));
assert_eq!(
c.ticks_to_next_section(400),
Some(400),
"マーカーの上なら次まで"
);
assert_eq!(c.ticks_to_next_section(800), None, "最後のマーカーの後");
assert_eq!(BeatClock::new(100, 4, []).ticks_to_next_section(5), None);
}
#[test]
fn tracker_marks_beats_bars_and_sections_once() {
let mut t = BeatTracker::new(clock());
let f = t.update(10, 0.016);
assert!(!f.beat_started && !f.bar_started, "最初は光らない");
let f = t.update(110, 0.016);
assert!(f.beat_started && !f.bar_started);
assert_eq!(f.beat_flash, 1.0);
let f = t.update(120, 0.016);
assert!(!f.beat_started, "同じ拍の中では出さない");
assert!(f.beat_flash < 1.0 && f.beat_flash > 0.5);
let f = t.update(390, 0.016);
assert!(f.beat_started && !f.bar_started);
let f = t.update(410, 0.016);
assert!(f.beat_started && f.bar_started);
assert_eq!(f.bar_flash, 1.0);
assert!(!f.section_changed, "最初のマーカー(800)はまだ");
let f = t.update(810, 0.016);
assert!(f.beat_started && f.bar_started);
assert!(f.section_changed);
assert_eq!(f.position.section, Some(0));
let f = t.update(1500, 0.016);
assert!(f.beat_started);
assert!(f.section_changed, "マーカー 2 つ目を通った");
}
#[test]
fn going_backwards_is_a_jump_without_a_flash() {
let mut t = BeatTracker::new(clock());
t.update(900, 0.016);
let f = t.update(50, 0.016);
assert!(f.jumped);
assert!(!f.beat_started && !f.bar_started);
assert!(f.section_changed, "セクションの外へ戻った");
let f = t.update(150, 0.016);
assert!(!f.jumped && f.beat_started);
}
#[test]
fn flashes_decay_to_zero_and_bad_dt_keeps_them() {
let mut t = BeatTracker::new(clock());
t.update(0, 0.016);
t.update(100, 0.016);
let f = t.update(100, f32::NAN);
assert_eq!(f.beat_flash, 1.0, "dt が不正なら減衰しない");
let mut last = f;
for _ in 0..200 {
last = t.update(100, 0.016);
}
assert_eq!(last.beat_flash, 0.0);
assert_eq!(last.bar_flash, 0.0);
}
#[test]
fn reset_and_set_clock_restart_without_a_flash() {
let mut t = BeatTracker::new(clock());
t.update(0, 0.016);
t.update(150, 0.016);
t.reset();
let f = t.update(250, 0.016);
assert!(!f.beat_started && f.beat_flash == 0.0);
t.set_clock(BeatClock::new(50, 3, []));
let f = t.update(10, 0.016);
assert!(!f.beat_started);
assert_eq!(f.position.section, None);
}
}