Still searching...
This commit is contained in:
@@ -1,25 +1,14 @@
|
||||
/// Microsecond-precision timestamp for audio synchronization.
|
||||
pub type SyncTime = u64;
|
||||
|
||||
/// Sentinel value representing "never" or "no scheduled time".
|
||||
pub const NEVER: SyncTime = SyncTime::MAX;
|
||||
|
||||
/// Convert beat duration to microseconds at given tempo.
|
||||
pub fn beats_to_micros(beats: f64, tempo: f64) -> SyncTime {
|
||||
fn beats_to_micros(beats: f64, tempo: f64) -> SyncTime {
|
||||
if tempo <= 0.0 {
|
||||
return 0;
|
||||
}
|
||||
((beats / tempo) * 60_000_000.0).round() as SyncTime
|
||||
}
|
||||
|
||||
/// Convert microseconds to beats at given tempo.
|
||||
pub fn micros_to_beats(micros: SyncTime, tempo: f64) -> f64 {
|
||||
if tempo <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
(tempo * (micros as f64)) / 60_000_000.0
|
||||
}
|
||||
|
||||
/// Timing boundary types for step and pattern scheduling.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StepTiming {
|
||||
@@ -67,9 +56,6 @@ pub fn substeps_crossed(prev_beat: f64, curr_beat: f64, speed: f64) -> usize {
|
||||
(curr_substep - prev_substep).clamp(0, 16) as usize
|
||||
}
|
||||
|
||||
/// Threshold for switching from sleep to active wait (100μs).
|
||||
pub const ACTIVE_WAIT_THRESHOLD_US: SyncTime = 100;
|
||||
|
||||
/// Calculate microseconds until the next substep boundary.
|
||||
pub fn micros_until_next_substep(current_beat: f64, speed: f64, tempo: f64) -> SyncTime {
|
||||
if tempo <= 0.0 || speed <= 0.0 {
|
||||
@@ -94,26 +80,9 @@ mod tests {
|
||||
assert_eq!(beats_to_micros(0.5, 120.0), 250_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_micros_to_beats_at_120_bpm() {
|
||||
// At 120 BPM, 500,000 microseconds = 1 beat
|
||||
assert!((micros_to_beats(500_000, 120.0) - 1.0).abs() < 1e-10);
|
||||
assert!((micros_to_beats(1_000_000, 120.0) - 2.0).abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_tempo() {
|
||||
assert_eq!(beats_to_micros(1.0, 0.0), 0);
|
||||
assert_eq!(micros_to_beats(1_000_000, 0.0), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_roundtrip() {
|
||||
let tempo = 135.0;
|
||||
let beats = 3.75;
|
||||
let micros = beats_to_micros(beats, tempo);
|
||||
let back = micros_to_beats(micros, tempo);
|
||||
assert!((back - beats).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user