Audio fades get wedges, drag handles and a crossfade key
Browse files at this commit- feat
- audio fades get wedges, drag handles and a crossfade key
10 changed files +373 -2
- crates/app/src/interact.rs +31 -0
- crates/app/src/keymap.rs +13 -2
- crates/app/src/main.rs +5 -0
- crates/app/src/player/actions.rs +2 -0
- crates/app/src/player/timeline_edit.rs +139 -0
- crates/app/src/tests/editing.rs +14 -0
- crates/app/src/timeline_math.rs +14 -0
- crates/app/src/ui/timeline.rs +85 -0
- crates/app/src/ui/widgets.rs +41 -0
- crates/engine/src/playback.rs +29 -0
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 three | 150 | /// 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 would | 151 | /// 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 picked | 152 | /// trim, and the clip could not be selected, dragged to another lane or picked |
crates/app/src/keymap.rsmodified+13 -2
| @@ -130,6 +130,10 @@ actions! { | |||
| 130 | Equalizer, | 130 | Equalizer, |
| 131 | Speed, | 131 | Speed, |
| 132 | Silence, | 132 | Silence, |
| 133 | + /// Crossfades the selected audio clip into its neighbour ([`Project::crossfade`]): | ||
| 134 | + /// two adjacent clips picked on one lane, or a single pick faded into the | ||
| 135 | + /// clip right after it. | ||
| 136 | + Crossfade, | ||
| 133 | Mix, | 137 | Mix, |
| 134 | ToggleSnap, | 138 | ToggleSnap, |
| 135 | ToggleSubtitles, | 139 | ToggleSubtitles, |
| @@ -220,6 +224,7 @@ impl ActionId { | |||
| 220 | ActionId::Equalizer => "Equalizer", | 224 | ActionId::Equalizer => "Equalizer", |
| 221 | ActionId::Speed => "Speed (tape)…", | 225 | ActionId::Speed => "Speed (tape)…", |
| 222 | ActionId::Silence => "Silences: cut or speed up…", | 226 | ActionId::Silence => "Silences: cut or speed up…", |
| 227 | + ActionId::Crossfade => "Crossfade into the next clip", | ||
| 223 | ActionId::Mix => "Mix: track volumes and the limiter…", | 228 | ActionId::Mix => "Mix: track volumes and the limiter…", |
| 224 | ActionId::ToggleSnap => "Snap on / off (edges, the playhead, the start)", | 229 | ActionId::ToggleSnap => "Snap on / off (edges, the playhead, the start)", |
| 225 | ActionId::ToggleSubtitles => "Subtitles on / off over the picture", | 230 | ActionId::ToggleSubtitles => "Subtitles on / off over the picture", |
| @@ -286,6 +291,7 @@ impl ActionId { | |||
| 286 | ActionId::Equalizer => "equalizer", | 291 | ActionId::Equalizer => "equalizer", |
| 287 | ActionId::Speed => "speed", | 292 | ActionId::Speed => "speed", |
| 288 | ActionId::Silence => "silence", | 293 | ActionId::Silence => "silence", |
| 294 | + ActionId::Crossfade => "crossfade", | ||
| 289 | ActionId::Mix => "mix", | 295 | ActionId::Mix => "mix", |
| 290 | ActionId::ToggleSnap => "toggle-snap", | 296 | ActionId::ToggleSnap => "toggle-snap", |
| 291 | ActionId::ToggleSubtitles => "toggle-subtitles", | 297 | ActionId::ToggleSubtitles => "toggle-subtitles", |
| @@ -339,7 +345,8 @@ impl ActionId { | |||
| 339 | // The scan reads a clip's sound, but what it does is edit the | 345 | // The scan reads a clip's sound, but what it does is edit the |
| 340 | // timeline the clip is on -- it is a clip card like the three | 346 | // timeline the clip is on -- it is a clip card like the three |
| 341 | // above it, opened on whichever half was picked. | 347 | // above it, opened on whichever half was picked. |
| 342 | - | ActionId::Silence => Category::Clips, | 348 | + | ActionId::Silence |
| 349 | + | ActionId::Crossfade => Category::Clips, | ||
| 343 | // The project's own picture size is not a clip's business, and not | 350 | // The project's own picture size is not a clip's business, and not |
| 344 | // a file operation either: it is what the viewer is looking at. | 351 | // a file operation either: it is what the viewer is looking at. |
| 345 | // Neither is how much of the timeline the panel shows: a zoom edits | 352 | // Neither is how much of the timeline the panel shows: a zoom edits |
| @@ -953,6 +960,10 @@ impl Keymap { | |||
| 953 | // delete (x and delete) -- what it opens is a card that can cut | 960 | // delete (x and delete) -- what it opens is a card that can cut |
| 954 | // forty places at once. | 961 | // forty places at once. |
| 955 | b(ActionId::Silence, "u", false), | 962 | b(ActionId::Silence, "u", false), |
| 963 | + // Crossfade takes ctrl+f: the mnemonic letter for "fade", with | ||
| 964 | + // ctrl its own room now that plain "f" is the mix card's own | ||
| 965 | + // (below) -- the same letter, the modifier telling them apart. | ||
| 966 | + b(ActionId::Crossfade, "f", true), | ||
| 956 | // The mix card takes "f", for the faders on it: "m" would be | 967 | // The mix card takes "f", for the faders on it: "m" would be |
| 957 | // the word but it is the monitoring mute, which is this | 968 | // the word but it is the monitoring mute, which is this |
| 958 | // machine's volume and not the project's -- two things one key | 969 | // machine's volume and not the project's -- two things one key |
| @@ -1265,7 +1276,7 @@ mod tests { | |||
| 1265 | #[test] | 1276 | #[test] |
| 1266 | fn every_default_stroke_reaches_its_action() { | 1277 | fn every_default_stroke_reaches_its_action() { |
| 1267 | let k = Keymap::defaults(); | 1278 | let k = Keymap::defaults(); |
| 1268 | - assert_eq!(k.entries().len(), 61); | 1279 | + assert_eq!(k.entries().len(), 62); |
| 1269 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); | 1280 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); |
| 1270 | assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); | 1281 | assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); |
| 1271 | assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); | 1282 | assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); |
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. See | 277 | /// `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 timeline | 284 | /// 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 the | 285 | /// 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 jump | 286 | /// 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/actions.rsmodified+2 -0
| @@ -53,6 +53,7 @@ impl Player { | |||
| 53 | | ActionId::AddSubtitleLane | 53 | | ActionId::AddSubtitleLane |
| 54 | | ActionId::RemoveSubtitleLane | 54 | | ActionId::RemoveSubtitleLane |
| 55 | | ActionId::ImportSubtitles | 55 | | ActionId::ImportSubtitles |
| 56 | + | ActionId::Crossfade | ||
| 56 | ) { | 57 | ) { |
| 57 | self.mark_dirty(); | 58 | self.mark_dirty(); |
| 58 | } | 59 | } |
| @@ -88,6 +89,7 @@ impl Player { | |||
| 88 | ActionId::Paste => self.paste(cx), | 89 | ActionId::Paste => self.paste(cx), |
| 89 | ActionId::Cut => self.cut(cx), | 90 | ActionId::Cut => self.cut(cx), |
| 90 | ActionId::Regroup => self.regroup(cx), | 91 | ActionId::Regroup => self.regroup(cx), |
| 92 | + ActionId::Crossfade => self.crossfade_selected(cx), | ||
| 91 | ActionId::Detach => self.detach(cx), | 93 | ActionId::Detach => self.detach(cx), |
| 92 | ActionId::Group => self.group(cx), | 94 | ActionId::Group => self.group(cx), |
| 93 | ActionId::Select => self.select_under_playhead(cx), | 95 | ActionId::Select => self.select_under_playhead(cx), |
crates/app/src/player/timeline_edit.rsmodified+139 -0
| @@ -1171,6 +1171,130 @@ 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 | + | ||
| 1270 | + /// Crossfades across the join the selection names: two picks on one audio | ||
| 1271 | + /// lane take the pair, one pick takes it and its right-hand neighbour. The | ||
| 1272 | + /// engine owns adjacency ([`Project::crossfade`]); a refusal is worded | ||
| 1273 | + /// here, in [`Player::regroup`]'s voice, because `false` is all it says. | ||
| 1274 | + pub(crate) fn crossfade_selected(&mut self, cx: &mut Context<Self>) { | ||
| 1275 | + if self.exporting().is_some() { | ||
| 1276 | + return; | ||
| 1277 | + } | ||
| 1278 | + let Some((lane, idx)) = (match self.selected.picks() { | ||
| 1279 | + [a, b] if a.0 == b.0 && a.1.abs_diff(b.1) == 1 => Some((a.0, a.1.min(b.1))), | ||
| 1280 | + _ => self.selected.anchor(), | ||
| 1281 | + }) else { | ||
| 1282 | + self.notify_user("NOTHING TO CROSSFADE — select an audio clip that has a neighbour".into()); | ||
| 1283 | + cx.notify(); | ||
| 1284 | + return; | ||
| 1285 | + }; | ||
| 1286 | + let frames = self.fps.round().max(1.) as u32; | ||
| 1287 | + if let Some(session) = &mut self.session { | ||
| 1288 | + if !session.crossfade(lane, idx, frames) { | ||
| 1289 | + self.notify_user( | ||
| 1290 | + "NOTHING TO CROSSFADE — it takes two audio clips sitting end to end on one lane" | ||
| 1291 | + .into(), | ||
| 1292 | + ); | ||
| 1293 | + } | ||
| 1294 | + } | ||
| 1295 | + cx.notify(); | ||
| 1296 | + } | ||
| 1297 | + | ||
| 1174 | /// The clip as the drag is showing it: an edge under the pointer moves its | 1298 | /// 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 is | 1299 | /// 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. | 1300 | /// committed. Display only -- the project is not touched until the release. |
| @@ -1370,6 +1494,16 @@ impl Player { | |||
| 1370 | } | 1494 | } |
| 1371 | return; | 1495 | return; |
| 1372 | } | 1496 | } |
| 1497 | + // A fade handle is [`FADE_HANDLE_W`] wide and the pointer leaves it | ||
| 1498 | + // on the first drag, same reason and same shape as a trim's own | ||
| 1499 | + // branch just above. | ||
| 1500 | + if self.fade_drag.is_some() { | ||
| 1501 | + match event.pressed_button { | ||
| 1502 | + Some(MouseButton::Left) => self.fade_drag_to(event.position.x, cx), | ||
| 1503 | + _ => self.commit_fade(cx), | ||
| 1504 | + } | ||
| 1505 | + return; | ||
| 1506 | + } | ||
| 1373 | // A colour slider is 4 px tall and the pointer leaves it just as | 1507 | // 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 of | 1508 | // 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. | 1509 | // its own -- what the last sample set is what the clip carries. |
| @@ -1468,6 +1602,11 @@ impl Player { | |||
| 1468 | self.commit_trim(cx); | 1602 | self.commit_trim(cx); |
| 1469 | return; | 1603 | return; |
| 1470 | } | 1604 | } |
| 1605 | + if self.fade_drag.is_some() { | ||
| 1606 | + self.fade_drag_to(event.position.x, cx); | ||
| 1607 | + self.commit_fade(cx); | ||
| 1608 | + return; | ||
| 1609 | + } | ||
| 1471 | if std::mem::take(&mut self.color_dragging) { | 1610 | if std::mem::take(&mut self.color_dragging) { |
| 1472 | // The release lands exactly where the hand let go, and | 1611 | // The release lands exactly where the hand let go, and |
| 1473 | // it is a live write like every other sample: the undo | 1612 | // it is a live write like every other sample: the undo |
crates/app/src/tests/editing.rsmodified+14 -0
| @@ -2042,3 +2042,17 @@ fn frac_along_measures_from_the_elements_own_left_edge() { | |||
| 2042 | // to +12 dB on the first press. | 2042 | // to +12 dB on the first press. |
| 2043 | assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); | 2043 | assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); |
| 2044 | } | 2044 | } |
| 2045 | + | ||
| 2046 | +/// A fade handle's pixels turn into frames at the bed's own scale, and a | ||
| 2047 | +/// degenerate scale (nothing painted) turns into no movement at all. | ||
| 2048 | +#[test] | ||
| 2049 | +fn a_fade_drag_converts_pixels_to_frames_at_the_beds_scale() { | ||
| 2050 | + use crate::timeline_math::fade_delta_frames; | ||
| 2051 | + // 100 px/s at 25 fps: one second of pixels is one second of frames. | ||
| 2052 | + assert_eq!(fade_delta_frames(100., 100., 25.), 25); | ||
| 2053 | + // Signed: dragging back shrinks the fade by the same conversion. | ||
| 2054 | + assert_eq!(fade_delta_frames(-50., 100., 25.), -13); | ||
| 2055 | + // Nothing drawn to drag against moves nothing. | ||
| 2056 | + assert_eq!(fade_delta_frames(40., 0., 25.), 0); | ||
| 2057 | + assert_eq!(fade_delta_frames(40., -1., 25.), 0); | ||
| 2058 | +} | ||
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 one | 337 | /// (`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 is | 338 | /// [`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/app/src/ui/timeline.rsmodified+85 -0
| @@ -1502,6 +1502,91 @@ impl Player { | |||
| 1502 | .child(inner), | 1502 | .child(inner), |
| 1503 | ) | 1503 | ) |
| 1504 | })) | 1504 | })) |
| 1505 | + // The fades: a translucent wedge at each end an | ||
| 1506 | + // audio clip ramps, drawn over the waveform the way | ||
| 1507 | + // every editor shades a fade. Width comes straight | ||
| 1508 | + // off the clip's own frames, never off the visible | ||
| 1509 | + // slice -- unlike the waveform a wedge that starts | ||
| 1510 | + // off the left edge of the bed still shades the | ||
| 1511 | + // clip's own corner once scrolled back into view, | ||
| 1512 | + // because `overflow_hidden` on the box already | ||
| 1513 | + // clips it, and clamped to the box's own width so a | ||
| 1514 | + // fade longer than a zoomed-in clip never draws | ||
| 1515 | + // past its neighbour. | ||
| 1516 | + .when(audio, |d| { | ||
| 1517 | + let fade_in = self.shown_fade_in(lane, i, clip); | ||
| 1518 | + let fade_out = self.shown_fade_out(lane, i, clip); | ||
| 1519 | + d.children( | ||
| 1520 | + [ | ||
| 1521 | + (fade_in > 0).then(|| { | ||
| 1522 | + div() | ||
| 1523 | + .absolute() | ||
| 1524 | + .left_0() | ||
| 1525 | + .top_0() | ||
| 1526 | + .h_full() | ||
| 1527 | + .w(px(scale | ||
| 1528 | + .width_px(f64::from(fade_in) / self.fps) | ||
| 1529 | + .min(width))) | ||
| 1530 | + .child(fade_wedge(true)) | ||
| 1531 | + }), | ||
| 1532 | + (fade_out > 0).then(|| { | ||
| 1533 | + div() | ||
| 1534 | + .absolute() | ||
| 1535 | + .right_0() | ||
| 1536 | + .top_0() | ||
| 1537 | + .h_full() | ||
| 1538 | + .w(px(scale | ||
| 1539 | + .width_px(f64::from(fade_out) / self.fps) | ||
| 1540 | + .min(width))) | ||
| 1541 | + .child(fade_wedge(false)) | ||
| 1542 | + }), | ||
| 1543 | + ] | ||
| 1544 | + .into_iter() | ||
| 1545 | + .flatten(), | ||
| 1546 | + ) | ||
| 1547 | + }) | ||
| 1548 | + // The fade handles: a small grab corner at each | ||
| 1549 | + // *top* of an audio clip's box, sitting just | ||
| 1550 | + // inside the [`EDGE_W`] trim strip rather than on | ||
| 1551 | + // top of it -- [`FADE_HANDLE_H`] never reaches | ||
| 1552 | + // past the label row, so a trim's own strip (the | ||
| 1553 | + // box's *full* height) is still the whole of what | ||
| 1554 | + // a press below the label starts. Gated on | ||
| 1555 | + // [`trims`] like the trim strips themselves: a box | ||
| 1556 | + // too small for an edge to trim by is too small | ||
| 1557 | + // for a fade handle either. | ||
| 1558 | + .when(audio && trims(span), |d| { | ||
| 1559 | + d.children([Edge::Start, Edge::End].into_iter().map(|edge| { | ||
| 1560 | + let is_in = edge == Edge::Start; | ||
| 1561 | + let mut handle = div() | ||
| 1562 | + .absolute() | ||
| 1563 | + .top_0() | ||
| 1564 | + .w(px(FADE_HANDLE_W)) | ||
| 1565 | + .h(px(FADE_HANDLE_H)) | ||
| 1566 | + .occlude() | ||
| 1567 | + .cursor(CursorStyle::ResizeLeftRight) | ||
| 1568 | + .hover(|s| s.bg(rgb(ACCENT_PRIMARY()))) | ||
| 1569 | + .on_mouse_down( | ||
| 1570 | + MouseButton::Left, | ||
| 1571 | + cx.listener( | ||
| 1572 | + move |this, event: &MouseDownEvent, _, cx| { | ||
| 1573 | + this.start_fade_drag( | ||
| 1574 | + lane, | ||
| 1575 | + i, | ||
| 1576 | + is_in, | ||
| 1577 | + event.position.x, | ||
| 1578 | + cx, | ||
| 1579 | + ); | ||
| 1580 | + }, | ||
| 1581 | + ), | ||
| 1582 | + ); | ||
| 1583 | + handle = match edge { | ||
| 1584 | + Edge::Start => handle.left(px(EDGE_W)), | ||
| 1585 | + Edge::End => handle.right(px(EDGE_W)), | ||
| 1586 | + }; | ||
| 1587 | + handle | ||
| 1588 | + })) | ||
| 1589 | + }) | ||
| 1505 | // A speeded clip says so on the box, in the corner | 1590 | // A speeded clip says so on the box, in the corner |
| 1506 | // the label does not reach: the box's width alone | 1591 | // the label does not reach: the box's width alone |
| 1507 | // cannot say whether a short clip is a trim or a | 1592 | // cannot say whether a short clip is a trim or a |
crates/app/src/ui/widgets.rsmodified+41 -0
| @@ -35,6 +35,47 @@ pub(crate) fn waveform(peaks: Arc<Vec<(f32, f32)>>, from: f64, to: f64) -> impl | |||
| 35 | .size_full() | 35 | .size_full() |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | +/// An audio clip's fade, drawn as a translucent wedge over its box: silent | ||
| 39 | +/// (full shade) at the clip's own edge, clear by the fade's far side -- | ||
| 40 | +/// [`is_in`] mirrors it for the tail's ramp-down. The same `canvas` + | ||
| 41 | +/// `PathBuilder` idiom [`waveform`] draws its envelope with, filled with the | ||
| 42 | +/// same wash the in/out range already paints the bed with | ||
| 43 | +/// (`ACCENT_WASH`, timeline.rs), so a fade reads as one more overlay on this | ||
| 44 | +/// bed rather than a shape borrowed from somewhere else. | ||
| 45 | +pub(crate) fn fade_wedge(is_in: bool) -> impl IntoElement { | ||
| 46 | + canvas( | ||
| 47 | + |_, _, _| (), | ||
| 48 | + move |bounds, _, window, _| { | ||
| 49 | + let (o, s) = (bounds.origin, bounds.size); | ||
| 50 | + let (w, h) = (f32::from(s.width), f32::from(s.height)); | ||
| 51 | + if w <= 0. || h <= 0. { | ||
| 52 | + return; | ||
| 53 | + } | ||
| 54 | + // A right triangle: the clip's silent corner (top of the clip's | ||
| 55 | + // own edge) down to the bed and across to the fade's far side -- | ||
| 56 | + // the wedge shape every editor draws for a ramp. | ||
| 57 | + let points = match is_in { | ||
| 58 | + true => vec![ | ||
| 59 | + point(o.x, o.y), | ||
| 60 | + point(o.x, o.y + px(h)), | ||
| 61 | + point(o.x + px(w), o.y + px(h)), | ||
| 62 | + ], | ||
| 63 | + false => vec![ | ||
| 64 | + point(o.x + px(w), o.y), | ||
| 65 | + point(o.x + px(w), o.y + px(h)), | ||
| 66 | + point(o.x, o.y + px(h)), | ||
| 67 | + ], | ||
| 68 | + }; | ||
| 69 | + let mut path = PathBuilder::fill(); | ||
| 70 | + path.add_polygon(&points, true); | ||
| 71 | + if let Ok(path) = path.build() { | ||
| 72 | + window.paint_path(path, rgba(ACCENT_WASH())); | ||
| 73 | + } | ||
| 74 | + }, | ||
| 75 | + ) | ||
| 76 | + .size_full() | ||
| 77 | +} | ||
| 78 | + | ||
| 38 | /// A toolbar button: its glyph, its name, and its key on hover. `id` only buys | 79 | /// A toolbar button: its glyph, its name, and its key on hover. `id` only buys |
| 39 | /// `on_click` and the tooltip -- it is still not focusable, so the root's own | 80 | /// `on_click` and the tooltip -- it is still not focusable, so the root's own |
| 40 | /// key listener keeps working after a press, and the click lands on mouse-up | 81 | /// key listener keeps working after a press, and the click lands on mouse-up |
crates/engine/src/playback.rsmodified+29 -0
| @@ -2556,6 +2556,35 @@ impl PlaybackSession { | |||
| 2556 | self.edit(Dirty::Picture, |p| p.set_fit(lane, idx, fit)) | 2556 | self.edit(Dirty::Picture, |p| p.set_fit(lane, idx, fit)) |
| 2557 | } | 2557 | } |
| 2558 | 2558 | ||
| 2559 | + /// Timeline frames of ramp-up from silence at the start of the clip at | ||
| 2560 | + /// `idx` of `lane`. `0` for an index that is not there. | ||
| 2561 | + pub fn fade_in_of(&self, lane: Lane, idx: usize) -> u32 { | ||
| 2562 | + self.project.fade_in_of(lane, idx) | ||
| 2563 | + } | ||
| 2564 | + | ||
| 2565 | + /// Sets that clip's fade-in, clamped to its own length. A sound-only | ||
| 2566 | + /// edit, same as an equalizer or a fader: no reseek needed. | ||
| 2567 | + pub fn set_fade_in(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { | ||
| 2568 | + self.edit(Dirty::Sound, |p| p.set_fade_in(lane, idx, frames)) | ||
| 2569 | + } | ||
| 2570 | + | ||
| 2571 | + /// Timeline frames of ramp-down to silence at the end of the clip at | ||
| 2572 | + /// `idx` of `lane`. `0` for an index that is not there. | ||
| 2573 | + pub fn fade_out_of(&self, lane: Lane, idx: usize) -> u32 { | ||
| 2574 | + self.project.fade_out_of(lane, idx) | ||
| 2575 | + } | ||
| 2576 | + | ||
| 2577 | + /// Sets that clip's fade-out. Same promises as [`Self::set_fade_in`]. | ||
| 2578 | + pub fn set_fade_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { | ||
| 2579 | + self.edit(Dirty::Sound, |p| p.set_fade_out(lane, idx, frames)) | ||
| 2580 | + } | ||
| 2581 | + | ||
| 2582 | + /// Crossfades the audio clip at `idx` of `lane` into its neighbour, over | ||
| 2583 | + /// `frames`. Same promises as [`Project::crossfade`]. | ||
| 2584 | + pub fn crossfade(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { | ||
| 2585 | + self.edit(Dirty::Sound, |p| p.crossfade(lane, idx, frames)) | ||
| 2586 | + } | ||
| 2587 | + | ||
| 2559 | /// The clip at `idx` -- what a caller copies. It is a pair of source frame | 2588 | /// The clip at `idx` -- what a caller copies. It is a pair of source frame |
| 2560 | /// numbers and nothing else, so a copy stays valid after the clip it came | 2589 | /// numbers and nothing else, so a copy stays valid after the clip it came |
| 2561 | /// from is deleted. `None` past the end. | 2590 | /// from is deleted. `None` past the end. |