Update automation lanes too

This commit is contained in:
Skyler Lehmkuhl
2026-04-01 10:17:52 -04:00
parent 3fc4773ec3
commit ae146533d9
7 changed files with 150 additions and 20 deletions
+18 -7
View File
@@ -1306,10 +1306,10 @@ impl Engine {
self.current_bpm = bpm as f64;
}
Command::ApplyBpmChange(bpm, fps, midi_durations) => {
self.current_bpm = bpm;
Command::ApplyBpmChange(from_bpm, to_bpm, fps, midi_durations) => {
self.current_bpm = to_bpm;
self.current_fps = fps;
self.project.apply_bpm_change(bpm, fps, &midi_durations);
self.project.apply_bpm_change(from_bpm, to_bpm, fps, &midi_durations);
self.refresh_clip_snapshot();
}
@@ -2181,13 +2181,16 @@ impl Engine {
if let Some(graph_node) = graph.get_graph_node_mut(node_idx) {
// Downcast to AutomationInputNode using as_any_mut
if let Some(auto_node) = graph_node.node.as_any_mut().downcast_mut::<AutomationInputNode>() {
let keyframe = AutomationKeyframe {
let mut keyframe = AutomationKeyframe {
time,
time_beats: 0.0,
time_frames: 0.0,
value,
interpolation,
ease_out,
ease_in,
};
keyframe.sync_from_seconds(self.current_bpm, self.current_fps);
auto_node.add_keyframe(keyframe);
} else {
eprintln!("Node {} is not an AutomationInputNode", node_id);
@@ -3632,6 +3635,12 @@ impl EngineController {
let _ = self.command_tx.push(Command::AutomationSetName(track_id, node_id, name));
}
/// Set the min/max output range of an AutomationInput node (param ids 0 = min, 1 = max)
pub fn automation_set_range(&mut self, track_id: TrackId, node_id: u32, min: f32, max: f32) {
self.graph_set_parameter(track_id, node_id, 0, min);
self.graph_set_parameter(track_id, node_id, 1, max);
}
/// Start recording on a track
pub fn start_recording(&mut self, track_id: TrackId, start_time: f64) {
let _ = self.command_tx.push(Command::StartRecording(track_id, start_time));
@@ -3692,10 +3701,12 @@ impl EngineController {
let _ = self.command_tx.push(Command::SetTempo(bpm, time_signature));
}
/// After a BPM change: update MIDI clip durations and sync all clip beats/frames.
/// After a BPM change: update MIDI clip durations, sync clip beats/frames, and rescale
/// automation keyframe times to preserve beat positions.
/// `from_bpm` is the BPM before the change; `to_bpm` is the new BPM.
/// Call this after move_clip() has been called for all affected clips.
pub fn apply_bpm_change(&mut self, bpm: f64, fps: f64, midi_durations: Vec<(crate::audio::MidiClipId, f64)>) {
let _ = self.command_tx.push(Command::ApplyBpmChange(bpm, fps, midi_durations));
pub fn apply_bpm_change(&mut self, from_bpm: f64, to_bpm: f64, fps: f64, midi_durations: Vec<(crate::audio::MidiClipId, f64)>) {
let _ = self.command_tx.push(Command::ApplyBpmChange(from_bpm, to_bpm, fps, midi_durations));
}
// Node graph operations
+14
View File
@@ -737,6 +737,18 @@ impl AudioGraph {
self.graph.node_indices()
}
/// BPM changed: rescale all AutomationInput keyframe times to preserve beat positions.
/// `from_bpm` is the BPM before the change (used to bootstrap beats if not yet populated).
/// `to_bpm` is the new BPM (used to re-derive seconds from beats).
pub fn apply_beats_to_automation_keyframes(&mut self, from_bpm: f64, to_bpm: f64, fps: f64) {
use super::nodes::AutomationInputNode;
for node in self.graph.node_weights_mut() {
if let Some(auto_node) = node.node.as_any_mut().downcast_mut::<AutomationInputNode>() {
auto_node.apply_beats_to_keyframes(from_bpm, to_bpm, fps);
}
}
}
/// Reallocate a node's output buffers to match its current port list.
///
/// Must be called after `SubtrackInputsNode::update_subtracks` changes the port count,
@@ -1358,6 +1370,8 @@ impl AudioGraph {
for kf in &serialized_node.automation_keyframes {
auto_node.add_keyframe(AutomationKeyframe {
time: kf.time,
time_beats: 0.0,
time_frames: 0.0,
value: kf.value,
interpolation: match kf.interpolation.as_str() {
"bezier" => InterpolationType::Bezier,
@@ -18,6 +18,12 @@ pub enum InterpolationType {
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,
/// CV output value
pub value: f32,
/// Interpolation type to next keyframe
@@ -32,12 +38,32 @@ impl AutomationKeyframe {
pub fn new(time: f64, 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
@@ -143,6 +169,24 @@ 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 {
if self.keyframes.is_empty() {
+10 -4
View File
@@ -216,19 +216,25 @@ impl Project {
self.tracks.iter().map(|(&id, node)| (id, node))
}
/// After a BPM change, update MIDI clip durations then sync all clip beats/frames from seconds.
/// After a BPM change, update MIDI clip durations, sync clip beats/frames, and rescale
/// automation keyframe times to preserve beat positions.
///
/// `from_bpm` is the BPM before the change (used to bootstrap beats on legacy data).
/// `to_bpm` is the new BPM.
/// `midi_durations` maps each MidiClipId to its new content duration in seconds.
/// Call this after the seconds positions have already been updated (e.g. via MoveClip).
pub fn apply_bpm_change(&mut self, bpm: f64, fps: f64, midi_durations: &[(crate::audio::midi::MidiClipId, f64)]) {
pub fn apply_bpm_change(&mut self, from_bpm: f64, to_bpm: f64, fps: f64, midi_durations: &[(crate::audio::midi::MidiClipId, f64)]) {
for (_, track) in self.tracks.iter_mut() {
match track {
crate::audio::track::TrackNode::Audio(t) => {
for clip in &mut t.clips {
clip.sync_from_seconds(bpm, fps);
clip.sync_from_seconds(to_bpm, fps);
}
}
crate::audio::track::TrackNode::Midi(t) => {
// Rescale automation keyframe times in this track's graph
t.instrument_graph.apply_beats_to_automation_keyframes(from_bpm, to_bpm, fps);
// Update content durations first so internal_end is correct before sync
for instance in &mut t.clip_instances {
if let Some(&new_dur) = midi_durations.iter()
@@ -242,7 +248,7 @@ impl Project {
instance.external_duration = instance.external_duration * new_dur / old_internal_dur;
}
}
instance.sync_from_seconds(bpm, fps);
instance.sync_from_seconds(to_bpm, fps);
}
// Update pool clip durations
for &(clip_id, new_dur) in midi_durations {
+4 -3
View File
@@ -144,9 +144,10 @@ pub enum Command {
SetMetronomeEnabled(bool),
/// Set project tempo and time signature (bpm, (numerator, denominator))
SetTempo(f32, (u32, u32)),
/// After a BPM change: update MIDI clip durations and sync all clip beats/frames from seconds.
/// (bpm, fps, midi_durations: Vec<(clip_id, new_duration_seconds)>)
ApplyBpmChange(f64, f64, Vec<(MidiClipId, f64)>),
/// After a BPM change: update MIDI clip durations, sync clip beats/frames, and rescale
/// automation keyframe times to preserve beat positions.
/// (from_bpm, to_bpm, fps, midi_durations: Vec<(clip_id, new_duration_seconds)>)
ApplyBpmChange(f64, f64, f64, Vec<(MidiClipId, f64)>),
// Node graph commands
/// Add a node to a track's instrument graph (track_id, node_type, position_x, position_y)