Type AudioClip duration by clip kind; fix MIDI clips growing too fast

The MIDI-clip-end-grows-too-fast bug was a units confusion: AudioClip.duration
is documented "seconds" but MIDI clips store their length in BEATS (they share
the AudioClip struct with sampled clips). The timeline's effective_clip_duration
read it as raw seconds, so at 120 BPM a MIDI clip rendered ~2x too long. The old
backend-snapshot display had hidden this by forcing timeline_duration in beats.

Root fix — make the domain explicit and unforgeable:
- `duration` is now a private field. Reading it in the wrong unit is impossible
  because access goes through typed accessors: AudioClip::content_duration() ->
  ClipDuration (a Seconds|Beats enum tagged by clip_type) and
  set_content_duration(). serde still serializes the private field, so the .beam
  format is unchanged (bare number).
- ClipDuration::to_seconds(tempo_map) for display/sizing; ::native() for code
  that already works in the clip's native domain (trim math shares it).
- get_clip_duration + the timeline-endpoint calc go through to_seconds, so MIDI
  is converted correctly; effective_clip_duration delegates to get_clip_duration.
- Recording mirrors (audio/MIDI progress + finalize) write via set_content_duration
  (debug-asserts the value's domain matches the clip type).

Every former raw read/write of the field (core actions, timeline, piano roll,
infopanel, asset library, recording handlers) now goes through the accessors.
Whole workspace compiles; 299 core tests pass.
This commit is contained in:
Skyler Lehmkuhl
2026-07-11 19:23:37 -04:00
parent c747126759
commit 8ed2320dca
17 changed files with 186 additions and 1216 deletions
@@ -472,8 +472,10 @@ impl Document {
}
crate::layer::AnyLayer::Audio(audio_layer) => {
for instance in &audio_layer.clip_instances {
if let Some(clip) = self.audio_clips.get(&instance.clip_id) {
let end_time = calculate_instance_end(instance, clip.duration);
// get_clip_duration yields seconds (converting MIDI's beats duration),
// which is what the closure expects.
if let Some(clip_duration) = self.get_clip_duration(&instance.clip_id) {
let end_time = calculate_instance_end(instance, clip_duration.seconds_to_f64());
max_end_time = max_end_time.max(end_time);
}
}
@@ -516,8 +518,8 @@ impl Document {
}
crate::layer::AnyLayer::Audio(al) => {
for inst in &al.clip_instances {
if let Some(clip) = doc.audio_clips.get(&inst.clip_id) {
*max_end = max_end.max(calc_end(inst, clip.duration));
if let Some(clip_duration) = doc.get_clip_duration(&inst.clip_id) {
*max_end = max_end.max(calc_end(inst, clip_duration.seconds_to_f64()));
}
}
}
@@ -908,7 +910,7 @@ impl Document {
// Avoid deep recursion — use stored duration for nested vector clips
Some(vc.content_duration(self.framerate, tempo_map))
} else if let Some(ac) = self.audio_clips.get(id) {
Some(ac.duration)
Some(ac.content_duration().to_seconds(tempo_map).seconds_to_f64())
} else if let Some(vc) = self.video_clips.get(id) {
Some(vc.duration)
} else if self.effect_definitions.contains_key(id) {
@@ -921,15 +923,9 @@ impl Document {
} else if let Some(clip) = self.video_clips.get(clip_id) {
Some(Seconds(clip.duration))
} else if let Some(clip) = self.audio_clips.get(clip_id) {
// MIDI clips store `duration` in BEATS (they share the AudioClip struct with
// sampled clips, whose duration is seconds). Convert to wall-clock seconds so
// the content-window sizing works uniformly.
match clip.clip_type {
crate::clip::AudioClipType::Midi { .. } => {
Some(self.tempo_map().beats_to_seconds(Beats(clip.duration)))
}
_ => Some(Seconds(clip.duration)),
}
// Interpret the clip's native-domain duration as wall-clock seconds (MIDI stores
// beats, sampled stores seconds — content_duration keeps that straight).
Some(clip.content_duration().to_seconds(self.tempo_map()))
} else if self.effect_definitions.contains_key(clip_id) {
// Effects have infinite internal duration - their timeline length
// is controlled by ClipInstance.trim_end