Massive tempo refactor - make beats canonical time rep and allow them to be non constant

This commit is contained in:
Skyler Lehmkuhl
2026-04-02 10:26:01 -04:00
parent ae146533d9
commit f372a84313
48 changed files with 1334 additions and 2136 deletions
@@ -1,5 +1,6 @@
use crate::audio::node_graph::{AudioNode, NodeCategory, NodePort, Parameter, ParameterUnit, SignalType};
use crate::audio::midi::MidiEvent;
use crate::time::Beats;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
@@ -16,14 +17,7 @@ pub enum InterpolationType {
/// A single keyframe in an automation curve
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutomationKeyframe {
/// Time in seconds (absolute project time)
pub time: f64,
/// Time in beats (derived; canonical in Measures mode)
#[serde(default)]
pub time_beats: f64,
/// Time in frames (derived; canonical in Frames mode)
#[serde(default)]
pub time_frames: f64,
pub time: Beats,
/// CV output value
pub value: f32,
/// Interpolation type to next keyframe
@@ -35,35 +29,15 @@ pub struct AutomationKeyframe {
}
impl AutomationKeyframe {
pub fn new(time: f64, value: f32) -> Self {
pub fn new(time: Beats, value: f32) -> Self {
Self {
time,
time_beats: 0.0,
time_frames: 0.0,
value,
interpolation: InterpolationType::Linear,
ease_out: (0.58, 1.0),
ease_in: (0.42, 0.0),
}
}
/// Populate beats/frames from the current seconds value.
pub fn sync_from_seconds(&mut self, bpm: f64, fps: f64) {
self.time_beats = self.time * bpm / 60.0;
self.time_frames = self.time * fps;
}
/// BPM changed; beats are canonical → recompute seconds and frames.
pub fn apply_beats(&mut self, bpm: f64, fps: f64) {
self.time = self.time_beats * 60.0 / bpm;
self.time_frames = self.time * fps;
}
/// FPS changed; frames are canonical → recompute seconds and beats.
pub fn apply_frames(&mut self, fps: f64, bpm: f64) {
self.time = self.time_frames / fps;
self.time_beats = self.time * bpm / 60.0;
}
}
/// Automation Input Node - outputs CV signal controlled by timeline curves
@@ -73,8 +47,10 @@ pub struct AutomationInputNode {
keyframes: Vec<AutomationKeyframe>,
outputs: Vec<NodePort>,
parameters: Vec<Parameter>,
/// Shared playback time (set by the graph before processing)
playback_time: Arc<RwLock<f64>>,
/// Shared playback time in beats (set by the graph before processing)
playback_time: Arc<RwLock<Beats>>,
/// Current BPM (set by the graph before processing, used for per-sample beat advancement)
bpm: f64,
/// Minimum output value (for UI display range)
pub value_min: f32,
/// Maximum output value (for UI display range)
@@ -97,22 +73,26 @@ impl AutomationInputNode {
Self {
name: name.clone(),
display_name: "Automation".to_string(),
keyframes: vec![AutomationKeyframe::new(0.0, 0.0)],
keyframes: vec![AutomationKeyframe::new(Beats::ZERO, 0.0)],
outputs,
parameters,
playback_time: Arc::new(RwLock::new(0.0)),
playback_time: Arc::new(RwLock::new(Beats::ZERO)),
bpm: 120.0,
value_min: -1.0,
value_max: 1.0,
}
}
/// Set the playback time (called by graph before processing)
pub fn set_playback_time(&mut self, time: f64) {
pub fn set_playback_time(&mut self, time: Beats) {
if let Ok(mut playback) = self.playback_time.write() {
*playback = time;
}
}
pub fn set_bpm(&mut self, bpm: f64) {
self.bpm = bpm;
}
/// Get the display name (shown in UI)
pub fn display_name(&self) -> &str {
&self.display_name
@@ -142,8 +122,7 @@ impl AutomationInputNode {
}
}
/// Remove keyframe at specific time (with tolerance)
pub fn remove_keyframe_at_time(&mut self, time: f64, tolerance: f64) -> bool {
pub fn remove_keyframe_at_time(&mut self, time: Beats, tolerance: Beats) -> bool {
if let Some(idx) = self.keyframes.iter().position(|kf| (kf.time - time).abs() < tolerance) {
self.keyframes.remove(idx);
true
@@ -152,10 +131,8 @@ impl AutomationInputNode {
}
}
/// Update an existing keyframe
pub fn update_keyframe(&mut self, keyframe: AutomationKeyframe) {
// Remove old keyframe at this time, then add new one
self.remove_keyframe_at_time(keyframe.time, 0.001);
self.remove_keyframe_at_time(keyframe.time, Beats(0.001));
self.add_keyframe(keyframe);
}
@@ -169,42 +146,20 @@ impl AutomationInputNode {
self.keyframes.clear();
}
/// Populate beats/frames on all keyframes from their current seconds values.
pub fn sync_keyframes_from_seconds(&mut self, bpm: f64, fps: f64) {
for kf in &mut self.keyframes {
kf.sync_from_seconds(bpm, fps);
}
}
/// BPM changed: for each keyframe, bootstrap beats from seconds (using `from_bpm`) if not yet
/// set, then re-derive seconds and frames from beats using `to_bpm`.
pub fn apply_beats_to_keyframes(&mut self, from_bpm: f64, to_bpm: f64, fps: f64) {
for kf in &mut self.keyframes {
if kf.time_beats == 0.0 && kf.time.abs() > 1e-9 {
kf.sync_from_seconds(from_bpm, fps);
}
kf.apply_beats(to_bpm, fps);
}
}
/// Evaluate curve at a specific time
fn evaluate_at_time(&self, time: f64) -> f32 {
fn evaluate_at_time(&self, time: Beats) -> f32 {
if self.keyframes.is_empty() {
return 0.0;
}
// Before first keyframe
if time <= self.keyframes[0].time {
return self.keyframes[0].value;
}
// After last keyframe
let last_idx = self.keyframes.len() - 1;
if time >= self.keyframes[last_idx].time {
return self.keyframes[last_idx].value;
}
// Find bracketing keyframes
for i in 0..self.keyframes.len() - 1 {
let kf1 = &self.keyframes[i];
let kf2 = &self.keyframes[i + 1];
@@ -217,14 +172,12 @@ impl AutomationInputNode {
0.0
}
/// Interpolate between two keyframes
fn interpolate(&self, kf1: &AutomationKeyframe, kf2: &AutomationKeyframe, time: f64) -> f32 {
// Calculate normalized position between keyframes (0.0 to 1.0)
fn interpolate(&self, kf1: &AutomationKeyframe, kf2: &AutomationKeyframe, time: Beats) -> f32 {
let t = if kf2.time == kf1.time {
0.0
0.0f64
} else {
((time - kf1.time) / (kf2.time - kf1.time)) as f32
};
(time - kf1.time) / (kf2.time - kf1.time)
} as f32;
match kf1.interpolation {
InterpolationType::Linear => {
@@ -301,19 +254,17 @@ impl AudioNode for AutomationInputNode {
let output = &mut outputs[0];
let length = output.len();
// Get the starting playback time
let playhead = if let Ok(playback) = self.playback_time.read() {
*playback
} else {
0.0
Beats::ZERO
};
// Calculate time per sample
let sample_duration = 1.0 / sample_rate as f64;
// Advance per sample in beats: beats_per_sample = bpm / 60 / sample_rate
let beats_per_sample = self.bpm / 60.0 / sample_rate as f64;
// Evaluate curve for each sample
for i in 0..length {
let time = playhead + (i as f64 * sample_duration);
let time = playhead + Beats(i as f64 * beats_per_sample);
output[i] = self.evaluate_at_time(time);
}
}
@@ -337,7 +288,8 @@ impl AudioNode for AutomationInputNode {
keyframes: self.keyframes.clone(),
outputs: self.outputs.clone(),
parameters: self.parameters.clone(),
playback_time: Arc::new(RwLock::new(0.0)),
playback_time: Arc::new(RwLock::new(Beats::ZERO)),
bpm: self.bpm,
value_min: self.value_min,
value_max: self.value_max,
})