forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

pan.rs112 lines, 4.0 KiBHistoryRawDownload
1//! Pan: the scrub fields of the Mix section -- one for the selected audio clip, one on each lane's
2//! strip. A drag follows the pointer and shows live; the engine is written once, on release, as one
3//! undo step (a pan rides in the clip's envelope, so each write rebuilds the sound).
4
5use crate::*;
6
7/// What a pan scrub turns.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub(crate) enum PanTarget {
10 Clip(Lane, usize),
11 Lane(Lane),
12}
13
14/// A pan being scrubbed: where the press was, the pan it had, and the pan now.
15#[derive(Clone, Copy, Debug, PartialEq)]
16pub(crate) struct PanScrub {
17 pub(crate) target: PanTarget,
18 pub(crate) x0: f32,
19 pub(crate) v0: f32,
20 pub(crate) cur: f32,
21 /// Pan per pixel of drag: the track's width sweeps the range.
22 pub(crate) per_px: f32,
23}
24
25/// The pan a drag of `dx` px from `v0` lands on, at `per_px` pan per pixel (a slider's track
26/// sweeps the whole range): on the 1% grid, within `-1..=1`, with the centre caught within 3%.
27pub(crate) fn scrub_pan_at(v0: f32, dx: f32, per_px: f32) -> f32 {
28 let v = ((v0 + dx * per_px).clamp(-1., 1.) * 100.).round() / 100.;
29 if v.abs() < 0.03 { 0. } else { v }
30}
31
32/// A pan as the field writes it: `Centre` for the middle, else the percent and the side
33/// (`40% L`, `25% R`).
34pub(crate) fn pan_text(pan: f32) -> String {
35 let pct = (pan.abs() * 100.).round() as u32;
36 match (pct, pan < 0.) {
37 (0, _) => "Centre".into(),
38 (n, true) => format!("{n}% L"),
39 (n, false) => format!("{n}% R"),
40 }
41}
42
43impl Player {
44 /// The pan the target has in the engine.
45 pub(crate) fn pan_of(&self, target: PanTarget) -> f32 {
46 let Some(s) = self.session.as_ref() else {
47 return 0.;
48 };
49 match target {
50 PanTarget::Clip(lane, idx) => s.clip_pan_of(lane, idx).unwrap_or(0.),
51 PanTarget::Lane(lane) => s.lane_pan(lane),
52 }
53 }
54
55 /// The pan to show: the scrub's while one is turning this target, else the engine's.
56 pub(crate) fn pan_shown(&self, target: PanTarget) -> f32 {
57 match self.pan_scrub {
58 Some(s) if s.target == target => s.cur,
59 _ => self.pan_of(target),
60 }
61 }
62
63 /// A press on a pan slider's track, `along` it (0..1) and `width` px wide: the pan jumps
64 /// under the pointer and the drag that follows carries it, the thumb keeping to the hand.
65 pub(crate) fn begin_pan_press(&mut self, target: PanTarget, x: f32, along: f32, width: f32) {
66 let v0 = self.pan_of(target);
67 let per_px = 2. / width.max(1.);
68 let dx = (along.clamp(0., 1.) * 2. - 1. - v0) / per_px;
69 let cur = scrub_pan_at(v0, dx, per_px);
70 self.pan_scrub = Some(PanScrub { target, x0: x - dx, v0, cur, per_px });
71 }
72
73 /// The pointer moved with a pan scrub in flight.
74 pub(crate) fn pan_scrub_move(&mut self, x: f32, cx: &mut Context<Self>) {
75 let Some(s) = self.pan_scrub else {
76 return;
77 };
78 let cur = scrub_pan_at(s.v0, x - s.x0, s.per_px);
79 if cur != s.cur {
80 self.pan_scrub = Some(PanScrub { cur, ..s });
81 cx.notify();
82 }
83 }
84
85 /// The button came up: the scrubbed pan is written as one undo step. `true` if a scrub ended.
86 pub(crate) fn pan_scrub_end(&mut self, cx: &mut Context<Self>) -> bool {
87 let Some(s) = self.pan_scrub.take() else {
88 return false;
89 };
90 if s.cur != s.v0 {
91 self.write_pan(s.target, s.cur);
92 }
93 cx.notify();
94 true
95 }
96
97 /// Puts a pan back in the middle (a double-click on its field).
98 pub(crate) fn reset_pan(&mut self, target: PanTarget, cx: &mut Context<Self>) {
99 self.write_pan(target, 0.);
100 cx.notify();
101 }
102
103 fn write_pan(&mut self, target: PanTarget, pan: f32) {
104 let Some(s) = self.session.as_mut() else {
105 return;
106 };
107 match target {
108 PanTarget::Clip(lane, idx) => s.set_clip_pan(lane, idx, pan),
109 PanTarget::Lane(lane) => s.set_lane_pan(lane, pan),
110 };
111 }
112}