From 946c68443c6ea77891ef13ec146ccd4354ca1e38 Mon Sep 17 00:00:00 2001 From: Chris Laupama <42254688+chrislaupama@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:08:21 +0000 Subject: [PATCH 1/3] Add expanded Distortion, Tape Saturation, Soft Clipper and Bit Crusher windows --- crates/dstudio-app/src/device_window.rs | 135 ++++++- crates/dstudio-app/src/device_window_wave.rs | 352 +++++++++++++++++++ crates/dstudio-app/src/main.rs | 1 + 3 files changed, 487 insertions(+), 1 deletion(-) create mode 100644 crates/dstudio-app/src/device_window_wave.rs diff --git a/crates/dstudio-app/src/device_window.rs b/crates/dstudio-app/src/device_window.rs index f60c4126..8743af41 100644 --- a/crates/dstudio-app/src/device_window.rs +++ b/crates/dstudio-app/src/device_window.rs @@ -105,6 +105,8 @@ pub(crate) const MENU_TARGET: i32 = -5; pub(crate) const MENU_MB_BAND: i32 = -6; /// The Multiband Compressor's bands, as its band picker names them. const MB_BANDS: [&str; 3] = ["Low", "Mid", "High"]; +/// Width of the waveshaper and Bit Crusher windows. +const WAVE_W: f32 = 960.0; /// Panel-menu code of the Delay's repeat-EQ band picker. pub(crate) const MENU_DELAY_BAND: i32 = -7; @@ -120,7 +122,9 @@ pub(crate) fn has_window(def: &DeviceDef) -> bool { matches!( def.id, "auto-filter" | "channeleq" | "character-eq" | "transient-shaper" | "multiband-compressor" | "delay" | "reverb" | "echo" - ) || is_field_window(def) + ) || is_wave_window(def) + || def.id == "bitcrusher" + || is_field_window(def) || is_dynamics_window(def) } @@ -129,6 +133,11 @@ fn is_dynamics_window(def: &DeviceDef) -> bool { crate::device_window_dynamics::spec(def.id).is_some() } +/// Windows of the waveshaper family (src/device_window_wave.rs). +fn is_wave_window(def: &DeviceDef) -> bool { + matches!(def.id, "distortion" | "tapesaturation" | "soft-clipper") +} + /// Windows of the plotted-field family (motion and response pictures). fn is_field_window(def: &DeviceDef) -> bool { matches!(def.id, "tremolo" | "autopanner" | "chorus" | "flanger" | "phaser" | "ring-mod" | "character-mod") @@ -169,6 +178,10 @@ pub(crate) fn kit(def: &DeviceDef) -> Kit { "transient-shaper" => field_kit(880.0, 228.0, 8.0, 8.0), // Multiband: the band field over the dB ladder, its Hz axis and legend below. "multiband-compressor" => field_kit(WIN_W, 336.0, 52.0, 43.0), + // Waveshapers: the transfer square and the wave over a dB axis and legend. + "distortion" | "tapesaturation" | "soft-clipper" => field_kit(WAVE_W, 454.0, 60.0, 44.0), + // Bit Crusher: one crushed cycle fills the display. + "bitcrusher" => field_kit(WAVE_W, 454.0, 8.0, 8.0), "tremolo" => field_kit(WIN_W, 234.0, 60.0, 44.0), "autopanner" => field_kit(WIN_W, 234.0, 40.0, 44.0), "chorus" | "flanger" => field_kit(WIN_W, 354.0, 56.0, 44.0), @@ -621,6 +634,8 @@ impl Controller { _ if is_dynamics_window(def) => self.dynamics_window_canvas(d, def, &k, accent), _ if def.id == "transient-shaper" => self.shaper_window_canvas(d, def, &k, accent), _ if def.id == "multiband-compressor" => self.multiband_window_canvas(d, def, &k, accent), + _ if is_wave_window(def) => self.wave_window_canvas(d, def, &k, accent), + _ if def.id == "bitcrusher" => self.crush_window_canvas(d, def, &k, accent), Some(View::Custom(_)) if crate::panel_fields::field_name(def).is_some() => self.field_window_display(d, def, &k), _ => PanelCanvas::default(), }; @@ -636,6 +651,7 @@ impl Controller { "delay" => self.delay_window(d, def, &mut b, dy + dh + 12.0), "reverb" => self.reverb_window(d, def, &mut b, dy + dh + 12.0), "echo" => self.echo_window(d, def, &mut b, dy + dh + 12.0), + _ if is_wave_window(def) || def.id == "bitcrusher" => self.wave_window(d, def, &mut b, dy + dh + 12.0), _ => self.field_window(d, def, &mut b, dy + dh + 12.0), }; let (meters, ticks, ticks_x) = self.window_meters(d.id, &k); @@ -1474,6 +1490,62 @@ impl Controller { rack.y + rack.h + 16.0 } + /// Waveshapers and the Bit Crusher: one card under the display. Distortion: Drive and + /// Bias, the Mode pills, Tone, then Gain, Mix and Output. Tape Saturation: Drive and + /// Compression, Mode, Warmth and Noise, Mix and Output. Soft Clipper: Input and Ceiling, + /// Shape and Softness, Gain Match, Mix and Output. Bit Crusher (its card under the + /// display only, the meters running beside it): Bit Depth, Dither, Downsample and + /// Jitter, Mix. + fn wave_window(&self, d: &Device, def: &'static DeviceDef, b: &mut Builder, y: f32) -> f32 { + let pills = |id: &str| -> Vec<(&'static str, f32)> { + def.param(id).map_or_else(Vec::new, |p| p.choices.iter().enumerate().map(|(i, c)| (*c, i as f32)).collect()) + }; + let knob = |id, name, size, w| Item::Knob(id, name, size, w, false); + let row = Row::main(y); + let clusters = match def.id { + "distortion" => vec![ + (vec![knob("drive", "Drive", 2, 70.0), knob("bias", "Bias", 1, 52.0)], 8.0), + (vec![Item::Pills("mode", pills("mode"), "Mode", 268.0)], 8.0), + (vec![knob("tone", "Tone", 1, 52.0)], 8.0), + (vec![knob("output", "Gain", 1, 62.0), knob("mix", "Mix", 1, 52.0), knob("output_gain", "Output", 1, 62.0)], 8.0), + ], + "tapesaturation" => vec![ + (vec![knob("drive", "Drive", 2, 70.0), knob("compression", "Compression", 1, 76.0)], 8.0), + (vec![Item::Pills("mode", pills("mode"), "Mode", 224.0)], 8.0), + (vec![knob("warmth", "Warmth", 1, 76.0), knob("noise", "Noise", 1, 76.0)], 8.0), + (vec![knob("mix", "Mix", 1, 76.0), knob("output_gain", "Output", 1, 76.0)], 8.0), + ], + "soft-clipper" => vec![ + (vec![knob("input", "Input", 2, 64.0), knob("ceiling", "Ceiling", 2, 64.0)], 12.0), + (vec![Item::Pills("shape", pills("shape"), "Shape", 184.0), knob("softness", "Softness", 1, 62.0)], 24.0), + ( + vec![ + Item::ToggleW("gain_match", "Gain Match", 72.0), + knob("mix", "Mix", 1, 62.0), + knob("output_gain", "Output", 1, 62.0), + ], + 14.0, + ), + ], + _ => vec![ + (vec![knob("bitdepth", "Bit Depth", 2, 70.0)], 8.0), + ( + vec![knob("dither", "Dither", 1, 76.0), knob("downsample", "Downsample", 1, 76.0), knob("jitter", "Jitter", 1, 76.0)], + 14.0, + ), + (vec![knob("mix", "Mix", 1, 76.0)], 8.0), + ], + }; + // The Bit Crusher's card ends under the display, its meters running down beside it. + let full = b.w; + if def.id == "bitcrusher" { + b.w = kit(def).display.2 + 2.0 * INSET; + } + self.win_card(d, def, b, row, clusters); + b.w = full; + row.y + row.h + 16.0 + } + /// Transient Shaper: Hold view in the header; one card with Attack and Sustain, Sense, /// the shape picture (its handles set the two times), then Mix, Output and Clip. fn shaper_window(&self, d: &Device, def: &'static DeviceDef, b: &mut Builder, y: f32) -> f32 { @@ -2917,6 +2989,63 @@ mod tests { assert!(!view(&c, did, def).pills.iter().any(|p| p.kind == PILL_MOMENTARY && p.active)); } + /// The waveshaper windows read their transfer and every card control; the caption + /// follows the output gain. + #[test] + fn wave_windows_show_the_transfer_and_every_control() { + use slint::Model; + for (id, knobs, pills) in [ + ("distortion", &["drive", "bias", "tone", "output", "mix", "output_gain"][..], "mode"), + ("tapesaturation", &["drive", "compression", "warmth", "noise", "mix", "output_gain"][..], "mode"), + ("soft-clipper", &["input", "ceiling", "softness", "mix", "output_gain"][..], "shape"), + ] { + let (mut c, did, def) = controller_with(id); + assert!(has_window(def), "{id}"); + let v = view(&c, did, def); + assert!((v.w / Z - WAVE_W).abs() < 0.5 && (v.h / Z - 640.0).abs() < 0.5, "{id} {} x {}", v.w / Z, v.h / Z); + let idx = |p: &str| def.index_of(p).unwrap() as i32; + for k in knobs { + assert!(v.knobs.iter().any(|n| n.param == idx(k)), "{id} {k}"); + } + let n = def.param(pills).unwrap().choices.len(); + assert_eq!(v.pills.iter().filter(|p| p.param == idx(pills)).count(), n, "{id}"); + assert!(v.display.texts.iter().any(|t| t.text == "Wave"), "{id}"); + let db = |c: &Controller| { + let d = c.project().device(did).unwrap().clone(); + let (caption, value) = c.wave_caption(&d, def); + (caption, value.trim_end_matches(" dB").replace('\u{2212}', "-").parse::().unwrap()) + }; + let (caption, before) = db(&c); + assert_eq!(caption, if id == "soft-clipper" { "Clipping" } else { "Transfer" }, "{id}"); + c.execute(Command::SetDeviceParam { device: did, param: "output_gain".into(), value: -12.0 }); + let after = db(&c).1; + if id != "soft-clipper" { + assert!((after - before + 12.0).abs() < 0.2, "{id} {before} -> {after}"); + } + } + } + + /// The Bit Crusher's window: the crushed cycle under its rate caption, the card under + /// the display only, every control on it. + #[test] + fn bitcrusher_window_reads_rate_and_levels() { + use slint::Model; + let (mut c, did, def) = controller_with("bitcrusher"); + assert!(has_window(def)); + c.execute(Command::SetDeviceParam { device: did, param: "downsample".into(), value: 2.0 }); + c.execute(Command::SetDeviceParam { device: did, param: "bitdepth".into(), value: 8.0 }); + let v = view(&c, did, def); + assert!(v.display.texts.iter().any(|t| t.text == "24.0 kHz")); + assert!(v.display.texts.iter().any(|t| t.text == "and 256 levels")); + let idx = |p: &str| def.index_of(p).unwrap() as i32; + for k in ["bitdepth", "dither", "downsample", "jitter", "mix"] { + assert!(v.knobs.iter().any(|n| n.param == idx(k)), "{k}"); + } + let card = v.boxes.iter().find(|b| b.kind == BOX_CARD).unwrap(); + let (dx, _, dw, _) = kit(def).display; + assert!(((card.x + card.w) / Z - (dx + dw)).abs() < 0.5, "{}", (card.x + card.w) / Z); + } + /// A full ten-second history draws as one line per series, however many samples it holds. #[test] fn dynamics_history_draws_once_per_series() { @@ -2948,6 +3077,10 @@ mod tests { "delay", "reverb", "echo", + "distortion", + "tapesaturation", + "soft-clipper", + "bitcrusher", ] { let (mut c, did, def) = controller_with(id); let h: std::collections::VecDeque<_> = diff --git a/crates/dstudio-app/src/device_window_wave.rs b/crates/dstudio-app/src/device_window_wave.rs new file mode 100644 index 00000000..56d58dfd --- /dev/null +++ b/crates/dstudio-app/src/device_window_wave.rs @@ -0,0 +1,352 @@ +//! Expanded window display of the waveshapers (Distortion, Tape Saturation, Soft Clipper): +//! the static transfer on a 48 dB square (input level against output level, both peak +//! dBFS of a sine), with the band between it and unity lit, and beside it two cycles of a +//! full-scale sine (dashed) and what the device makes of it. A vertical drag on the wave +//! moves the device's first parameter (Drive, or the Soft Clipper's Input), as on the card. +//! The Bit Crusher's display is one cycle of a sine through its sample-and-hold and +//! quantiser, under a caption of the held rate and the number of levels. + +use crate::PanelCanvas; +use crate::controller::Controller; +use crate::device_canvas::{dashed, fill, rgba, stroke, text, white}; +use crate::device_panel::Z; +use crate::device_window::Kit; +use crate::{PanelPath, PanelText, PanelZone}; +use dstudio_core::Device; +use dstudio_core::builtin::{self, DeviceDef}; +use slint::{Color, ModelRc, VecModel}; +use std::f32::consts::TAU; +use std::rc::Rc; + +fn model(v: Vec) -> ModelRc { + ModelRc::from(Rc::new(VecModel::from(v))) +} + +/// The square's range: input and output peak level (dBFS). +const DB_LO: f32 = -36.0; +const DB_HI: f32 = 12.0; +const TICKS: [f32; 4] = [-24.0, -12.0, 0.0, 12.0]; +/// Gap between the square and the wave panel (pt). +const GAP: f32 = 40.0; +const FILL: f32 = 0.30; + +/// A device's sample law and the open (unclipped) path it is measured against. +pub(crate) struct Law { + pub curve: Box f32>, + pub open: Box f32>, + /// The Soft Clipper's caption reads its clipping; the others their gain. + pub clipper: bool, +} + +pub(crate) fn law(def: &DeviceDef, v: &dyn Fn(&str) -> f32) -> Law { + match def.id { + "tapesaturation" => { + let (dr, cp, mx, out, mode) = (v("drive"), v("compression"), v("mix"), v("output_gain"), v("mode").round() as u32); + Law { curve: Box::new(move |x| builtin::tape_transfer(x, dr, cp, mx, out, mode)), open: Box::new(|x| x), clipper: false } + } + "soft-clipper" => { + let (inp, ceil, soft, shape, gm, mx, out) = + (v("input"), v("ceiling"), v("softness"), v("shape").round() as u32, v("gain_match") >= 0.5, v("mix"), v("output_gain")); + Law { + curve: Box::new(move |x| builtin::clipper_transfer(x, inp, ceil, soft, shape, gm, mx, out)), + open: Box::new(move |x| builtin::clipper_open(x, inp, gm, mx, out)), + clipper: true, + } + } + _ => { + let (dr, bias, gain, mx, out, mode) = + (v("drive"), v("bias"), v("output"), v("mix"), v("output_gain"), v("mode").round() as u32); + Law { + curve: Box::new(move |x| builtin::distortion_transfer(x, dr, bias, gain, mx, out, mode)), + open: Box::new(|x| x), + clipper: false, + } + } + } +} + +/// Peak (dBFS) of f over one cycle of a sine at `level` dBFS. +pub(crate) fn peak_db(f: &dyn Fn(f32) -> f32, level: f32) -> f32 { + let a = 10f32.powf(level / 20.0); + let n = 96; + let peak = (0..n).map(|i| f(a * (TAU * (i as f32 + 0.25) / n as f32).sin()).abs()).fold(0.0f32, f32::max); + 20.0 * peak.max(1e-6).log10() +} + +/// "−12.0 dB". +fn db_text(db: f32) -> String { + let a = (db.abs() * 10.0).round() / 10.0; + if a < 0.05 { + "0.0 dB".into() + } else if db < 0.0 { + format!("\u{2212}{a:.1} dB") + } else { + format!("+{a:.1} dB") + } +} + +fn tick_label(db: f32) -> String { + if db > 0.0 { + format!("+{db:.0}") + } else if db < 0.0 { + format!("\u{2212}{:.0}", -db) + } else { + "0".into() + } +} + +impl Controller { + /// The waveshaper's caption: ("Transfer", its gain at 0 dBFS) or ("Clipping", how far + /// the clipper holds a full-scale sine under the open path). + pub(crate) fn wave_caption(&self, d: &Device, def: &'static DeviceDef) -> (&'static str, String) { + let v = |id: &str| def.param(id).map_or(0.0, |p| self.panel_value(d, def, p)); + let l = law(def, &v); + if l.clipper { + ("Clipping", db_text(peak_db(&*l.curve, 0.0) - peak_db(&*l.open, 0.0))) + } else { + ("Transfer", db_text(peak_db(&*l.curve, 0.0))) + } + } + + pub(crate) fn wave_window_canvas(&self, d: &Device, def: &'static DeviceDef, k: &Kit, accent: Color) -> PanelCanvas { + let v = |id: &str| def.param(id).map_or(0.0, |p| self.panel_value(d, def, p)); + let l = law(def, &v); + let (_, _, dw, dh) = k.display; + let (pl, pt, pr, pb) = k.plot; + let (pw, ph) = ((dw - pl - pr) * Z, (dh - pt - pb) * Z); + let side = ph; + let per_db = side / (DB_HI - DB_LO); + let x_of = |db: f32| (db.clamp(DB_LO, DB_HI) - DB_LO) * per_db; + let y_of = |db: f32| (DB_HI - db.clamp(DB_LO, DB_HI)) * per_db; + let mut paths: Vec = Vec::new(); + let mut texts: Vec = Vec::new(); + let dim = white(0.3); + + // The square: grid every 6 dB, the 0 dB lines a step brighter, the ladder and axis. + let mut grid = String::new(); + let mut zero = String::new(); + let mut db = DB_LO + 6.0; + while db < DB_HI - 0.1 { + let (x, y) = (x_of(db).round() + 0.5, y_of(db).round() + 0.5); + let s = if db == 0.0 { &mut zero } else { &mut grid }; + s.push_str(&format!("M{x:.1} 0 V{side:.1} M0 {y:.1} H{side:.1} ")); + db += 6.0; + } + paths.push(stroke(grid, white(0.05), 1.0)); + paths.push(stroke(zero, white(0.14), 1.0)); + for db in [DB_HI, 0.0, -12.0, -24.0, DB_LO] { + texts.push(text(-48.0 * Z, y_of(db), 40.0 * Z, tick_label(db), dim, 11.0 * Z, 2)); + } + let axis_y = side + 12.0 * Z; + for db in TICKS.iter().take(3) { + texts.push(text(x_of(*db) - 20.0 * Z, axis_y, 40.0 * Z, tick_label(*db), dim, 10.0 * Z, 1)); + } + texts.push(text(-48.0 * Z, axis_y, 44.0 * Z, "dBFS", dim, 10.0 * Z, 2)); + texts.push(text(side - 60.0 * Z, axis_y, 60.0 * Z, "dBFS in", dim, 10.0 * Z, 2)); + + // Transfer, unity and the band between them (gain, or what the clipper takes off). + let n = 96; + let ins: Vec = (0..=n).map(|i| DB_LO + (DB_HI - DB_LO) * i as f32 / n as f32).collect(); + let out: Vec<(f32, f32)> = ins.iter().map(|&x| (x_of(x), y_of(peak_db(&*l.curve, x)))).collect(); + let open: Vec<(f32, f32)> = ins.iter().map(|&x| (x_of(x), y_of(if l.clipper { peak_db(&*l.open, x) } else { x }))).collect(); + let mut band = crate::device_canvas::path(&out); + for (x, y) in open.iter().rev() { + band.push_str(&format!(" L{x:.1} {y:.1}")); + } + band.push_str(" Z"); + paths.push(fill(band, rgba(accent, FILL))); + let unity: Vec<(f32, f32)> = ins.iter().map(|&x| (x_of(x), y_of(x))).collect(); + paths.push(stroke(dashed(&unity, 3.0 * Z, 3.0 * Z), white(0.35), 1.0)); + paths.push(stroke(crate::device_canvas::path(&out), white(0.95), 1.5 * Z)); + + // The wave: two cycles of a full-scale sine (dashed) and the device's output, filled + // to the zero line, between full-scale rules. + let (wx0, wx1) = (side + GAP * Z, pw); + let mid = side / 2.0; + let amp = 0.4 * side; + let wy = |s: f32| mid - s.clamp(-1.25, 1.25) * amp; + paths.push(stroke(format!("M{wx0:.1} {a:.1} H{wx1:.1} M{wx0:.1} {b:.1} H{wx1:.1}", a = wy(1.0), b = wy(-1.0)), white(0.08), 1.0)); + paths.push(stroke(format!("M{wx0:.1} {mid:.1} H{wx1:.1}"), white(0.14), 1.0)); + let m = ((wx1 - wx0) / Z).round().max(64.0) as usize; + let ph_of = |i: usize| TAU * 2.0 * i as f32 / m as f32; + let xs = |i: usize| wx0 + (wx1 - wx0) * i as f32 / m as f32; + let input: Vec<(f32, f32)> = (0..=m).map(|i| (xs(i), wy(ph_of(i).sin()))).collect(); + let shaped: Vec<(f32, f32)> = (0..=m).map(|i| (xs(i), wy((l.curve)(ph_of(i).sin())))).collect(); + paths.push(stroke(dashed(&input, 3.0 * Z, 3.0 * Z), white(0.3), 1.0)); + let mut wave = crate::device_canvas::path(&shaped); + wave.push_str(&format!(" L{wx1:.1} {mid:.1} L{wx0:.1} {mid:.1} Z")); + paths.push(fill(wave, rgba(accent, FILL))); + paths.push(stroke(crate::device_canvas::path(&shaped), accent, 1.5 * Z)); + texts.push(text(wx1 - 80.0 * Z, -14.0 * Z, 80.0 * Z, "Wave", dim, 10.0 * Z, 2)); + + // Legend under the axis. + let ly = side + 31.0 * Z; + let (glyph, gap) = (14.0 * Z, 6.0 * Z); + let mut x = 0.0; + let band_name = if l.clipper { "Clipping" } else { "Gain" }; + for (kind, label) in [(0, "Transfer"), (1, "Unity"), (2, band_name)] { + match kind { + 0 => paths.push(stroke(format!("M{x:.1} {ly:.1} h{glyph:.1}"), white(0.9), 1.5 * Z)), + 1 => paths.push(stroke(dashed(&[(x, ly), (x + glyph, ly)], 3.0 * Z, 3.0 * Z), white(0.4), 1.5 * Z)), + _ => paths.push(fill( + format!("M{x:.1} {:.1} h{glyph:.1} v{:.1} h{:.1} Z", ly - 4.0 * Z, 8.0 * Z, -glyph), + rgba(accent, FILL + 0.05), + )), + } + texts.push(text(x + glyph + gap, ly, 90.0 * Z, label, white(0.7), 9.0 * Z, 0)); + x += glyph + gap + crate::text_measure::value_width(label) * 0.9 * Z + 16.0 * Z; + } + + // Caption above the plot. + let (caption, value) = self.wave_caption(d, def); + let cy = -(pt - 12.0) * Z; + texts.push(text(0.0, cy, 120.0 * Z, caption, white(0.55), 12.0 * Z, 0)); + let at = (crate::text_measure::value_width(caption) + 12.0) * Z; + texts.push(text(at, cy, 160.0 * Z, value, white(0.92), 13.0 * Z, 0)); + + // A vertical drag on the wave moves the first parameter, as on the card. + let p0 = &def.params[0]; + let zones = vec![PanelZone { + x: wx0, + y: 0.0, + w: wx1 - wx0, + h: side, + param_x: -1, + param_y: def.index_of(p0.id).map_or(-1, |i| i as i32), + rate_x: 0.0, + rate_y: 1.0 / (side - 16.0 * Z).max(8.0), + }]; + PanelCanvas { + visible: true, + w: pw, + h: ph, + paths: model(paths), + texts: model(texts), + zones: model(zones), + label: caption.into(), + ..Default::default() + } + } + + /// Bit Crusher: one cycle through the crusher, filled to the zero line, the dry sine + /// dashed beneath; "Rate 24.0 kHz and 256 levels" above. Right drag lowers Downsample, + /// up raises Bit Depth, as on the card. + pub(crate) fn crush_window_canvas(&self, d: &Device, def: &'static DeviceDef, k: &Kit, accent: Color) -> PanelCanvas { + let v = |id: &str| def.param(id).map_or(0.0, |p| self.panel_value(d, def, p)); + let (_, _, dw, dh) = k.display; + let (pl, pt, pr, pb) = k.plot; + let (pw, ph) = ((dw - pl - pr) * Z, (dh - pt - pb) * Z); + let bits = v("bitdepth"); + let pic = builtin::crush_picture(bits, v("downsample"), v("mix"), v("dither"), v("jitter")); + let mid = ph / 2.0; + let amp = (mid - 12.0 * Z) / 1.15; + let y_of = |s: f32| mid - s.clamp(-1.15, 1.15) * amp; + let n = pic.len(); + let x_of = |i: usize| pw * i as f32 / n as f32; + let mut paths: Vec = Vec::new(); + paths.push(stroke(format!("M0 {a:.1} H{pw:.1} M0 {b:.1} H{pw:.1}", a = y_of(1.0), b = y_of(-1.0)), white(0.06), 1.0)); + paths.push(stroke(format!("M0 {mid:.1} H{pw:.1}"), white(0.14), 1.0)); + let dry: Vec<(f32, f32)> = (0..=n).map(|i| (x_of(i), y_of(pic[i.min(n - 1)].0))).collect(); + let mut steps = Vec::with_capacity(2 * n); + for (i, (_, c)) in pic.iter().enumerate() { + steps.push((x_of(i), y_of(*c))); + steps.push((x_of(i + 1), y_of(*c))); + } + let line = crate::device_canvas::path(&steps); + paths.push(stroke(dashed(&dry, 3.0 * Z, 3.0 * Z), white(0.25), 1.0)); + paths.push(fill(format!("{line} L{pw:.1} {mid:.1} L0 {mid:.1} Z"), rgba(accent, FILL))); + paths.push(stroke(line, white(0.95), 1.5 * Z)); + let (rate, levels) = crush_caption(bits, v("downsample")); + let cy = -(pt - 12.0) * Z; + let at = (crate::text_measure::value_width("Rate") + 12.0) * Z; + let at2 = at + (crate::text_measure::value_width(&rate) * 1.08 + 12.0) * Z; + let texts = vec![ + text(0.0, cy, 60.0 * Z, "Rate", white(0.55), 12.0 * Z, 0), + text(at, cy, 120.0 * Z, rate, white(0.92), 13.0 * Z, 0), + text(at2, cy, 200.0 * Z, levels, white(0.5), 11.0 * Z, 0), + ]; + let idx = |id: &str| def.index_of(id).map_or(-1, |i| i as i32); + let zones = vec![PanelZone { + x: 0.0, + y: 0.0, + w: pw, + h: ph, + param_x: idx("downsample"), + param_y: idx("bitdepth"), + rate_x: -1.0 / (pw - 16.0 * Z).max(8.0), + rate_y: 1.0 / (ph - 16.0 * Z).max(8.0), + }]; + PanelCanvas { + visible: true, + w: pw, + h: ph, + paths: model(paths), + texts: model(texts), + zones: model(zones), + label: "Rate".into(), + ..Default::default() + } + } +} + +/// The crusher's held rate at 48 kHz and its number of levels: ("24.0 kHz", "and 256 levels"). +pub(crate) fn crush_caption(bits: f32, downsample: f32) -> (String, String) { + let khz = 48.0 / downsample.max(1.0); + let rate = if khz >= 10.0 { format!("{khz:.1} kHz") } else { format!("{:.0} Hz", khz * 1000.0) }; + let b = bits.round().clamp(1.0, 32.0) as u32; + let levels = if b <= 20 { + let n = 1u64 << b; + let mut s = n.to_string(); + let mut i = s.len() as i32 - 3; + while i > 0 { + s.insert(i as usize, ','); + i -= 3; + } + s + } else { + format!("2^{b}") + }; + (rate, format!("and {levels} levels")) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// With nothing driven, the transfer is unity shifted by the output gain. + #[test] + fn transfer_follows_the_output_gain() { + let def = builtin::find("tapesaturation").unwrap(); + let v = |id: &str| match id { + "drive" | "compression" | "mix" | "mode" => 0.0, + "output_gain" => -12.0, + _ => 0.0, + }; + let l = law(def, &v); + for lv in [-24.0, -12.0, 0.0] { + assert!((peak_db(&*l.curve, lv) - (lv - 12.0)).abs() < 0.2, "{lv}"); + } + } + + /// The soft clipper holds a hot sine under its open path. + #[test] + fn clipper_holds_a_hot_sine_under_the_open_path() { + let def = builtin::find("soft-clipper").unwrap(); + let v = |id: &str| match id { + "input" => 12.0, + "ceiling" => -6.0, + "softness" => 50.0, + "mix" => 100.0, + _ => 0.0, + }; + let l = law(def, &v); + assert!(peak_db(&*l.curve, 0.0) < peak_db(&*l.open, 0.0) - 6.0); + } + + #[test] + fn crush_caption_reads_rate_and_levels() { + assert_eq!(crush_caption(1.0, 1.0), ("48.0 kHz".to_string(), "and 2 levels".to_string())); + assert_eq!(crush_caption(16.0, 2.0), ("24.0 kHz".to_string(), "and 65,536 levels".to_string())); + assert_eq!(crush_caption(8.0, 12.0).0, "4000 Hz"); + } +} diff --git a/crates/dstudio-app/src/main.rs b/crates/dstudio-app/src/main.rs index 73b06f9c..f6f6a892 100644 --- a/crates/dstudio-app/src/main.rs +++ b/crates/dstudio-app/src/main.rs @@ -59,6 +59,7 @@ mod device_window_dynamics; mod device_window_fields; mod device_window_multiband; mod device_window_shaper; +mod device_window_wave; mod devices; mod devminimap; mod dialogs; From bbc78cef264453f6c68119e997d989cc0827861b Mon Sep 17 00:00:00 2001 From: Chris Laupama <42254688+chrislaupama@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:44:46 +0000 Subject: [PATCH 2/3] Halve the waveshaper window's transfer sampling; document the wave windows --- CHANGELOG.md | 3 +++ README.md | 3 ++- crates/dstudio-app/src/device_window_wave.rs | 14 ++++++++------ docs/devices.md | 9 +++++++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a26539dc..35ce4def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,3 +55,6 @@ Nothing has been released yet. The first release, 0.1.0, will cover what is on ` - An expanded window for Echo: the machine's tape strip with its erase, record and playback heads (lit while they play), the tail across the display, a main card with the spacing, transport, width, mix and a Throw pill that sends while held, and a rack that shows the tail filters and ducking, or the machine's character behind the Character latch. +- Expanded windows for Distortion, Tape Saturation and Soft Clipper (the static transfer on a 48 dB square with the + gain or clipping it applies lit against unity, two cycles of a full-scale sine through the device, and every control + in one card) and Bit Crusher (one crushed cycle across the display under its held rate and number of levels). diff --git a/README.md b/README.md index de82147f..c92ff0ab 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,8 @@ or 10 seconds of input, output and gain reduction under a draggable threshold, b Limiter adds a short-term loudness lane against a target), Transient Shaper, whose window scopes the output with the gain it applied, and Multiband Compressor, whose window draws each band's level, gain and thresholds on a frequency axis with draggable crossovers, and Delay, Echo and Reverb, whose window draws the card's echo train, tail picture or repeat -EQ at full size with every control in a row below; the other devices open their card in a window for now. +EQ at full size with every control in a row below, Distortion, Tape Saturation and Soft Clipper, whose window draws +the transfer curve beside a sine through the device, and Bit Crusher, whose window draws one crushed cycle; the other devices open their card in a window for now. [docs/devices.md](docs/devices.md) explains how they work and how to add one. | Kind | Devices | diff --git a/crates/dstudio-app/src/device_window_wave.rs b/crates/dstudio-app/src/device_window_wave.rs index 56d58dfd..ffb83de2 100644 --- a/crates/dstudio-app/src/device_window_wave.rs +++ b/crates/dstudio-app/src/device_window_wave.rs @@ -65,11 +65,13 @@ pub(crate) fn law(def: &DeviceDef, v: &dyn Fn(&str) -> f32) -> Law { } } -/// Peak (dBFS) of f over one cycle of a sine at `level` dBFS. +/// Peak (dBFS) of f over one cycle of a sine at `level` dBFS: the two crests exactly, and +/// a coarse pass between them for shapers that fold back before the crest. pub(crate) fn peak_db(f: &dyn Fn(f32) -> f32, level: f32) -> f32 { let a = 10f32.powf(level / 20.0); - let n = 96; - let peak = (0..n).map(|i| f(a * (TAU * (i as f32 + 0.25) / n as f32).sin()).abs()).fold(0.0f32, f32::max); + let n = 8; + let crest = f(a).abs().max(f(-a).abs()); + let peak = (0..n).map(|i| f(a * (TAU * (i as f32 + 0.5) / n as f32).sin()).abs()).fold(crest, f32::max); 20.0 * peak.max(1e-6).log10() } @@ -145,7 +147,7 @@ impl Controller { texts.push(text(side - 60.0 * Z, axis_y, 60.0 * Z, "dBFS in", dim, 10.0 * Z, 2)); // Transfer, unity and the band between them (gain, or what the clipper takes off). - let n = 96; + let n = 48; let ins: Vec = (0..=n).map(|i| DB_LO + (DB_HI - DB_LO) * i as f32 / n as f32).collect(); let out: Vec<(f32, f32)> = ins.iter().map(|&x| (x_of(x), y_of(peak_db(&*l.curve, x)))).collect(); let open: Vec<(f32, f32)> = ins.iter().map(|&x| (x_of(x), y_of(if l.clipper { peak_db(&*l.open, x) } else { x }))).collect(); @@ -167,7 +169,7 @@ impl Controller { let wy = |s: f32| mid - s.clamp(-1.25, 1.25) * amp; paths.push(stroke(format!("M{wx0:.1} {a:.1} H{wx1:.1} M{wx0:.1} {b:.1} H{wx1:.1}", a = wy(1.0), b = wy(-1.0)), white(0.08), 1.0)); paths.push(stroke(format!("M{wx0:.1} {mid:.1} H{wx1:.1}"), white(0.14), 1.0)); - let m = ((wx1 - wx0) / Z).round().max(64.0) as usize; + let m = ((wx1 - wx0) / (2.0 * Z)).round().max(64.0) as usize; let ph_of = |i: usize| TAU * 2.0 * i as f32 / m as f32; let xs = |i: usize| wx0 + (wx1 - wx0) * i as f32 / m as f32; let input: Vec<(f32, f32)> = (0..=m).map(|i| (xs(i), wy(ph_of(i).sin()))).collect(); @@ -259,7 +261,7 @@ impl Controller { let (rate, levels) = crush_caption(bits, v("downsample")); let cy = -(pt - 12.0) * Z; let at = (crate::text_measure::value_width("Rate") + 12.0) * Z; - let at2 = at + (crate::text_measure::value_width(&rate) * 1.08 + 12.0) * Z; + let at2 = at + (crate::text_measure::value_width(&rate) * 1.15 + 16.0) * Z; let texts = vec![ text(0.0, cy, 60.0 * Z, "Rate", white(0.55), 12.0 * Z, 0), text(at, cy, 120.0 * Z, rate, white(0.92), 13.0 * Z, 0), diff --git a/docs/devices.md b/docs/devices.md index c3ab8ec0..ff20e24d 100644 --- a/docs/devices.md +++ b/docs/devices.md @@ -66,7 +66,8 @@ flowchart LR Character EQ and the plotted-field family: Tremolo, Auto-Panner, Chorus, Flanger, Phaser, Ring Mod and Character Mod; and the dynamics family: Compressor, Limiter, Gate, Glue Compressor, Character Compressor and Mastering - Limiter; Transient Shaper; Multiband Compressor; Delay, Echo and Reverb), otherwise the card itself in a window. A + Limiter; Transient Shaper; Multiband Compressor; Delay, Echo and Reverb; Distortion, Tape Saturation, + Soft Clipper and Bit Crusher), otherwise the card itself in a window. A window is a face 960 pt wide (Ring Mod 860): a top bar (power, presets, the device's header pills), a large display (the card's canvas drawn at window size with its axis and legend), In / Out meters, and rows of controls laid out with the same @@ -118,7 +119,11 @@ flowchart LR erase and record heads and each playback head at its distance, lit while it plays. Its Throw pill is momentary (`PILL_MOMENTARY`, `panel_latch`), and its Character latch (`ACT_SECTION`) swaps the rack between the tail - filters and ducking and the machine's character. The + filters and ducking and the machine's character. The waveshapers + (src/device_window_wave.rs) draw the static transfer from the core's + `distortion_transfer`, `tape_transfer` and `clipper_transfer` as the sine + peak out against the sine peak in on a 48 dB square, and the Bit Crusher + draws `crush_picture` across its display. The meters read `se_builtin_set_metering` / `se_builtin_levels` (`Engine::builtin_levels(id, &mut [f32; 4])`): while a window is open the audio thread keeps the input and output peak of each block, and the app From bf177daaf55838dfba0619573ae42db39beadcba Mon Sep 17 00:00:00 2001 From: Chris Laupama <42254688+chrislaupama@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:28:21 +0000 Subject: [PATCH 3/3] Anchor the wave window's ladder and axis labels at the square --- crates/dstudio-app/src/device_window_wave.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/dstudio-app/src/device_window_wave.rs b/crates/dstudio-app/src/device_window_wave.rs index ffb83de2..fe42fb68 100644 --- a/crates/dstudio-app/src/device_window_wave.rs +++ b/crates/dstudio-app/src/device_window_wave.rs @@ -137,14 +137,14 @@ impl Controller { paths.push(stroke(grid, white(0.05), 1.0)); paths.push(stroke(zero, white(0.14), 1.0)); for db in [DB_HI, 0.0, -12.0, -24.0, DB_LO] { - texts.push(text(-48.0 * Z, y_of(db), 40.0 * Z, tick_label(db), dim, 11.0 * Z, 2)); + texts.push(text(-8.0 * Z, y_of(db), 40.0 * Z, tick_label(db), dim, 11.0 * Z, 2)); } let axis_y = side + 12.0 * Z; for db in TICKS.iter().take(3) { - texts.push(text(x_of(*db) - 20.0 * Z, axis_y, 40.0 * Z, tick_label(*db), dim, 10.0 * Z, 1)); + texts.push(text(x_of(*db), axis_y, 40.0 * Z, tick_label(*db), dim, 10.0 * Z, 1)); } - texts.push(text(-48.0 * Z, axis_y, 44.0 * Z, "dBFS", dim, 10.0 * Z, 2)); - texts.push(text(side - 60.0 * Z, axis_y, 60.0 * Z, "dBFS in", dim, 10.0 * Z, 2)); + texts.push(text(-8.0 * Z, axis_y, 44.0 * Z, "dBFS", dim, 10.0 * Z, 2)); + texts.push(text(side, axis_y, 60.0 * Z, "dBFS in", dim, 10.0 * Z, 2)); // Transfer, unity and the band between them (gain, or what the clipper takes off). let n = 48; @@ -179,7 +179,7 @@ impl Controller { wave.push_str(&format!(" L{wx1:.1} {mid:.1} L{wx0:.1} {mid:.1} Z")); paths.push(fill(wave, rgba(accent, FILL))); paths.push(stroke(crate::device_canvas::path(&shaped), accent, 1.5 * Z)); - texts.push(text(wx1 - 80.0 * Z, -14.0 * Z, 80.0 * Z, "Wave", dim, 10.0 * Z, 2)); + texts.push(text(wx1, 6.0 * Z, 80.0 * Z, "Wave", dim, 10.0 * Z, 2)); // Legend under the axis. let ly = side + 31.0 * Z;