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.

mix: transient-aware gain on struck stems of gentle styles (mixDsp.transientGain: fast peak vs body envelope, 4 dB threshold, 3:1 on the excess, max 6 dB, 2 ms look-ahead, energy-matched; plucks, plucked basses, piano). No master compressor; gentleGlue stays behind RenderOpts.gentleGlue, default off. Measured at 48 kHz: guitarWistful -17.90 -> -16.00 LUFS (-3.40 dBTP), S4 steel-guitar phrase -16.69 -> -16.00 (-2.25 dBTP), upright -1.28 -> -3.62 dBTP headroom at -16, nocturne -19.46 -> -17.51 (its 9.6 LU range is the rest: gentleGlue would give -16.69)

Alex Greenshpun4473dc511ae8
diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts
index cfcb028..9f051ac 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts
@@ -81,6 +81,37 @@ export const ampSim = (L: Float32Array, R: Float32Array, sr: number, drive: numb
if (e1 > 0) { const k = Math.sqrt(e0 / e1); for (let i = 0; i < L.length; i++) { L[i] *= k; R[i] *= k; } }
};
+/**
+ * Transient-aware gain for one stem (gentle styles: the plucks, plucked basses and piano, whose picks and hammers
+ * pin a static master at the -1 dBTP ceiling 2-3 dB short of -16 LUFS). A fast peak envelope (instant attack, 5 ms
+ * release) against the note's own body (a 20 ms / 150 ms mean level) says how far a sample stands above its body;
+ * only that excess comes down (above `thrDb`, at `ratio`, at most `maxDb`), with a 2 ms look-ahead so the dip lands
+ * on the pick, a 40 ms recovery, and the stem's energy matched back. The body, the decay and the balance between
+ * stems do not move: only the crest of each attack does. Not a compressor on the master (spec 08 forbids that on
+ * gentle styles); gentleGlue stays a separate, default-off decision. Returns the p99 gain reduction in dB.
+ */
+export const transientGain = (L: Float32Array, R: Float32Array, sr: number, o: { maxDb?: number; thrDb?: number; ratio?: number } = {}) => {
+ const n = L.length, maxDb = o.maxDb ?? 6, thr = o.thrDb ?? 4, k = 1 - 1 / (o.ratio ?? 3);
+ let e0 = 0; for (let i = 0; i < n; i++) e0 += L[i] * L[i] + R[i] * R[i];
+ if (e0 <= 0) return 0;
+ const rF = Math.exp(-1 / (0.005 * sr)), aS = Math.exp(-1 / (0.02 * sr)), rS = Math.exp(-1 / (0.15 * sr)), want = new Float32Array(n);
+ let fast = 0, slow = 0;
+ for (let i = 0; i < n; i++) {
+ const a = Math.max(Math.abs(L[i]), Math.abs(R[i]));
+ fast = a > fast ? a : rF * fast; slow = fast > slow ? aS * slow + (1 - aS) * fast : rS * slow + (1 - rS) * fast;
+ const over = slow > 1e-7 ? 20 * Math.log10(fast / slow) - thr : 0;
+ want[i] = over > 0 ? Math.min(maxDb, over * k) : 0;
+ }
+ // look-ahead: the reduction starts 2 ms before the peak it answers (running max over the next window), then a smooth recovery
+ const la = Math.max(1, Math.round(0.002 * sr)), rec = Math.exp(-1 / (0.04 * sr)), g = new Float32Array(n), q: number[] = [];
+ for (let i = n - 1; i >= 0; i--) { while (q.length && want[q[q.length - 1]] <= want[i]) q.pop(); q.push(i); while (q[0] > i + la) q.shift(); g[i] = want[q[0]]; }
+ let cur = 0; const grs: number[] = [];
+ for (let i = 0; i < n; i++) { cur = g[i] > cur ? g[i] : rec * cur + (1 - rec) * g[i]; const m = Math.pow(10, -cur / 20); L[i] *= m; R[i] *= m; if (cur > 0.05 && (i & 63) === 0) grs.push(cur); }
+ let e1 = 0; for (let i = 0; i < n; i++) e1 += L[i] * L[i] + R[i] * R[i];
+ if (e1 > 0) { const m = Math.sqrt(e0 / e1); for (let i = 0; i < n; i++) { L[i] *= m; R[i] *= m; } }
+ grs.sort((a, b) => a - b); return grs.length ? grs[Math.floor(grs.length * 0.99)] : 0;
+};
+
// ---------------------------------------------------------------- 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. */
diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts b/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts
index 1b8cc0a..a7b8ddf 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/mixProfiles.ts
@@ -80,7 +80,11 @@ export type MixProfile = {
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>>;
+ /** gentle masters only: the transient-aware gain on plucked stems (mixDsp.transientGain; null = off) */ transient?: { maxDb: number; thrDb: number; ratio: number } | null;
};
+/** The stems whose attacks pin a static gentle master at the true-peak ceiling: plucks, plucked basses, piano. */
+export const isStruck = (inst: InstId, opts?: Record<string, number | boolean | string>) => EQ_CLASS[inst] === "pluck" || EQ_CLASS[inst] === "piano" || (inst === "bass" && opts?.kind !== "synth");
+export const TRANSIENT_DEFAULT = { maxDb: 6, thrDb: 4, ratio: 3 };
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 });
diff --git a/skills/anidoodle/engine/src/canvas-core/music/render.ts b/skills/anidoodle/engine/src/canvas-core/music/render.ts
index 4dcf771..00fad63 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts
@@ -15,14 +15,14 @@ import { pumpCurve, lofiStem, lofiMaster, LOFI_DUSTY } from "./lofiFx";
import { loudness, truePeak, stemBalance } from "./meter";
import { STYLES } from "./tables";
import { rng as mkRng } from "../core";
-import { eqChain, runEq, stereoPan, compress, saturate, bandWidth, smoothLimiter, ampSim } from "./mixDsp";
+import { eqChain, runEq, stereoPan, compress, saturate, bandWidth, smoothLimiter, ampSim, transientGain } from "./mixDsp";
import { reverb, type Space } from "./mixReverb";
-import { mixProfile, partEq, ROLE_SEND, type MixProfile } from "./mixProfiles";
+import { mixProfile, partEq, ROLE_SEND, isStruck, TRANSIENT_DEFAULT, type MixProfile } from "./mixProfiles";
export type RenderOpts = { /** sound v2, gentle styles only: a <= 1 dB 2:1 glue on the mix bus (spec 08 forbids a compressor on gentle masters; Alex decides) */ gentleGlue?: boolean; expressive?: boolean; piano?: PianoOpts; flatVelocity?: number; seconds?: number; tempo?: number; master?: "auto" | "gentle" | "dense" | "none"; stems?: boolean; only?: (p: Part) => boolean };
/** dry = the pre-room mix; wet = the room's late tail (for music-box: its single reflection). Kept so the guards can measure reverb-to-dry per bar. */
export type Rendered = { L: Float32Array; R: Float32Array; perf: Performance; tempo: number; gainDb: number; masterMode: string; stems: Record<string, [Float32Array, Float32Array]>; piece: Piece; dry: [Float32Array, Float32Array]; wet: [Float32Array, Float32Array];
- /** sound v2: what the chain did (gain reduction in dB) */ mixReport?: { drumBusGr?: number; glueGr?: number; limiterGr?: number; space?: string } };
+ /** sound v2: what the chain did (gain reduction in dB) */ mixReport?: { drumBusGr?: number; glueGr?: number; limiterGr?: number; transientGr?: number; space?: string } };
const voice = (pt: Part, keys: Played[], sr: number, n: number, seed: number, ctx: KitCtx = {}) => {
// Piece.legacy is the one freeze flag: legacy pieces hand every voice `legacy: true` so a rebuilt voice keeps its old path for them
@@ -135,6 +135,7 @@ const renderV2 = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
const pump = dk && byIdx >= 0 ? pumpCurve(perf.parts[byIdx].keys, tempo, sr, n, dk.depth ?? 0.4, dk.release) : null;
// lo-fi production (lofiFx.ts): a lofi-style piece without its own profile gets the dusty one; a loop's modulations close on its length
const lofi = piece.fx?.lofi ?? (style.id === "lofi" && !piece.fx?.clean ? LOFI_DUSTY : undefined), period = piece.plan.loop ? (piece.plan.sections.reduce((a, s) => a + s.bars * beatsPerBar(piece.plan.meter), 0) * 60) / tempo : undefined;
+ const report: NonNullable<Rendered["mixReport"]> = {};
const kitCtx: KitCtx = { legacy: false, style: piece.plan.style, choke: chokeTimes(piece.parts.map((pt, pi) => ({ inst: pt.inst, opts: pt.opts, keys: perf.parts[pi].keys }))) };
piece.parts.forEach((pt, pi) => {
if (o.only && !o.only(pt)) return;
@@ -145,6 +146,9 @@ const renderV2 = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
else { const r = voice(pt, keys, sr, n, piece.seed * 101 + pi, kitCtx); sL = r.L; sR = r.R; }
if (lofi) lofiStem(lofi, pt.id, sL, sR, sr, keys.map((k) => k.t), piece.seed, period);
if (typeof pt.opts?.amp === "number") ampSim(sL, sR, sr, pt.opts.amp); // a guitar (or any DI) through the amp + cab
+ // gentle masters are one static gain: a pick or hammer spike would hold the whole mix under the ceiling, so struck stems lose their crest, not their level
+ const tg = prof.transient === undefined ? TRANSIENT_DEFAULT : prof.transient;
+ if (style.master === "gentle" && tg && isStruck(pt.inst, pt.opts)) report.transientGr = Math.max(report.transientGr ?? 0, transientGain(sL, sR, sr, tg));
if (pump && dk!.parts.includes(pt.id)) for (let i = 0; i < n; i++) { sL[i] *= pump[i]; sR[i] *= pump[i]; }
// corrective EQ, then match the stem's RMS back (+-3 dB cap): EQ shapes the tone, the fader keeps the calibrated balance
// the stem meter reads the part as the fader set it (pre-EQ): the EQ is energy-neutral in the audible band, while stemRms
@@ -163,7 +167,6 @@ const renderV2 = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
if (toRoom) anyRoom = true;
if (send > 0) for (let i = 0; i < n; i++) { dst[0][i] += sL[i] * send; dst[1][i] += sR[i] * send; }
});
- const report: NonNullable<Rendered["mixReport"]> = {};
// ---- drum bus: glue the kit (4:1-ish, 3-4 dB on the hits), then oversampled saturation in parallel
if (anyDrum && prof.drumBus) {
const b = prof.drumBus; report.drumBusGr = compress(dL, dR, sr, { ratio: b.ratio, attackMs: b.attackMs, releaseMs: b.releaseMs, targetGrDb: b.targetGrDb }).p90GrDb;