forge
Explore
Log in

Don't you dare go Hollow

merge audio-fades engine (edith 18) and wire fade drag state

Browse files at this commit

55dd056e37502de35bdcdb0265fca0d4478d4cbfAhmet Kaan GümüşParent: 4e4ec16
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.rsmodified+31 -0
@@ -116,6 +116,37 @@ pub(crate) struct Trim {
116 /// still the body.116 /// still the body.
117 pub(crate) const EDGE_W: f32 = 6.;117 pub(crate) const EDGE_W: f32 = 6.;
118 118
119+/// A fade handle's own hit zone, at each *top* corner of an audio clip's box --
120+/// never the trim strip's height, which is [`EDGE_W`] wide down the whole box
121+/// ([`trims`]) and would swallow a fade press if the two overlapped. Sitting
122+/// just inside the trim strip rather than on top of it, so one gesture is
123+/// still exactly one thing: the outer [`EDGE_W`] column lengthens or shortens
124+/// the clip, this small square at its top corner shapes the fade instead.
125+pub(crate) const FADE_HANDLE_W: f32 = 10.;
126+/// Only the label row's own height tall -- a fade handle reaching down into
127+/// the waveform would sit on top of the body drag and the waveform's own
128+/// tooltip both.
129+pub(crate) const FADE_HANDLE_H: f32 = 10.;
130+
131+/// A fade handle being dragged: which clip, which end (`is_in` for the head's
132+/// ramp-up, the tail's ramp-down otherwise), and the gesture's own state --
133+/// `press_x` to measure the pixel delta from, `start` the fade length the
134+/// press found, `to` what the drag is showing right now, `cap` the clip's own
135+/// length in frames ([`Project::set_fade_in`]'s own clamp, so the drag never
136+/// draws past what a release would refuse). One clip, no group: unlike a trim
137+/// a fade never drags a neighbour, and unlike a caption it never wants a
138+/// track's walls.
139+#[derive(Clone, Copy)]
140+pub(crate) struct FadeDrag {
141+ pub(crate) lane: Lane,
142+ pub(crate) idx: usize,
143+ pub(crate) is_in: bool,
144+ pub(crate) press_x: Pixels,
145+ pub(crate) start: u32,
146+ pub(crate) to: u32,
147+ pub(crate) cap: u32,
148+}
149+
119 /// Whether a clip box this wide gets its two trim strips at all. Below three150 /// Whether a clip box this wide gets its two trim strips at all. Below three
120 /// handles wide the pair would occlude the whole box: every press on it would151 /// handles wide the pair would occlude the whole box: every press on it would
121 /// trim, and the clip could not be selected, dragged to another lane or picked152 /// trim, and the clip could not be selected, dragged to another lane or picked
crates/app/src/main.rsmodified+5 -0
@@ -277,6 +277,10 @@ struct Player {
277 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See277 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See
278 /// [`Trim`].278 /// [`Trim`].
279 trim: Option<Trim>,279 trim: Option<Trim>,
280+ /// A drag that started on a fade handle, tracked on the root for the same
281+ /// reason a trim is: a 10 px square is not where the pointer stays. See
282+ /// [`FadeDrag`].
283+ fade_drag: Option<FadeDrag>,
280 /// How far into the clip the last press on a box landed, in timeline284 /// How far into the clip the last press on a box landed, in timeline
281 /// frames: what a drag lets go of is the *point that was grabbed*, so the285 /// frames: what a drag lets go of is the *point that was grabbed*, so the
282 /// head lands that much in front of the pointer and the clip does not jump286 /// head lands that much in front of the pointer and the clip does not jump
@@ -828,6 +832,7 @@ fn main() {
828 splits: Splits::default(),832 splits: Splits::default(),
829 split_drag: None,833 split_drag: None,
830 trim: None,834 trim: None,
835+ fade_drag: None,
831 grab: 0,836 grab: 0,
832 snap: true,837 snap: true,
833 subs_on: true,838 subs_on: true,
crates/app/src/player/timeline_edit.rsmodified+111 -0
@@ -1171,6 +1171,102 @@ impl Player {
1171 cx.notify();1171 cx.notify();
1172 }1172 }
1173 1173
1174+ /// A press on a fade handle: the start of the drag that lengthens or
1175+ /// shortens the ramp at that end of an audio clip. Ctrl-free, unlike a
1176+ /// trim's press -- a fade handle sits inside the trim strip's own column
1177+ /// ([`FADE_HANDLE_W`]) and a ctrl-click there would be read as picking the
1178+ /// clip up rather than as the toggle it means on a trim.
1179+ pub(crate) fn start_fade_drag(
1180+ &mut self,
1181+ lane: Lane,
1182+ idx: usize,
1183+ is_in: bool,
1184+ x: Pixels,
1185+ cx: &mut Context<Self>,
1186+ ) {
1187+ if self.modal() || self.exporting().is_some() {
1188+ return;
1189+ }
1190+ let Some(clip) = self
1191+ .session
1192+ .as_ref()
1193+ .and_then(|session| session.lane_clips(lane).get(idx).copied())
1194+ else {
1195+ return;
1196+ };
1197+ self.pick((lane, idx), false, cx);
1198+ let start = match is_in {
1199+ true => clip.fade_in,
1200+ false => clip.fade_out,
1201+ };
1202+ self.fade_drag = Some(FadeDrag {
1203+ lane,
1204+ idx,
1205+ is_in,
1206+ press_x: x,
1207+ start,
1208+ to: start,
1209+ cap: clip.frames(),
1210+ });
1211+ cx.notify();
1212+ }
1213+
1214+ /// Where the pointer has pulled a fade handle to, in the fade's own
1215+ /// frames -- pulling away from the clip's edge lengthens the ramp for
1216+ /// both handles alike, which is why the tail's own delta is negated: its
1217+ /// handle sits at the *right* of the box, so a hand dragging left (toward
1218+ /// the body) is the same "make it longer" motion the head's handle reads
1219+ /// dragging right.
1220+ pub(crate) fn fade_drag_to(&mut self, x: Pixels, cx: &mut Context<Self>) {
1221+ let Some(fade) = self.fade_drag else {
1222+ return;
1223+ };
1224+ let dx = f32::from(x) - f32::from(fade.press_x);
1225+ let dx = match fade.is_in {
1226+ true => dx,
1227+ false => -dx,
1228+ };
1229+ let delta = fade_delta_frames(dx, self.scale.pps, self.fps);
1230+ let to = (i64::from(fade.start) + delta).clamp(0, i64::from(fade.cap)) as u32;
1231+ self.fade_drag = Some(FadeDrag { to, ..fade });
1232+ cx.notify();
1233+ }
1234+
1235+ /// The release: one edit, one undo step, exactly as [`Player::commit_trim`]
1236+ /// pays for a trim. Autosave's own reason a fade drag has to call
1237+ /// [`Player::mark_dirty`] same as every other edit that reaches the
1238+ /// engine.
1239+ pub(crate) fn commit_fade(&mut self, cx: &mut Context<Self>) {
1240+ let Some(fade) = self.fade_drag.take() else {
1241+ return;
1242+ };
1243+ self.mark_dirty();
1244+ let set = self.session.as_mut().is_some_and(|session| match fade.is_in {
1245+ true => session.set_fade_in(fade.lane, fade.idx, fade.to),
1246+ false => session.set_fade_out(fade.lane, fade.idx, fade.to),
1247+ });
1248+ if set {
1249+ cx.notify();
1250+ }
1251+ }
1252+
1253+ /// The clip's fade-in as the drag is showing it: display only, same as
1254+ /// [`Player::trimmed`] -- the engine hears about it once, at the release.
1255+ pub(crate) fn shown_fade_in(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 {
1256+ match self.fade_drag {
1257+ Some(f) if f.is_in && (f.lane, f.idx) == (lane, idx) => f.to,
1258+ _ => clip.fade_in,
1259+ }
1260+ }
1261+
1262+ /// [`Player::shown_fade_in`]'s twin, for the tail.
1263+ pub(crate) fn shown_fade_out(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 {
1264+ match self.fade_drag {
1265+ Some(f) if !f.is_in && (f.lane, f.idx) == (lane, idx) => f.to,
1266+ _ => clip.fade_out,
1267+ }
1268+ }
1269+
1174 /// The clip as the drag is showing it: an edge under the pointer moves its1270 /// The clip as the drag is showing it: an edge under the pointer moves its
1175 /// own box, and the boxes of everything linked to it, before anything is1271 /// own box, and the boxes of everything linked to it, before anything is
1176 /// committed. Display only -- the project is not touched until the release.1272 /// committed. Display only -- the project is not touched until the release.
@@ -1370,6 +1466,16 @@ impl Player {
1370 }1466 }
1371 return;1467 return;
1372 }1468 }
1469+ // A fade handle is [`FADE_HANDLE_W`] wide and the pointer leaves it
1470+ // on the first drag, same reason and same shape as a trim's own
1471+ // branch just above.
1472+ if self.fade_drag.is_some() {
1473+ match event.pressed_button {
1474+ Some(MouseButton::Left) => self.fade_drag_to(event.position.x, cx),
1475+ _ => self.commit_fade(cx),
1476+ }
1477+ return;
1478+ }
1373 // A colour slider is 4 px tall and the pointer leaves it just as1479 // A colour slider is 4 px tall and the pointer leaves it just as
1374 // fast; every sample is live, so the release owes no write of1480 // fast; every sample is live, so the release owes no write of
1375 // its own -- what the last sample set is what the clip carries.1481 // its own -- what the last sample set is what the clip carries.
@@ -1468,6 +1574,11 @@ impl Player {
1468 self.commit_trim(cx);1574 self.commit_trim(cx);
1469 return;1575 return;
1470 }1576 }
1577+ if self.fade_drag.is_some() {
1578+ self.fade_drag_to(event.position.x, cx);
1579+ self.commit_fade(cx);
1580+ return;
1581+ }
1471 if std::mem::take(&mut self.color_dragging) {1582 if std::mem::take(&mut self.color_dragging) {
1472 // The release lands exactly where the hand let go, and1583 // The release lands exactly where the hand let go, and
1473 // it is a live write like every other sample: the undo1584 // it is a live write like every other sample: the undo
crates/app/src/timeline_math.rsmodified+14 -0
@@ -337,6 +337,20 @@ pub(crate) fn trimmed_clip(clip: Clip, edge: Edge, to: u32, still: bool) -> Clip
337 /// (`Project::trim_sub_room`). Never zero: a placement of no frames is the one337 /// (`Project::trim_sub_room`). Never zero: a placement of no frames is the one
338 /// [`Project::place_sub`] refuses as empty, and a track shorter than a frame is338 /// [`Project::place_sub`] refuses as empty, and a track shorter than a frame is
339 /// still a caption somebody dragged.339 /// still a caption somebody dragged.
340+/// A fade handle's drag, turned into timeline frames: `dx` pixels at `pps`
341+/// pixels per second, at `fps` frames per second -- the one conversion a
342+/// fade-in or fade-out handle needs, and the app-side twin of every other
343+/// pixel-to-frame door on this bed ([`Scale::time_at`]). Zero for a
344+/// degenerate scale (nothing drawn to drag against), never negative on its
345+/// own -- the caller adds this to the fade's length before clamping to the
346+/// clip's own room, so a delta can still shrink a fade by coming back signed.
347+pub(crate) fn fade_delta_frames(dx: f32, pps: f64, fps: f64) -> i64 {
348+ if pps <= 0. {
349+ return 0;
350+ }
351+ ((f64::from(dx) / pps) * fps).round() as i64
352+}
353+
340 pub(crate) fn frames_of_us(us: i64, fps: f64) -> u32 {354 pub(crate) fn frames_of_us(us: i64, fps: f64) -> u32 {
341 match fps.is_finite() && fps > 0. {355 match fps.is_finite() && fps > 0. {
342 true => ((us as f64) * fps / 1e6)356 true => ((us as f64) * fps / 1e6)
crates/engine/src/playback.rsmodified+29 -0
@@ -2552,6 +2552,35 @@ impl PlaybackSession {
2552 self.edit(Dirty::Picture, |p| p.set_fit(lane, idx, fit))2552 self.edit(Dirty::Picture, |p| p.set_fit(lane, idx, fit))
2553 }2553 }
2554 2554
2555+ /// Timeline frames of ramp-up from silence at the start of the clip at
2556+ /// `idx` of `lane`. `0` for an index that is not there.
2557+ pub fn fade_in_of(&self, lane: Lane, idx: usize) -> u32 {
2558+ self.project.fade_in_of(lane, idx)
2559+ }
2560+
2561+ /// Sets that clip's fade-in, clamped to its own length. A sound-only
2562+ /// edit, same as an equalizer or a fader: no reseek needed.
2563+ pub fn set_fade_in(&mut self, lane: Lane, idx: usize, frames: u32) -> bool {
2564+ self.edit(Dirty::Sound, |p| p.set_fade_in(lane, idx, frames))
2565+ }
2566+
2567+ /// Timeline frames of ramp-down to silence at the end of the clip at
2568+ /// `idx` of `lane`. `0` for an index that is not there.
2569+ pub fn fade_out_of(&self, lane: Lane, idx: usize) -> u32 {
2570+ self.project.fade_out_of(lane, idx)
2571+ }
2572+
2573+ /// Sets that clip's fade-out. Same promises as [`Self::set_fade_in`].
2574+ pub fn set_fade_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool {
2575+ self.edit(Dirty::Sound, |p| p.set_fade_out(lane, idx, frames))
2576+ }
2577+
2578+ /// Crossfades the audio clip at `idx` of `lane` into its neighbour, over
2579+ /// `frames`. Same promises as [`Project::crossfade`].
2580+ pub fn crossfade(&mut self, lane: Lane, idx: usize, frames: u32) -> bool {
2581+ self.edit(Dirty::Sound, |p| p.crossfade(lane, idx, frames))
2582+ }
2583+
2555 /// The clip at `idx` -- what a caller copies. It is a pair of source frame2584 /// The clip at `idx` -- what a caller copies. It is a pair of source frame
2556 /// numbers and nothing else, so a copy stays valid after the clip it came2585 /// numbers and nothing else, so a copy stays valid after the clip it came
2557 /// from is deleted. `None` past the end.2586 /// from is deleted. `None` past the end.