Fix looping bugs
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! Loop clip instances action
|
||||
//!
|
||||
//! Handles extending clip instances beyond their content duration to enable looping,
|
||||
//! by setting timeline_duration on the ClipInstance.
|
||||
//! by setting timeline_duration and/or loop_before on the ClipInstance.
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::document::Document;
|
||||
@@ -9,14 +9,17 @@ use crate::layer::AnyLayer;
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Per-instance loop change: (instance_id, old_timeline_duration, new_timeline_duration, old_loop_before, new_loop_before)
|
||||
pub type LoopEntry = (Uuid, Option<f64>, Option<f64>, Option<f64>, Option<f64>);
|
||||
|
||||
/// Action that changes the loop duration of clip instances
|
||||
pub struct LoopClipInstancesAction {
|
||||
/// Map of layer IDs to vectors of (instance_id, old_timeline_duration, new_timeline_duration)
|
||||
layer_loops: HashMap<Uuid, Vec<(Uuid, Option<f64>, Option<f64>)>>,
|
||||
/// Map of layer IDs to vectors of loop entries
|
||||
layer_loops: HashMap<Uuid, Vec<LoopEntry>>,
|
||||
}
|
||||
|
||||
impl LoopClipInstancesAction {
|
||||
pub fn new(layer_loops: HashMap<Uuid, Vec<(Uuid, Option<f64>, Option<f64>)>>) -> Self {
|
||||
pub fn new(layer_loops: HashMap<Uuid, Vec<LoopEntry>>) -> Self {
|
||||
Self { layer_loops }
|
||||
}
|
||||
}
|
||||
@@ -34,9 +37,10 @@ impl Action for LoopClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
};
|
||||
|
||||
for (instance_id, _old, new) in loops {
|
||||
for (instance_id, _old_dur, new_dur, _old_lb, new_lb) in loops {
|
||||
if let Some(instance) = clip_instances.iter_mut().find(|ci| ci.id == *instance_id) {
|
||||
instance.timeline_duration = *new;
|
||||
instance.timeline_duration = *new_dur;
|
||||
instance.loop_before = *new_lb;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,9 +59,10 @@ impl Action for LoopClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
};
|
||||
|
||||
for (instance_id, old, _new) in loops {
|
||||
for (instance_id, old_dur, _new_dur, old_lb, _new_lb) in loops {
|
||||
if let Some(instance) = clip_instances.iter_mut().find(|ci| ci.id == *instance_id) {
|
||||
instance.timeline_duration = *old;
|
||||
instance.timeline_duration = *old_dur;
|
||||
instance.loop_before = *old_lb;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +107,7 @@ impl LoopClipInstancesAction {
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
for (instance_id, old, new) in loops {
|
||||
for (instance_id, old_dur, new_dur, old_lb, new_lb) in loops {
|
||||
let instance = clip_instances.iter()
|
||||
.find(|ci| ci.id == *instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", instance_id))?;
|
||||
@@ -110,32 +115,39 @@ impl LoopClipInstancesAction {
|
||||
let clip = document.get_audio_clip(&instance.clip_id)
|
||||
.ok_or_else(|| format!("Audio clip {} not found", instance.clip_id))?;
|
||||
|
||||
// Determine which duration to send: on rollback use old, otherwise use new (current)
|
||||
let target_duration = if rollback { old } else { new };
|
||||
let (target_duration, target_loop_before) = if rollback {
|
||||
(old_dur, old_lb)
|
||||
} else {
|
||||
(new_dur, new_lb)
|
||||
};
|
||||
|
||||
// If timeline_duration is None, the external duration equals the content window
|
||||
let content_window = {
|
||||
let trim_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
(trim_end - instance.trim_start).max(0.0)
|
||||
};
|
||||
let external_duration = target_duration.unwrap_or(content_window);
|
||||
let right_duration = target_duration.unwrap_or(content_window);
|
||||
let left_duration = target_loop_before.unwrap_or(0.0);
|
||||
let external_duration = left_duration + right_duration;
|
||||
let external_start = instance.timeline_start - left_duration;
|
||||
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
controller.extend_clip(*track_id, *midi_clip_id, external_duration);
|
||||
}
|
||||
AudioClipType::Sampled { .. } => {
|
||||
let backend_instance_id = backend.clip_instance_to_backend_map.get(instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", instance_id))?;
|
||||
|
||||
match backend_instance_id {
|
||||
crate::action::BackendClipInstanceId::Audio(audio_id) => {
|
||||
controller.extend_clip(*track_id, *audio_id, external_duration);
|
||||
let get_backend_clip_id = |inst_id: &Uuid| -> Result<u32, String> {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => Ok(*midi_clip_id),
|
||||
AudioClipType::Sampled { .. } => {
|
||||
let backend_id = backend.clip_instance_to_backend_map.get(inst_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", inst_id))?;
|
||||
match backend_id {
|
||||
crate::action::BackendClipInstanceId::Audio(audio_id) => Ok(*audio_id),
|
||||
_ => Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
_ => return Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
AudioClipType::Recording => Err("Cannot sync recording clip".to_string()),
|
||||
}
|
||||
AudioClipType::Recording => {}
|
||||
};
|
||||
|
||||
if let Ok(backend_clip_id) = get_backend_clip_id(instance_id) {
|
||||
controller.move_clip(*track_id, backend_clip_id, external_start);
|
||||
controller.extend_clip(*track_id, backend_clip_id, external_duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,6 +573,12 @@ pub struct ClipInstance {
|
||||
/// Compatible with daw-backend's Clip.gain
|
||||
/// Default: 1.0
|
||||
pub gain: f32,
|
||||
|
||||
/// How far (in seconds) the looped content extends before timeline_start.
|
||||
/// When set, loop iterations are drawn/played before the content start.
|
||||
/// Default: None (no pre-loop)
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub loop_before: Option<f64>,
|
||||
}
|
||||
|
||||
impl ClipInstance {
|
||||
@@ -590,6 +596,7 @@ impl ClipInstance {
|
||||
trim_end: None,
|
||||
playback_speed: 1.0,
|
||||
gain: 1.0,
|
||||
loop_before: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,6 +614,7 @@ impl ClipInstance {
|
||||
trim_end: None,
|
||||
playback_speed: 1.0,
|
||||
gain: 1.0,
|
||||
loop_before: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -683,6 +691,17 @@ impl ClipInstance {
|
||||
(end - self.trim_start).max(0.0)
|
||||
}
|
||||
|
||||
/// Get the effective start position on the timeline, accounting for loop_before.
|
||||
/// This is the left edge of the clip's visual extent.
|
||||
pub fn effective_start(&self) -> f64 {
|
||||
self.timeline_start - self.loop_before.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Get the total visual duration including both loop_before and effective_duration.
|
||||
pub fn total_duration(&self, clip_duration: f64) -> f64 {
|
||||
self.loop_before.unwrap_or(0.0) + self.effective_duration(clip_duration)
|
||||
}
|
||||
|
||||
/// Remap timeline time to clip content time
|
||||
///
|
||||
/// Takes a global timeline time and returns the corresponding time within this
|
||||
|
||||
@@ -604,15 +604,13 @@ impl Document {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate instance end time
|
||||
// Calculate instance extent (accounting for loop_before)
|
||||
let Some(clip_duration) = self.get_clip_duration(&instance.clip_id) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let instance_start = instance.timeline_start;
|
||||
let trim_start = instance.trim_start;
|
||||
let trim_end = instance.trim_end.unwrap_or(clip_duration);
|
||||
let instance_end = instance_start + (trim_end - trim_start);
|
||||
let instance_start = instance.effective_start();
|
||||
let instance_end = instance.timeline_start + instance.effective_duration(clip_duration);
|
||||
|
||||
// Check overlap: start_a < end_b AND start_b < end_a
|
||||
if start_time < instance_end && instance_start < end_time {
|
||||
@@ -667,10 +665,8 @@ impl Document {
|
||||
}
|
||||
|
||||
if let Some(clip_dur) = self.get_clip_duration(&instance.clip_id) {
|
||||
let inst_start = instance.timeline_start;
|
||||
let trim_start = instance.trim_start;
|
||||
let trim_end = instance.trim_end.unwrap_or(clip_dur);
|
||||
let inst_end = inst_start + (trim_end - trim_start);
|
||||
let inst_start = instance.effective_start();
|
||||
let inst_end = instance.timeline_start + instance.effective_duration(clip_dur);
|
||||
occupied_ranges.push((inst_start, inst_end, instance.id));
|
||||
}
|
||||
}
|
||||
@@ -762,8 +758,9 @@ impl Document {
|
||||
continue;
|
||||
}
|
||||
if let Some(dur) = self.get_clip_duration(&inst.clip_id) {
|
||||
let end = inst.timeline_start + (inst.trim_end.unwrap_or(dur) - inst.trim_start);
|
||||
non_group.push((inst.timeline_start, end));
|
||||
let start = inst.effective_start();
|
||||
let end = inst.timeline_start + inst.effective_duration(dur);
|
||||
non_group.push((start, end));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -828,10 +825,9 @@ impl Document {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate other clip's end time
|
||||
// Calculate other clip's extent (accounting for loop_before)
|
||||
if let Some(clip_duration) = self.get_clip_duration(&other.clip_id) {
|
||||
let trim_end = other.trim_end.unwrap_or(clip_duration);
|
||||
let other_end = other.timeline_start + (trim_end - other.trim_start);
|
||||
let other_end = other.timeline_start + other.effective_duration(clip_duration);
|
||||
|
||||
// If this clip is to the left and closer than current nearest
|
||||
if other_end <= current_timeline_start && other_end > nearest_end {
|
||||
@@ -878,9 +874,10 @@ impl Document {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this clip is to the right and closer than current nearest
|
||||
if other.timeline_start >= current_end && other.timeline_start < nearest_start {
|
||||
nearest_start = other.timeline_start;
|
||||
// Use effective_start to account for loop_before on the other clip
|
||||
let other_start = other.effective_start();
|
||||
if other_start >= current_end && other_start < nearest_start {
|
||||
nearest_start = other_start;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -890,6 +887,48 @@ impl Document {
|
||||
(nearest_start - current_end).max(0.0) // Gap between our end and next clip's start
|
||||
}
|
||||
}
|
||||
/// Find the maximum amount we can extend loop_before to the left without overlapping.
|
||||
///
|
||||
/// Returns the max additional loop_before distance (from the current effective start).
|
||||
pub fn find_max_loop_extend_left(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
current_effective_start: f64,
|
||||
) -> f64 {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
return current_effective_start;
|
||||
};
|
||||
|
||||
if matches!(layer, AnyLayer::Vector(_)) {
|
||||
return current_effective_start;
|
||||
}
|
||||
|
||||
let instances: &[ClipInstance] = match layer {
|
||||
AnyLayer::Audio(audio) => &audio.clip_instances,
|
||||
AnyLayer::Video(video) => &video.clip_instances,
|
||||
AnyLayer::Effect(effect) => &effect.clip_instances,
|
||||
AnyLayer::Vector(vector) => &vector.clip_instances,
|
||||
};
|
||||
|
||||
let mut nearest_end = 0.0;
|
||||
|
||||
for other in instances {
|
||||
if &other.id == instance_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(clip_duration) = self.get_clip_duration(&other.clip_id) {
|
||||
let other_end = other.timeline_start + other.effective_duration(clip_duration);
|
||||
|
||||
if other_end <= current_effective_start && other_end > nearest_end {
|
||||
nearest_end = other_end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_effective_start - nearest_end
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user