Timeline types stage 2: fix editor seconds↔beats at every clip boundary

Thread Beats/Seconds through the editor now that lightningbeam-core is typed.
The timeline UI is seconds-domain (pixels_per_second, viewport_start_time,
playback_time, drag_offset), while ClipInstance.timeline_start/duration/
loop_before are beats — the compiler flagged every place the two were mixed.

Fixes the reported bugs and the whole class behind them:
- Recording placement (audio + MIDI + webcam): the new clip's timeline_start
  was written from playback_time (seconds) into a beats field, so a second
  recording landed at the wrong time. Now converted via the tempo map.
- Drag/move: introduce snapped_move_offset -> Beats (snap in the seconds/pixel
  domain, express the anchor's movement in beats) and moved_start(); the group
  clamp, live preview, and commit all use one uniform beats offset instead of
  adding a seconds delta to a beats position.
- Trim/loop drag preview + commit: overlap limits come from the timeline
  (beats) but trims are content seconds — converted at the boundary.
- Drop (asset drag, stage + timeline), paste, duplicate, split-at-playhead:
  all convert the seconds drop/playhead position to beats before placing.
- Stage playback gates + clip-local time remap: compared seconds playback_time
  against beats timeline_start; now both in seconds.
- Piano roll / infopanel / effect export: clip_dur is Seconds; effect and
  waveform data converted at use.

Core API refinement (motivated by the above): find_max_trim_extend_left/right
now return Seconds (the content-seconds gap) instead of raw Beats, since every
trim caller wants seconds; loop-extend callers convert to beats. Added a
beats_to_x() timeline helper.

Whole workspace compiles; 299 core tests pass.
This commit is contained in:
Skyler Lehmkuhl
2026-07-11 14:10:38 -04:00
parent 053a77cfa1
commit 0050c18623
8 changed files with 398 additions and 277 deletions
@@ -204,20 +204,12 @@ impl Action for TrimClipInstancesAction {
{
// If extending to the left (new_trim < old_trim)
if should_validate && new_trim < old_trim {
// Max we can extend left is the timeline gap to the
// previous clip (beats).
let max_extend_beats = document.find_max_trim_extend_left(
// Max leftward extension as content seconds (the gap's wall-clock span).
let max_extend_secs = document.find_max_trim_extend_left(
layer_id,
instance_id,
instance.timeline_start,
);
// Audio plays 1 content-second per timeline-second, so the
// revealable content equals that gap's wall-clock span.
let tempo_map = document.tempo_map();
let old_timeline_secs = tempo_map.beats_to_seconds(old_timeline);
let max_extend_secs = (old_timeline_secs
- tempo_map.beats_to_seconds(old_timeline - max_extend_beats))
.seconds_to_f64();
).seconds_to_f64();
// Calculate how much we want to extend (content seconds)
let desired_extend = old_trim - new_trim;
@@ -226,8 +218,9 @@ impl Action for TrimClipInstancesAction {
let actual_extend = desired_extend.min(max_extend_secs);
let clamped_trim_start = old_trim - actual_extend;
// Move the timeline left by the same wall-clock seconds.
let tempo_map = document.tempo_map();
let clamped_timeline_start = tempo_map
.seconds_to_beats(old_timeline_secs - Seconds(actual_extend))
.seconds_to_beats(tempo_map.beats_to_seconds(old_timeline) - Seconds(actual_extend))
.max(Beats::ZERO);
clamped_new = TrimData::left(clamped_trim_start, clamped_timeline_start);
@@ -248,19 +241,13 @@ impl Action for TrimClipInstancesAction {
tempo_map.beats_to_seconds(instance.timeline_start) + content_secs,
) - instance.timeline_start;
// Max we can extend right is the timeline gap to the next
// clip (beats).
let max_extend_beats = document.find_max_trim_extend_right(
// Max rightward extension as content seconds (the gap's wall-clock span).
let max_extend_secs = document.find_max_trim_extend_right(
layer_id,
instance_id,
instance.timeline_start,
current_effective_duration,
);
// Convert that gap to content seconds at the clip's right edge.
let right_edge = instance.timeline_start + current_effective_duration;
let max_extend_secs = (tempo_map.beats_to_seconds(right_edge + max_extend_beats)
- tempo_map.beats_to_seconds(right_edge))
.seconds_to_f64();
).seconds_to_f64();
// Calculate how much we want to extend (content seconds)
let desired_extend = new_trim_end - old_trim_end;
@@ -1186,19 +1186,21 @@ impl Document {
///
/// Returns the distance to the nearest clip to the left, or the distance to
/// timeline start (0.0) if no clips exist to the left.
/// Returns the max leftward trim extension as a content-seconds span (the wall-clock
/// length of the timeline gap to the previous clip); the trim domain is seconds.
pub fn find_max_trim_extend_left(
&self,
layer_id: &Uuid,
instance_id: &Uuid,
current_timeline_start: Beats,
) -> Beats {
) -> Seconds {
let Some(layer) = self.get_layer(layer_id) else {
return current_timeline_start; // No limit if layer not found
return self.tempo_map().beats_to_seconds(current_timeline_start); // No limit if layer not found
};
// Only check audio, video, and effect layers
if matches!(layer, AnyLayer::Vector(_) | AnyLayer::Group(_)) {
return current_timeline_start; // No limit for vector/group layers
return self.tempo_map().beats_to_seconds(current_timeline_start); // No limit for vector/group layers
};
// Find the nearest clip to the left
@@ -1231,27 +1233,27 @@ impl Document {
}
}
current_timeline_start - nearest_end
self.tempo_map().beats_to_seconds(current_timeline_start) - self.tempo_map().beats_to_seconds(nearest_end)
}
/// Find the maximum amount we can extend a clip to the right without overlapping
/// Find the maximum amount we can extend a clip to the right without overlapping.
///
/// Returns the distance to the nearest clip to the right, or f64::MAX if no
/// clips exist to the right.
/// Returns the content-seconds span of the timeline gap to the nearest clip on the
/// right, or Seconds(f64::MAX) if none. `current_effective_duration` is beats (timeline).
pub fn find_max_trim_extend_right(
&self,
layer_id: &Uuid,
instance_id: &Uuid,
current_timeline_start: Beats,
current_effective_duration: Beats,
) -> Beats {
) -> Seconds {
let Some(layer) = self.get_layer(layer_id) else {
return Beats(f64::MAX); // No limit if layer not found
return Seconds(f64::MAX); // No limit if layer not found
};
// Only check audio, video, and effect layers
if matches!(layer, AnyLayer::Vector(_) | AnyLayer::Group(_)) {
return Beats(f64::MAX); // No limit for vector/group layers
return Seconds(f64::MAX); // No limit for vector/group layers
}
let instances: &[ClipInstance] = match layer {
@@ -1280,9 +1282,10 @@ impl Document {
}
if nearest_start == Beats(f64::MAX) {
Beats(f64::MAX) // No clip to the right, can extend freely
Seconds(f64::MAX) // No clip to the right, can extend freely
} else {
(nearest_start - current_end).max(Beats::ZERO) // Gap between our end and next clip's start
// Gap between our end and next clip's start, as content seconds.
(self.tempo_map().beats_to_seconds(nearest_start) - self.tempo_map().beats_to_seconds(current_end)).max(Seconds::ZERO)
}
}
/// Find the maximum amount we can extend loop_before to the left without overlapping.