Add count-in

This commit is contained in:
Skyler Lehmkuhl
2026-03-20 20:51:50 -04:00
parent c938ea44b0
commit 121fa3a50a
8 changed files with 189 additions and 38 deletions
+6 -5
View File
@@ -107,7 +107,7 @@ impl Metronome {
pub fn process(
&mut self,
output: &mut [f32],
playhead_samples: u64,
playhead_samples: i64,
playing: bool,
sample_rate: u32,
channels: u32,
@@ -120,20 +120,21 @@ impl Metronome {
let frames = output.len() / channels as usize;
for frame in 0..frames {
let current_sample = playhead_samples + frame as u64;
let current_sample = playhead_samples + frame as i64;
// Calculate current beat number
let current_time_seconds = current_sample as f64 / sample_rate as f64;
let beats_per_second = self.bpm as f64 / 60.0;
let current_beat = (current_time_seconds * beats_per_second).floor() as i64;
// Check if we crossed a beat boundary
if current_beat != self.last_beat && current_beat >= 0 {
// Check if we crossed a beat boundary (including negative beats during count-in pre-roll)
if current_beat != self.last_beat {
self.last_beat = current_beat;
// Determine which click to play.
// Beat 0 of each measure gets the accent (high click).
let beat_in_measure = (current_beat as u32 % self.time_signature_numerator) as usize;
// Use rem_euclid so negative beat numbers map correctly (e.g. -4 % 4 = 0).
let beat_in_measure = current_beat.rem_euclid(self.time_signature_numerator as i64) as usize;
self.playing_high_click = beat_in_measure == 0;
self.click_position = 0; // Start from beginning of click
}