Skip to content

Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.

music v2: mix-chain DSP (stem EQ, oversampled saturation, auto-threshold compressor, equal-power stereo pan, per-band M/S width, smooth true-peak limiter), three reverbs (modulated diffused FDN, Dattorro plate, synthesized-IR convolution), per-style mix/space/feel profiles

Alex Greenshpunff3dfa8a366e
diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts
new file mode 100644
index 0000000..95a165a
--- /dev/null
+++ b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts
@@ -0,0 +1,144 @@
+// Mix-chain building blocks (sound v2). Pure functions over whole float buffers, deterministic,
+// no rng. Written from the textbook forms: RBJ cookbook EQ (dsp.ts), feed-forward soft-knee
+// compressor (Giannoulis, Massberg and Reiss, JAES 2012), halfband 2x oversampling, Linkwitz-Riley
+// crossovers, the Web Audio "StereoPannerNode" equal-power law for stereo sources, and a
+// hold + box-filter look-ahead limiter (the Signalsmith "smooth limiter" construction).
+import { Biquad, db } from "./dsp";
+
+const lin2db = (x: number) => 20 * Math.log10(Math.max(x, 1e-12));
+
+// ---------------------------------------------------------------- EQ
+/** A stem's EQ: 12 dB/oct high-pass and low-pass, a tilt around 1 kHz (dB from the low end to the high end), bells [f, q, dB]. */
+export type StemEq = { hp?: number; lp?: number; tilt?: number; bells?: [number, number, number][]; air?: number };
+export const eqChain = (sr: number, e: StemEq): Biquad[] => {
+ const fs: Biquad[] = [], ny = sr * 0.45;
+ if (e.hp && e.hp > 10) fs.push(Biquad.make(sr, "hp", e.hp, 0.7071));
+ if (e.tilt) { fs.push(Biquad.make(sr, "lowshelf", 1000, 0.5, -e.tilt / 2)); fs.push(Biquad.make(sr, "highshelf", 1000, 0.5, e.tilt / 2)); }
+ for (const [f, q, g] of e.bells ?? []) if (f < ny && g) fs.push(Biquad.make(sr, "peak", f, q, g));
+ if (e.air && 10000 < ny) fs.push(Biquad.make(sr, "highshelf", 10000, 0.7, e.air));
+ if (e.lp && e.lp < ny) fs.push(Biquad.make(sr, "lp", e.lp, 0.7071));
+ return fs;
+};
+export const runEq = (buf: Float32Array, fs: Biquad[]) => { for (const f of fs) f.run(buf); return buf; };
+
+// ---------------------------------------------------------------- pan
+/** True stereo pan (Web Audio StereoPanner, equal power): pan 0 is the identity; a panned stereo source keeps its power. */
+export const stereoPan = (L: Float32Array, R: Float32Array, p: number) => {
+ if (!p) return;
+ const q = Math.max(-1, Math.min(1, p)), x = q <= 0 ? q + 1 : q, gl = Math.cos((x * Math.PI) / 2), gr = Math.sin((x * Math.PI) / 2);
+ for (let i = 0; i < L.length; i++) {
+ const l = L[i], r = R[i];
+ if (q <= 0) { L[i] = l + r * gl; R[i] = r * gr; } else { L[i] = l * gl; R[i] = r + l * gr; }
+ }
+};
+
+// ---------------------------------------------------------------- oversampled saturation
+/** Halfband FIR (odd taps only + centre 0.5), 2x up/down. Kaiser-ish window, ~80 dB image rejection above 0.6 Nyquist. */
+const HB = (() => { const M = 15, h: number[] = []; for (let k = 1; k <= M; k += 2) { const w = 0.42 + 0.5 * Math.cos((Math.PI * k) / (M + 1)) + 0.08 * Math.cos((2 * Math.PI * k) / (M + 1)); h.push((0.5 * Math.sin((Math.PI * k) / 2)) / ((Math.PI * k) / 2) * w); } return h; })(); // h[j] is the tap at k = 2j+1 (and -k)
+/** Run `f` on a 2x-oversampled copy of x (in place). */
+export const oversample2 = (x: Float32Array, f: (v: number) => number) => {
+ const n = x.length, H = HB.length, up = new Float64Array(2 * n);
+ for (let i = 0; i < n; i++) {
+ up[2 * i] = f(x[i]);
+ let s = 0; for (let j = 0; j < H; j++) { const k = 2 * j + 1, a = i + ((1 - k) >> 1), b = i + ((1 + k) >> 1); s += HB[j] * ((a >= 0 && a < n ? x[a] : 0) + (b >= 0 && b < n ? x[b] : 0)); }
+ up[2 * i + 1] = f(2 * s);
+ }
+ for (let i = 0; i < n; i++) {
+ const m = 2 * i; let s = 0.5 * up[m];
+ for (let j = 0; j < H; j++) { const k = 2 * j + 1; s += HB[j] * ((m - k >= 0 ? up[m - k] : 0) + (m + k < 2 * n ? up[m + k] : 0)); }
+ x[i] = s;
+ }
+ return x;
+};
+/** Asymmetric tanh saturation, unity small-signal gain; `bias` adds even harmonics (tape/tube). DC removed after. */
+export const saturate = (x: Float32Array, sr: number, drive: number, bias = 0, mix = 1) => {
+ if (drive <= 0 || mix <= 0) return x;
+ const d = drive, t0 = Math.tanh(d * bias), g0 = d * (1 - t0 * t0), dry = mix < 1 ? Float32Array.from(x) : null;
+ oversample2(x, (v) => (Math.tanh(d * (v + bias)) - t0) / g0);
+ // DC blocker (5 Hz)
+ const R = Math.exp((-2 * Math.PI * 5) / sr); let x1 = 0, y1 = 0; for (let i = 0; i < x.length; i++) { const y = x[i] - x1 + R * y1; x1 = x[i]; y1 = y; x[i] = y; }
+ if (dry) for (let i = 0; i < x.length; i++) x[i] = dry[i] + (x[i] - dry[i]) * mix;
+ return x;
+};
+
+// ---------------------------------------------------------------- compressor
+export type CompOpts = { ratio: number; attackMs: number; releaseMs: number; kneeDb?: number; /** target gain reduction on the loud passages (p90 of GR while active); the threshold is solved for it */ targetGrDb: number; maxGrDb?: number };
+/** Stereo-linked feed-forward compressor with auto threshold (solved so the loud passages see `targetGrDb`) and RMS makeup. Returns the measured GR stats. */
+export const compress = (L: Float32Array, R: Float32Array, sr: number, o: CompOpts) => {
+ const n = L.length, W = o.kneeDb ?? 6, slope = 1 / o.ratio - 1;
+ // detector: peak-ish level in dB (fast 1 ms attack, 40 ms release on the absolute value)
+ const det = new Float32Array(n), aA = Math.exp(-1 / (0.001 * sr)), aR = Math.exp(-1 / (0.04 * sr)); let e = 0, mx = -200;
+ for (let i = 0; i < n; i++) { const a = Math.max(Math.abs(L[i]), Math.abs(R[i])); e = a > e ? aA * e + (1 - aA) * a : aR * e + (1 - aR) * a; det[i] = lin2db(e); if (det[i] > mx) mx = det[i]; }
+ if (mx < -80) return { thresholdDb: 0, p90GrDb: 0, maxGrDb: 0 };
+ const gc = (x: number, T: number) => { const ov = x - T; if (2 * ov < -W) return 0; if (2 * Math.abs(ov) <= W) return (slope * (ov + W / 2) ** 2) / (2 * W); return slope * ov; };
+ const ga = Math.exp(-1 / ((o.attackMs / 1000) * sr)), gr = Math.exp(-1 / ((o.releaseMs / 1000) * sr));
+ const run = (T: number, out?: Float32Array, step = 1) => {
+ let g = 0; const act: number[] = [], ga2 = Math.pow(ga, step), gr2 = Math.pow(gr, step);
+ for (let i = 0; i < n; i += step) { const c = gc(det[i], T); g = c < g ? ga2 * g + (1 - ga2) * c : gr2 * g + (1 - gr2) * c; if (out) out[i] = g; if (det[i] > mx - 30) act.push(-g); }
+ act.sort((a, b) => a - b); return act.length ? act[Math.floor(act.length * 0.9)] : 0;
+ };
+ // bisection on the threshold: p90 GR is monotone in T
+ let lo = mx - 40, hi = mx + 1; const step = Math.max(1, Math.floor(sr / 6000));
+ for (let it = 0; it < 22; it++) { const mid = (lo + hi) / 2; if (run(mid, undefined, step) > o.targetGrDb) lo = mid; else hi = mid; }
+ const T = (lo + hi) / 2, gcurve = new Float32Array(n), p90 = run(T, gcurve);
+ let e0 = 0, e1 = 0, maxGr = 0;
+ for (let i = 0; i < n; i++) { const gg = Math.max(gcurve[i], -(o.maxGrDb ?? 12)); if (-gg > maxGr) maxGr = -gg; const k = db(gg); e0 += L[i] * L[i] + R[i] * R[i]; L[i] *= k; R[i] *= k; e1 += L[i] * L[i] + R[i] * R[i]; }
+ const mk = e1 > 0 ? Math.sqrt(e0 / e1) : 1; for (let i = 0; i < n; i++) { L[i] *= mk; R[i] *= mk; }
+ return { thresholdDb: T, p90GrDb: p90, maxGrDb: maxGr };
+};
+
+// ---------------------------------------------------------------- width
+/** Linkwitz-Riley 4th-order split of x into [low, high] at f. */
+export const lr4 = (x: Float32Array, sr: number, f: number): [Float32Array, Float32Array] => {
+ const lo = Float32Array.from(x), hi = Float32Array.from(x);
+ for (let k = 0; k < 2; k++) { Biquad.make(sr, "lp", f, 0.7071).run(lo); Biquad.make(sr, "hp", f, 0.7071).run(hi); }
+ return [lo, hi];
+};
+/** Per-band M/S width: the side below `monoHz` is removed (mono lows), `mid` scales the side from monoHz to `splitHz`, `high` above it. */
+export const bandWidth = (L: Float32Array, R: Float32Array, sr: number, o: { monoHz: number; splitHz: number; mid: number; high: number }) => {
+ const n = L.length, M = new Float32Array(n), S = new Float32Array(n);
+ for (let i = 0; i < n; i++) { M[i] = 0.5 * (L[i] + R[i]); S[i] = 0.5 * (L[i] - R[i]); }
+ const [, above] = lr4(S, sr, o.monoHz), [sMid, sHigh] = lr4(above, sr, o.splitHz);
+ for (let i = 0; i < n; i++) { const s = sMid[i] * o.mid + sHigh[i] * o.high; L[i] = M[i] + s; R[i] = M[i] - s; }
+};
+
+// ---------------------------------------------------------------- limiter
+/** Inter-sample peak estimate per sample (4x windowed sinc, same kernel as meter.truePeak), skipped where the sample itself is far below `floor`. */
+const TP = (() => { const O = 4, T = 16, h: number[][] = []; for (let ph = 1; ph < O; ph++) { const row: number[] = []; for (let t = 0; t < T; t++) { const x = t - T / 2 + 1 - ph / O, w = 0.5 + 0.5 * Math.cos((Math.PI * x) / (T / 2)); row.push(x === 0 ? 1 : (Math.sin(Math.PI * x) / (Math.PI * x)) * w); } h.push(row); } return h; })();
+const ispPeak = (c: Float32Array, out: Float32Array, floor: number) => {
+ const n = c.length;
+ for (let i = 0; i < n; i++) {
+ let p = Math.abs(c[i]);
+ if (p > floor || (i + 1 < n && Math.abs(c[i + 1]) > floor)) for (const row of TP) { let s = 0; for (let t = 0; t < 16; t++) { const j = i + t - 7; if (j >= 0 && j < n) s += c[j] * row[t]; } const v = Math.abs(s); if (v > p) p = v; }
+ if (p > out[i]) out[i] = p;
+ }
+};
+/**
+ * Smooth true-peak limiter: 4x inter-sample detection, look-ahead hold (`lookMs`), instant-attack
+ * release (`releaseMs`), then a box filter the length of the look-ahead so the gain ramps in with no
+ * corners (no clicks, no brickwall buzz). The ramp is provably at or under the needed gain at every
+ * peak. Offline, so no output delay. Returns the maximum gain reduction in dB.
+ */
+export const smoothLimiter = (L: Float32Array, R: Float32Array, sr: number, ceil: number, lookMs = 5, releaseMs = 90) => {
+ const n = L.length, D = Math.max(2, Math.round((lookMs / 1000) * sr)), pk = new Float32Array(n);
+ ispPeak(L, pk, ceil * 0.5); ispPeak(R, pk, ceil * 0.5);
+ const need = new Float32Array(n); for (let i = 0; i < n; i++) need[i] = pk[i] > ceil ? ceil / pk[i] : 1;
+ // hold: m[j] = min(need[j-D .. j]) via a monotone deque
+ const m = new Float32Array(n), dq = new Int32Array(n); let h = 0, t = 0;
+ for (let j = 0; j < n; j++) { while (t > h && need[dq[t - 1]] >= need[j]) t--; dq[t++] = j; while (dq[h] < j - D) h++; m[j] = need[dq[h]]; }
+ // release (instant attack): never above m
+ const rc = 1 - Math.exp(-1 / ((releaseMs / 1000) * sr)); let r = 1;
+ for (let j = 0; j < n; j++) { r = Math.min(m[j], r + (1 - r) * rc); m[j] = r; }
+ // box filter over D, read D ahead: s at sample i averages m[i+1 .. i+D], all <= need[i]
+ let acc = 0, maxGr = 0; const at = (j: number) => (j < n ? m[j] : 1);
+ for (let j = 1; j <= D; j++) acc += at(j);
+ for (let i = 0; i < n; i++) { const g = acc / D; L[i] *= g; R[i] *= g; if (g < 1) maxGr = Math.max(maxGr, -lin2db(g)); acc += at(i + 1 + D) - at(i + 1); }
+ return maxGr;
+};
+
+/** RMS of a stereo pair over its samples above -70 dBFS (the active part). */
+export const activeRms = (L: Float32Array, R: Float32Array) => {
+ let e = 0, k = 0; const th = 1e-7;
+ for (let i = 0; i < L.length; i++) { const v = L[i] * L[i] + R[i] * R[i]; if (v > th) { e += v; k++; } }
+ return k ? Math.sqrt(e / (2 * k)) : 0;
+};
diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts b/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts
new file mode 100644
index 0000000..ccb4f41
--- /dev/null
+++ b/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts
@@ -0,0 +1,135 @@
+// Each style's MIX and SPACE profile and its FEEL (sound v2). Data, like tables.ts: a style names a
+// profile, a piece may override any field (Material.mix / Piece.mix). The engine reads these in
+// render.ts (EQ per stem, drum bus, glue, width, space, limiter) and perform.ts (feel).
+// Numbers come from the research doc (anidoodle-research/launch-film/research/music-synthesis-research.md
+// sections 3-5) and common mixing practice; every profile is `unconfirmed` until a human listen.
+import type { InstId, Role } from "./plan";
+import type { StyleId } from "./tables";
+import type { StemEq } from "./mixDsp";
+import type { Space } from "./mixReverb";
+
+/** What an instrument IS for the mixer. */
+export type EqClass = "kick" | "snare" | "hat" | "chipDrum" | "boom" | "bass" | "chip" | "piano" | "keys" | "pad" | "lead" | "pluck" | "bell" | "fx";
+export const EQ_CLASS: Record<InstId, EqClass> = {
+ kick: "kick", snare: "snare", hat: "hat", noiseDrum: "chipDrum", timpani: "boom", bass: "bass", sub: "bass", triangle: "chip", pulse: "chip",
+ piano: "piano", ePiano: "keys", organ: "keys", warmPad: "pad", strings: "pad", choir: "pad", brass: "lead", woodwind: "lead", leadSynth: "lead", bowedSolo: "lead",
+ guitar: "pluck", harp: "pluck", softPluck: "pluck", musicBox: "bell", bell: "bell", celesta: "bell", marimba: "bell", vibes: "bell", fmBell: "bell", vinyl: "fx",
+};
+/** Corrective EQ per class: clear the mud (200-500 Hz), give each thing its own band, roll off what it doesn't use. */
+export const CLASS_EQ: Record<EqClass, StemEq> = {
+ kick: { hp: 28, bells: [[62, 1.1, 1.5], [330, 1.3, -3], [3800, 1.2, 1.5]] },
+ snare: { hp: 90, bells: [[210, 1.2, 1], [850, 1.4, -1.5], [5500, 0.8, 1.5]] },
+ hat: { hp: 320, bells: [[9000, 0.9, 1]], lp: 17500 },
+ chipDrum: { hp: 60 },
+ boom: { hp: 26, bells: [[300, 1.2, -2]] },
+ bass: { hp: 30, bells: [[240, 1.1, -1.5], [900, 1.2, 1]], lp: 9000 },
+ chip: { hp: 40 },
+ piano: { hp: 30, bells: [[320, 1.0, -1], [3200, 0.9, 0.8]], air: 0.8 },
+ keys: { hp: 70, bells: [[330, 1.1, -1.5], [2400, 1, 0.8]], lp: 14000 },
+ pad: { hp: 110, bells: [[380, 1.0, -2], [3000, 0.8, 0.5]], lp: 13000 },
+ lead: { hp: 110, bells: [[300, 1.2, -1], [2600, 1.0, 1.2]] },
+ pluck: { hp: 85, bells: [[260, 1.2, -1.5], [4500, 0.8, 1.2]] },
+ bell: { hp: 170, bells: [[600, 1.0, -0.8]], lp: 15000 },
+ fx: { hp: 200 },
+};
+/** Default reverb send when a part names none (the legacy default was 1 for everything, bass included). */
+export const ROLE_SEND: Record<Role, number> = { melody: 0.5, inner: 0.5, accomp: 0.6, color: 0.7, bass: 0.1, drum: 0.25 };
+
+export type Lane = "kick" | "snare" | "ghost" | "hat" | "perc" | "bass" | "chords" | "lead" | "counter" | "arp";
+/**
+ * FEEL. `lanes`: a systematic offset per lane in ms (+ = late): the groove's shape, the same every
+ * bar. `humanize`: a phrase-correlated drift (AR(1), sigma ms, correlation per onset): the player
+ * leaning ahead or behind over a phrase. `jitter`: small uncorrelated error per note (sigma ms): the
+ * hand. Drums and pitched parts get their own. `hatDriftMs`: 1/f-ish wander of the hats (lo-fi).
+ * `velHumanize`/`velJitter`: the same split for velocity, relative (0.06 = +-6 %, about +-0.5 dB). `toneGamma`: Played.tone = v^gamma, the
+ * timbre hook every voice may read (brightness, attack, partial mix follow velocity).
+ */
+export type Feel = {
+ lanes: Partial<Record<Lane, number>>;
+ drum: { humanizeMs: number; corr: number; jitterMs: number }; pitched: { humanizeMs: number; corr: number; jitterMs: number };
+ hatDriftMs?: number; velHumanize: number; velJitter: number; toneGamma: number;
+};
+const F = (lanes: Feel["lanes"], drum: [number, number, number], pitched: [number, number, number], x: Partial<Feel> = {}): Feel => ({ lanes, drum: { humanizeMs: drum[0], corr: drum[1], jitterMs: drum[2] }, pitched: { humanizeMs: pitched[0], corr: pitched[1], jitterMs: pitched[2] }, velHumanize: 0.06, velJitter: 0.04, toneGamma: 0.8, ...x });
+export const FEELS = {
+ /** acoustic, rubato-led music: the performance rules carry it, a light human drift on top */
+ natural: F({}, [3, 0.8, 1.5], [6, 0.85, 1.2]),
+ /** a band that locks: snare a hair late, hats a hair early */
+ tight: F({ kick: 0, snare: 4, ghost: 3, hat: -3, perc: -2, bass: 2 }, [2.2, 0.8, 1.2], [3.5, 0.85, 1]),
+ /** electronic drive: hats pushed ahead, everything else on the grid */
+ pushed: F({ kick: 0, snare: 2, ghost: 2, hat: -6, perc: -4, bass: 1, arp: -1 }, [1.2, 0.7, 0.6], [2, 0.8, 0.6], { velJitter: 0.03 }),
+ /** lo-fi / hip-hop: snare late, ghosts late, hats early and wandering, bass behind the kick, keys lazy */
+ laidBack: F({ kick: 0, snare: 18, ghost: 12, hat: -5, perc: 6, bass: 9, chords: 12, lead: 7, counter: 9 }, [3.5, 0.9, 1.8], [5, 0.9, 1.5], { hatDriftMs: 4, velHumanize: 0.08 }),
+ /** swing: ride pushed, backbeat late, walking bass on top of the beat, comping behind */
+ swing: F({ kick: 0, snare: 10, ghost: 8, hat: -8, perc: -4, bass: -3, chords: 12, lead: 4 }, [3.5, 0.85, 2], [5, 0.85, 1.5], { velHumanize: 0.08 }),
+ /** orchestras speak late in the low and slow sections; players drift together */
+ orchestral: F({ bass: 8, chords: 10, counter: 6, perc: 0, kick: 0 }, [4, 0.9, 2.5], [8, 0.9, 2.5]),
+ /** machines: nothing moves (chiptune, grid parts) */
+ grid: F({}, [0, 0, 0], [0, 0, 0], { velHumanize: 0, velJitter: 0 }),
+} satisfies Record<string, Feel>;
+export type FeelId = keyof typeof FEELS;
+
+/** The whole mix profile of a style. */
+export type MixProfile = {
+ /** scales every class-EQ gain (0 = flat, 1 = as tabled) */ eq: number;
+ /** extra EQ per class for this style (merged over CLASS_EQ) */ classEq?: Partial<Record<EqClass, StemEq>>;
+ drumBus: { ratio: number; targetGrDb: number; attackMs: number; releaseMs: number; drive: number; satMix: number } | null;
+ /** mix-bus glue: 2:1, slow attack, auto release-ish; `targetGrDb` 1-2 dB for dense. Gentle styles: only with RenderOpts.gentleGlue (<= 1 dB), a decision for Alex. */
+ glue: { ratio: number; targetGrDb: number; attackMs: number; releaseMs: number } | null;
+ /** mix-bus tilt (dB low end to high end) and light saturation (dense styles) */ tilt: number; busDrive: number;
+ width: { monoHz: number; splitHz: number; mid: number; high: number };
+ space: Space | null; /** a short room for the drums (sends of role "drum"); null = drums share the main space */ drumRoom: Space | null;
+ feel: FeelId;
+ /** default sends per role for parts that name none (over ROLE_SEND) */ sends?: Partial<Record<Role, number>>;
+};
+const conv = (rt60: number, predelayMs: number, o: Partial<Space> = {}): Space => ({ kind: "conv", rt60, predelayMs, hp: 220, lp: 8000, er: 0.45, late: 0.32, size: 0.55, highMult: 0.5, lowMult: 1.1, ...o });
+const fdn = (rt60: number, predelayMs: number, o: Partial<Space> = {}): Space => ({ kind: "fdn", rt60, predelayMs, hp: 250, lp: 7500, er: 0.4, late: 0.3, size: 0.5, highMult: 0.45, lowMult: 1.1, mod: 1, ...o });
+const plate = (rt60: number, predelayMs: number, o: Partial<Space> = {}): Space => ({ kind: "plate", rt60, predelayMs, hp: 300, lp: 9000, er: 0, late: 0.3, size: 0.5, highMult: 0.6, mod: 1, ...o });
+const kitRoom = (rt60 = 0.7): Space => fdn(rt60, 6, { size: 0.2, er: 0.55, late: 0.22, hp: 180, lp: 9000, highMult: 0.5 });
+
+const W = (mid: number, high: number, monoHz = 120) => ({ monoHz, splitHz: 2000, mid, high });
+const intimate: MixProfile = { eq: 0.8, drumBus: { ratio: 3, targetGrDb: 2, attackMs: 15, releaseMs: 120, drive: 1.2, satMix: 0.5 }, glue: null, tilt: 0, busDrive: 0, width: W(1.0, 1.08), space: conv(1.5, 18), drumRoom: null, feel: "natural" };
+const band: MixProfile = { eq: 1, drumBus: { ratio: 4, targetGrDb: 3.5, attackMs: 10, releaseMs: 100, drive: 1.6, satMix: 0.6 }, glue: { ratio: 2, targetGrDb: 1.5, attackMs: 20, releaseMs: 200 }, tilt: 0, busDrive: 1.1, width: W(1.05, 1.15), space: conv(1.3, 15, { size: 0.45 }), drumRoom: kitRoom(0.7), feel: "tight" };
+const electronic: MixProfile = { eq: 1, drumBus: { ratio: 4, targetGrDb: 4, attackMs: 10, releaseMs: 90, drive: 2, satMix: 0.7 }, glue: { ratio: 2, targetGrDb: 1.8, attackMs: 15, releaseMs: 180 }, tilt: 0, busDrive: 1.2, width: W(1.1, 1.25), space: plate(1.6, 22), drumRoom: kitRoom(0.6), feel: "pushed" };
+const cinematicP: MixProfile = { eq: 0.9, drumBus: { ratio: 3, targetGrDb: 2.5, attackMs: 20, releaseMs: 150, drive: 1.3, satMix: 0.4 }, glue: null, tilt: 0, busDrive: 0, width: W(1.1, 1.2), space: conv(2.4, 30, { size: 0.9, late: 0.34, er: 0.35, hp: 200, lp: 7500 }), drumRoom: null, feel: "orchestral" };
+const P = (base: MixProfile, o: Partial<MixProfile>): MixProfile => ({ ...base, ...o });
+
+export const MIX_PROFILES: Record<StyleId, MixProfile> = {
+ nocturne: P(intimate, { sends: { melody: 0.5, accomp: 0.35, inner: 0.35, bass: 0.3, color: 0.5 }, space: conv(1.7, 24, { size: 0.6, late: 0.17, er: 0.4, hp: 300, lp: 6500 }), /* hp 300: the left hand's low-mids stay out of the room (masking guard 75 -> 81 %) */ width: W(1.0, 1.1, 150), classEq: { piano: { hp: 28 } } }),
+ lullaby: P(intimate, { space: conv(1.5, 18, { size: 0.5 }), tilt: -0.8 }),
+ musicBox: P(intimate, { space: null /* the recipe: one reflection, handled in render */, width: W(1.0, 1.05) }),
+ folk: P(intimate, { space: conv(1.2, 12, { size: 0.4, late: 0.28 }), width: W(1.05, 1.15), feel: "natural" }),
+ minimalist: P(intimate, { space: conv(1.3, 14, { size: 0.5 }), feel: "tight" }),
+ jazz: P(intimate, { space: conv(1.2, 12, { size: 0.45 }), drumRoom: kitRoom(0.8), feel: "swing" }),
+ world: P(band, { space: conv(1.4, 16), glue: null, busDrive: 0, feel: "natural" }),
+ playful: P(band, { space: fdn(1.0, 12, { size: 0.35 }), glue: null, busDrive: 0 }),
+ rock: P(band, { space: plate(1.4, 20) }),
+ cinematic: cinematicP,
+ orchestral: P(cinematicP, { space: conv(2.6, 32, { size: 1, late: 0.34, er: 0.35, hp: 200, lp: 7000 }) }),
+ choral: P(cinematicP, { space: conv(3.0, 35, { size: 1, late: 0.36, er: 0.3, hp: 180, lp: 7000 }), feel: "natural" }),
+ suspense: P(cinematicP, { space: fdn(2.8, 25, { size: 0.9, late: 0.34, mod: 1.5 }) }),
+ ambient: P(cinematicP, { space: fdn(4.0, 40, { size: 1, late: 0.36, er: 0.25, mod: 2, highMult: 0.55 }), width: W(1.15, 1.3), feel: "natural" }),
+ drive: P(electronic, { space: plate(1.8, 25, { size: 0.7 }) }),
+ house: P(electronic, { space: plate(1.3, 18) }),
+ synthwave: P(electronic, { space: plate(2.2, 28, { size: 0.8, late: 0.34 }), feel: "tight" }),
+ hipHop: P(electronic, { space: fdn(1.1, 14, { size: 0.35 }), feel: "laidBack", width: W(1.0, 1.15) }),
+ lofiElectronic: P(electronic, { space: fdn(1.4, 20, { size: 0.45 }), drumBus: { ratio: 3, targetGrDb: 3, attackMs: 12, releaseMs: 110, drive: 1.5, satMix: 0.5 }, feel: "laidBack", width: W(1.05, 1.15) }),
+ lofi: P(electronic, { space: fdn(1.2, 16, { size: 0.35, lp: 6000, highMult: 0.35 }), drumBus: { ratio: 4, targetGrDb: 4, attackMs: 8, releaseMs: 100, drive: 2.2, satMix: 0.7 }, tilt: -1.5, width: W(0.9, 0.95, 150), feel: "laidBack" }),
+ chiptune: { eq: 0.3, drumBus: null, glue: { ratio: 2, targetGrDb: 1, attackMs: 20, releaseMs: 150 }, tilt: 0, busDrive: 0, width: W(1.0, 1.0), space: null, drumRoom: null, feel: "grid" },
+};
+
+/** The profile a piece plays with: its style's, with the piece's own overrides. */
+export const mixProfile = (style: StyleId, over?: Partial<MixProfile>): MixProfile => ({ ...MIX_PROFILES[style], ...(over ?? {}) });
+/** The EQ a part gets: class EQ (+ style extra), scaled by the profile's `eq`, with role rules (a bass-role part keeps its lows). */
+export const partEq = (prof: MixProfile, inst: InstId, role: Role): StemEq => {
+ const c = EQ_CLASS[inst], base = { ...CLASS_EQ[c], ...(prof.classEq?.[c] ?? {}) }, k = prof.eq;
+ const e: StemEq = { ...base, bells: (base.bells ?? []).map(([f, q, g]) => [f, q, g * k] as [number, number, number]), air: (base.air ?? 0) * k, tilt: (base.tilt ?? 0) * k };
+ if (role === "bass" && c !== "kick" && c !== "boom") e.hp = Math.min(e.hp ?? 30, 32);
+ if (role === "accomp" && e.hp && c !== "bass" && c !== "piano") e.hp *= 1.15;
+ if (role === "melody" && c !== "bass") e.bells = [...(e.bells ?? []), [2800, 1.1, 0.8 * k]];
+ return e;
+};
+/** A one-line summary for tools/music.mjs vocab. */
+export const describeMix = (style: StyleId) => {
+ const p = MIX_PROFILES[style], s = p.space;
+ return `space ${s ? `${s.kind} ${s.rt60}s, predelay ${s.predelayMs} ms` : style === "musicBox" ? "one early reflection" : "none"}${p.drumRoom ? `, kit room ${p.drumRoom.rt60}s` : ""}; drum bus ${p.drumBus ? `${p.drumBus.ratio}:1 ~${p.drumBus.targetGrDb} dB + sat x${p.drumBus.drive}` : "none"}; glue ${p.glue ? `${p.glue.ratio}:1 ~${p.glue.targetGrDb} dB` : "none (gentle)"}; width mid x${p.width.mid} high x${p.width.high}, mono < ${p.width.monoHz} Hz; feel ${p.feel}`;
+};
diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts b/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts
new file mode 100644
index 0000000..64e6d12
--- /dev/null
+++ b/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts
@@ -0,0 +1,159 @@
+// Reverbs (sound v2). Three spaces, all deterministic from a seed, all returning the wet signal as
+// [full L, full R, late L, late R] (late = the diffuse tail only, which the reverb guard measures):
+// fdn diffuser (4 steps of random delays, random polarity, Hadamard mix) into a 16-line
+// Householder feedback network, each line delay-modulated (+-0.3 ms, 0.1-1 Hz) with
+// frequency-dependent loss (low T60 x lowMult, high T60 x highMult). After Signalsmith's
+// "Let's Write a Reverb" (design re-implemented, no code copied).
+// plate Dattorro 1997 (AES "Effect Design, Part 1"): 4 input allpasses into a modulated
+// figure-of-eight tank, the paper's output taps. Implemented from the published algorithm.
+// conv convolution with a SYNTHESIZED impulse response: decorrelated L/R noise in 8 octave
+// bands, each with its own exponential decay (T60 falls above 4 kHz), a sparse early-
+// reflection pattern and a 20 ms fade-in; FFT overlap-add. No recorded IR.
+import { Biquad, onePoleCoef, gauss, type Rng } from "./dsp";
+import { fft } from "./meter";
+
+export type SpaceKind = "fdn" | "plate" | "conv";
+export type Space = { kind: SpaceKind; rt60: number; predelayMs: number; /** send filter */ hp: number; lp: number; /** early reflections level (0..1) and the late tail level (0..1) */ er: number; late: number; /** room size 0..1 (fdn/conv delay spread) */ size?: number; /** HF / LF decay multipliers */ highMult?: number; lowMult?: number; /** tank modulation depth 0..1 */ mod?: number };
+type Wet = readonly [Float32Array, Float32Array, Float32Array, Float32Array];
+
+const sendFilter = (L: Float32Array, R: Float32Array, sr: number, s: Space) => {
+ const a = Float32Array.from(L), b = Float32Array.from(R);
+ for (const c of [a, b]) { Biquad.make(sr, "hp", s.hp, 0.7071).run(c); Biquad.make(sr, "lp", s.lp, 0.7071).run(c); }
+ return [a, b] as const;
+};
+/** A delay line with a fractional (cubic Hermite) read. */
+class Line {
+ buf: Float64Array; w = 0; mask: number;
+ constructor(len: number) { let s = 1; while (s < len + 8) s <<= 1; this.buf = new Float64Array(s); this.mask = s - 1; }
+ push(x: number) { this.buf[this.w] = x; this.w = (this.w + 1) & this.mask; }
+ at(d: number) { return this.buf[(this.w - 1 - d) & this.mask]; } // integer delay d >= 0 (0 = last pushed)
+ frac(d: number) { const i = Math.floor(d), f = d - i, y0 = this.at(i - 1), y1 = this.at(i), y2 = this.at(i + 1), y3 = this.at(i + 2); const c1 = 0.5 * (y2 - y0), c2 = y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3, c3 = 0.5 * (y3 - y0) + 1.5 * (y1 - y2); return ((c3 * f + c2) * f + c1) * f + y1; }
+}
+const hadamard = (h: Float64Array) => { const N = h.length; for (let s = 1; s < N; s <<= 1) for (let j = 0; j < N; j += s << 1) for (let q = j; q < j + s; q++) { const a = h[q], b = h[q + s]; h[q] = a + b; h[q + s] = a - b; } const k = 1 / Math.sqrt(N); for (let j = 0; j < N; j++) h[j] *= k; };
+
+// ---------------------------------------------------------------- FDN
+export const fdnReverb = (L: Float32Array, R: Float32Array, sr: number, s: Space, rng: Rng): Wet => {
+ const n = L.length, [inL, inR] = sendFilter(L, R, sr, s), size = s.size ?? 0.6, pd = Math.round((s.predelayMs / 1000) * sr);
+ const wL = new Float32Array(n), wR = new Float32Array(n), tL = new Float32Array(n), tR = new Float32Array(n);
+ // diffuser: 4 steps x 8 channels, each step's delays random inside a range that halves each step
+ const DC = 8, steps = 4, dif: { lines: Line[]; d: number[]; flip: number[] }[] = [];
+ let range = (0.012 + 0.03 * size) * sr;
+ for (let k = 0; k < steps; k++) { const d = Array.from({ length: DC }, (_, c) => Math.max(1, Math.round((range * (c + rng())) / DC))); dif.push({ lines: d.map((x) => new Line(x + 2)), d, flip: d.map(() => (rng() < 0.5 ? -1 : 1)) }); range /= 2; }
+ // feedback network: 16 lines, 0.6-1.6 x base, base 45-110 ms by size
+ const N = 16, base = (0.045 + 0.065 * size) * sr, lens = Array.from({ length: N }, (_, j) => Math.round(base * Math.pow(2, (j / N) * 1.3) * (0.92 + 0.16 * rng())));
+ const lines = lens.map((l) => new Line(l + 64)), rate = lens.map(() => 0.1 + 0.9 * rng()), phase = lens.map(() => rng() * Math.PI * 2), depth = (s.mod ?? 1) * 0.0003 * sr;
+ const rtH = s.rt60 * (s.highMult ?? 0.45), rtL = s.rt60 * (s.lowMult ?? 1.15);
+ // per line: Jot's absorptive one-pole, DC gain from the low T60 and Nyquist gain from the high T60 (monotone, so always stable)
+ const gDc = lens.map((l) => Math.pow(10, (-3 * l) / (sr * rtL))), gNy = lens.map((l) => Math.pow(10, (-3 * l) / (sr * rtH * 0.7)));
+ const pa = lens.map((_, j) => (gDc[j] - gNy[j]) / (gDc[j] + gNy[j])), pb = lens.map((_, j) => gDc[j] * (1 - pa[j])), zf = new Float64Array(N);
+ const x8 = new Float64Array(DC), fb = new Float64Array(N), out = new Float64Array(N), twoPi = Math.PI * 2;
+ const erG = s.er * 0.75, lateG = s.late * 1.6; // calibrated: at late 0.3 the tail sits where the legacy hall's did (same input, -19.5 dB)
+ for (let i = 0; i < n; i++) {
+ const xl = i >= pd ? inL[i - pd] : 0, xr = i >= pd ? inR[i - pd] : 0;
+ for (let c = 0; c < DC; c++) x8[c] = c % 2 ? xr : xl;
+ for (const st of dif) { for (let c = 0; c < DC; c++) { st.lines[c].push(x8[c]); x8[c] = st.lines[c].at(st.d[c]) * st.flip[c]; } hadamard(x8); }
+ // early: the diffuser output, alternate channels to each side
+ const el = x8[0] - x8[2] + x8[4] - x8[6], er = x8[1] - x8[3] + x8[5] - x8[7];
+ for (let j = 0; j < N; j++) { const md = depth * Math.sin(twoPi * rate[j] * (i / sr) + phase[j]); out[j] = lines[j].frac(lens[j] + md); }
+ // Householder: y = x - 2/N sum(x)
+ let sum = 0; for (let j = 0; j < N; j++) sum += out[j]; sum *= 2 / N;
+ for (let j = 0; j < N; j++) {
+ const v = out[j] - sum;
+ zf[j] = pb[j] * v + pa[j] * zf[j]; fb[j] = zf[j];
+ lines[j].push(fb[j] + x8[j % DC] * 0.5);
+ }
+ let ll = 0, rr = 0; for (let j = 0; j < N; j++) { const sg = (j >> 1) % 2 ? -1 : 1; if (j % 2) rr += out[j] * sg; else ll += out[j] * sg; }
+ tL[i] = ll * lateG * 0.25; tR[i] = rr * lateG * 0.25;
+ wL[i] = tL[i] + el * erG * 0.35; wR[i] = tR[i] + er * erG * 0.35;
+ }
+ return [wL, wR, tL, tR];
+};
+
+// ---------------------------------------------------------------- Dattorro plate
+export const plateReverb = (L: Float32Array, R: Float32Array, sr: number, s: Space, rng: Rng): Wet => {
+ const n = L.length, [inL, inR] = sendFilter(L, R, sr, s), k = sr / 29761, S = (x: number) => Math.max(1, Math.round(x * k * (0.6 + 0.8 * (s.size ?? 0.5))));
+ const wL = new Float32Array(n), wR = new Float32Array(n), tL = new Float32Array(n), tR = new Float32Array(n), pd = Math.round((s.predelayMs / 1000) * sr);
+ const bw = onePoleCoef(sr, s.lp * 0.9);
+ const ap = (len: number) => ({ l: new Line(len + 40), len });
+ const ind = [ap(S(142)), ap(S(107)), ap(S(379)), ap(S(277))], indG = [0.75, 0.75, 0.625, 0.625];
+ const mA = ap(S(672)), dA = new Line(S(4453) + 4), aA2 = ap(S(1800)), dA2 = new Line(S(3720) + 4);
+ const mB = ap(S(908)), dB = new Line(S(4217) + 4), aB2 = ap(S(2656)), dB2 = new Line(S(3163) + 4);
+ const lA = S(4453), lA2 = S(3720), lB = S(4217), lB2 = S(3163);
+ const half = (S(672) + lA + S(1800) + lA2) / sr, decay = Math.min(0.97, Math.pow(10, (-3 * half) / s.rt60));
+ const dampF = onePoleCoef(sr, Math.min(sr * 0.45, 2500 + 6000 * (s.highMult ?? 0.5))), exc = 16 * k * (s.mod ?? 1), rA = 0.5 + 0.4 * rng(), rB = 0.6 + 0.4 * rng();
+ let bwz = 0, zA = 0, zB = 0, fbA = 0, fbB = 0;
+ const apTick = (a: { l: Line; len: number }, x: number, g: number, d = a.len) => { const z = d === a.len ? a.l.at(a.len - 1) : a.l.frac(d - 1); const v = x - g * z; a.l.push(v); return z + g * v; };
+ const tap = (l: Line, d: number) => l.at(Math.max(0, S(d) - 1));
+ const tapAp = (a: { l: Line }, d: number) => a.l.at(Math.max(0, S(d) - 1));
+ for (let i = 0; i < n; i++) {
+ const x = i >= pd ? 0.5 * (inL[i - pd] + inR[i - pd]) : 0;
+ bwz += bw * (x - bwz); let v = bwz;
+ for (let j = 0; j < 4; j++) v = apTick(ind[j], v, indG[j]);
+ const t = i / sr;
+ // branch A
+ let a = apTick(mA, v + fbB * decay, -0.7, mA.len + exc * Math.sin(2 * Math.PI * rA * t)); dA.push(a); a = dA.at(lA - 1);
+ zA += dampF * (a - zA); a = zA * decay; a = apTick(aA2, a, 0.5); dA2.push(a); const outA = dA2.at(lA2 - 1);
+ // branch B
+ let b = apTick(mB, v + fbA * decay, -0.7, mB.len + exc * Math.sin(2 * Math.PI * rB * t + 1.3)); dB.push(b); b = dB.at(lB - 1);
+ zB += dampF * (b - zB); b = zB * decay; b = apTick(aB2, b, 0.5); dB2.push(b); const outB = dB2.at(lB2 - 1);
+ fbA = outA; fbB = outB;
+ const yl = tap(dB, 266) + tap(dB, 2974) - tapAp(aB2, 1913) + tap(dB2, 1996) - tap(dA, 1990) - tapAp(aA2, 187) - tap(dA2, 1066);
+ const yr = tap(dA, 353) + tap(dA, 3627) - tapAp(aA2, 1228) + tap(dA2, 2673) - tap(dB, 2111) - tapAp(aB2, 335) - tap(dB2, 121);
+ tL[i] = yl * 0.62 * s.late; tR[i] = yr * 0.62 * s.late; wL[i] = tL[i]; wR[i] = tR[i];
+ }
+ return [wL, wR, tL, tR];
+};
+
+// ---------------------------------------------------------------- synthesized IR + convolution
+/** A stereo IR: [er L, er R, late L, late R], each normalized so the late tail has unit energy per channel. */
+export const synthIR = (sr: number, s: Space, rng: Rng) => {
+ const len = Math.min(Math.round(Math.max(0.3, s.rt60 * 1.25) * sr), Math.round(8 * sr)), size = s.size ?? 0.6;
+ const erL = new Float32Array(len), erR = new Float32Array(len), lL = new Float32Array(len), lR = new Float32Array(len);
+ // sparse early reflections, 4-60 ms scaled by size, falling gain, random sign, each side its own pattern
+ const nEr = 10, erEnd = (0.02 + 0.045 * size) * sr;
+ for (const [buf, off] of [[erL, 0], [erR, 0.3]] as const) for (let t = 0; t < nEr; t++) { const d = Math.round(0.004 * sr + erEnd * Math.pow((t + rng() * 0.8 + off) / nEr, 1.4)); if (d < len) buf[d] += (rng() < 0.5 ? -1 : 1) * Math.pow(0.82, t) * (0.6 + 0.4 * rng()); }
+ // late: 8 octave bands (63 Hz .. 8 kHz), per band T60, onset fade over 20 ms from the end of the early part
+ const bands = [63, 125, 250, 500, 1000, 2000, 4000, 8000], fadeA = Math.round(0.006 * sr + erEnd * 0.4), fade = Math.round(0.02 * sr);
+ const t60 = (f: number) => s.rt60 * (f <= 250 ? (s.lowMult ?? 1.15) : f >= 4000 ? (s.highMult ?? 0.5) * (f >= 8000 ? 0.75 : 1) : f >= 2000 ? 0.5 + 0.5 * (s.highMult ?? 0.5) : 1);
+ for (const [buf] of [[lL], [lR]] as const) {
+ for (const f of bands) {
+ if (f > sr * 0.45) continue;
+ const noise = new Float32Array(len); for (let i = 0; i < len; i++) noise[i] = gauss(rng);
+ const q = 1.2; Biquad.make(sr, "bp", f, q).run(noise); Biquad.make(sr, "bp", f, q).run(noise);
+ const tau = t60(f) / 6.91; // exp(-t/tau) reaches -60 dB at T60
+ for (let i = 0; i < len; i++) { const env = i < fadeA ? 0 : i < fadeA + fade ? (i - fadeA) / fade : 1; buf[i] += noise[i] * env * Math.exp(-i / sr / tau); }
+ }
+ }
+ const norm = (a: Float32Array, b: Float32Array) => { let e = 0; for (let i = 0; i < len; i++) e += a[i] * a[i] + b[i] * b[i]; const g = e > 0 ? Math.sqrt(2 / e) : 0; for (let i = 0; i < len; i++) { a[i] *= g; b[i] *= g; } };
+ norm(lL, lR); norm(erL, erR);
+ return { erL, erR, lL, lR, len };
+};
+/** Linear convolution of x with h by FFT overlap-add (block = the FFT size minus the IR). */
+export const convolve = (x: Float32Array, h: Float32Array, out: Float32Array, gain = 1) => {
+ const n = x.length, m = h.length; let N = 1; while (N < 2 * m) N <<= 1; N = Math.max(N, 1 << 15); const B = N - m + 1;
+ const Hr = new Float64Array(N), Hi = new Float64Array(N); Hr.set(h); fft(Hr, Hi);
+ const re = new Float64Array(N), im = new Float64Array(N);
+ for (let a = 0; a < n; a += B) {
+ re.fill(0); im.fill(0); let any = false;
+ for (let i = 0; i < B && a + i < n; i++) { re[i] = x[a + i]; if (x[a + i] !== 0) any = true; }
+ if (!any) continue;
+ fft(re, im);
+ for (let k = 0; k < N; k++) { const r = re[k] * Hr[k] - im[k] * Hi[k], ii = re[k] * Hi[k] + im[k] * Hr[k]; re[k] = r; im[k] = -ii; } // conjugate for the inverse via forward fft
+ fft(re, im);
+ for (let i = 0; i < N && a + i < out.length; i++) out[a + i] += (re[i] / N) * gain;
+ }
+};
+export const convReverb = (L: Float32Array, R: Float32Array, sr: number, s: Space, rng: Rng): Wet => {
+ const n = L.length, [inL, inR] = sendFilter(L, R, sr, s), ir = synthIR(sr, s, rng), pd = Math.round((s.predelayMs / 1000) * sr);
+ const xL = new Float32Array(n), xR = new Float32Array(n); for (let i = pd; i < n; i++) { xL[i] = inL[i - pd]; xR[i] = inR[i - pd]; }
+ const wL = new Float32Array(n), wR = new Float32Array(n), tL = new Float32Array(n), tR = new Float32Array(n), eL = new Float32Array(n), eR = new Float32Array(n);
+ // true stereo: L feeds both sides through decorrelated responses (the cross path 6 dB down)
+ const lateG = s.late * 0.36, erG = s.er * 0.51; // calibrated against the legacy room (see fdn)
+ convolve(xL, ir.lL, tL, lateG); convolve(xR, ir.lR, tR, lateG); convolve(xR, ir.lL, tL, lateG * 0.5); convolve(xL, ir.lR, tR, lateG * 0.5);
+ convolve(xL, ir.erL, eL, erG); convolve(xR, ir.erR, eR, erG);
+ for (let i = 0; i < n; i++) { wL[i] = tL[i] + eL[i]; wR[i] = tR[i] + eR[i]; }
+ return [wL, wR, tL, tR];
+};
+
+export const reverb = (L: Float32Array, R: Float32Array, sr: number, s: Space, rng: Rng): Wet =>
+ s.kind === "plate" ? plateReverb(L, R, sr, s, rng) : s.kind === "conv" ? convReverb(L, R, sr, s, rng) : fdnReverb(L, R, sr, s, rng);