forge
Explore
Log in

Don't you dare go Hollow

🔀 merge(C5): the equalizer grew into its window — ten draggable bands, numbers you can read, bells you can see

Browse files at this commit

989073067ea86b772ea232e97436ad5113f8e643Ahmet Kaan GümüşParent: 2d567f6b8fa325
crates/app/src/keymap.rsmodified+27 -2
@@ -333,7 +333,7 @@ pub struct Fixed {
333333 pub category: Category,
334334 }
335335
336-pub const FIXED: [Fixed; 21] = [
336+pub const FIXED: [Fixed; 26] = [
337337 // Not a chord at all but a way of pressing one, and the only place the
338338 // editor can say so: holding a key that moves a *value* runs it, and
339339 // holding anything else still does what one press did.
@@ -401,7 +401,7 @@ pub const FIXED: [Fixed; 21] = [
401401 // its own: a band nothing but a drag can reach is a band half the users of
402402 // this editor cannot move at all. Card-local, so none of them is bindable.
403403 Fixed {
404- chord: "1–5",
404+ chord: "1–9, 0",
405405 label: "Pick an equalizer band",
406406 category: Category::Audio,
407407 },
@@ -415,6 +415,31 @@ pub const FIXED: [Fixed; 21] = [
415415 label: "Lower the picked band 1 dB",
416416 category: Category::Audio,
417417 },
418+ Fixed {
419+ chord: "← / →",
420+ label: "Move the picked band down or up in frequency",
421+ category: Category::Audio,
422+ },
423+ Fixed {
424+ chord: "shift ← / →",
425+ label: "Widen or narrow the picked band (its Q)",
426+ category: Category::Audio,
427+ },
428+ Fixed {
429+ chord: "a",
430+ label: "Add an equalizer band beside the picked one",
431+ category: Category::Audio,
432+ },
433+ Fixed {
434+ chord: "x",
435+ label: "Remove the picked equalizer band",
436+ category: Category::Audio,
437+ },
438+ Fixed {
439+ chord: "f",
440+ label: "Flatten the picked band (double-click does the same)",
441+ category: Category::Audio,
442+ },
418443 Fixed {
419444 chord: "r",
420445 label: "Flatten every band",
crates/app/src/main.rsmodified+499 -94
@@ -569,6 +569,36 @@ const EQ_FREQ_HIGH: f32 = 20_000.;
569569 /// and short enough that the card still fits a 360 px window.
570570 const EQ_GRAPH_H: f32 = 132.;
571571
572+/// How wide the equalizer card is allowed to get. It is the one card that is a
573+/// *graph*: every pixel across is frequency resolution, and at the 320 px the
574+/// other cards use, a third of an octave was a couple of pixels. Past this the
575+/// curve stops gaining anything and the card starts reading as a wall.
576+const EQ_W_MAX: f32 = 720.;
577+
578+/// The gap the card leaves either side of it, so it reads as a card on a scrim
579+/// rather than as a second window: it takes the width it can get inside that.
580+const EQ_W_MARGIN: f32 = 32.;
581+
582+/// How many bands one clip's equalizer may carry from this card. Ten because
583+/// the keyboard picks a band with a digit and a keyboard has ten of them --
584+/// past that a band would be reachable by pointer only. The engine itself caps
585+/// nothing (`EqParams::bands` is a plain `Vec`), so a file may still carry more
586+/// and this card will draw and edit every one it finds.
587+const EQ_BANDS_MAX: usize = 10;
588+
589+/// One press of the frequency keys, as a factor: a sixth of an octave, so a
590+/// band walks the whole axis in about sixty presses and still lands close
591+/// enough to a named frequency to aim at one.
592+const EQ_FREQ_STEP: f32 = 1.122_462;
593+
594+/// One press of the Q keys, as a factor, and the range they move in. Below the
595+/// bottom a peak is barely a peak any more; above the top it is a whistle on
596+/// one frequency. 0.707 -- the flat-shelf value, and the default -- sits inside
597+/// them, so nothing a file carries has to be dragged into range first.
598+const EQ_Q_STEP: f32 = 1.25;
599+const EQ_Q_LOW: f32 = 0.3;
600+const EQ_Q_HIGH: f32 = 12.;
601+
572602 /// How many points the curve is drawn from. One per ~3 px across the card:
573603 /// past that the line is smooth and the extra biquad evaluations are wasted.
574604 const EQ_CURVE_STEPS: usize = 96;
@@ -617,6 +647,11 @@ const EQ_SPECTRUM_INK: u32 = 0x7f95ad66;
617647 /// its weight: it is what makes a boost read as a hill rather than as a wire.
618648 const EQ_FILL_INK: u32 = 0x4a9eff26;
619649
650+/// One band's own response, drawn under the sum: a dim thread per band, so a
651+/// boost pushed against a cut can be *seen* as two bands rather than as the
652+/// flat line their total makes.
653+const EQ_BELL_INK: u32 = 0x4a9eff66;
654+
620655 /// The colour card's four controls, in the order it lists them: what each is
621656 /// called and the range it moves in. The order is `ColorParams`' own, which is
622657 /// what [`color_band`] indexes.
@@ -2034,41 +2069,78 @@ impl Player {
20342069 cx.notify();
20352070 }
20362071
2037- /// Moves one band and says whether anything changed. Live only: the write
2038- /// is [`commit_eq`](Player::commit_eq)'s.
2039- fn set_band(&mut self, band: usize, gain_db: f32) -> bool {
2040- let gain_db = gain_db.clamp(-EQ_GAIN_LIMIT, EQ_GAIN_LIMIT);
2041- match self.eq_params.bands.get_mut(band) {
2042- Some(b) if b.gain_db != gain_db => {
2043- b.gain_db = gain_db;
2044- true
2045- }
2046- _ => false,
2072+ /// Changes the picked band in place and says whether anything moved. Every
2073+ /// edit of a band goes through here -- the drag, each key, each stepper
2074+ /// button -- so the card has exactly one place that clamps a band into what
2075+ /// the graph can draw, and no caller has to remember the limits.
2076+ fn set_band(&mut self, change: impl FnOnce(&mut Band)) -> bool {
2077+ let Some(band) = self.eq_params.bands.get_mut(self.eq_band) else {
2078+ return false;
2079+ };
2080+ let was = *band;
2081+ change(band);
2082+ band.freq_hz = band.freq_hz.clamp(EQ_FREQ_LOW, EQ_FREQ_HIGH);
2083+ band.gain_db = band.gain_db.clamp(-EQ_GAIN_LIMIT, EQ_GAIN_LIMIT);
2084+ band.q = band.q.clamp(EQ_Q_LOW, EQ_Q_HIGH);
2085+ *band != was
2086+ }
2087+
2088+ /// The keyboard's and the buttons' version of a drag: one step on the picked
2089+ /// band, committed straight away -- neither has a release to wait for.
2090+ fn nudge_band(&mut self, change: impl FnOnce(&mut Band), cx: &mut Context<Self>) {
2091+ if self.set_band(change) {
2092+ self.commit_eq(cx);
20472093 }
20482094 }
20492095
2050- /// The keyboard's version of a drag: one step on the picked band, committed
2051- /// straight away -- a keystroke has no release to wait for.
2052- fn nudge_band(&mut self, by: f32, cx: &mut Context<Self>) {
2053- let gain = self
2054- .eq_params
2055- .bands
2056- .get(self.eq_band)
2057- .map_or(0., |b| b.gain_db);
2058- if self.set_band(self.eq_band, gain + by) {
2059- self.commit_eq(cx);
2096+ /// Where the pointer sits in the graph, as the picked band's frequency and
2097+ /// gain: across is the frequency axis and down is the gain one, so the
2098+ /// handle follows the hand both ways rather than sliding up a rail. Called
2099+ /// on every pointer sample, so the curve bends under it; the write is the
2100+ /// release's ([`commit_eq`](Player::commit_eq)).
2101+ fn drag_band(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) {
2102+ let bounds = self.eq_graph.get();
2103+ let gain = (0.5 - frac_down(at.y, bounds)) * 2. * EQ_GAIN_LIMIT;
2104+ let freq = eq_freq(frac_along(at.x, bounds));
2105+ if self.set_band(|b| {
2106+ b.gain_db = gain;
2107+ b.freq_hz = freq;
2108+ }) {
2109+ cx.notify();
20602110 }
20612111 }
20622112
2063- /// Where the pointer sits down the graph, as a gain on the picked band: the
2064- /// middle is flat and the top and bottom edges are the limit either way.
2065- /// Called on every pointer sample, so the curve bends under the hand; the
2066- /// write is the release's ([`commit_eq`](Player::commit_eq)).
2067- fn drag_band(&mut self, y: Pixels, cx: &mut Context<Self>) {
2068- let gain = (0.5 - frac_down(y, self.eq_graph.get())) * 2. * EQ_GAIN_LIMIT;
2069- if self.set_band(self.eq_band, gain) {
2113+ /// A band added beside the picked one, at the frequency with the most room
2114+ /// around it ([`inserted_band`]), and picked so the next keystroke moves the
2115+ /// band that was just made. Refused rather than silently ignored at the cap.
2116+ fn add_band(&mut self, cx: &mut Context<Self>) {
2117+ if self.eq_params.bands.len() >= EQ_BANDS_MAX {
2118+ self.notice = Some(
2119+ format!(
2120+ "EQUALIZER FULL — {EQ_BANDS_MAX} bands is all this card holds; move one instead"
2121+ )
2122+ .into(),
2123+ );
20702124 cx.notify();
2125+ return;
20712126 }
2127+ let band = inserted_band(&self.eq_params.bands, self.eq_band);
2128+ self.eq_band = (self.eq_band + 1).min(self.eq_params.bands.len());
2129+ self.eq_params.bands.insert(self.eq_band, band);
2130+ self.commit_eq(cx);
2131+ }
2132+
2133+ /// Takes the picked band out. The last one stays: an equalizer of no bands
2134+ /// is a card with nothing to edit, and flattening is what "off" means here.
2135+ fn remove_band(&mut self, cx: &mut Context<Self>) {
2136+ if self.eq_params.bands.len() <= 1 {
2137+ self.notice = Some("LAST BAND — flatten it instead (r), or close the card".into());
2138+ cx.notify();
2139+ return;
2140+ }
2141+ self.eq_params.bands.remove(self.eq_band);
2142+ self.eq_band = self.eq_band.min(self.eq_params.bands.len() - 1);
2143+ self.commit_eq(cx);
20722144 }
20732145
20742146 /// Which band a press on the graph grabs: the nearest one along the
@@ -3778,31 +3850,55 @@ impl Render for Player {
37783850 // cannot move at all, and every one of them is listed in the
37793851 // keys menu (keymap.rs `FIXED`) rather than being a secret.
37803852 if this.eq_open.is_some() {
3853+ // Shift makes the two horizontal keys Q instead of
3854+ // frequency: both are the *width* of the same hump, so they
3855+ // sit on the same axis rather than on two keys nobody would
3856+ // guess. Wider is a lower Q, which is why left widens.
3857+ let shift = event.keystroke.modifiers.shift;
37813858 if key == ESCAPE {
37823859 // Nothing to undo: every change is already at the clip,
37833860 // and undo is undo's own key.
37843861 this.eq_open = None;
37853862 this.eq_dragging = false;
37863863 } else if key == "up" {
3787- this.nudge_band(EQ_STEP, cx);
3864+ this.nudge_band(|b| b.gain_db += EQ_STEP, cx);
37883865 } else if key == "down" {
3789- this.nudge_band(-EQ_STEP, cx);
3866+ this.nudge_band(|b| b.gain_db -= EQ_STEP, cx);
3867+ } else if key == "left" && shift {
3868+ this.nudge_band(|b| b.q /= EQ_Q_STEP, cx);
3869+ } else if key == "right" && shift {
3870+ this.nudge_band(|b| b.q *= EQ_Q_STEP, cx);
3871+ } else if key == "left" {
3872+ this.nudge_band(|b| b.freq_hz /= EQ_FREQ_STEP, cx);
3873+ } else if key == "right" {
3874+ this.nudge_band(|b| b.freq_hz *= EQ_FREQ_STEP, cx);
37903875 } else if key == "r" {
37913876 for band in &mut this.eq_params.bands {
37923877 band.gain_db = 0.;
37933878 }
37943879 this.commit_eq(cx);
3880+ } else if key == "f" {
3881+ // This one band back to flat, which is the undo of one
3882+ // hand movement -- `r` is the undo of the whole card.
3883+ this.nudge_band(|b| b.gain_db = 0., cx);
3884+ } else if key == "a" {
3885+ this.add_band(cx);
3886+ } else if key == "x" {
3887+ this.remove_band(cx);
37953888 } else if key == "s" {
37963889 // The analyser off and on. Nothing is committed: it is
37973890 // what the card *shows*, so it survives no further than
37983891 // this window.
37993892 this.eq_spectrum = !this.eq_spectrum;
38003893 } else if let Ok(digit) = key.parse::<usize>() {
3801- // 1-based, as the rows are numbered on screen; a digit
3802- // past the last band picks nothing rather than panics.
3803- if let Some(band) = digit.checked_sub(1)
3804- && band < this.eq_params.bands.len()
3805- {
3894+ // As the keys are laid out: 1-9 then 0 for the tenth,
3895+ // which is the cap ([`EQ_BANDS_MAX`]). A digit past the
3896+ // last band picks nothing rather than panics.
3897+ let band = match digit {
3898+ 0 => EQ_BANDS_MAX - 1,
3899+ n => n - 1,
3900+ };
3901+ if band < this.eq_params.bands.len() {
38063902 this.eq_band = band;
38073903 }
38083904 }
@@ -3964,7 +4060,7 @@ impl Render for Player {
39644060 // the equalizer drag is tracked here for the ruler's reason.
39654061 if this.eq_dragging {
39664062 if event.pressed_button == Some(MouseButton::Left) {
3967- this.drag_band(event.position.y, cx);
4063+ this.drag_band(event.position, cx);
39684064 } else {
39694065 // Released outside the window: the up below never came,
39704066 // so this is where the gesture ends -- and it still owes
@@ -4034,7 +4130,7 @@ impl Render for Player {
40344130 if std::mem::take(&mut this.eq_dragging) {
40354131 // The release lands exactly, then the gesture is written
40364132 // once -- the append-only table's whole reason.
4037- this.drag_band(event.position.y, cx);
4133+ this.drag_band(event.position, cx);
40384134 this.commit_eq(cx);
40394135 return;
40404136 }
@@ -4136,7 +4232,7 @@ impl Render for Player {
41364232 // column, and only one of the two is ever up.
41374233 .children(self.keys_overlay(cx))
41384234 .children(self.export_card(window.viewport_size(), cx))
4139- .children(self.eq_card(cx))
4235+ .children(self.eq_card(window.viewport_size(), cx))
41404236 .children(self.color_card(cx))
41414237 .children(self.speed_card(cx))
41424238 .children(self.silence_card(cx))
@@ -5595,20 +5691,28 @@ impl Player {
55955691 }
55965692
55975693 /// The equalizer of one audio clip: its frequency response drawn as a
5598- /// curve, a handle per band sitting on it, and a row that flattens them
5599- /// all. The curve is the clip's actual filter (`EqParams::response_db`
5600- /// reads the coefficients the samples go through), and it is redrawn on
5601- /// every pointer sample of a drag, so the shape bends under the hand.
5694+ /// curve, a handle per band sitting on it, each band's own bell under the
5695+ /// sum, and a row that reads and moves the picked band's three numbers. The
5696+ /// curve is the clip's actual filter (`EqParams::response_db` reads the
5697+ /// coefficients the samples go through), and it is redrawn on every pointer
5698+ /// sample of a drag, so the shape bends under the hand.
56025699 ///
5603- /// The same scrim and width as the other two cards, and the same plain divs
5604- /// -- nothing here takes focus, so the root keeps the keyboard and the
5605- /// card's own strokes (`1`–`5`, up, down, `r`) reach it.
5700+ /// Wider than the other cards and wider still on a bigger window
5701+ /// ([`eq_card_w`]): every pixel across is frequency resolution, which none
5702+ /// of the row-shaped cards have any use for. The same scrim and the same
5703+ /// plain divs -- nothing here takes focus, so the root keeps the keyboard
5704+ /// and the card's own strokes (a digit, the arrows, `a`, `x`, `f`, `r`,
5705+ /// `s`) reach it.
56065706 ///
56075707 /// Every change is written at the clip as it is made
56085708 /// ([`Player::commit_eq`]), so what the card shows is always what is
56095709 /// playing: there is no OK button to forget, and closing it changes
56105710 /// nothing. What takes a curve back off is undo, like every other edit.
5611- fn eq_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
5711+ fn eq_card(
5712+ &self,
5713+ viewport: Size<Pixels>,
5714+ cx: &mut Context<Self>,
5715+ ) -> Option<impl IntoElement> {
56125716 let (lane, idx) = self.eq_open?;
56135717 // The rate the engine filters this timeline at, so the drawn curve is
56145718 // the one those coefficients make -- near the top of the axis a 44.1 kHz
@@ -5662,13 +5766,20 @@ impl Player {
56625766 .bg(rgb(HOVER_DIM))
56635767 .cursor_pointer()
56645768 // The press picks the band under it *and* is already the first
5665- // sample of the drag, so a plain click sets a value.
5769+ // sample of the drag, so a plain click sets a value. A second click
5770+ // takes that band back to flat instead: the gesture that undoes one
5771+ // handle, with no modifier to remember.
56665772 .on_mouse_down(
56675773 MouseButton::Left,
56685774 cx.listener(|this, event: &MouseDownEvent, _, cx| {
56695775 this.eq_band = this.nearest_band(event.position.x);
5776+ if event.click_count >= 2 {
5777+ this.eq_dragging = false;
5778+ this.nudge_band(|b| b.gain_db = 0., cx);
5779+ return;
5780+ }
56705781 this.eq_dragging = true;
5671- this.drag_band(event.position.y, cx);
5782+ this.drag_band(event.position, cx);
56725783 }),
56735784 )
56745785 .child(bounds_probe(self.eq_graph.clone()))
@@ -5760,7 +5871,7 @@ impl Player {
57605871 })
57615872 .child(
57625873 div()
5763- .w(px(KEYS_W))
5874+ .w(px(eq_card_w(f32::from(viewport.width))))
57645875 .flex()
57655876 .flex_col()
57665877 .gap(px(2.))
@@ -5781,28 +5892,15 @@ impl Player {
57815892 .text_size(px(11.))
57825893 .text_color(rgb(INK_DIM))
57835894 .child(self.notice.clone().unwrap_or_else(|| {
5784- "drag the curve, or 1–5 then up/down — s hides the spectrum, esc closes".into()
5895+ "drag a handle, or a digit picks a band — ←→ moves it, ↑↓ its gain, shift+←→ its width; a adds, x removes, f flattens it, r all, s spectrum, esc closes".into()
57855896 })),
57865897 )
57875898 .child(graph)
5788- // Which band the keyboard is holding and what it is set
5789- // to: the curve shows the sum, and a band pushed against
5790- // one pulling the other way is not readable off it.
5791- .child(div().flex_none().px(px(6.)).text_size(px(11.)).child(
5792- match picked {
5793- // Q as well as the two the curve shows: it is
5794- // how wide the hump under the handle is, and
5795- // nothing else on the card says so.
5796- Some(band) => format!(
5797- "{} {} {:+.1} dB — Q {:.2}",
5798- self.eq_band + 1,
5799- band_label(band),
5800- band.gain_db,
5801- band.q
5802- ),
5803- None => "no bands".into(),
5804- },
5805- ))
5899+ // Which band the keyboard is holding and every number it
5900+ // is set to, each with the pair of buttons that moves it:
5901+ // the curve shows the sum, and a band pushed against one
5902+ // pulling the other way is not readable off it.
5903+ .child(self.eq_numbers(picked, cx))
58065904 .child(
58075905 div()
58085906 .mt(px(4.))
@@ -5826,7 +5924,45 @@ impl Player {
58265924 }
58275925 this.commit_eq(cx);
58285926 }))
5829- .child("Flatten"),
5927+ .child("Flatten all"),
5928+ )
5929+ // The two that change how many bands there are.
5930+ // The engine takes any cascade -- the count was
5931+ // only ever fixed because this card had no way
5932+ // to say otherwise.
5933+ .child(
5934+ div()
5935+ .id("eq-add")
5936+ .flex()
5937+ .flex_1()
5938+ .h(px(CONTROL_H))
5939+ .items_center()
5940+ .justify_center()
5941+ .rounded(px(3.))
5942+ .bg(rgb(CHROME))
5943+ .cursor_pointer()
5944+ .hover(|s| s.bg(rgb(HOVER)))
5945+ .on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
5946+ this.add_band(cx)
5947+ }))
5948+ .child("Add band"),
5949+ )
5950+ .child(
5951+ div()
5952+ .id("eq-remove")
5953+ .flex()
5954+ .flex_1()
5955+ .h(px(CONTROL_H))
5956+ .items_center()
5957+ .justify_center()
5958+ .rounded(px(3.))
5959+ .bg(rgb(CHROME))
5960+ .cursor_pointer()
5961+ .hover(|s| s.bg(rgb(HOVER)))
5962+ .on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
5963+ this.remove_band(cx)
5964+ }))
5965+ .child("Remove band"),
58305966 )
58315967 // The analyser's switch, next to the one other
58325968 // button the card has: `s` does the same, and a
@@ -5861,6 +5997,103 @@ impl Player {
58615997 )
58625998 }
58635999
6000+ /// The picked band's three numbers -- where it sits, how far it pushes and
6001+ /// how wide it is -- each beside the pair of buttons that moves it. The
6002+ /// arrows do the same three things, but a value only a key can change is a
6003+ /// value a hand on the pointer cannot reach at all, which is the same reason
6004+ /// [`Player::mbps_steppers`] exists.
6005+ fn eq_numbers(&self, picked: Option<&Band>, cx: &mut Context<Self>) -> impl IntoElement {
6006+ let row = div()
6007+ .flex_none()
6008+ .flex()
6009+ .items_center()
6010+ .gap(px(10.))
6011+ .px(px(6.))
6012+ .text_size(px(11.));
6013+ let Some(band) = picked.copied() else {
6014+ return row.child("no bands — a adds one");
6015+ };
6016+ let step = |id: &'static str,
6017+ label: &'static str,
6018+ change: fn(&mut Band),
6019+ cx: &mut Context<Self>| {
6020+ div()
6021+ .id(id)
6022+ .flex()
6023+ .w(px(HIT_MIN))
6024+ .h(px(HIT_MIN))
6025+ .items_center()
6026+ .justify_center()
6027+ .rounded(px(3.))
6028+ .bg(rgb(CHROME))
6029+ .cursor_pointer()
6030+ .hover(|s| s.bg(rgb(HOVER)))
6031+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
6032+ this.nudge_band(change, cx)
6033+ }))
6034+ .child(label)
6035+ };
6036+ let number = |value: String,
6037+ ids: (&'static str, &'static str),
6038+ by: (fn(&mut Band), fn(&mut Band)),
6039+ cx: &mut Context<Self>| {
6040+ div()
6041+ .flex()
6042+ .items_center()
6043+ .gap(px(4.))
6044+ .child(div().child(value))
6045+ .child(step(ids.0, "−", by.0, cx))
6046+ .child(step(ids.1, "+", by.1, cx))
6047+ };
6048+ row.child(format!(
6049+ "Band {} of {}",
6050+ self.eq_band + 1,
6051+ self.eq_params.bands.len()
6052+ ))
6053+ .child(number(
6054+ band_label(&band),
6055+ ("eq-freq-down", "eq-freq-up"),
6056+ (
6057+ |b| b.freq_hz /= EQ_FREQ_STEP,
6058+ |b| b.freq_hz *= EQ_FREQ_STEP,
6059+ ),
6060+ cx,
6061+ ))
6062+ .child(number(
6063+ format!("{:+.1} dB", band.gain_db),
6064+ ("eq-gain-down", "eq-gain-up"),
6065+ (|b| b.gain_db -= EQ_STEP, |b| b.gain_db += EQ_STEP),
6066+ cx,
6067+ ))
6068+ // Q is width, so its buttons are labelled by what they *do* to the
6069+ // hump rather than by which way the number goes: a wider band is a
6070+ // smaller Q, and nobody should have to know that to use the card.
6071+ .child(number(
6072+ format!("Q {:.2}", band.q),
6073+ ("eq-q-wider", "eq-q-narrower"),
6074+ (|b| b.q /= EQ_Q_STEP, |b| b.q *= EQ_Q_STEP),
6075+ cx,
6076+ ))
6077+ .child(
6078+ div()
6079+ .id("eq-flat-band")
6080+ .flex()
6081+ .h(px(HIT_MIN))
6082+ .px(px(8.))
6083+ .items_center()
6084+ .rounded(px(3.))
6085+ .bg(rgb(CHROME))
6086+ .cursor_pointer()
6087+ .hover(|s| s.bg(rgb(HOVER)))
6088+ .on_click(
6089+ cx.listener(|this, _: &ClickEvent, _, cx| {
6090+ this.nudge_band(|b| b.gain_db = 0., cx)
6091+ }),
6092+ )
6093+ .child("Flatten this"),
6094+ )
6095+ }
6096+
58646097 /// The colour card: the graded frame's histogram over a row per control,
58656098 /// each row a bar the pointer drags straight to a value -- no stepper
58666099 /// buttons, because a slider is a thing to pull, and the arrow keys still
@@ -7552,6 +7785,36 @@ fn eq_x(freq_hz: f32) -> f32 {
75527785 ((freq_hz.max(1.) / EQ_FREQ_LOW).log10() / span).clamp(0., 1.)
75537786 }
75547787
7788+/// The frequency at a fraction across the graph -- [`eq_x`] backwards, which is
7789+/// how a drag and the curve's own sample points read one. Clamped to the axis
7790+/// either way, so a pointer that leaves the box stops at 20 Hz or 20 kHz.
7791+fn eq_freq(along: f32) -> f32 {
7792+ EQ_FREQ_LOW * (EQ_FREQ_HIGH / EQ_FREQ_LOW).powf(along.clamp(0., 1.))
7793+}
7794+
7795+/// The band an "add" makes: a flat peak half way -- in octaves, which is what
7796+/// the log axis draws -- between the picked band and whatever sits above it, so
7797+/// a new band lands in the gap on screen rather than on top of its neighbour.
7798+/// Above the topmost band the gap is the rest of the axis.
7799+fn inserted_band(bands: &[Band], after: usize) -> Band {
7800+ let below = bands.get(after).map_or(1000., |b| b.freq_hz);
7801+ let above = bands
7802+ .iter()
7803+ .map(|b| b.freq_hz)
7804+ .filter(|f| *f > below)
7805+ .min_by(f32::total_cmp)
7806+ .unwrap_or(EQ_FREQ_HIGH);
7807+ Band {
7808+ freq_hz: (below * above).sqrt().clamp(EQ_FREQ_LOW, EQ_FREQ_HIGH),
7809+ gain_db: 0.,
7810+ // A shade narrower than the flat-shelf 0.707 the defaults use: a band
7811+ // someone asked for is a band they mean to aim, and a wide one aimed at
7812+ // 300 Hz is really a band at everything.
7813+ q: 1.,
7814+ kind: BandKind::Peak,
7815+ }
7816+}
7817+
75557818 /// Where a gain sits *down* the graph, 0..1 from the top: flat is the middle,
75567819 /// so a cut reads as a dip below the line it is a cut from. The inverse of
75577820 /// [`Player::drag_band`]'s reading of the pointer, and clamped like it, so a
@@ -7561,14 +7824,24 @@ fn eq_y(gain_db: f32) -> f32 {
75617824 0.5 - (gain_db / EQ_GAIN_LIMIT).clamp(-1., 1.) / 2.
75627825 }
75637826
7827+/// A frequency as the card writes it. Two decimals of a kHz at most, with the
7828+/// zeroes trimmed, so "1 kHz" stays "1 kHz" and a band nudged off it reads as
7829+/// 1.12 kHz rather than as the same "1 kHz" it was before the keystroke -- a
7830+/// number that does not move under an edit is worse than no number.
7831+fn eq_freq_label(freq_hz: f32) -> String {
7832+ if freq_hz < 1000. {
7833+ return format!("{freq_hz:.0} Hz");
7834+ }
7835+ let khz = format!("{:.2}", freq_hz / 1000.);
7836+ let khz = khz.trim_end_matches('0').trim_end_matches('.');
7837+ format!("{khz} kHz")
7838+}
7839+
75647840 /// What a band row calls itself: the corner or centre frequency, and for a
75657841 /// shelf the fact that it tilts everything past it -- which is the difference
75667842 /// between "12 kHz" moving the last octave and moving one band inside it.
75677843 fn band_label(band: &Band) -> String {
7568- let freq = match band.freq_hz >= 1000. {
7569- true => format!("{:.0} kHz", band.freq_hz / 1000.),
7570- false => format!("{:.0} Hz", band.freq_hz),
7571- };
7844+ let freq = eq_freq_label(band.freq_hz);
75727845 match band.kind {
75737846 BandKind::LowShelf => format!("{freq} low shelf"),
75747847 BandKind::HighShelf => format!("{freq} high shelf"),
@@ -7923,6 +8196,15 @@ fn unusable(info: &StreamInfo, timeline_audio: Option<(u32, u16)>) -> Option<Str
79238196 })
79248197 }
79258198
8199+/// How wide the equalizer card is drawn in a window this wide: all of it bar a
8200+/// margin, up to [`EQ_W_MAX`]. The card is a graph, and a graph of twenty
8201+/// thousand hertz on 320 px spends four pixels on the octave below middle C --
8202+/// so it takes the room a big window has and stays inside a small one. Floored
8203+/// at the other cards' width, which is the last size the rows still read at.
8204+fn eq_card_w(window_w: f32) -> f32 {
8205+ (window_w - EQ_W_MARGIN).clamp(KEYS_W, EQ_W_MAX)
8206+}
8207+
79268208 /// What the media list is given of the window. A share of it, so a narrow
79278209 /// window gives the panel less rather than giving the picture nothing, floored
79288210 /// where a name stops being readable and capped at a third of the window --
@@ -9320,11 +9602,15 @@ fn eq_spectrum_curve(levels: Vec<f32>) -> impl IntoElement {
93209602 .size_full()
93219603 }
93229604
9323-/// The cascade's frequency response, drawn as one line across the graph.
9605+/// The cascade's frequency response, drawn as one line across the graph, with
9606+/// each band's own response threaded dimly under it.
93249607 ///
93259608 /// Every point comes from `EqParams::response_db`, which reads the very
93269609 /// coefficients the samples are filtered through: the curve cannot drift from
9327-/// what is heard, because there is no second copy of the maths.
9610+/// what is heard, because there is no second copy of the maths. A single band's
9611+/// thread is that same call on a cascade of one, so the two cannot disagree
9612+/// either -- and it is what makes a boost sitting inside a cut visible at all,
9613+/// where the sum alone would draw a flat line and say nothing.
93289614 ///
93299615 /// ponytail: bands that overlap can sum past the ±`EQ_GAIN_LIMIT` axis and the
93309616 /// curve then rides the edge of the box; upgrade = a wider dB axis with the
@@ -9334,16 +9620,37 @@ fn eq_curve(params: EqParams, sample_rate: u32) -> impl IntoElement {
93349620 |_, _, _| (),
93359621 move |bounds, _, window, _| {
93369622 let (o, s) = (bounds.origin, bounds.size);
9337- let points: Vec<_> = (0..=EQ_CURVE_STEPS)
9338- .map(|step| {
9339- let along = step as f32 / EQ_CURVE_STEPS as f32;
9340- let freq = EQ_FREQ_LOW * (EQ_FREQ_HIGH / EQ_FREQ_LOW).powf(along);
9341- point(
9342- o.x + s.width * along,
9343- o.y + s.height * eq_y(params.response_db(freq, sample_rate)),
9344- )
9623+ let line = |of: &EqParams| -> Vec<_> {
9624+ (0..=EQ_CURVE_STEPS)
9625+ .map(|step| {
9626+ let along = step as f32 / EQ_CURVE_STEPS as f32;
9627+ point(
9628+ o.x + s.width * along,
9629+ o.y + s.height
9630+ * eq_y(of.response_db(eq_freq(along), sample_rate)),
9631+ )
9632+ })
9633+ .collect()
9634+ };
9635+ // One thread per band, first, so the sum is drawn over them.
9636+ for band in &params.bands {
9637+ let mut bell = PathBuilder::stroke(px(1.));
9638+ for (step, at) in line(&EqParams {
9639+ bands: vec![*band],
93459640 })
9346- .collect();
9641+ .into_iter()
9642+ .enumerate()
9643+ {
9644+ match step {
9645+ 0 => bell.move_to(at),
9646+ _ => bell.line_to(at),
9647+ }
9648+ }
9649+ if let Ok(bell) = bell.build() {
9650+ window.paint_path(bell, rgba(EQ_BELL_INK));
9651+ }
9652+ }
9653+ let points = line(&params);
93479654 // The area between the curve and 0 dB, closed along that line: a
93489655 // boost and a cut wind opposite ways around it, which is exactly
93499656 // what makes both of them fill and the flat parts stay empty.
@@ -9445,9 +9752,10 @@ fn timecode(t: f64, fps: f64) -> String {
94459752 #[cfg(test)]
94469753 mod tests {
94479754 use super::{
9448- ACCENT, COLOR_BANDS, COLOR_BAR_W, COLOR_STEP, COLOR_W, CONTROL_H, Clip, Ctx,
9449- EQ_CURVE_STEPS, EQ_FFT, EQ_FREQ_HIGH, EQ_FREQ_LOW, EQ_GAIN_LIMIT, EQ_GRAPH_H, EQ_HANDLE,
9450- EQ_SPECTRUM_DB, EQ_TICKS, ESCAPE, EXPORT_FIXED_H, EXPORT_ROWS_H, EXPORT_W, Enable, FORMATS,
9755+ ACCENT, COLOR_BANDS, COLOR_BAR_W, COLOR_STEP, COLOR_W, CONTROL_H, Clip, Ctx, EQ_BANDS_MAX,
9756+ EQ_CURVE_STEPS, EQ_FFT, EQ_FREQ_HIGH, EQ_FREQ_LOW, EQ_FREQ_STEP, EQ_GAIN_LIMIT, EQ_GRAPH_H,
9757+ EQ_HANDLE, EQ_Q_HIGH, EQ_Q_LOW, EQ_Q_STEP, EQ_SPECTRUM_DB, EQ_TICKS, EQ_W_MAX, ESCAPE,
9758+ EXPORT_FIXED_H, EXPORT_ROWS_H, EXPORT_W, Enable, FORMATS,
94519759 Format, HEADER_GAP, HEADER_H, HEADER_W, HIST_BINS, HIST_H, HIST_SAMPLES, HIT_MIN, INK,
94529760 INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, KeyRow, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,
94539761 LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H,
@@ -9455,9 +9763,11 @@ mod tests {
94559763 SOURCE_TINTS, SPEED_PRESETS, SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo,
94569764 Transport, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, Wave, band_label, bitrate_refusal,
94579765 can_add, cancels_export, clipboard_after_remove, color_snap, containers, enable, envelope,
9458- eq_spectrum, eq_x, eq_y, estimated_mb, export_path, export_settings, format_key,
9766+ eq_card_w, eq_freq, eq_freq_label, eq_spectrum, eq_x, eq_y, estimated_mb, export_path,
9767+ export_settings, format_key,
94599768 format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram,
9460- is_bare_modifier, is_project, keymap, keys_rows, lanes_h, marked, menu_at, next_container,
9769+ inserted_band, is_bare_modifier, is_project, keymap, keys_rows, lanes_h, marked, menu_at,
9770+ next_container,
94619771 normalise, nothing_to_play, panel_h, project_path, push_digit, retarget, scrub_due,
94629772 show_label, silence_rate, snap_cue, snap_marks, snapped, source_tint, span_partner,
94639773 speed_at, summary_head, summary_tail, timecode, transport, unseen_paths, unseen_sources,
@@ -11286,6 +11596,9 @@ mod tests {
1128611596 "12 kHz high shelf"
1128711597 ]
1128811598 );
11599+ // A band moved off a round number reads as where it *is*: a keystroke
11600+ // that changes the filter and not the number on the card is a keystroke
11601+ // nobody can aim.
1128911602 assert_eq!(
1129011603 band_label(&Band {
1129111604 freq_hz: 2600.,
@@ -11293,23 +11606,38 @@ mod tests {
1129311606 q: 1.,
1129411607 kind: BandKind::Peak
1129511608 }),
11296- "3 kHz",
11297- "rounded, because a band label is not a measurement"
11609+ "2.6 kHz"
11610+ );
11611+ assert_eq!(eq_freq_label(1122.), "1.12 kHz");
11612+ assert_eq!(eq_freq_label(12000.), "12 kHz", "no zeroes to read past");
11613+ assert_eq!(eq_freq_label(80.), "80 Hz");
11614+
11615+ // The card fits the smallest window and takes the room a bigger one has
11616+ // -- it is a graph, and the width *is* the frequency resolution.
11617+ assert!(eq_card_w(640.) <= 640. - 24., "card too wide for 640");
11618+ assert!(eq_card_w(1280.) > eq_card_w(640.), "card ignores the window");
11619+ assert_eq!(eq_card_w(1920.), EQ_W_MAX, "card grows without end");
11620+ assert!(eq_card_w(320.) >= KEYS_W, "card narrower than a row of text");
11621+ // At the smallest window the graph is still a graph: three across for
11622+ // one down, so an octave is wide enough to put a handle in.
11623+ assert!(
11624+ eq_card_w(640.) - 24. >= 3. * EQ_GRAPH_H,
11625+ "graph too square to aim at"
1129811626 );
1129911627
1130011628 // The same shape as the other two cards, so it fits where they do: the
1130111629 // graph stands where the export card's rows do, and is shorter than
11302- // they are.
11303- let (title, status, readout, gaps, padding) = (17., 28., 17., 4. * 2., 24.);
11630+ // they are. The numbers row is a row of buttons now, so it is one of
11631+ // those tall rather than one line of text.
11632+ let (title, status, gaps, padding) = (17., 28., 4. * 2., 24.);
1130411633 assert!(
11305- title + status + EQ_GRAPH_H + readout + gaps + padding + CONTROL_H <= 360.,
11634+ title + status + EQ_GRAPH_H + HIT_MIN + gaps + padding + CONTROL_H <= 360.,
1130611635 "card too tall"
1130711636 );
1130811637 assert!(
1130911638 EQ_GRAPH_H <= EXPORT_ROWS_H,
1131011639 "graph taller than a card of rows"
1131111640 );
11312- assert!(KEYS_W <= 640., "card too wide");
1131311641 // What is dragged is the whole graph -- the handle is a 10 px dot, but
1131411642 // a press anywhere in the box takes the band nearest it -- so WCAG
1131511643 // 2.5.8 is satisfied by the box, which is far past the minimum.
@@ -11321,6 +11649,83 @@ mod tests {
1132111649 assert!(KEYS_ROW_H >= HIT_MIN);
1132211650 }
1132311651
11652+ /// Editing a band, which is what the card is *for*: the pointer reads a
11653+ /// frequency off the axis the same way the axis draws one, a new band lands
11654+ /// in the gap beside the picked one rather than on top of it, and every band
11655+ /// the card will hold has a digit that picks it.
11656+ #[test]
11657+ fn a_band_can_be_moved_added_and_reached() {
11658+ use engine::eq::{Band, BandKind, EqParams};
11659+ // Across the graph and back: a drag sets the frequency the handle is
11660+ // then drawn at, so the two mappings have to be one another's inverse or
11661+ // the handle walks away from the pointer.
11662+ for freq in [20., 80., 250., 1000., 4000., 12000., 20000.] {
11663+ let read = eq_freq(eq_x(freq));
11664+ assert!(
11665+ (read / freq - 1.).abs() < 1e-3,
11666+ "{freq} Hz read back as {read}"
11667+ );
11668+ }
11669+ // Off the box either end stops at the axis, never past it.
11670+ assert_eq!(eq_freq(-1.), EQ_FREQ_LOW);
11671+ assert_eq!(eq_freq(2.), EQ_FREQ_HIGH);
11672+
11673+ // A step of the frequency keys is a real move on screen -- a keystroke
11674+ // that changes nothing visible is a keystroke that reads as broken --
11675+ // and small enough to aim with.
11676+ let step = eq_x(1000. * EQ_FREQ_STEP) - eq_x(1000.);
11677+ assert!(step > 0.01 && step < 0.06, "frequency key steps {step}");
11678+
11679+ // A new band lands between the picked one and the next one up, in
11680+ // octaves: 250 Hz and 1 kHz put it at 500, which is a gap on screen.
11681+ let bands = EqParams::default_layout().bands;
11682+ let added = inserted_band(&bands, 1);
11683+ assert!((added.freq_hz - 500.).abs() < 1., "landed at {added:?}");
11684+ assert_eq!(added.gain_db, 0., "a new band changes nothing until moved");
11685+ assert_eq!(added.kind, BandKind::Peak, "a new band is not a shelf");
11686+ assert!(eq_x(added.freq_hz) - eq_x(bands[1].freq_hz) > 0.1);
11687+ // Above the topmost band the gap is the rest of the axis, and the band
11688+ // still lands on it rather than off the end.
11689+ let top = inserted_band(&bands, bands.len() - 1);
11690+ assert!(
11691+ top.freq_hz > bands[4].freq_hz && top.freq_hz <= EQ_FREQ_HIGH,
11692+ "landed at {top:?}"
11693+ );
11694+ // Bands out of frequency order (a drag may cross two over) still get a
11695+ // sane neighbour: the next one *up*, not the next one along the list.
11696+ let crossed = vec![
11697+ Band {
11698+ freq_hz: 4000.,
11699+ gain_db: 0.,
11700+ q: 1.,
11701+ kind: BandKind::Peak,
11702+ },
11703+ Band {
11704+ freq_hz: 100.,
11705+ gain_db: 0.,
11706+ q: 1.,
11707+ kind: BandKind::Peak,
11708+ },
11709+ ];
11710+ let between = inserted_band(&crossed, 1);
11711+ assert!(
11712+ (between.freq_hz - 632.).abs() < 2.,
11713+ "landed at {between:?}, not between 100 and 4000"
11714+ );
11715+
11716+ // Every band the card will hold is one digit away: the keyboard has ten
11717+ // digits, which is exactly why the cap is what it is.
11718+ assert!(
11719+ EQ_BANDS_MAX <= 10,
11720+ "a band past the tenth has no key that picks it"
11721+ );
11722+ assert!(EQ_BANDS_MAX > EqParams::default_layout().bands.len());
11723+ // The Q range holds the default, so a file's band never opens out of
11724+ // range and needs dragging back in before it can be edited.
11725+ assert!((EQ_Q_LOW..=EQ_Q_HIGH).contains(&0.707));
11726+ assert!(EQ_Q_STEP > 1.);
11727+ }
11728+
1132411729 /// The analyser drawn behind the curve: a tone has to land on its own
1132511730 /// frequency, or the backdrop is a decoration rather than a reading of what
1132611731 /// is playing -- and someone shaping a band against it would be aiming at