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.

merge track/S2-drums: voice() takes S2's KitCtx and derives the legacy opts from ctx.legacy (Piece.legacy, the one freeze flag); renderLegacy passes { legacy: true }, renderV2 builds the kit context (style kit, hat choke)

Alex Greenshpun0c8f60e82cd6
diff --cc skills/anidoodle/engine/src/canvas-core/music/compose.ts
index f6404da,7799ee2..9e10439
--- a/skills/anidoodle/engine/src/canvas-core/music/compose.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/compose.ts
@@@ -8,10 -8,9 +8,10 @@@ import type { ModeId } from "./theory"
import type { MoodId, StyleId, MelodyType } from "./tables";
import { pcOf } from "./theory";
import { perform } from "./perform";
- import { drumBar, type Groove, type Lane } from "./grooves";
+ import { drumBar, laneKind, type Groove, type Lane } from "./grooves";
import { VOCAB_TRIM, VOCAB_TARGET_FIX } from "./vocabTrim";
import { VOCAB, KINDS, BASE_SLOTS, resolveVoice, moodVoice, moodFx, fullMood, type Slot, type Voice, type SectionKind, type MoodControls } from "./vocab";
+import type { MixProfile } from "./mixProfiles";
/** One chord of YOUR harmony: its voicing (a notation chord) and a one-bar bass line you wrote for it. */
export type ComposedChord = { voicing: string; bass?: string };
diff --cc skills/anidoodle/engine/src/canvas-core/music/index.ts
index e359226,e5084dd..8a699b7
--- a/skills/anidoodle/engine/src/canvas-core/music/index.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/index.ts
@@@ -17,8 -17,7 +17,9 @@@ export * from "./calibration"
export * as instruments from "./instruments";
export * from "./sfx";
export * as orchestra from "./orchestra";
+export * as lofiFx from "./lofiFx";
+export { MIX_PROFILES, FEELS, mixProfile, describeMix, type MixProfile, type Feel } from "./mixProfiles";
+ export * as drums from "./drums";
import { nocturne, pianoPhrase8 } from "./pieces/nocturne";
import { launchLofi, launchLofi2, launchLofi3, launchLofi3Material } from "./pieces/launch";
import { musicBoxJoy, minorPianoMelancholy, cinematicAwe, chiptunePlayful, lofiNostalgic } from "./pieces/samplers";
diff --cc skills/anidoodle/engine/src/canvas-core/music/render.ts
index 1374a82,a61ac4e..b359fa2
--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts
@@@ -7,24 -7,19 +7,25 @@@ import { perform, type Performance, typ
import { renderPiano, PIANO_REAL, type PianoOpts } from "./piano";
import * as I from "./instruments";
import * as O from "./orchestra";
+ import { timpani as kitTimpani, chokeTimes, type KitCtx } from "./drums";
import { room, Biquad, db } from "./dsp";
import { warmPad, softPluck, sub, duckCurve, tape } from "./lofiKit";
+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 } from "./mixDsp";
+import { reverb, type Space } from "./mixReverb";
+import { mixProfile, partEq, ROLE_SEND, type MixProfile } from "./mixProfiles";
-export type RenderOpts = { expressive?: boolean; piano?: PianoOpts; flatVelocity?: number; seconds?: number; tempo?: number; master?: "auto" | "gentle" | "dense" | "none"; stems?: boolean; only?: (p: Part) => boolean };
+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] };
+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 } };
- const voice = (pt: Part, keys: Played[], sr: number, n: number, seed: number, legacy = false) => {
- // legacy pieces hand every voice `legacy: true` so a rebuilt voice can keep its old path for them
- const o = legacy ? { ...(pt.opts ?? {}), legacy: true } : pt.opts ?? {}, r = mkRng(seed);
+ const voice = (pt: Part, keys: Played[], sr: number, n: number, seed: number, ctx: KitCtx = {}) => {
- const o = pt.opts ?? {}, r = mkRng(seed);
++ // Piece.legacy is the one freeze flag: legacy pieces hand every voice `legacy: true` so a rebuilt voice keeps its old path for them
++ const o = ctx.legacy ? { ...(pt.opts ?? {}), legacy: true } : pt.opts ?? {}, r = mkRng(seed);
switch (pt.inst) {
case "musicBox": return I.musicBox(keys, sr, n, o);
case "bell": return I.bell(keys, sr, n, o);
@@@ -75,7 -66,7 +74,7 @@@ const renderLegacy = (piece: Piece, sr
const keys = perf.parts[pi].keys; if (!keys.length) return;
let sL: Float32Array, sR: Float32Array;
if (pt.inst === "piano") { const r = renderPiano(keys, perf.pedal, sr, n, o.piano ?? PIANO_REAL, piece.seed + pi); sL = r.L; sR = r.R; if ((o.piano ?? PIANO_REAL).pedal) for (let i = 0; i < n; i++) haloL[i] += r.halo[i] * db(pt.gainDb ?? 0); }
- else { const r = voice(pt, keys, sr, n, piece.seed * 101 + pi, true); sL = r.L; sR = r.R; }
- else { const r = voice(pt, keys, sr, n, piece.seed * 101 + pi, kitCtx); sL = r.L; sR = r.R; }
++ else { const r = voice(pt, keys, sr, n, piece.seed * 101 + pi, { legacy: true }); sL = r.L; sR = r.R; }
if (pump && dk!.parts.includes(pt.id)) for (let i = 0; i < n; i++) { sL[i] *= pump[i]; sR[i] *= pump[i]; }
const g = db(pt.gainDb ?? 0);
if (pt.pan) { const a = Math.max(0, pt.pan), b = Math.max(0, -pt.pan); for (let i = 0; i < n; i++) { sL[i] *= 1 - a * 0.6; sR[i] *= 1 - b * 0.6; } }
@@@ -109,99 -100,8 +108,100 @@@
return { L, R, perf, tempo, gainDb, masterMode, stems, piece, dry, wet };
};
+/**
+ * Sound v2 (mixProfiles.ts per style): voice -> pump -> corrective EQ per stem (level-matched, so
+ * the calibrated balance holds) -> fader -> true stereo pan -> drum bus (comp + 2x oversampled
+ * saturation) / music bus -> sends per part to the style's space (and a short kit room) -> mix bus:
+ * tilt, glue (dense; gentle only with `gentleGlue`), light saturation (dense), per-band width with
+ * mono lows -> master (gentle: static gain; dense: the smooth true-peak limiter).
+ */
+const energy = (L: Float32Array, R: Float32Array) => { let e = 0; for (let i = 0; i < L.length; i++) e += L[i] * L[i] + R[i] * R[i]; return e; };
+/** Audible energy: above 40 Hz, so a voice's DC or subsonic drift never counts as level (the EQ's high-pass removes it, and the makeup must not replace it with audible gain). */
+const audible = (L: Float32Array, R: Float32Array, sr: number) => energy(Biquad.make(sr, "hp", 40, 0.7071).run(Float32Array.from(L)), Biquad.make(sr, "hp", 40, 0.7071).run(Float32Array.from(R)));
+const renderV2 = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
+ const expressive = o.expressive ?? true, tempo = o.tempo ?? piece.plan.tempo, style = STYLES[piece.plan.style], prof: MixProfile = mixProfile(style.id, piece.mix);
+ const perf = perform(piece, tempo, { expressive, flatVelocity: o.flatVelocity });
+ const n = o.seconds ? Math.round(o.seconds * sr) : Math.ceil((perf.lastOnset + piece.tail) * sr);
+ const L = new Float32Array(n), R = new Float32Array(n), haloL = new Float32Array(n), stems: Record<string, [Float32Array, Float32Array]> = {};
+ const dL = new Float32Array(n), dR = new Float32Array(n); // drum bus
+ const mS = [new Float32Array(n), new Float32Array(n)], rS = [new Float32Array(n), new Float32Array(n)]; // main space send, kit room send
+ let anyDrum = false, anyRoom = false;
+ const dk = piece.fx?.duck, byIdx = dk ? piece.parts.findIndex((p) => p.id === dk.by) : -1;
+ 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 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;
+ const keys = perf.parts[pi].keys; if (!keys.length) return;
+ let sL: Float32Array, sR: Float32Array;
+ if (pt.inst === "piano") { const r = renderPiano(keys, perf.pedal, sr, n, o.piano ?? PIANO_REAL, piece.seed + pi); sL = r.L; sR = r.R; if ((o.piano ?? PIANO_REAL).pedal) for (let i = 0; i < n; i++) haloL[i] += r.halo[i] * db(pt.gainDb ?? 0); }
- else { const r = voice(pt, keys, sr, n, piece.seed * 101 + pi); sL = r.L; sR = r.R; }
++ 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 (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
+ // (left channel, -80 dB floor) also counts subsonic residue the high-pass removes, which read one harp 6.8 dB "hotter"
+ const pre = o.stems ? [Float32Array.from(sL), Float32Array.from(sR)] as [Float32Array, Float32Array] : null;
+ const before = audible(sL, sR, sr), e = partEq(prof, pt.inst, pt.role); runEq(sL, eqChain(sr, e)); runEq(sR, eqChain(sr, e));
+ const after = audible(sL, sR, sr), mk = after > 0 && before > 0 ? Math.min(db(3), Math.max(db(-3), Math.sqrt(before / after))) : 1;
+ const g = db(pt.gainDb ?? 0) * mk;
+ for (let i = 0; i < n; i++) { sL[i] *= g; sR[i] *= g; }
+ stereoPan(sL, sR, pt.pan ?? 0);
+ if (pre) { const k = db(pt.gainDb ?? 0); for (let i = 0; i < n; i++) { pre[0][i] *= k; pre[1][i] *= k; } stereoPan(pre[0], pre[1], pt.pan ?? 0); stems[pt.id] = pre; }
+ const isDrum = pt.role === "drum";
+ for (let i = 0; i < n; i++) { if (isDrum) { dL[i] += sL[i]; dR[i] += sR[i]; } else { L[i] += sL[i]; R[i] += sR[i]; } }
+ if (isDrum) anyDrum = true;
+ const send = pt.send ?? prof.sends?.[pt.role] ?? ROLE_SEND[pt.role], toRoom = isDrum && prof.drumRoom !== null, dst = toRoom ? rS : mS;
+ 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;
+ if (b.drive > 1) { const before = energy(dL, dR); saturate(dL, sr, b.drive, 0.08, b.satMix); saturate(dR, sr, b.drive, 0.08, b.satMix); const after = energy(dL, dR); if (after > 0) { const k = Math.sqrt(before / after); for (let i = 0; i < n; i++) { dL[i] *= k; dR[i] *= k; } } }
+ }
+ for (let i = 0; i < n; i++) { L[i] += dL[i]; R[i] += dR[i]; }
+ // ---- space
+ const rng = mkRng(piece.seed * 7 + 5), dry: [Float32Array, Float32Array] = [Float32Array.from(L), Float32Array.from(R)];
+ let wet: [Float32Array, Float32Array] = [new Float32Array(n), new Float32Array(n)];
+ const sp = piece.plan.space;
+ if (style.id === "musicBox" && !sp && !piece.mix?.space) { // the recipe: ONE early reflection, 30 ms late, 14 dB down, no tail
+ const d = Math.round(0.03 * sr), g = db(-14); for (let i = d; i < n; i++) { wet[0][i] = dry[0][i - d] * g; wet[1][i] = dry[1][i - d] * g * 0.9; L[i] += wet[0][i]; R[i] += wet[1][i]; }
+ } else {
+ const main: Space | null = prof.space || sp ? { ...(prof.space ?? { kind: "fdn", rt60: 1.5, predelayMs: 20, hp: 300, lp: 7000, er: 0.4, late: 0.25 }), ...(sp ?? {}) } : null;
+ if (main) {
+ for (let i = 0; i < n; i++) { mS[0][i] += haloL[i] * 0.35; mS[1][i] += haloL[i] * 0.35; } // pedal halo: undamped strings ring into the room
+ if (!prof.drumRoom) for (let i = 0; i < n; i++) { mS[0][i] += rS[0][i]; mS[1][i] += rS[1][i]; }
+ const [wL, wR, tL, tR] = reverb(mS[0], mS[1], sr, main, rng);
+ for (let i = 0; i < n; i++) { L[i] += wL[i]; R[i] += wR[i]; }
+ wet = [tL, tR]; report.space = `${main.kind} ${main.rt60}s`;
+ }
+ if (prof.drumRoom && anyRoom) {
+ const [wL, wR, tL, tR] = reverb(rS[0], rS[1], sr, prof.drumRoom, mkRng(piece.seed * 11 + 3));
+ for (let i = 0; i < n; i++) { L[i] += wL[i]; R[i] += wR[i]; wet[0][i] += tL[i]; wet[1][i] += tR[i]; }
+ report.space = `${report.space ?? "no main space"} + kit room ${prof.drumRoom.rt60}s`;
+ }
+ }
+ // ---- mix bus
+ if (prof.tilt) for (const c of [L, R]) { Biquad.make(sr, "lowshelf", 1000, 0.5, -prof.tilt / 2).run(c); Biquad.make(sr, "highshelf", 1000, 0.5, prof.tilt / 2).run(c); }
+ const glue = style.master === "dense" ? prof.glue : o.gentleGlue ? { ratio: 2, targetGrDb: 0.8, attackMs: 30, releaseMs: 250 } : null;
+ if (glue) report.glueGr = compress(L, R, sr, { ...glue, kneeDb: 8, maxGrDb: 4 }).p90GrDb;
+ if (style.master === "dense" && prof.busDrive > 1) { saturate(L, sr, prof.busDrive, 0.03); saturate(R, sr, prof.busDrive, 0.03); }
+ bandWidth(L, R, sr, prof.width);
+ // ---- master: DC/rumble high-pass (4th-order Butterworth at 30 Hz), the lo-fi machines (lofiFx: tape, vinyl, room tone, tilt), end fade
+ for (const q of [0.5412, 1.3066]) { Biquad.make(sr, "hp", 30, q).run(L); Biquad.make(sr, "hp", 30, q).run(R); }
+ if (lofi || piece.fx?.tape) lofiMaster(L, R, sr, piece.fx?.tape && { ...piece.fx.tape, period }, lofi, piece.seed);
+ const fade = Math.min(n, Math.round(0.25 * sr)); for (let i = 0; i < fade; i++) { const g = 0.5 - 0.5 * Math.cos((Math.PI * i) / fade); L[n - 1 - i] *= g; R[n - 1 - i] *= g; }
+ let masterMode = o.master ?? "auto";
+ if (masterMode === "auto") masterMode = style.master;
+ const gainDb = masterMode === "none" ? 0 : master(L, R, sr, masterMode as "gentle" | "dense", true);
+ return { L, R, perf, tempo, gainDb, masterMode, stems, piece, dry, wet, mixReport: report };
+};
+
/** The master stage, in place. Gentle: one static gain to -16 LUFS, pulled down if the true peak passes -1 dBTP. Dense: -14 LUFS through the look-ahead limiter. Returns the gain in dB. */
-export const master = (L: Float32Array, R: Float32Array, sr: number, masterMode: "gentle" | "dense"): number => {
+export const master = (L: Float32Array, R: Float32Array, sr: number, masterMode: "gentle" | "dense", v2 = false): number => {
const n = L.length, target = masterMode === "dense" ? -14 : -16;
let gainDb = target - loudness([L, R], sr).integrated;
for (let i = 0; i < n; i++) { L[i] *= db(gainDb); R[i] *= db(gainDb); }