Skip to main content

rlx_core/render/scenes/lines/
spectrum.rs

1//! Spectrum scene (Plan 0034 / ADR-0036): the analysis frame's log-spaced band
2//! array, drawn as N elements.
3//!
4//! This is a **fourth consumer of the existing line idiom**, not a fifth render
5//! idiom: N bars, an N-point polyline and a radial ring of N spokes are all
6//! segment lists, and they go out through the same shared
7//! [`LineRenderer`] the three other line scenes draw
8//! through (ADR-0007). Nothing new is uploaded, no new pipeline is built, and
9//! the `Scene` trait is untouched — `update` already receives the whole
10//! [`AnalysisFrame`], bands included.
11//!
12//! The per-frame work is a chain of small pure steps — `downsample`, the
13//! per-element ease, then one of the three [`SpectrumLayout`] builders — all
14//! free functions over preallocated buffers. They are separate from the scene so
15//! the claims that matter (low elements track low frequencies; the 64 → N
16//! reduction loses nothing) are testable without a GPU.
17//!
18//! **The composite vocabulary this scene honors**, since a silent no-op is the
19//! failure mode the shared-surface work exists to avoid:
20//!
21//! - `zoom` / `pan_x` / `pan_y` — the shared view transform (ADR-0018), applied
22//!   by the renderer exactly as for every other line scene.
23//! - `mirror_order` / `mirror_reflect` — the geometry mirror (Plan 0018 Phase
24//!   4). It replicates real geometry *before* rasterization and costs this scene
25//!   nothing, so refusing it would be a no-op the author could not see. On the
26//!   radial ring it is nearly the identity (the ring is already rotationally
27//!   symmetric, the same near-no-op the Hankin star has); on bars and polyline
28//!   it is genuinely transformative, because those figures are not centred.
29//! - `[palette]` / `[palette_b]` / `palette_mix` / `hue` / `hue_spread` /
30//!   `saturation` — the colour surface (ADR-0021), sampled on the CPU. Each line
31//!   scene walks `hue_spread` along the axis its own generator makes meaningful
32//!   (ADR-0059) — path position on [`parametric`](super::parametric), generation
33//!   depth on [`lsystem`](super::lsystem), radius on [`star`](super::star) — and
34//!   this scene's is **band index**. The default `spectrum` palette is the engine
35//!   cosine, so an author who sets no `[palette]` sees the usual colour language.
36//! - `thickness` / `brightness` / `scale` / `base` — ordinary stroke styling.
37//! - `curve` — the level-shaping exponent (ADR-0040, Plan 0038 Phase 3), applied
38//!   to the downsampled level **before** the per-element smoother so the easing
39//!   operates in the displayed domain. `1.0` is exactly linear. It is the third
40//!   per-element lever on an element's length, beside `base` and `scale`.
41//! - `glow` — the line renderer's per-segment falloff multiplier (Plan 0038),
42//!   whole-figure like on the other three line scenes. Not a post bloom.
43//! - `softness` — the across-the-stroke profile (ADR-0124), whole-figure and
44//!   shared with the other three line scenes. Default `0.25`: a solid bar with
45//!   a short shoulder. `1.0` is the pure quadratic falloff; `0` is solid with a
46//!   one-pixel edge. A different quantity from `glow`, which scales the light and
47//!   never the coverage.
48//!
49//! Three parameters are **layout-specific**, and each is a no-op on the layouts
50//! it does not describe — stated in `presets/README.md` and `docs/presets.md`
51//! rather than left for an author to discover:
52//!
53//! - `radius` is the ring's inner radius; no meaning for bars or the polyline.
54//! - `span` and `baseline` place the bars/polyline figure in **world** space; no
55//!   meaning for the ring, which `radius` sizes instead (Plan 0038 Phase 2).
56
57// Hot-path panic-denial pragma (Plan 0002 Phase 2, extended to scenes by Plan
58// 0003 Phase 0). `update`/`render` run every displayed frame.
59#![deny(
60    clippy::unwrap_used,
61    clippy::expect_used,
62    clippy::indexing_slicing,
63    clippy::panic,
64    clippy::unreachable
65)]
66
67use std::cell::RefCell;
68use std::rc::Rc;
69
70use super::super::common;
71use super::super::{Scene, SeriesBound};
72use super::renderer::{LineRenderer, SegmentInstance, StrokeMetric, miter_extension};
73use super::{
74    CapOverflow, GeneratorConfig, MirrorSpec, OverflowContext, ViewTransform, replicate_mirror,
75};
76use crate::dsp::AnalysisFrame;
77use crate::preset::Easing;
78use crate::render::palette::{self, Palette, desaturate};
79use crate::render::scenes::{ParamGroup, ParamKind, ParamSpec, default_of};
80
81/// Largest element count a `[spectrum]` table may ask for — the band count
82/// itself, because above it the 64 → N reduction stops being a partition of the
83/// array. The loader validates against this, and the render layer sizes its
84/// per-element scratch to it (Plan 0034 Phase 4), so the two cannot disagree.
85pub const MAX_ELEMENTS: usize = crate::dsp::SPECTRUM_BINS;
86
87// Parameter defaults — a legible, calm readout when a preset binds nothing.
88const DEFAULT_THICKNESS: f32 = 6.0;
89const DEFAULT_HUE: f32 = 0.55;
90const DEFAULT_HUE_SPREAD: f32 = 0.0;
91const DEFAULT_BRIGHTNESS: f32 = 1.0;
92/// The line renderer's **per-segment falloff** multiplier (Plan 0038 Phase 1) —
93/// not a post-process bloom. `1.0` is the value this scene passed as a literal
94/// before it was bound, so the default is exactly today's look.
95const DEFAULT_GLOW: f32 = 1.0;
96const DEFAULT_SCALE: f32 = default_of(PARAMS, "scale");
97/// Minimum element length, in world units. Non-zero on purpose: a spectrum
98/// readout at rest is a comb, not an empty frame, so the figure stays on screen
99/// (and legible) through a silence instead of vanishing.
100const DEFAULT_BASE: f32 = default_of(PARAMS, "base");
101/// Inner radius of the radial ring (ignored by the other two layouts).
102const DEFAULT_RADIUS: f32 = default_of(PARAMS, "radius");
103/// World-space **half-width** the readout spans, so the figure is `2 * span`
104/// wide — `1.0` is what an unbound preset gets.
105///
106/// It is a **world** quantity, not a screen one. The renderer divides x by the
107/// target aspect on the GPU, so this scene never sees an aspect and cannot take
108/// one from the wrong place (ADR-0037). The honest consequence: *"fill the
109/// width"* is aspect-dependent — `span ≈ 1.78` fills a 16:9 frame and leaves an
110/// ultrawide short. There is deliberately no `fit` mode.
111///
112/// Applies to [`SpectrumLayout::Bars`] and [`SpectrumLayout::Polyline`]; a
113/// **no-op on [`SpectrumLayout::RadialRing`]**, which is sized by `radius`
114/// instead — the mirror image of `radius` already being a no-op on the
115/// other two.
116const DEFAULT_SPAN: f32 = default_of(PARAMS, "span");
117/// World-space y the bars and the polyline rest on — what an unbound preset
118/// gets. Also a **no-op on [`SpectrumLayout::RadialRing`]**, whose spokes start
119/// on the ring.
120///
121/// `baseline = 0` is what makes `mirror_reflect` mean what it means everywhere
122/// else: the mirror reflects across the **x-axis**, so a figure standing on the
123/// axis reflects into a symmetric "landscape and its reflection" about the
124/// frame centre, while one standing at `-0.85` throws its copy against the top
125/// edge (design-backlog 0018).
126const DEFAULT_BASELINE: f32 = default_of(PARAMS, "baseline");
127/// Level-shaping exponent (ADR-0040). `1.0` is exactly linear — `powf(x, 1.0) ==
128/// x` — `0.5` is a square root, and lower values compress harder.
129///
130/// It applies to the **downsampled level, before the per-element smoother**, so
131/// `[spectrum] smoothing` eases the displayed quantity the way meter ballistics
132/// do. That ordering is the ADR's whole content; see [`curve_level`].
133const DEFAULT_CURVE: f32 = default_of(PARAMS, "curve");
134/// The range [`curve_level`] clamps the exponent into before the `powf`.
135///
136/// **Totality is part of ADR-0040's decision, not an implementation detail.**
137/// This runs per element per frame on the render path, where a `NaN` or an
138/// infinite length must not reach the geometry. A floor strictly above zero is
139/// what rules out `pow(0, 0)` and `pow(0, -1)` for every author expression.
140const CURVE_MIN: f32 = 0.05;
141const CURVE_MAX: f32 = 4.0;
142const DEFAULT_ROTATION: f32 = default_of(PARAMS, "rotation");
143// Shared view transform (ADR-0018): identity by default.
144const DEFAULT_ZOOM: f32 = 1.0;
145// Geometry mirror (Plan 0018 Phase 4): identity by default.
146const DEFAULT_MIRROR_ORDER: f32 = 1.0;
147const DEFAULT_MIRROR_REFLECT: f32 = 0.0;
148
149/// Parameter vocabulary — see [`fragment_field::PARAMS`](crate::render::scenes::fragment_field::PARAMS).
150/// **Keep in sync with `set_param` below.**
151pub const PARAMS: &[ParamSpec] = &[
152    ParamSpec {
153        name: "base",
154        default: 0.06,
155        range: Some([0.0, 1.0]),
156        doc: "Height the readout sits at when the band is silent.",
157        kind: ParamKind::Modal,
158        group: ParamGroup::Shape,
159        main: false,
160    },
161    ParamSpec {
162        name: "scale",
163        default: 1.2,
164        range: Some([0.0, 4.0]),
165        doc: "How far a full band pushes the readout above its base.",
166        kind: ParamKind::Modal,
167        group: ParamGroup::Shape,
168        main: true,
169    },
170    ParamSpec {
171        name: "curve",
172        default: 1.0,
173        range: Some([CURVE_MIN, CURVE_MAX]),
174        doc: "Exponent on each band's level: 1 is linear, below 1 lifts quiet detail, above 1 pushes it down.",
175        kind: ParamKind::Modal,
176        group: ParamGroup::Shape,
177        main: false,
178    },
179    ParamSpec {
180        name: "radius",
181        default: 0.35,
182        range: Some([0.0, 1.0]),
183        doc: "Radius of the ring the readout is drawn around, in the radial layouts.",
184        kind: ParamKind::Modal,
185        group: ParamGroup::Shape,
186        main: true,
187    },
188    ParamSpec {
189        name: "span",
190        default: 1.0,
191        range: Some([0.0, 1.0]),
192        doc: "How much of the frequency axis is shown; below 1 the top end is cut.",
193        kind: ParamKind::Modal,
194        group: ParamGroup::Shape,
195        main: false,
196    },
197    ParamSpec {
198        name: "baseline",
199        default: -0.85,
200        range: None,
201        doc: "Where the flat layout's zero line sits vertically.",
202        kind: ParamKind::Modal,
203        group: ParamGroup::Shape,
204        main: false,
205    },
206    ParamSpec {
207        name: "rotation",
208        default: 0.0,
209        range: Some([0.0, std::f32::consts::TAU]),
210        doc: "Turns the readout, in radians.",
211        kind: ParamKind::Modal,
212        group: ParamGroup::Motion,
213        main: false,
214    },
215    crate::render::scenes::lines::thickness(DEFAULT_THICKNESS),
216    crate::render::scenes::common::hue(DEFAULT_HUE),
217    crate::render::scenes::lines::hue_spread(DEFAULT_HUE_SPREAD),
218    crate::render::scenes::common::SATURATION,
219    crate::render::scenes::common::PALETTE_MIX,
220    crate::render::scenes::common::PALETTE_STEPS,
221    crate::render::scenes::common::PALETTE_CONTOUR,
222    crate::render::scenes::common::brightness(DEFAULT_BRIGHTNESS),
223    crate::render::scenes::lines::GLOW,
224    crate::render::scenes::lines::SOFTNESS,
225    crate::render::scenes::lines::STROKE_BLEND,
226    crate::render::scenes::common::zoom(1.0),
227    crate::render::scenes::common::PAN_X,
228    crate::render::scenes::common::PAN_Y,
229    crate::render::scenes::lines::MIRROR_ORDER,
230    crate::render::scenes::lines::MIRROR_REFLECT,
231];
232
233/// Which figure the elements form. Selected once at preset load through the
234/// `[spectrum]` table; an unknown name is a surfaced load error (ADR-0007).
235#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
236pub enum SpectrumLayout {
237    /// Upright bars standing on a common baseline — the classic readout.
238    #[default]
239    Bars,
240    /// A single continuous line through one point per element: the same data as
241    /// a contour rather than a comb.
242    Polyline,
243    /// Spokes radiating outward from a ring, one per element, with the frequency
244    /// axis wrapped around the circle.
245    RadialRing,
246}
247
248impl SpectrumLayout {
249    /// The accepted `[spectrum] layout` names, in the order the error message
250    /// lists them. The single source for both parsing and the message.
251    pub const NAMES: [&'static str; 3] = ["bars", "polyline", "radial_ring"];
252
253    /// Parse a `[spectrum] layout` name, or `None` if unknown.
254    pub fn from_name(name: &str) -> Option<Self> {
255        Some(match name {
256            "bars" => SpectrumLayout::Bars,
257            "polyline" => SpectrumLayout::Polyline,
258            "radial_ring" => SpectrumLayout::RadialRing,
259            _ => return None,
260        })
261    }
262}
263
264/// Where the figure sits — the scalars that belong to the **whole** readout
265/// rather than to an element, so they stay off the per-element arrays.
266#[derive(Debug, Clone, Copy)]
267pub(crate) struct Placement {
268    /// Inner radius — [`SpectrumLayout::RadialRing`] only.
269    pub radius: f32,
270    /// World-space half-width — [`SpectrumLayout::Bars`] and
271    /// [`SpectrumLayout::Polyline`] only. See [`DEFAULT_SPAN`].
272    pub span: f32,
273    /// World-space y the figure rests on — bars and polyline only. See
274    /// [`DEFAULT_BASELINE`].
275    pub baseline: f32,
276    /// Whole-figure rotation in radians, about the world origin.
277    pub rotation: f32,
278}
279
280/// The scene's own defaults rather than zeroes: a `Placement` with `span = 0`
281/// would collapse the figure to a point, which is never a sensible fallback.
282impl Default for Placement {
283    fn default() -> Self {
284        Self {
285            radius: 0.0,
286            span: DEFAULT_SPAN,
287            baseline: DEFAULT_BASELINE,
288            rotation: 0.0,
289        }
290    }
291}
292
293/// The parameters this scene accepts as a **per-element series** (Plan 0034
294/// Phase 4) — the ones whose effect is genuinely per element. Everything else in
295/// [`PARAMS`] describes the whole figure (`radius`, `rotation`, the view
296/// transform, the mirror, `hue_spread`, `palette_mix`, `saturation`), so a
297/// series aimed at one of those degrades to its `index = 0` value, which is what
298/// the trait default does.
299///
300/// **Index order matches the `SERIES_*` constants below** — the two are read
301/// together by `set_param_series` and `update`.
302const SERIES_PARAMS: [&str; 6] = ["base", "scale", "curve", "thickness", "brightness", "hue"];
303const SERIES_BASE: usize = 0;
304const SERIES_SCALE: usize = 1;
305/// `curve` sits with `base` and `scale` because it is the third lever on an
306/// element's *length*, and per-element is a shape ADR-0040 names explicitly
307/// ("walk it per element with `index`") — a series aimed at it has to reach the
308/// elements rather than degrade to its `index = 0` value.
309const SERIES_CURVE: usize = 2;
310const SERIES_THICKNESS: usize = 3;
311const SERIES_BRIGHTNESS: usize = 4;
312const SERIES_HUE: usize = 5;
313
314/// Reduce the engine's band array to `levels.len()` elements by **averaging each
315/// element's own contiguous slice of bands**.
316///
317/// Element `i` covers `[i * bands / n, (i + 1) * bands / n)`. That is a genuine
318/// partition — contiguous, non-overlapping, and complete — so no band is dropped
319/// or double-counted at any element count up to the band count (which is why the
320/// loader caps the count there). It is also deterministic: integer arithmetic on
321/// lengths, no clock and no rounding mode to disagree about.
322pub(crate) fn downsample(spectrum: &[f32], levels: &mut [f32]) {
323    let bands = spectrum.len();
324    let n = levels.len();
325    if bands == 0 || n == 0 {
326        levels.fill(0.0);
327        return;
328    }
329    for (i, level) in levels.iter_mut().enumerate() {
330        let lo = i * bands / n;
331        // `hi` is the next element's `lo`, which makes the ranges abut exactly.
332        // The `max` only bites when n > bands, where a strict partition is not
333        // available; the loader caps the count so that never happens.
334        let hi = (((i + 1) * bands / n).min(bands)).max(lo + 1);
335        let slice = spectrum.get(lo..hi).unwrap_or(&[]);
336        *level = if slice.is_empty() {
337            0.0
338        } else {
339            slice.iter().sum::<f32>() / slice.len() as f32
340        };
341    }
342}
343
344/// Shape a raw downsampled level by the exponent `curve` — ADR-0040's decision,
345/// and the step that runs **before** the per-element smoother.
346///
347/// Audio level is perceptually logarithmic, so the linear map this scene had
348/// spent most of its range on the loudest element. `curve = 1.0` is exactly the
349/// identity, which is what lets the default leave every existing preset and
350/// every golden baseline unchanged.
351///
352/// **Total by construction**, because it runs per element per frame on the
353/// render path and no author guard stands between an expression and this call:
354///
355/// - the level is floored at `0` — `f32::max` returns the non-`NaN` operand, so
356///   a `NaN` level floors to `0` too, and a negative one can never become a
357///   fractional power of a negative base;
358/// - the exponent is clamped into `[CURVE_MIN, CURVE_MAX]`, a range that
359///   excludes `0`, so neither `pow(0, 0)` nor `pow(0, -1)` is reachable. `NaN`
360///   has no clamped image (`f32::clamp` propagates it), so it is mapped to the
361///   linear default rather than allowed through.
362pub(crate) fn curve_level(level: f32, curve: f32) -> f32 {
363    let exponent = if curve.is_nan() {
364        DEFAULT_CURVE
365    } else {
366        curve.clamp(CURVE_MIN, CURVE_MAX)
367    };
368    level.max(0.0).powf(exponent)
369}
370
371/// The world-space length an element reaches, `base + scale * level`, floored at
372/// zero so a degenerate level can never invert the element.
373pub(crate) fn element_length(level: f32, base: f32, scale: f32) -> f32 {
374    (base + scale * level).max(0.0)
375}
376
377/// Build the segment list for `layout` into `out` (cleared first).
378/// Allocation-free into a preallocated buffer; the per-frame half of the scene.
379///
380/// `lengths`, `widths` and `colors` are read **positionally, per element**, which
381/// is what lets a per-element binding vary any of them across the figure (Plan
382/// 0034 Phase 4). A short list falls back to a sane constant rather than
383/// panicking — which cannot happen, since all three are sized together at load,
384/// but keeps the hot path total.
385pub(crate) fn build(
386    layout: SpectrumLayout,
387    lengths: &[f32],
388    widths: &[f32],
389    colors: &[[f32; 3]],
390    place: Placement,
391    out: &mut Vec<SegmentInstance>,
392) {
393    out.clear();
394    if lengths.is_empty() {
395        return;
396    }
397    let (sin, cos) = place.rotation.sin_cos();
398    // The whole-figure rotation is applied here, at the point where world-space
399    // endpoints are emitted, so it composes with every layout identically.
400    let turn = |p: [f32; 2]| -> [f32; 2] { [p[0] * cos - p[1] * sin, p[0] * sin + p[1] * cos] };
401    let color_of = |i: usize| colors.get(i).copied().unwrap_or([1.0, 1.0, 1.0]);
402    let width_of = |i: usize| widths.get(i).copied().unwrap_or(0.01);
403
404    match layout {
405        SpectrumLayout::Bars => {
406            let step = 2.0 * place.span / lengths.len() as f32;
407            for (i, &length) in lengths.iter().enumerate() {
408                let x = -place.span + step * (i as f32 + 0.5);
409                out.push(SegmentInstance {
410                    a: turn([x, place.baseline]),
411                    b: turn([x, place.baseline + length]),
412                    color: color_of(i),
413                    width: width_of(i),
414                    alpha: 1.0,
415                    // Isolated: one segment per element, both ends free. Bars
416                    // must keep exactly their previous geometry, or a bar would
417                    // hang below `baseline` and break the centre-mirror.
418                    ext_a: 0.0,
419                    ext_b: 0.0,
420                });
421            }
422        }
423        SpectrumLayout::Polyline => {
424            // One point per element, spanning edge to edge, joined by n-1
425            // segments. A single element has no segment to draw, which is why
426            // the loader's minimum count is 2.
427            let gaps = lengths.len().saturating_sub(1);
428            if gaps == 0 {
429                return;
430            }
431            let step = 2.0 * place.span / gaps as f32;
432            let point = |i: usize, length: f32| -> [f32; 2] {
433                turn([-place.span + step * i as f32, place.baseline + length])
434            };
435            // Point `i` of the readout, for the neighbours a joint's interior
436            // angle needs. Out of range reads as a zero length, which the two
437            // free ends never consult.
438            let pt = |i: usize| point(i, lengths.get(i).copied().unwrap_or(0.0));
439            let mut prev = point(0, lengths.first().copied().unwrap_or(0.0));
440            for (i, &length) in lengths.iter().enumerate().skip(1) {
441                let next = point(i, length);
442                // Chained (ADR-0158): consecutive segments share a point, so
443                // every interior endpoint is a joint and reaches its corner's
444                // point by the miter the two arms subtend. Only the two ends of
445                // the whole figure are free — segment `i` runs from point
446                // `i - 1` to point `i`, so its `a` is joined for every segment
447                // but the first and its `b` for every segment but the last.
448                //
449                // The extension is resolved against **this segment's own**
450                // width: `width_of` is per element, so a neighbour's is not
451                // necessarily the same number.
452                let width = width_of(i);
453                let ext_a = if i > 1 {
454                    miter_extension(width, pt(i - 2), prev, next)
455                } else {
456                    0.0
457                };
458                let ext_b = if i < gaps {
459                    miter_extension(width, prev, next, pt(i + 1))
460                } else {
461                    0.0
462                };
463                out.push(SegmentInstance {
464                    a: prev,
465                    b: next,
466                    color: color_of(i),
467                    width,
468                    alpha: 1.0,
469                    ext_a,
470                    ext_b,
471                });
472                prev = next;
473            }
474        }
475        SpectrumLayout::RadialRing => {
476            // The frequency axis wrapped around the circle: element 0 points
477            // along +x and the rest follow counter-clockwise, each spoke running
478            // outward from the ring.
479            let n = lengths.len() as f32;
480            let inner = place.radius.max(0.0);
481            for (i, &length) in lengths.iter().enumerate() {
482                let angle = place.rotation + std::f32::consts::TAU * i as f32 / n;
483                let (s, c) = angle.sin_cos();
484                let outer = inner + length;
485                out.push(SegmentInstance {
486                    a: [c * inner, s * inner],
487                    b: [c * outer, s * outer],
488                    color: color_of(i),
489                    width: width_of(i),
490                    alpha: 1.0,
491                    // Isolated, like the bars: a spoke that extended inward
492                    // would grow through `radius` and fill the inner circle.
493                    ext_a: 0.0,
494                    ext_b: 0.0,
495                });
496            }
497        }
498    }
499}
500
501/// The spectrum readout: N elements driven by the analysis frame's band array,
502/// drawn through the shared line renderer.
503pub struct SpectrumScene {
504    /// The single line renderer, shared with the other line scenes (ADR-0007:
505    /// "one line renderer"). Only the active scene draws in a frame.
506    renderer: Rc<RefCell<LineRenderer>>,
507    /// The drawn geometry, after the mirror. Preallocated to the cap.
508    segments: Vec<SegmentInstance>,
509    /// The single (pre-mirror) figure, replicated into
510    /// [`segments`](Self::segments). Preallocated to the cap.
511    single_buf: Vec<SegmentInstance>,
512    /// The active tier's segment ceiling
513    /// ([`TierConfig::max_segments`](crate::render::TierConfig::max_segments)),
514    /// resolved once at construction (Plan 0044). A field rather than a constant
515    /// so the tier can raise it; the buffers above are preallocated to it, which
516    /// is what keeps the per-frame replication allocation-free.
517    max_segments: usize,
518    /// Set when this frame's mirror replication overflowed the cap.
519    mirror_overflow: Option<CapOverflow>,
520    /// This frame's downsampled band levels, before easing.
521    raw_levels: Vec<f32>,
522    /// The **held** (eased) levels actually drawn — the per-element envelope
523    /// state. Sized at load beside [`raw_levels`](Self::raw_levels).
524    levels: Vec<f32>,
525    /// Per-element stroke colour, rebuilt each frame into a buffer sized at load.
526    colors: Vec<[f32; 3]>,
527    /// Per-element world-space length, rebuilt each frame. Sized at load.
528    lengths: Vec<f32>,
529    /// Per-element stroke half-width, rebuilt each frame. Sized at load.
530    widths: Vec<f32>,
531    /// Per-element binding overrides (Plan 0034 Phase 4), one row per
532    /// [`SERIES_PARAMS`] entry, each sized at load.
533    series: [Vec<f32>; SERIES_PARAMS.len()],
534    /// Which rows this frame's bindings actually wrote. Cleared in
535    /// `reset_params` alongside the scalars, so a series never outlives the frame
536    /// that produced it — the same lifetime rule every other param follows.
537    series_active: [bool; SERIES_PARAMS.len()],
538    /// The figure, from `[spectrum] layout`.
539    layout: SpectrumLayout,
540    /// Per-element easing, from `[spectrum] smoothing`.
541    easing: Easing,
542    /// Real elapsed seconds for this frame, injected through
543    /// [`advance`](Scene::advance) — what makes the easing frame-rate
544    /// independent (ADR-0019).
545    dt: f32,
546    /// The preset's baked colour LUT (ADR-0021), sampled on the CPU per element.
547    palette: Palette,
548    thickness: f32,
549    /// The shared palette knobs (ADR-0021).
550    colour: common::PaletteParams,
551    /// The shared view transform (ADR-0018).
552    pan: common::PanParams,
553    hue_spread: f32,
554    glow: f32,
555    softness: f32,
556    /// Whether this figure draws through the **opacity-preserving** seam
557    /// rather than the additive one, from `stroke_blend` (ADR-0138).
558    ///
559    /// At or above [`OPAQUE_BLEND`](super::OPAQUE_BLEND) the whole batch
560    /// composites over: a stroke laid on another replaces the interior of what
561    /// it covers instead of summing with it, so a quantized palette keeps its
562    /// plateaus. Below it the batch is additive light. `0` is the default, so a
563    /// preset that does not bind this draws exactly what it drew.
564    stroke_blend: f32,
565    scale: f32,
566    base: f32,
567    curve: f32,
568    radius: f32,
569    span: f32,
570    baseline: f32,
571    rotation: f32,
572    zoom: f32,
573    mirror_order: f32,
574    mirror_reflect: f32,
575}
576
577impl SpectrumScene {
578    /// Build the scene over the shared line renderer, preallocating its segment
579    /// buffers to the cap. The element buffers are sized by `configure`, which
580    /// the renderer runs on every preset switch.
581    pub fn new(renderer: Rc<RefCell<LineRenderer>>, max_segments: usize) -> Self {
582        Self {
583            renderer,
584            segments: Vec::with_capacity(max_segments),
585            single_buf: Vec::with_capacity(max_segments),
586            max_segments,
587            mirror_overflow: None,
588            raw_levels: Vec::new(),
589            levels: Vec::new(),
590            colors: Vec::new(),
591            lengths: Vec::new(),
592            widths: Vec::new(),
593            series: Default::default(),
594            series_active: [false; SERIES_PARAMS.len()],
595            layout: SpectrumLayout::default(),
596            easing: Easing::INSTANT,
597            dt: 0.0,
598            // Replaced by the preset's palette on the next switch; the default
599            // is the engine cosine, so an unconfigured scene still colours.
600            palette: Palette::default_spectrum(),
601            thickness: DEFAULT_THICKNESS,
602            colour: common::PaletteParams::new(DEFAULT_HUE, DEFAULT_BRIGHTNESS),
603            pan: common::PanParams::default(),
604            hue_spread: DEFAULT_HUE_SPREAD,
605            glow: DEFAULT_GLOW,
606            softness: super::DEFAULT_SOFTNESS,
607            stroke_blend: super::ADDITIVE_BLEND,
608            scale: DEFAULT_SCALE,
609            base: DEFAULT_BASE,
610            curve: DEFAULT_CURVE,
611            radius: DEFAULT_RADIUS,
612            span: DEFAULT_SPAN,
613            baseline: DEFAULT_BASELINE,
614            rotation: DEFAULT_ROTATION,
615            zoom: DEFAULT_ZOOM,
616            mirror_order: DEFAULT_MIRROR_ORDER,
617            mirror_reflect: DEFAULT_MIRROR_REFLECT,
618        }
619    }
620
621    /// Resize the per-element buffers and clear the envelope state. Off the hot
622    /// path (preset load only) — the scratch every frame writes into is sized
623    /// here, never per frame.
624    fn resize(&mut self, elements: usize) {
625        self.raw_levels.clear();
626        self.raw_levels.resize(elements, 0.0);
627        // Cleared rather than resized in place: a preset switch must not show
628        // the previous preset's envelope decaying under the new one.
629        self.levels.clear();
630        self.levels.resize(elements, 0.0);
631        self.colors.clear();
632        self.colors.resize(elements, [0.0; 3]);
633        self.lengths.clear();
634        self.lengths.resize(elements, 0.0);
635        self.widths.clear();
636        self.widths.resize(elements, 0.0);
637        for row in &mut self.series {
638            row.clear();
639            row.resize(elements, 0.0);
640        }
641        self.series_active = [false; SERIES_PARAMS.len()];
642    }
643
644    /// Element `i`'s value for series row `row`, or `fallback` when no binding
645    /// drove that row this frame. Total — an out-of-range row or element reads
646    /// the fallback rather than panicking on the hot path.
647    fn series_value(&self, row: usize, i: usize, fallback: f32) -> f32 {
648        if !self.series_active.get(row).copied().unwrap_or(false) {
649            return fallback;
650        }
651        self.series
652            .get(row)
653            .and_then(|values| values.get(i))
654            .copied()
655            .unwrap_or(fallback)
656    }
657}
658
659impl SeriesBound for SpectrumScene {
660    fn set_param_series(&mut self, name: &str, values: &[f32]) {
661        let Some(row) = SERIES_PARAMS.iter().position(|&p| p == name) else {
662            // Not a per-element parameter on this scene (the whole-figure ones:
663            // radius, rotation, the view transform, the mirror, hue_spread,
664            // palette_mix, saturation). Fall back to the element-0 value rather
665            // than dropping the binding — the same reading the caller gives a
666            // scene with no per-element surface at all.
667            if let Some(&first) = values.first() {
668                self.set_param(name, first);
669            }
670            return;
671        };
672        let (Some(dst), Some(active)) = (self.series.get_mut(row), self.series_active.get_mut(row))
673        else {
674            return; // unreachable: `position` returned an in-range row
675        };
676        // Copy rather than borrow: the caller's slice is the renderer's scratch,
677        // reused by the next binding. `n` is the overlap, so a scratch sized for
678        // a different element count can neither overrun nor leave stale values
679        // beyond it (the rest of the row keeps whatever it held; only the first
680        // `n` are read, because `update` walks `lengths`, which is `n` long).
681        let n = dst.len().min(values.len());
682        if let (Some(dst), Some(src)) = (dst.get_mut(..n), values.get(..n)) {
683            dst.copy_from_slice(src);
684        }
685        *active = n > 0;
686    }
687}
688
689impl Scene for SpectrumScene {
690    fn name(&self) -> &'static str {
691        "spectrum"
692    }
693
694    fn advance(&mut self, dt: f32) {
695        self.dt = dt;
696    }
697
698    fn reset_params(&mut self) {
699        self.thickness = DEFAULT_THICKNESS;
700        self.colour.reset();
701        self.pan.reset();
702        self.hue_spread = DEFAULT_HUE_SPREAD;
703        self.glow = DEFAULT_GLOW;
704        self.softness = super::DEFAULT_SOFTNESS;
705        self.stroke_blend = super::ADDITIVE_BLEND;
706        self.scale = DEFAULT_SCALE;
707        self.base = DEFAULT_BASE;
708        self.curve = DEFAULT_CURVE;
709        self.radius = DEFAULT_RADIUS;
710        self.span = DEFAULT_SPAN;
711        self.baseline = DEFAULT_BASELINE;
712        self.rotation = DEFAULT_ROTATION;
713        self.zoom = DEFAULT_ZOOM;
714        self.mirror_order = DEFAULT_MIRROR_ORDER;
715        self.mirror_reflect = DEFAULT_MIRROR_REFLECT;
716        // A per-element series lives exactly one frame, like every scalar above:
717        // the rows keep their storage (sized at load) but stop being read until
718        // a binding writes them again.
719        self.series_active = [false; SERIES_PARAMS.len()];
720    }
721
722    fn set_param(&mut self, name: &str, value: f32) {
723        // The shared param blocks first, this scene's own names after
724        // (`scenes::common`).
725        if self.colour.set(name, value) || self.pan.set(name, value) {
726            return;
727        }
728        match name {
729            "base" => self.base = value,
730            "scale" => self.scale = value,
731            "curve" => self.curve = value,
732            "radius" => self.radius = value,
733            "span" => self.span = value,
734            "baseline" => self.baseline = value,
735            "rotation" => self.rotation = value,
736            "thickness" => self.thickness = value,
737            "hue_spread" => self.hue_spread = value,
738            "glow" => self.glow = value,
739            "softness" => self.softness = value,
740            "stroke_blend" => self.stroke_blend = value,
741            "zoom" => self.zoom = value,
742            "mirror_order" => self.mirror_order = value,
743            "mirror_reflect" => self.mirror_reflect = value,
744            _ => {}
745        }
746    }
747
748    fn as_series_bound(&mut self) -> Option<&mut dyn SeriesBound> {
749        Some(self)
750    }
751
752    fn set_palette(&mut self, palette: &Palette) {
753        self.palette = palette.clone();
754    }
755
756    fn configure(&mut self, cfg: &GeneratorConfig) -> Option<CapOverflow> {
757        // This scene's own variant and no other. A new variant has to be
758        // acknowledged in exactly one place -- `GeneratorConfig::element_count`,
759        // which is exhaustive and must answer for every variant -- rather than
760        // in every scene that does not consume it.
761        if let GeneratorConfig::Spectrum {
762            elements,
763            layout,
764            easing,
765        } = cfg
766        {
767            self.layout = *layout;
768            self.easing = *easing;
769            self.resize(*elements);
770        }
771        // Nothing is built here — the element count is validated at load and is
772        // orders of magnitude under the segment cap — so nothing truncates.
773        None
774    }
775
776    fn mirror_overflow(&self) -> Option<&CapOverflow> {
777        self.mirror_overflow.as_ref()
778    }
779
780    fn update(&mut self, frame: &AnalysisFrame) {
781        downsample(&frame.spectrum, &mut self.raw_levels);
782        // `downsample -> curve -> ease`, in that order, which is ADR-0040's
783        // decision and not an incidental arrangement of two lines: the smoother's
784        // state **is** the displayed quantity, so a fall's time constant is
785        // exactly the `release` the preset wrote, at every value of `curve`.
786        // Easing first would have made the effective release `release / curve` —
787        // engaging `curve = 0.5` would silently double every fall time, and a
788        // fall to a non-zero floor would stop being exponential at all, so
789        // `release` would name no duration.
790        //
791        // ADR-0040 originally argued this ordering bought a perceptually *even*
792        // fall. Plan 0038 Phase 3 measured that and it is false — both orderings
793        // are exponentials of identical shape and differ only in speed. See the
794        // ADR's Outcome section; the ordering survives on the reason above.
795        //
796        // The easing itself is per element on the injected real `dt`, through the
797        // same `Easing` the `[smoothing]` table uses (ADR-0035) — so "0.2
798        // seconds" means the same thing here as on a binding, at any frame rate.
799        // The default is `INSTANT`, which passes the curved level straight
800        // through, and the default `curve` is `1.0`, which is the identity.
801        let (easing, dt) = (self.easing, self.dt);
802        for i in 0..self.levels.len() {
803            let raw = self.raw_levels.get(i).copied().unwrap_or(0.0);
804            let shaped = curve_level(raw, self.series_value(SERIES_CURVE, i, self.curve));
805            if let Some(held) = self.levels.get_mut(i) {
806                *held = easing.step(*held, shaped, dt);
807            }
808        }
809
810        // Per-element geometry and colour. Each scalar below reads its own series
811        // row when a binding drove one this frame (Plan 0034 Phase 4) and the
812        // whole-figure param otherwise — so `thickness = "0.01 + bin(index) * 5"`
813        // varies the stroke across the figure while `thickness = "2"` does not,
814        // with no branch in the preset and no second code path here.
815        let count = self.levels.len();
816        let span = count.max(1) as f32;
817        for i in 0..count {
818            let level = self.levels.get(i).copied().unwrap_or(0.0);
819            let base = self.series_value(SERIES_BASE, i, self.base);
820            let scale = self.series_value(SERIES_SCALE, i, self.scale);
821            let thickness = self.series_value(SERIES_THICKNESS, i, self.thickness);
822            let brightness = self.series_value(SERIES_BRIGHTNESS, i, self.colour.brightness);
823            // `hue_spread` walks the palette along the axis on top of whatever
824            // `hue` is — at the default spread of 0 the figure is one hue, at 1
825            // it spans the palette from the lowest element to the highest.
826            let hue = self.series_value(SERIES_HUE, i, self.colour.hue)
827                + self.hue_spread * (i as f32 / span);
828            // Hard bands on the palette coordinate (ADR-0078), the canonical
829            // `palette::band_coord` called rather than copied. `palette_steps <= 1`
830            // returns it untouched, so an unbound preset is byte-unchanged.
831            let banded = palette::band_coord(hue, self.colour.steps);
832            let rgb = desaturate(
833                self.palette.sample(banded, self.colour.mix),
834                self.colour.saturation,
835            );
836
837            if let Some(slot) = self.lengths.get_mut(i) {
838                *slot = element_length(level, base, scale);
839            }
840            if let Some(slot) = self.widths.get_mut(i) {
841                *slot = super::half_width(thickness);
842            }
843            if let Some(slot) = self.colors.get_mut(i) {
844                *slot = [
845                    rgb[0] * brightness,
846                    rgb[1] * brightness,
847                    rgb[2] * brightness,
848                ];
849            }
850        }
851
852        let place = Placement {
853            radius: self.radius,
854            span: self.span,
855            baseline: self.baseline,
856            rotation: self.rotation,
857        };
858        build(
859            self.layout,
860            &self.lengths,
861            &self.widths,
862            &self.colors,
863            place,
864            &mut self.single_buf,
865        );
866
867        let mirror = MirrorSpec::from_params(self.mirror_order, self.mirror_reflect);
868        if mirror.is_identity() {
869            // Identity replication would copy the whole set to produce exactly
870            // what it was given; swap instead (Plan 0031 Phase 4). Both buffers
871            // are preallocated to the cap, so neither can grow later.
872            std::mem::swap(&mut self.single_buf, &mut self.segments);
873            self.mirror_overflow = None;
874            return;
875        }
876        let dropped = replicate_mirror(
877            &self.single_buf,
878            mirror,
879            self.max_segments,
880            &mut self.segments,
881        );
882        self.mirror_overflow = (dropped > 0).then_some(CapOverflow {
883            dropped,
884            context: OverflowContext::Mirror(mirror.order),
885            cap: self.max_segments,
886        });
887    }
888
889    fn render(
890        &mut self,
891        queue: &wgpu::Queue,
892        encoder: &mut wgpu::CommandEncoder,
893        view: &wgpu::TextureView,
894        aspect: f32,
895    ) {
896        let xform = ViewTransform {
897            zoom: self.zoom,
898            pan: [self.pan.x, self.pan.y],
899            _pad: 0.0,
900        };
901        let mut renderer = self.renderer.borrow_mut();
902        if self.stroke_blend >= super::OPAQUE_BLEND {
903            renderer.draw_opaque(
904                queue,
905                encoder,
906                view,
907                aspect,
908                self.glow,
909                self.softness,
910                StrokeMetric::World,
911                xform,
912                &self.segments,
913                &[],
914            );
915        } else {
916            renderer.draw(
917                queue,
918                encoder,
919                view,
920                aspect,
921                self.glow,
922                self.softness,
923                StrokeMetric::World,
924                xform,
925                &self.segments,
926            );
927        }
928    }
929}
930
931#[cfg(test)]
932mod tests;