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/keymap.rs b/crates/app/src/keymap.rs index b322447..22367e9 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -130,6 +130,10 @@ actions! { Equalizer, Speed, Silence, + /// Crossfades the selected audio clip into its neighbour ([`Project::crossfade`]): + /// two adjacent clips picked on one lane, or a single pick faded into the + /// clip right after it. + Crossfade, Mix, ToggleSnap, ToggleSubtitles, @@ -220,6 +224,7 @@ impl ActionId { ActionId::Equalizer => "Equalizer", ActionId::Speed => "Speed (tape)…", ActionId::Silence => "Silences: cut or speed up…", + ActionId::Crossfade => "Crossfade into the next clip", ActionId::Mix => "Mix: track volumes and the limiter…", ActionId::ToggleSnap => "Snap on / off (edges, the playhead, the start)", ActionId::ToggleSubtitles => "Subtitles on / off over the picture", @@ -286,6 +291,7 @@ impl ActionId { ActionId::Equalizer => "equalizer", ActionId::Speed => "speed", ActionId::Silence => "silence", + ActionId::Crossfade => "crossfade", ActionId::Mix => "mix", ActionId::ToggleSnap => "toggle-snap", ActionId::ToggleSubtitles => "toggle-subtitles", @@ -339,7 +345,8 @@ impl ActionId { // The scan reads a clip's sound, but what it does is edit the // timeline the clip is on -- it is a clip card like the three // above it, opened on whichever half was picked. - | ActionId::Silence => Category::Clips, + | ActionId::Silence + | ActionId::Crossfade => Category::Clips, // The project's own picture size is not a clip's business, and not // a file operation either: it is what the viewer is looking at. // Neither is how much of the timeline the panel shows: a zoom edits @@ -953,6 +960,10 @@ impl Keymap { // delete (x and delete) -- what it opens is a card that can cut // forty places at once. b(ActionId::Silence, "u", false), + // Crossfade takes ctrl+f: the mnemonic letter for "fade", with + // ctrl its own room now that plain "f" is the mix card's own + // (below) -- the same letter, the modifier telling them apart. + b(ActionId::Crossfade, "f", true), // The mix card takes "f", for the faders on it: "m" would be // the word but it is the monitoring mute, which is this // machine's volume and not the project's -- two things one key @@ -1265,7 +1276,7 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 61); + assert_eq!(k.entries().len(), 62); assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); 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/actions.rs b/crates/app/src/player/actions.rs index 5a21b37..ca77480 100644 --- a/crates/app/src/player/actions.rs +++ b/crates/app/src/player/actions.rs @@ -53,6 +53,7 @@ impl Player { | ActionId::AddSubtitleLane | ActionId::RemoveSubtitleLane | ActionId::ImportSubtitles + | ActionId::Crossfade ) { self.mark_dirty(); } @@ -88,6 +89,7 @@ impl Player { ActionId::Paste => self.paste(cx), ActionId::Cut => self.cut(cx), ActionId::Regroup => self.regroup(cx), + ActionId::Crossfade => self.crossfade_selected(cx), ActionId::Detach => self.detach(cx), ActionId::Group => self.group(cx), ActionId::Select => self.select_under_playhead(cx), diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 4455d02..e856c25 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1171,6 +1171,130 @@ 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, + } + } + + /// Crossfades across the join the selection names: two picks on one audio + /// lane take the pair, one pick takes it and its right-hand neighbour. The + /// engine owns adjacency ([`Project::crossfade`]); a refusal is worded + /// here, in [`Player::regroup`]'s voice, because `false` is all it says. + pub(crate) fn crossfade_selected(&mut self, cx: &mut Context) { + if self.exporting().is_some() { + return; + } + let Some((lane, idx)) = (match self.selected.picks() { + [a, b] if a.0 == b.0 && a.1.abs_diff(b.1) == 1 => Some((a.0, a.1.min(b.1))), + _ => self.selected.anchor(), + }) else { + self.notify_user("NOTHING TO CROSSFADE — select an audio clip that has a neighbour".into()); + cx.notify(); + return; + }; + let frames = self.fps.round().max(1.) as u32; + if let Some(session) = &mut self.session { + if !session.crossfade(lane, idx, frames) { + self.notify_user( + "NOTHING TO CROSSFADE — it takes two audio clips sitting end to end on one lane" + .into(), + ); + } + } + cx.notify(); + } + /// 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 +1494,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 +1602,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/tests/editing.rs b/crates/app/src/tests/editing.rs index 0421f74..5a57fe8 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -2042,3 +2042,17 @@ fn frac_along_measures_from_the_elements_own_left_edge() { // to +12 dB on the first press. assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); } + +/// A fade handle's pixels turn into frames at the bed's own scale, and a +/// degenerate scale (nothing painted) turns into no movement at all. +#[test] +fn a_fade_drag_converts_pixels_to_frames_at_the_beds_scale() { + use crate::timeline_math::fade_delta_frames; + // 100 px/s at 25 fps: one second of pixels is one second of frames. + assert_eq!(fade_delta_frames(100., 100., 25.), 25); + // Signed: dragging back shrinks the fade by the same conversion. + assert_eq!(fade_delta_frames(-50., 100., 25.), -13); + // Nothing drawn to drag against moves nothing. + assert_eq!(fade_delta_frames(40., 0., 25.), 0); + assert_eq!(fade_delta_frames(40., -1., 25.), 0); +} 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/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index f375619..6ea34fb 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -1502,6 +1502,91 @@ impl Player { .child(inner), ) })) + // The fades: a translucent wedge at each end an + // audio clip ramps, drawn over the waveform the way + // every editor shades a fade. Width comes straight + // off the clip's own frames, never off the visible + // slice -- unlike the waveform a wedge that starts + // off the left edge of the bed still shades the + // clip's own corner once scrolled back into view, + // because `overflow_hidden` on the box already + // clips it, and clamped to the box's own width so a + // fade longer than a zoomed-in clip never draws + // past its neighbour. + .when(audio, |d| { + let fade_in = self.shown_fade_in(lane, i, clip); + let fade_out = self.shown_fade_out(lane, i, clip); + d.children( + [ + (fade_in > 0).then(|| { + div() + .absolute() + .left_0() + .top_0() + .h_full() + .w(px(scale + .width_px(f64::from(fade_in) / self.fps) + .min(width))) + .child(fade_wedge(true)) + }), + (fade_out > 0).then(|| { + div() + .absolute() + .right_0() + .top_0() + .h_full() + .w(px(scale + .width_px(f64::from(fade_out) / self.fps) + .min(width))) + .child(fade_wedge(false)) + }), + ] + .into_iter() + .flatten(), + ) + }) + // The fade handles: a small grab corner at each + // *top* of an audio clip's box, sitting just + // inside the [`EDGE_W`] trim strip rather than on + // top of it -- [`FADE_HANDLE_H`] never reaches + // past the label row, so a trim's own strip (the + // box's *full* height) is still the whole of what + // a press below the label starts. Gated on + // [`trims`] like the trim strips themselves: a box + // too small for an edge to trim by is too small + // for a fade handle either. + .when(audio && trims(span), |d| { + d.children([Edge::Start, Edge::End].into_iter().map(|edge| { + let is_in = edge == Edge::Start; + let mut handle = div() + .absolute() + .top_0() + .w(px(FADE_HANDLE_W)) + .h(px(FADE_HANDLE_H)) + .occlude() + .cursor(CursorStyle::ResizeLeftRight) + .hover(|s| s.bg(rgb(ACCENT_PRIMARY()))) + .on_mouse_down( + MouseButton::Left, + cx.listener( + move |this, event: &MouseDownEvent, _, cx| { + this.start_fade_drag( + lane, + i, + is_in, + event.position.x, + cx, + ); + }, + ), + ); + handle = match edge { + Edge::Start => handle.left(px(EDGE_W)), + Edge::End => handle.right(px(EDGE_W)), + }; + handle + })) + }) // A speeded clip says so on the box, in the corner // the label does not reach: the box's width alone // cannot say whether a short clip is a trim or a diff --git a/crates/app/src/ui/widgets.rs b/crates/app/src/ui/widgets.rs index 82d2200..4223c48 100644 --- a/crates/app/src/ui/widgets.rs +++ b/crates/app/src/ui/widgets.rs @@ -35,6 +35,47 @@ pub(crate) fn waveform(peaks: Arc>, from: f64, to: f64) -> impl .size_full() } +/// An audio clip's fade, drawn as a translucent wedge over its box: silent +/// (full shade) at the clip's own edge, clear by the fade's far side -- +/// [`is_in`] mirrors it for the tail's ramp-down. The same `canvas` + +/// `PathBuilder` idiom [`waveform`] draws its envelope with, filled with the +/// same wash the in/out range already paints the bed with +/// (`ACCENT_WASH`, timeline.rs), so a fade reads as one more overlay on this +/// bed rather than a shape borrowed from somewhere else. +pub(crate) fn fade_wedge(is_in: bool) -> impl IntoElement { + canvas( + |_, _, _| (), + move |bounds, _, window, _| { + let (o, s) = (bounds.origin, bounds.size); + let (w, h) = (f32::from(s.width), f32::from(s.height)); + if w <= 0. || h <= 0. { + return; + } + // A right triangle: the clip's silent corner (top of the clip's + // own edge) down to the bed and across to the fade's far side -- + // the wedge shape every editor draws for a ramp. + let points = match is_in { + true => vec![ + point(o.x, o.y), + point(o.x, o.y + px(h)), + point(o.x + px(w), o.y + px(h)), + ], + false => vec![ + point(o.x + px(w), o.y), + point(o.x + px(w), o.y + px(h)), + point(o.x, o.y + px(h)), + ], + }; + let mut path = PathBuilder::fill(); + path.add_polygon(&points, true); + if let Ok(path) = path.build() { + window.paint_path(path, rgba(ACCENT_WASH())); + } + }, + ) + .size_full() +} + /// A toolbar button: its glyph, its name, and its key on hover. `id` only buys /// `on_click` and the tooltip -- it is still not focusable, so the root's own /// key listener keeps working after a press, and the click lands on mouse-up diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index d29140d..17d9187 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -2556,6 +2556,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.