All events now have three time references for seconds, measures/beats, frames

This commit is contained in:
Skyler Lehmkuhl
2026-03-30 10:15:55 -04:00
parent 65a550d8f4
commit cfb8e4462b
19 changed files with 1317 additions and 59 deletions
+61
View File
@@ -15,6 +15,7 @@ pub type ClipId = AudioClipInstanceId;
/// ## Timing Model
/// - `internal_start` / `internal_end`: Define the region of the source audio to play (trimming)
/// - `external_start` / `external_duration`: Define where the clip appears on the timeline and how long
/// - `*_beats` / `*_frames`: Derived representations for Measures/Frames mode display
///
/// ## Looping
/// If `external_duration` is greater than `internal_end - internal_start`,
@@ -26,13 +27,21 @@ pub struct AudioClipInstance {
/// Start position within the audio content (seconds)
pub internal_start: f64,
#[serde(default)] pub internal_start_beats: f64,
#[serde(default)] pub internal_start_frames: f64,
/// End position within the audio content (seconds)
pub internal_end: f64,
#[serde(default)] pub internal_end_beats: f64,
#[serde(default)] pub internal_end_frames: f64,
/// Start position on the timeline (seconds)
pub external_start: f64,
#[serde(default)] pub external_start_beats: f64,
#[serde(default)] pub external_start_frames: f64,
/// Duration on the timeline (seconds) - can be longer than internal duration for looping
pub external_duration: f64,
#[serde(default)] pub external_duration_beats: f64,
#[serde(default)] pub external_duration_frames: f64,
/// Clip-level gain
pub gain: f32,
@@ -62,9 +71,17 @@ impl AudioClipInstance {
id,
audio_pool_index,
internal_start,
internal_start_beats: 0.0,
internal_start_frames: 0.0,
internal_end,
internal_end_beats: 0.0,
internal_end_frames: 0.0,
external_start,
external_start_beats: 0.0,
external_start_frames: 0.0,
external_duration,
external_duration_beats: 0.0,
external_duration_frames: 0.0,
gain: 1.0,
read_ahead: None,
}
@@ -83,9 +100,17 @@ impl AudioClipInstance {
id,
audio_pool_index,
internal_start: offset,
internal_start_beats: 0.0,
internal_start_frames: 0.0,
internal_end: offset + duration,
internal_end_beats: 0.0,
internal_end_frames: 0.0,
external_start: start_time,
external_start_beats: 0.0,
external_start_frames: 0.0,
external_duration: duration,
external_duration_beats: 0.0,
external_duration_frames: 0.0,
gain: 1.0,
read_ahead: None,
}
@@ -147,4 +172,40 @@ impl AudioClipInstance {
pub fn set_gain(&mut self, gain: f32) {
self.gain = gain.max(0.0);
}
/// Populate beats/frames from the current seconds values.
pub fn sync_from_seconds(&mut self, bpm: f64, fps: f64) {
self.external_start_beats = self.external_start * bpm / 60.0;
self.external_start_frames = self.external_start * fps;
self.external_duration_beats = self.external_duration * bpm / 60.0;
self.external_duration_frames = self.external_duration * fps;
self.internal_start_beats = self.internal_start * bpm / 60.0;
self.internal_start_frames = self.internal_start * fps;
self.internal_end_beats = self.internal_end * bpm / 60.0;
self.internal_end_frames = self.internal_end * fps;
}
/// BPM changed; recompute seconds/frames from the stored beats values.
pub fn apply_beats(&mut self, bpm: f64, fps: f64) {
self.external_start = self.external_start_beats * 60.0 / bpm;
self.external_start_frames = self.external_start * fps;
self.external_duration = self.external_duration_beats * 60.0 / bpm;
self.external_duration_frames = self.external_duration * fps;
self.internal_start = self.internal_start_beats * 60.0 / bpm;
self.internal_start_frames = self.internal_start * fps;
self.internal_end = self.internal_end_beats * 60.0 / bpm;
self.internal_end_frames = self.internal_end * fps;
}
/// FPS changed; recompute seconds/beats from the stored frames values.
pub fn apply_frames(&mut self, fps: f64, bpm: f64) {
self.external_start = self.external_start_frames / fps;
self.external_start_beats = self.external_start * bpm / 60.0;
self.external_duration = self.external_duration_frames / fps;
self.external_duration_beats = self.external_duration * bpm / 60.0;
self.internal_start = self.internal_start_frames / fps;
self.internal_start_beats = self.internal_start * bpm / 60.0;
self.internal_end = self.internal_end_frames / fps;
self.internal_end_beats = self.internal_end * bpm / 60.0;
}
}
+59 -8
View File
@@ -108,6 +108,11 @@ pub struct Engine {
timing_worst_render_us: u64,
timing_sum_total_us: u64,
timing_overrun_count: u64,
// Current tempo/framerate — kept in sync with SetTempo/ApplyBpmChange so that
// newly-created clip instances can be immediately synced via sync_from_seconds.
current_bpm: f64,
current_fps: f64,
}
impl Engine {
@@ -184,6 +189,8 @@ impl Engine {
timing_worst_render_us: 0,
timing_sum_total_us: 0,
timing_overrun_count: 0,
current_bpm: 120.0,
current_fps: 30.0,
}
}
@@ -728,16 +735,19 @@ impl Engine {
}
Command::MoveClip(track_id, clip_id, new_start_time) => {
// Moving just changes external_start, external_duration stays the same
let bpm = self.current_bpm;
let fps = self.current_fps;
match self.project.get_track_mut(track_id) {
Some(crate::audio::track::TrackNode::Audio(track)) => {
if let Some(clip) = track.clips.iter_mut().find(|c| c.id == clip_id) {
clip.external_start = new_start_time;
clip.sync_from_seconds(bpm, fps);
}
}
Some(crate::audio::track::TrackNode::Midi(track)) => {
// Note: clip_id here is the pool clip ID, not instance ID
if let Some(instance) = track.clip_instances.iter_mut().find(|c| c.clip_id == clip_id) {
if let Some(instance) = track.clip_instances.iter_mut().find(|c| c.id == clip_id) {
instance.external_start = new_start_time;
instance.sync_from_seconds(bpm, fps);
}
}
_ => {}
@@ -747,13 +757,15 @@ impl Engine {
Command::TrimClip(track_id, clip_id, new_internal_start, new_internal_end) => {
// Trim changes which portion of the source content is used
// Also updates external_duration to match internal duration (no looping after trim)
let bpm = self.current_bpm;
let fps = self.current_fps;
match self.project.get_track_mut(track_id) {
Some(crate::audio::track::TrackNode::Audio(track)) => {
if let Some(clip) = track.clips.iter_mut().find(|c| c.id == clip_id) {
clip.internal_start = new_internal_start;
clip.internal_end = new_internal_end;
// By default, trimming sets external_duration to match internal duration
clip.external_duration = new_internal_end - new_internal_start;
clip.sync_from_seconds(bpm, fps);
}
}
Some(crate::audio::track::TrackNode::Midi(track)) => {
@@ -761,8 +773,8 @@ impl Engine {
if let Some(instance) = track.clip_instances.iter_mut().find(|c| c.clip_id == clip_id) {
instance.internal_start = new_internal_start;
instance.internal_end = new_internal_end;
// By default, trimming sets external_duration to match internal duration
instance.external_duration = new_internal_end - new_internal_start;
instance.sync_from_seconds(bpm, fps);
}
}
_ => {}
@@ -771,16 +783,20 @@ impl Engine {
}
Command::ExtendClip(track_id, clip_id, new_external_duration) => {
// Extend changes the external duration (enables looping if > internal duration)
let bpm = self.current_bpm;
let fps = self.current_fps;
match self.project.get_track_mut(track_id) {
Some(crate::audio::track::TrackNode::Audio(track)) => {
if let Some(clip) = track.clips.iter_mut().find(|c| c.id == clip_id) {
clip.external_duration = new_external_duration;
clip.sync_from_seconds(bpm, fps);
}
}
Some(crate::audio::track::TrackNode::Midi(track)) => {
// Note: clip_id here is the pool clip ID, not instance ID
if let Some(instance) = track.clip_instances.iter_mut().find(|c| c.clip_id == clip_id) {
instance.external_duration = new_external_duration;
instance.sync_from_seconds(bpm, fps);
}
}
_ => {}
@@ -899,13 +915,14 @@ impl Engine {
}
Command::AddAudioClip(track_id, clip_id, pool_index, start_time, duration, offset) => {
// Create a new clip instance with the pre-assigned clip_id
let clip = AudioClipInstance::from_legacy(
let mut clip = AudioClipInstance::from_legacy(
clip_id,
pool_index,
start_time,
duration,
offset,
);
clip.sync_from_seconds(self.current_bpm, self.current_fps);
// Add clip to track
if let Some(crate::audio::track::TrackNode::Audio(track)) = self.project.get_track_mut(track_id) {
@@ -933,7 +950,8 @@ impl Engine {
// Create an instance for this clip on the track
let instance_id = self.project.next_midi_clip_instance_id();
let instance = MidiClipInstance::from_full_clip(instance_id, clip_id, duration, start_time);
let mut instance = MidiClipInstance::from_full_clip(instance_id, clip_id, duration, start_time);
instance.sync_from_seconds(self.current_bpm, self.current_fps);
if let Some(crate::audio::track::TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
track.clip_instances.push(instance);
@@ -973,7 +991,15 @@ impl Engine {
}
Command::AddLoadedMidiClip(track_id, clip, start_time) => {
// Add a pre-loaded MIDI clip to the track with the given start time
let _ = self.project.add_midi_clip_at(track_id, clip, start_time);
let bpm = self.current_bpm;
let fps = self.current_fps;
if let Ok(instance_id) = self.project.add_midi_clip_at(track_id, clip, start_time) {
if let Some(crate::audio::track::TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
if let Some(inst) = track.clip_instances.iter_mut().find(|i| i.id == instance_id) {
inst.sync_from_seconds(bpm, fps);
}
}
}
self.refresh_clip_snapshot();
}
Command::UpdateMidiClipNotes(_track_id, clip_id, notes) => {
@@ -1277,6 +1303,14 @@ impl Engine {
Command::SetTempo(bpm, time_sig) => {
self.metronome.update_timing(bpm, time_sig);
self.project.set_tempo(bpm, time_sig.0);
self.current_bpm = bpm as f64;
}
Command::ApplyBpmChange(bpm, fps, midi_durations) => {
self.current_bpm = bpm;
self.current_fps = fps;
self.project.apply_bpm_change(bpm, fps, &midi_durations);
self.refresh_clip_snapshot();
}
// Node graph commands
@@ -2716,8 +2750,18 @@ impl Engine {
}
Query::AddMidiClipSync(track_id, clip, start_time) => {
// Add MIDI clip to track and return the instance ID
let bpm = self.current_bpm;
let fps = self.current_fps;
let result = match self.project.add_midi_clip_at(track_id, clip, start_time) {
Ok(instance_id) => QueryResponse::MidiClipInstanceAdded(Ok(instance_id)),
Ok(instance_id) => {
// Sync beats/frames on the newly created instance
if let Some(crate::audio::track::TrackNode::Midi(track)) = self.project.get_track_mut(track_id) {
if let Some(inst) = track.clip_instances.iter_mut().find(|i| i.id == instance_id) {
inst.sync_from_seconds(bpm, fps);
}
}
QueryResponse::MidiClipInstanceAdded(Ok(instance_id))
}
Err(e) => QueryResponse::MidiClipInstanceAdded(Err(e.to_string())),
};
self.refresh_clip_snapshot();
@@ -2728,6 +2772,7 @@ impl Engine {
// Assign instance ID
let instance_id = self.project.next_midi_clip_instance_id();
instance.id = instance_id;
instance.sync_from_seconds(self.current_bpm, self.current_fps);
let result = match self.project.add_midi_clip_instance(track_id, instance) {
Ok(_) => QueryResponse::MidiClipInstanceAdded(Ok(instance_id)),
@@ -3642,6 +3687,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.
/// 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));
}
// Node graph operations
/// Add a node to a track's instrument graph
+91
View File
@@ -3,6 +3,12 @@
pub struct MidiEvent {
/// Time position within the clip in seconds (sample-rate independent)
pub timestamp: f64,
/// Time position in beats (quarter-note beats from clip start); derived from timestamp
#[serde(default)]
pub timestamp_beats: f64,
/// Time position in frames; derived from timestamp
#[serde(default)]
pub timestamp_frames: f64,
/// MIDI status byte (includes channel)
pub status: u8,
/// First data byte (note number, CC number, etc.)
@@ -16,6 +22,8 @@ impl MidiEvent {
pub fn new(timestamp: f64, status: u8, data1: u8, data2: u8) -> Self {
Self {
timestamp,
timestamp_beats: 0.0,
timestamp_frames: 0.0,
status,
data1,
data2,
@@ -26,6 +34,8 @@ impl MidiEvent {
pub fn note_on(timestamp: f64, channel: u8, note: u8, velocity: u8) -> Self {
Self {
timestamp,
timestamp_beats: 0.0,
timestamp_frames: 0.0,
status: 0x90 | (channel & 0x0F),
data1: note,
data2: velocity,
@@ -36,12 +46,32 @@ impl MidiEvent {
pub fn note_off(timestamp: f64, channel: u8, note: u8, velocity: u8) -> Self {
Self {
timestamp,
timestamp_beats: 0.0,
timestamp_frames: 0.0,
status: 0x80 | (channel & 0x0F),
data1: note,
data2: velocity,
}
}
/// Sync beats and frames from seconds (call after constructing or when seconds is canonical)
pub fn sync_from_seconds(&mut self, bpm: f64, fps: f64) {
self.timestamp_beats = self.timestamp * bpm / 60.0;
self.timestamp_frames = self.timestamp * fps;
}
/// Recompute seconds and frames from beats (call when BPM changes in Measures mode)
pub fn apply_beats(&mut self, bpm: f64, fps: f64) {
self.timestamp = self.timestamp_beats * 60.0 / bpm;
self.timestamp_frames = self.timestamp * fps;
}
/// Recompute seconds and beats from frames (call when FPS changes in Frames mode)
pub fn apply_frames(&mut self, fps: f64, bpm: f64) {
self.timestamp = self.timestamp_frames / fps;
self.timestamp_beats = self.timestamp * bpm / 60.0;
}
/// Check if this is a note on event (with non-zero velocity)
pub fn is_note_on(&self) -> bool {
(self.status & 0xF0) == 0x90 && self.data2 > 0
@@ -128,6 +158,7 @@ impl MidiClip {
/// ## Timing Model
/// - `internal_start` / `internal_end`: Define the region of the source clip to play (trimming)
/// - `external_start` / `external_duration`: Define where the instance appears on the timeline and how long
/// - `*_beats` / `*_frames`: Derived representations for Measures/Frames mode display
///
/// ## Looping
/// If `external_duration` is greater than `internal_end - internal_start`,
@@ -139,13 +170,21 @@ pub struct MidiClipInstance {
/// Start position within the clip content (seconds)
pub internal_start: f64,
#[serde(default)] pub internal_start_beats: f64,
#[serde(default)] pub internal_start_frames: f64,
/// End position within the clip content (seconds)
pub internal_end: f64,
#[serde(default)] pub internal_end_beats: f64,
#[serde(default)] pub internal_end_frames: f64,
/// Start position on the timeline (seconds)
pub external_start: f64,
#[serde(default)] pub external_start_beats: f64,
#[serde(default)] pub external_start_frames: f64,
/// Duration on the timeline (seconds) - can be longer than internal duration for looping
pub external_duration: f64,
#[serde(default)] pub external_duration_beats: f64,
#[serde(default)] pub external_duration_frames: f64,
}
impl MidiClipInstance {
@@ -162,9 +201,17 @@ impl MidiClipInstance {
id,
clip_id,
internal_start,
internal_start_beats: 0.0,
internal_start_frames: 0.0,
internal_end,
internal_end_beats: 0.0,
internal_end_frames: 0.0,
external_start,
external_start_beats: 0.0,
external_start_frames: 0.0,
external_duration,
external_duration_beats: 0.0,
external_duration_frames: 0.0,
}
}
@@ -179,9 +226,17 @@ impl MidiClipInstance {
id,
clip_id,
internal_start: 0.0,
internal_start_beats: 0.0,
internal_start_frames: 0.0,
internal_end: clip_duration,
internal_end_beats: 0.0,
internal_end_frames: 0.0,
external_start,
external_start_beats: 0.0,
external_start_frames: 0.0,
external_duration: clip_duration,
external_duration_beats: 0.0,
external_duration_frames: 0.0,
}
}
@@ -215,6 +270,42 @@ impl MidiClipInstance {
self.external_start < range_end && self.external_end() > range_start
}
/// Populate beats/frames from the current seconds values.
pub fn sync_from_seconds(&mut self, bpm: f64, fps: f64) {
self.external_start_beats = self.external_start * bpm / 60.0;
self.external_start_frames = self.external_start * fps;
self.external_duration_beats = self.external_duration * bpm / 60.0;
self.external_duration_frames = self.external_duration * fps;
self.internal_start_beats = self.internal_start * bpm / 60.0;
self.internal_start_frames = self.internal_start * fps;
self.internal_end_beats = self.internal_end * bpm / 60.0;
self.internal_end_frames = self.internal_end * fps;
}
/// BPM changed; recompute seconds/frames from the stored beats values.
pub fn apply_beats(&mut self, bpm: f64, fps: f64) {
self.external_start = self.external_start_beats * 60.0 / bpm;
self.external_start_frames = self.external_start * fps;
self.external_duration = self.external_duration_beats * 60.0 / bpm;
self.external_duration_frames = self.external_duration * fps;
self.internal_start = self.internal_start_beats * 60.0 / bpm;
self.internal_start_frames = self.internal_start * fps;
self.internal_end = self.internal_end_beats * 60.0 / bpm;
self.internal_end_frames = self.internal_end * fps;
}
/// FPS changed; recompute seconds/beats from the stored frames values.
pub fn apply_frames(&mut self, fps: f64, bpm: f64) {
self.external_start = self.external_start_frames / fps;
self.external_start_beats = self.external_start * bpm / 60.0;
self.external_duration = self.external_duration_frames / fps;
self.external_duration_beats = self.external_duration * bpm / 60.0;
self.internal_start = self.internal_start_frames / fps;
self.internal_start_beats = self.internal_start * bpm / 60.0;
self.internal_end = self.internal_end_frames / fps;
self.internal_end_beats = self.internal_end * bpm / 60.0;
}
/// Get events that should be triggered in a given timeline range
///
/// This handles:
+40
View File
@@ -216,6 +216,46 @@ 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.
///
/// `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)]) {
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);
}
}
crate::audio::track::TrackNode::Midi(t) => {
// 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()
.find(|(id, _)| *id == instance.clip_id)
.map(|(_, d)| d)
{
let old_internal_dur = instance.internal_duration();
instance.internal_end = instance.internal_start + new_dur;
// Scale external_duration by the same ratio (works for both looping and non-looping)
if old_internal_dur > 1e-12 {
instance.external_duration = instance.external_duration * new_dur / old_internal_dur;
}
}
instance.sync_from_seconds(bpm, fps);
}
// Update pool clip durations
for &(clip_id, new_dur) in midi_durations {
if let Some(clip) = self.midi_clip_pool.get_clip_mut(clip_id) {
clip.duration = new_dur;
}
}
}
_ => {}
}
}
}
/// Get oscilloscope data from a node in a track's graph
pub fn get_oscilloscope_data(&self, track_id: TrackId, node_id: u32, sample_count: usize) -> Option<(Vec<f32>, Vec<f32>)> {
if let Some(TrackNode::Midi(track)) = self.tracks.get(&track_id) {
+3
View File
@@ -144,6 +144,9 @@ 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)>),
// Node graph commands
/// Add a node to a track's instrument graph (track_id, node_type, position_x, position_y)