From 55dd056e37502de35bdcdb0265fca0d4478d4cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 02:13:13 +0300 Subject: [PATCH] merge audio-fades engine (edith 18) and wire fade drag state Merges the fades/crossfade engine branch in, adds PlaybackSession wrappers (fade_in_of/set_fade_in/fade_out_of/set_fade_out/crossfade) and the FadeDrag gesture plumbing on the root (start/move/commit), ahead of the drawing and keymap work. --- crates/app/src/interact.rs | 31 +++++++ crates/app/src/main.rs | 5 ++ crates/app/src/player/timeline_edit.rs | 111 +++++++++++++++++++++++++ crates/app/src/timeline_math.rs | 14 ++++ crates/engine/src/playback.rs | 29 +++++++ 5 files changed, 190 insertions(+) diff --git a/crates/app/src/interact.rs b/crates/app/src/interact.rs index 17c9830..ebfeb6a 100644 --- a/crates/app/src/interact.rs +++ b/crates/app/src/interact.rs @@ -116,6 +116,37 @@ pub(crate) struct Trim { /// still the body. pub(crate) const EDGE_W: f32 = 6.; +/// A fade handle's own hit zone, at each *top* corner of an audio clip's box -- +/// never the trim strip's height, which is [`EDGE_W`] wide down the whole box +/// ([`trims`]) and would swallow a fade press if the two overlapped. Sitting +/// just inside the trim strip rather than on top of it, so one gesture is +/// still exactly one thing: the outer [`EDGE_W`] column lengthens or shortens +/// the clip, this small square at its top corner shapes the fade instead. +pub(crate) const FADE_HANDLE_W: f32 = 10.; +/// Only the label row's own height tall -- a fade handle reaching down into +/// the waveform would sit on top of the body drag and the waveform's own +/// tooltip both. +pub(crate) const FADE_HANDLE_H: f32 = 10.; + +/// A fade handle being dragged: which clip, which end (`is_in` for the head's +/// ramp-up, the tail's ramp-down otherwise), and the gesture's own state -- +/// `press_x` to measure the pixel delta from, `start` the fade length the +/// press found, `to` what the drag is showing right now, `cap` the clip's own +/// length in frames ([`Project::set_fade_in`]'s own clamp, so the drag never +/// draws past what a release would refuse). One clip, no group: unlike a trim +/// a fade never drags a neighbour, and unlike a caption it never wants a +/// track's walls. +#[derive(Clone, Copy)] +pub(crate) struct FadeDrag { + pub(crate) lane: Lane, + pub(crate) idx: usize, + pub(crate) is_in: bool, + pub(crate) press_x: Pixels, + pub(crate) start: u32, + pub(crate) to: u32, + pub(crate) cap: u32, +} + /// Whether a clip box this wide gets its two trim strips at all. Below three /// handles wide the pair would occlude the whole box: every press on it would /// trim, and the clip could not be selected, dragged to another lane or picked diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index a65360a..4f6d7b3 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -277,6 +277,10 @@ struct Player { /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See /// [`Trim`]. trim: Option, + /// A drag that started on a fade handle, tracked on the root for the same + /// reason a trim is: a 10 px square is not where the pointer stays. See + /// [`FadeDrag`]. + fade_drag: Option, /// How far into the clip the last press on a box landed, in timeline /// frames: what a drag lets go of is the *point that was grabbed*, so the /// head lands that much in front of the pointer and the clip does not jump @@ -828,6 +832,7 @@ fn main() { splits: Splits::default(), split_drag: None, trim: None, + fade_drag: None, grab: 0, snap: true, subs_on: true, diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 4455d02..c796653 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1171,6 +1171,102 @@ impl Player { cx.notify(); } + /// A press on a fade handle: the start of the drag that lengthens or + /// shortens the ramp at that end of an audio clip. Ctrl-free, unlike a + /// trim's press -- a fade handle sits inside the trim strip's own column + /// ([`FADE_HANDLE_W`]) and a ctrl-click there would be read as picking the + /// clip up rather than as the toggle it means on a trim. + pub(crate) fn start_fade_drag( + &mut self, + lane: Lane, + idx: usize, + is_in: bool, + x: Pixels, + cx: &mut Context, + ) { + if self.modal() || self.exporting().is_some() { + return; + } + let Some(clip) = self + .session + .as_ref() + .and_then(|session| session.lane_clips(lane).get(idx).copied()) + else { + return; + }; + self.pick((lane, idx), false, cx); + let start = match is_in { + true => clip.fade_in, + false => clip.fade_out, + }; + self.fade_drag = Some(FadeDrag { + lane, + idx, + is_in, + press_x: x, + start, + to: start, + cap: clip.frames(), + }); + cx.notify(); + } + + /// Where the pointer has pulled a fade handle to, in the fade's own + /// frames -- pulling away from the clip's edge lengthens the ramp for + /// both handles alike, which is why the tail's own delta is negated: its + /// handle sits at the *right* of the box, so a hand dragging left (toward + /// the body) is the same "make it longer" motion the head's handle reads + /// dragging right. + pub(crate) fn fade_drag_to(&mut self, x: Pixels, cx: &mut Context) { + let Some(fade) = self.fade_drag else { + return; + }; + let dx = f32::from(x) - f32::from(fade.press_x); + let dx = match fade.is_in { + true => dx, + false => -dx, + }; + let delta = fade_delta_frames(dx, self.scale.pps, self.fps); + let to = (i64::from(fade.start) + delta).clamp(0, i64::from(fade.cap)) as u32; + self.fade_drag = Some(FadeDrag { to, ..fade }); + cx.notify(); + } + + /// The release: one edit, one undo step, exactly as [`Player::commit_trim`] + /// pays for a trim. Autosave's own reason a fade drag has to call + /// [`Player::mark_dirty`] same as every other edit that reaches the + /// engine. + pub(crate) fn commit_fade(&mut self, cx: &mut Context) { + let Some(fade) = self.fade_drag.take() else { + return; + }; + self.mark_dirty(); + let set = self.session.as_mut().is_some_and(|session| match fade.is_in { + true => session.set_fade_in(fade.lane, fade.idx, fade.to), + false => session.set_fade_out(fade.lane, fade.idx, fade.to), + }); + if set { + cx.notify(); + } + } + + /// The clip's fade-in as the drag is showing it: display only, same as + /// [`Player::trimmed`] -- the engine hears about it once, at the release. + pub(crate) fn shown_fade_in(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 { + match self.fade_drag { + Some(f) if f.is_in && (f.lane, f.idx) == (lane, idx) => f.to, + _ => clip.fade_in, + } + } + + /// [`Player::shown_fade_in`]'s twin, for the tail. + pub(crate) fn shown_fade_out(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 { + match self.fade_drag { + Some(f) if !f.is_in && (f.lane, f.idx) == (lane, idx) => f.to, + _ => clip.fade_out, + } + } + /// The clip as the drag is showing it: an edge under the pointer moves its /// own box, and the boxes of everything linked to it, before anything is /// committed. Display only -- the project is not touched until the release. @@ -1370,6 +1466,16 @@ impl Player { } return; } + // A fade handle is [`FADE_HANDLE_W`] wide and the pointer leaves it + // on the first drag, same reason and same shape as a trim's own + // branch just above. + if self.fade_drag.is_some() { + match event.pressed_button { + Some(MouseButton::Left) => self.fade_drag_to(event.position.x, cx), + _ => self.commit_fade(cx), + } + return; + } // A colour slider is 4 px tall and the pointer leaves it just as // fast; every sample is live, so the release owes no write of // its own -- what the last sample set is what the clip carries. @@ -1468,6 +1574,11 @@ impl Player { self.commit_trim(cx); return; } + if self.fade_drag.is_some() { + self.fade_drag_to(event.position.x, cx); + self.commit_fade(cx); + return; + } if std::mem::take(&mut self.color_dragging) { // The release lands exactly where the hand let go, and // it is a live write like every other sample: the undo diff --git a/crates/app/src/timeline_math.rs b/crates/app/src/timeline_math.rs index d9e6eca..a4f77c2 100644 --- a/crates/app/src/timeline_math.rs +++ b/crates/app/src/timeline_math.rs @@ -337,6 +337,20 @@ pub(crate) fn trimmed_clip(clip: Clip, edge: Edge, to: u32, still: bool) -> Clip /// (`Project::trim_sub_room`). Never zero: a placement of no frames is the one /// [`Project::place_sub`] refuses as empty, and a track shorter than a frame is /// still a caption somebody dragged. +/// A fade handle's drag, turned into timeline frames: `dx` pixels at `pps` +/// pixels per second, at `fps` frames per second -- the one conversion a +/// fade-in or fade-out handle needs, and the app-side twin of every other +/// pixel-to-frame door on this bed ([`Scale::time_at`]). Zero for a +/// degenerate scale (nothing drawn to drag against), never negative on its +/// own -- the caller adds this to the fade's length before clamping to the +/// clip's own room, so a delta can still shrink a fade by coming back signed. +pub(crate) fn fade_delta_frames(dx: f32, pps: f64, fps: f64) -> i64 { + if pps <= 0. { + return 0; + } + ((f64::from(dx) / pps) * fps).round() as i64 +} + pub(crate) fn frames_of_us(us: i64, fps: f64) -> u32 { match fps.is_finite() && fps > 0. { true => ((us as f64) * fps / 1e6) diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index fa16859..d88c339 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -2552,6 +2552,35 @@ impl PlaybackSession { self.edit(Dirty::Picture, |p| p.set_fit(lane, idx, fit)) } + /// Timeline frames of ramp-up from silence at the start of the clip at + /// `idx` of `lane`. `0` for an index that is not there. + pub fn fade_in_of(&self, lane: Lane, idx: usize) -> u32 { + self.project.fade_in_of(lane, idx) + } + + /// Sets that clip's fade-in, clamped to its own length. A sound-only + /// edit, same as an equalizer or a fader: no reseek needed. + pub fn set_fade_in(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { + self.edit(Dirty::Sound, |p| p.set_fade_in(lane, idx, frames)) + } + + /// Timeline frames of ramp-down to silence at the end of the clip at + /// `idx` of `lane`. `0` for an index that is not there. + pub fn fade_out_of(&self, lane: Lane, idx: usize) -> u32 { + self.project.fade_out_of(lane, idx) + } + + /// Sets that clip's fade-out. Same promises as [`Self::set_fade_in`]. + pub fn set_fade_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { + self.edit(Dirty::Sound, |p| p.set_fade_out(lane, idx, frames)) + } + + /// Crossfades the audio clip at `idx` of `lane` into its neighbour, over + /// `frames`. Same promises as [`Project::crossfade`]. + pub fn crossfade(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { + self.edit(Dirty::Sound, |p| p.crossfade(lane, idx, frames)) + } + /// The clip at `idx` -- what a caller copies. It is a pair of source frame /// numbers and nothing else, so a copy stays valid after the clip it came /// from is deleted. `None` past the end.