mirror of
https://github.com/got-feedBack/feedBack.git
synced 2026-07-21 12:21:49 +00:00
18 functions, 1,245 lines. highway.js 3,972 -> 2,727 (-31%). The biggest R3c slice: notes,
sustains, chords, strum groups, unison bends and lyrics — everything the default renderer
paints each frame.
━━━ MUTABILITY, NOT LOCATION, DECIDES WHERE A THING BELONGS ━━━
Three per-instance caches came out with this slice, and they are why it needed care:
_frameMismatchWarned a warn-once Set of chord ids (feedBack#88)
_chordRenderInfo a WeakMap of chord -> chain info
_lyricMeasureCache Map<fontSize, Map<text, width>>
All three are MUTATED. Left at module scope they would be SHARED ACROSS PANELS — one
highway's lyric widths and chord chains stomping another's, silently, with nothing throwing.
createHighway() is a factory (the constitution publishes window.createHighway so a plugin can
build a second highway), so they are lifted onto hwState, which is exactly what hwState is for.
The shimmer LUT went the OTHER way — to MODULE scope in highway-geometry.js. It is a
deterministic xorshift table, byte-for-byte identical for every instance, so sharing it is not
merely safe but BETTER: built once for the page rather than once per panel.
Same slice, opposite directions, decided entirely by whether the thing mutates.
━━━ MY SCRIPT WAS WRONG TWICE. THE GATES CAUGHT BOTH. ━━━
1. HAND-LISTED THE MOVE SET. I listed 10 functions and missed six that drawChords needs
(_ensureChordRenderCache, bsearchChords, getChordTemplateInfo, _computeChordBox,
_updateFretLinePreview, _drawFretLineChordPreview). The no-undef gate named every one. The
set is now DERIVED from the dependency closure — 18, not 10.
2. JUDGED PURITY TOO EARLY, and this one is subtle. I classified _computeChordBox as pure
because its ORIGINAL body never mentions hwState. Then the call-site rewriter injected
`fretX(hwState, …)` INTO it — fretX takes hwState now (#916) — leaving a function that
references an hwState it was never given. Purity has to be judged from the body AS IT WILL
BE, so the classifier iterates to a fixed point: a function needs hwState if it mentions it,
OR calls anything that now takes it. That moved _computeChordBox to the stateful side.
VERIFIED. A/B against origin/main: IDENTICAL, zero page errors. The PLUGIN BUNDLE contract is
byte-identical (b.fretX arity 3, b.getNoteState arity 2, both stable references, both correct
under the old calling convention). PERF GATE PASSES AT 1.92ms against its 12ms budget — and
this is the slice that could really have cost something: the ENTIRE per-frame drawing path is
now cross-module. It costs nothing measurable.
TESTS. highway_teaching_marks follows strumGroupBuckets to the new module. The two source-shape
harnesses now read highway.js AND every static/js/highway-*.js, rather than being re-pinned at
whichever file currently holds a function — re-pinning breaks again next time, and a shape
assertion that silently stops finding its target is indistinguishable from one that passes.
node 1045, pytest 2416, ESLint 0, no-undef 0, Codex 0.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
104 lines
4.8 KiB
JavaScript
104 lines
4.8 KiB
JavaScript
// highway.js's PURE geometry + label primitives.
|
|
//
|
|
// Every function here is a pure function of its arguments. None of them touches hwState, and
|
|
// none closes over the canvas context — roundRect() already took `ctx` explicitly, and the
|
|
// rest need nothing but numbers. project() reads only the module-level constants from
|
|
// ./highway-constants.js.
|
|
//
|
|
// THAT PURITY IS WHY THIS SLICE IS SAFE, and why it is the one to do first. createHighway() is
|
|
// a FACTORY — a plugin can build a second highway for its own panel — so anything holding
|
|
// per-instance state (hwState) must be passed it as an argument rather than importing it, or
|
|
// two panels silently share one clock and palette. These six hold no state at all, so they
|
|
// move VERBATIM: not one call site changes.
|
|
//
|
|
// The primitives that DO need hwState (fretX, fillTextReadable, _noteState, _paintGemGlow)
|
|
// are deliberately left behind. They need an explicit hwState parameter threaded through 53
|
|
// call sites, which is a real change and belongs in its own commit, not smuggled in beside a
|
|
// provably-identical move.
|
|
import { VISIBLE_SECONDS, Z_CAM, Z_MAX, _SHIMMER_LUT_SIZE } from './highway-constants.js';
|
|
|
|
// ── Projection ───────────────────────────────────────────────────────
|
|
export function project(tOffset) {
|
|
if (tOffset > VISIBLE_SECONDS || tOffset < -0.05) return null;
|
|
if (tOffset < 0) return { y: 0.82 + Math.abs(tOffset) * 0.3, scale: 1.0 };
|
|
|
|
const z = tOffset * (Z_MAX / VISIBLE_SECONDS);
|
|
const denom = z + Z_CAM;
|
|
if (denom < 0.01) return null;
|
|
const scale = Z_CAM / denom;
|
|
const y = 0.82 + (0.08 - 0.82) * (1.0 - scale);
|
|
return { y, scale };
|
|
}
|
|
|
|
export function bnvNormalizedPoints(bnv, sus) {
|
|
if (!Array.isArray(bnv) || bnv.length === 0) return [];
|
|
// Map each point's time over the NOTE's span [0, sus] so it sits at its
|
|
// real fraction of the note (a bend that completes before the note ends
|
|
// draws short of the glyph's right edge). Fall back to the curve's own
|
|
// t-range only when the note has no usable sustain.
|
|
if (Number.isFinite(sus) && sus > 0) {
|
|
return bnv.map(p => ({ x: Math.min(Math.max(p.t / sus, 0), 1), v: p.v }));
|
|
}
|
|
const t0 = bnv[0].t;
|
|
const span = bnv[bnv.length - 1].t - t0;
|
|
return bnv.map(p => ({ x: span > 0 ? (p.t - t0) / span : 0, v: p.v }));
|
|
}
|
|
|
|
export function teachingFingerLabel(fg) {
|
|
if (!Number.isInteger(fg) || fg < 0 || fg > 4) return '';
|
|
return fg === 0 ? 'T' : String(fg);
|
|
}
|
|
|
|
export function teachingDegreeLabel(sd) {
|
|
if (!Number.isInteger(sd) || sd < 0 || sd > 11) return '';
|
|
return String(sd);
|
|
}
|
|
|
|
export function chordHarmonyLabels(fn, voicing, caged, guideTones) {
|
|
const rn = (fn && typeof fn.rn === 'string') ? fn.rn.trim() : '';
|
|
const vc = (typeof voicing === 'string') ? voicing.trim() : '';
|
|
const cg = (typeof caged === 'string' && /^[CAGED]$/.test(caged.trim()))
|
|
? 'CAGED: ' + caged.trim() : '';
|
|
const gt = Array.isArray(guideTones)
|
|
? guideTones.filter(n => Number.isInteger(n) && n >= 0 && n <= 11) : [];
|
|
return { rn, voicing: vc, caged: cg, guideTones: gt.length ? 'gt ' + gt.join(',') : '' };
|
|
}
|
|
|
|
export function roundRect(ctx, x, y, w, h, r) {
|
|
ctx.beginPath();
|
|
ctx.moveTo(x + r, y);
|
|
ctx.lineTo(x + w - r, y);
|
|
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
|
|
ctx.lineTo(x + w, y + h - r);
|
|
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
|
|
ctx.lineTo(x + r, y + h);
|
|
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
|
|
ctx.lineTo(x, y + r);
|
|
ctx.quadraticCurveTo(x, y, x + r, y);
|
|
ctx.closePath();
|
|
}
|
|
|
|
|
|
// ── The shimmer noise LUT ───────────────────────────────────────────────────────
|
|
//
|
|
// A DETERMINISTIC xorshift table: no randomness, no state, byte-for-byte identical for every
|
|
// highway instance. Unlike the three per-instance caches that came out of the drawing layer (a
|
|
// warn-once Set, a chord WeakMap, a lyric-width Map — all MUTATED, all lifted onto hwState so
|
|
// two panels cannot stomp each other), this one is not merely SAFE to share but BETTER shared:
|
|
// built once for the page instead of once per panel.
|
|
//
|
|
// MUTABILITY, NOT LOCATION, IS WHAT DECIDES WHERE A THING BELONGS.
|
|
const _shimmerLut = new Float32Array(_SHIMMER_LUT_SIZE);
|
|
for (let i = 0; i < _SHIMMER_LUT_SIZE; i++) {
|
|
let x = (i + 1) | 0; // +1 dodges the all-zero xorshift trap
|
|
x ^= x << 13;
|
|
x ^= x >>> 17;
|
|
x ^= x << 5;
|
|
_shimmerLut[i] = (x >>> 0) / 4294967296;
|
|
}
|
|
|
|
export function _shimmerNoise(seed) {
|
|
// Mask works only because _SHIMMER_LUT_SIZE is a power of two.
|
|
return _shimmerLut[(seed >>> 0) & (_SHIMMER_LUT_SIZE - 1)];
|
|
}
|