Add beat mode

This commit is contained in:
Skyler Lehmkuhl
2026-02-22 18:43:17 -05:00
parent 205dc9dd67
commit eab116c930
11 changed files with 402 additions and 105 deletions
@@ -0,0 +1,44 @@
//! Beat/measure ↔ seconds conversion utilities
use crate::document::TimeSignature;
/// Position expressed as measure, beat, tick
#[derive(Debug, Clone, Copy)]
pub struct MeasurePosition {
pub measure: u32, // 1-indexed
pub beat: u32, // 1-indexed
pub tick: u32, // 0-999 (subdivision of beat)
}
/// Convert a time in seconds to a measure position
pub fn time_to_measure(time: f64, bpm: f64, time_sig: &TimeSignature) -> MeasurePosition {
let beats_per_second = bpm / 60.0;
let total_beats = (time * beats_per_second).max(0.0);
let beats_per_measure = time_sig.numerator as f64;
let measure = (total_beats / beats_per_measure).floor() as u32 + 1;
let beat = (total_beats.rem_euclid(beats_per_measure)).floor() as u32 + 1;
let tick = ((total_beats.rem_euclid(1.0)) * 1000.0).floor() as u32;
MeasurePosition { measure, beat, tick }
}
/// Convert a measure position to seconds
pub fn measure_to_time(pos: MeasurePosition, bpm: f64, time_sig: &TimeSignature) -> f64 {
let beats_per_measure = time_sig.numerator as f64;
let total_beats = (pos.measure as f64 - 1.0) * beats_per_measure
+ (pos.beat as f64 - 1.0)
+ (pos.tick as f64 / 1000.0);
let beats_per_second = bpm / 60.0;
total_beats / beats_per_second
}
/// Get the duration of one beat in seconds
pub fn beat_duration(bpm: f64) -> f64 {
60.0 / bpm
}
/// Get the duration of one measure in seconds
pub fn measure_duration(bpm: f64, time_sig: &TimeSignature) -> f64 {
beat_duration(bpm) * time_sig.numerator as f64
}
@@ -70,6 +70,21 @@ impl Default for GraphicsObject {
}
}
/// Musical time signature
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TimeSignature {
pub numerator: u32, // beats per measure (e.g., 4)
pub denominator: u32, // beat unit (e.g., 4 = quarter note)
}
impl Default for TimeSignature {
fn default() -> Self {
Self { numerator: 4, denominator: 4 }
}
}
fn default_bpm() -> f64 { 120.0 }
/// Asset category for folder tree access
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssetCategory {
@@ -101,6 +116,14 @@ pub struct Document {
/// Framerate (frames per second)
pub framerate: f64,
/// Tempo in beats per minute
#[serde(default = "default_bpm")]
pub bpm: f64,
/// Time signature
#[serde(default)]
pub time_signature: TimeSignature,
/// Duration in seconds
pub duration: f64,
@@ -182,6 +205,8 @@ impl Default for Document {
width: 1920.0,
height: 1080.0,
framerate: 60.0,
bpm: 120.0,
time_signature: TimeSignature::default(),
duration: 10.0,
root: GraphicsObject::default(),
vector_clips: HashMap::new(),
@@ -1,6 +1,7 @@
// Lightningbeam Core Library
// Shared data structures and types
pub mod beat_time;
pub mod gpu;
pub mod layout;
pub mod pane;