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 tools: parallel voices + a voice cache for check/render, the peak's bar and parts, mp3 never deletes x.wav, --fit keeps a tempo that already fits

Alex Greenshpun98451ed6c8b9
diff --git a/skills/anidoodle/engine/src/canvas-core/music/guards.ts b/skills/anidoodle/engine/src/canvas-core/music/guards.ts
index be75780..667a2eb 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/guards.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/guards.ts
@@ -11,8 +11,9 @@
// Thresholds are calibrated so the loved reference (Kevin Ngo piano) PASSES and the ghost fixture
// (slow pad, long tail, no rhythm, no cadence) FAILS; see tools/music.mjs `guards`.
import { onsets, mono, loudness } from "./meter";
-import { renderPiece, type Rendered } from "./render";
-import { pcOf } from "./theory";
+import { renderPiece, type Rendered, type RenderOpts, type VoiceCache } from "./render";
+import { perform, type Performance } from "./perform";
+import { pcOf, nameOf } from "./theory";
import { beatsPerBar, type Piece, type Role } from "./plan";
export const GHOST = { maxOnsetsPerBeat: 1.0, minSustainedShare: 0.6, reverbWithinDb: -12, windowBars: 4, fallbackWindowS: 8 };
@@ -80,27 +81,57 @@ const bandDb = (L: Float32Array, R: Float32Array, sr: number, spans: [number, nu
return spans.map(([a, b]) => { let e = 0; for (let i = Math.round(a * sr); i < Math.min(z.length, Math.round(b * sr)); i++) e += z[i] * z[i]; return 10 * Math.log10(e / Math.max(1, (b - a) * sr) + 1e-15); });
};
-/** Masking: per bar where the melody sounds, melody band energy minus the loudest other role's (dB). Pass: >= 3 dB in >= 80 % of those bars. */
-export const maskingCheck = (piece: Piece, sr: number, o: { seconds?: number; tempo?: number } = {}) => {
+/**
+ * Masking, in three steps so a pool can render the roles in parallel (tools/music.mjs check):
+ * `maskingPlan` (which roles, which bars; each role plays exactly as it does in the mix: the same key
+ * presses, the same final ritard), `roleBandDb` (one role rendered alone, its 500 Hz-4 kHz energy per
+ * bar), `maskingFromBands` (the verdict). `maskingCheck` runs all three in turn.
+ */
+export const maskingPlan = (piece: Piece, o: { seconds?: number; tempo?: number; perf?: Performance; cache?: VoiceCache } = {}) => {
const roles = [...new Set(piece.parts.flatMap((p) => p.notes.map((n) => n.role)))] as Role[];
if (!roles.includes("melody")) return null;
- const only = (r: Role) => ({ ...piece, parts: piece.parts.map((p) => ({ ...p, notes: p.notes.filter((n) => n.role === r) })) });
- const base = { seconds: o.seconds, tempo: o.tempo, master: "none" as const };
- const mel = renderPiece(only("melody"), sr, base);
+ const perf = o.perf ?? perform(piece, o.tempo ?? piece.plan.tempo, { expressive: true });
+ const played = (r: Role): Performance => ({ ...perf, parts: perf.parts.map((p) => ({ ...p, keys: p.keys.filter((k) => k.role === r) })) });
const bpb = beatsPerBar(piece.plan.meter), total = piece.plan.sections.reduce((a, s) => a + s.bars * bpb, 0);
- const spans: [number, number][] = []; for (let b = 0; b < total; b += bpb) spans.push([mel.perf.sec(b), mel.perf.sec(b + bpb)]);
+ const spans: [number, number][] = []; for (let b = 0; b < total; b += bpb) spans.push([perf.sec(b), perf.sec(b + bpb)]);
const melNotes = piece.parts.flatMap((p) => p.notes.filter((n) => n.role === "melody"));
const sounding = spans.map((_, i) => melNotes.some((n) => n.t < (i + 1) * bpb && n.t + n.d > i * bpb));
- const m = bandDb(mel.L, mel.R, sr, spans);
- const others = roles.filter((r) => r !== "melody").map((r) => { const x = renderPiece(only(r), sr, base); return { role: r, db: bandDb(x.L, x.R, sr, spans) }; });
- const margins = spans.map((_, i) => (sounding[i] ? m[i] - Math.max(...others.map((x) => x.db[i]), -300) : NaN)).filter((x) => Number.isFinite(x));
+ return { roles, spans, sounding, opts: (r: Role): RenderOpts => ({ seconds: o.seconds, tempo: o.tempo, master: "none", cache: o.cache, perf: played(r) }) };
+};
+export type MaskingPlan = NonNullable<ReturnType<typeof maskingPlan>>;
+export const roleBandDb = (piece: Piece, sr: number, opts: RenderOpts, spans: [number, number][]) => { const x = renderPiece(piece, sr, opts); return bandDb(x.L, x.R, sr, spans); };
+export const maskingFromBands = (pl: MaskingPlan, bands: Partial<Record<Role, number[]>>) => {
+ const m = bands.melody!, others = pl.roles.filter((r) => r !== "melody").map((r) => ({ role: r, db: bands[r]! }));
+ const margins = pl.spans.map((_, i) => (pl.sounding[i] ? m[i] - Math.max(...others.map((x) => x.db[i]), -300) : NaN)).filter((x) => Number.isFinite(x));
const ok = margins.filter((x) => x >= 3).length / Math.max(1, margins.length);
return { pass: ok >= 0.8, shareOfBarsClear: ok, worstMarginDb: Math.min(...margins), meanMarginDb: margins.reduce((a, b) => a + b, 0) / Math.max(1, margins.length) };
};
+/** Masking: per bar where the melody sounds, melody band energy minus the loudest other role's (dB). Pass: >= 3 dB in >= 80 % of those bars. */
+export const maskingCheck = (piece: Piece, sr: number, o: { seconds?: number; tempo?: number; perf?: Performance; cache?: VoiceCache } = {}) => {
+ const pl = maskingPlan(piece, o); if (!pl) return null;
+ return maskingFromBands(pl, Object.fromEntries(pl.roles.map((r) => [r, roleBandDb(piece, sr, pl.opts(r), pl.spans)])));
+};
/** Everything at once for a rendered piece. */
-export const guardReport = (r: Rendered, sr: number, seconds: number, o: { masking?: boolean } = {}) => {
+export const guardReport = (r: Rendered, sr: number, seconds: number, o: { masking?: boolean; cache?: VoiceCache; /** each role's band energy, rendered elsewhere (a worker pool) from maskingPlan */ bands?: Partial<Record<Role, number[]>> } = {}) => {
const bpb = beatsPerBar(r.piece.plan.meter);
const ghost = ghostCheck([r.L, r.R], sr, { bpm: r.tempo, beatsPerBar: bpb, wet: r.wet, dry: r.dry, cadences: cadenceTimes(r) });
- return { ghost: { pass: ghost.pass, failures: ghost.failures, windows: ghost.windows.length, minOnsetsPerBeat: Math.min(...ghost.windows.map((w) => w.onsetsPerBeat)), maxSustainedShare: Math.max(...ghost.windows.map((w) => w.sustainedShare)) }, reverb: reverbCheck(r, sr), masking: o.masking === false ? null : maskingCheck(r.piece, sr, { seconds, tempo: r.tempo }), lufs: loudness([r.L, r.R], sr).integrated };
+ return { ghost: { pass: ghost.pass, failures: ghost.failures, windows: ghost.windows.length, minOnsetsPerBeat: Math.min(...ghost.windows.map((w) => w.onsetsPerBeat)), maxSustainedShare: Math.max(...ghost.windows.map((w) => w.sustainedShare)) }, reverb: reverbCheck(r, sr), masking: o.masking === false ? null : o.bands ? maskingFromBands(maskingPlan(r.piece, { seconds, tempo: r.tempo, perf: r.perf })!, o.bands) : maskingCheck(r.piece, sr, { seconds, tempo: r.tempo, perf: r.perf, cache: o.cache }), lufs: loudness([r.L, r.R], sr).integrated };
+};
+
+/**
+ * Where the master's peak is, for the note that the -1 dBTP ceiling capped the gain: the sample peak
+ * of the mastered mix (the true peak sits within a sample of it), its bar and beat, and the parts
+ * piling up there: each stem's peak within 10 ms of it (needs a render with `stems: true`), loudest
+ * first, the ones within 10 dB of the loudest, with the notes they are sounding.
+ */
+export const peakReport = (r: Rendered, sr: number) => {
+ let at = 0, m = 0; for (let i = 0; i < r.L.length; i++) { const a = Math.max(Math.abs(r.L[i]), Math.abs(r.R[i])); if (a > m) { m = a; at = i; } }
+ const t = at / sr, bpb = beatsPerBar(r.piece.plan.meter), total = r.piece.plan.sections.reduce((a, s) => a + s.bars * bpb, 0);
+ let lo = -(r.piece.plan.pickupBeats ?? 0), hi = total + 16; for (let k = 0; k < 40; k++) { const mid = (lo + hi) / 2; if (r.perf.sec(mid) <= t) lo = mid; else hi = mid; }
+ const b = Math.round(lo * 4) / 4, bar = Math.floor(b / bpb + 1e-9), beat = +(b - bar * bpb + 1).toFixed(2), w = Math.round(0.01 * sr);
+ const parts = Object.entries(r.stems).map(([id, [L, R]]) => { let p = 0; for (let i = Math.max(0, at - w); i < Math.min(L.length, at + w); i++) p = Math.max(p, Math.abs(L[i]), Math.abs(R[i]));
+ const pi = r.piece.parts.findIndex((x) => x.id === id), notes = pi < 0 ? [] : [...new Set(r.perf.parts[pi].keys.filter((k) => k.t <= t + 0.01 && k.off + 0.05 >= t).map((k) => nameOf(k.p)))];
+ return { id, db: 20 * Math.log10(p + 1e-12), notes: r.piece.parts[pi]?.role === "drum" ? [] : notes }; }).filter((x) => x.db > -120).sort((a, b) => b.db - a.db);
+ return { sec: t, bar: bar + 1, beat, dbfs: 20 * Math.log10(m + 1e-12), parts: parts.filter((x) => x.db >= (parts[0]?.db ?? 0) - 10).slice(0, 6) };
};
diff --git a/skills/anidoodle/engine/src/canvas-core/music/render.ts b/skills/anidoodle/engine/src/canvas-core/music/render.ts
index df06efb..7b3d021 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts
@@ -11,7 +11,7 @@ import * as O from "./orchestra";
import { 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 { pumpCurve, lofiStem, lofiMaster, LOFI_DUSTY, type LofiFx } from "./lofiFx";
import { loudness, truePeak, stemBalance } from "./meter";
import { STYLES } from "./tables";
import { rng as mkRng } from "../core";
@@ -19,7 +19,67 @@ import { eqChain, runEq, stereoPan, compress, saturate, bandWidth, smoothLimiter
import { reverb, type Space } from "./mixReverb";
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 };
+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;
+ /** play this performance instead of performing the piece (the guards render one role of the SAME performance the mix plays) */ perf?: Performance;
+ /** reuse synthesized voices by their exact inputs (tools/music.mjs check: each part is synthesized once for the mix, the guards and the stems); output is bit-identical */ cache?: VoiceCache };
+
+/**
+ * Voice synthesis as data: one part's voice, its exact inputs. `runVoiceJob` is the only place a
+ * voice is synthesized, so a cache hit (or a worker that ran the same job) is bit-identical to a
+ * fresh synthesis. Keyed by the whole job, so a different key, option, length or seed never hits.
+ */
+export type VoiceOut = { L: Float32Array; R: Float32Array; halo?: Float32Array; /** the transient gain's reduction (dB), when the job ran it */ transientGr?: number };
+/** Per-stem processing that depends on nothing but the stem (sound v2): the lo-fi stem colour, the amp, the gentle master's transient gain. */
+type StemPost = { lofi?: LofiFx; id: string; seed: number; period?: number; amp?: number; transient?: { maxDb: number; thrDb: number; ratio: number } };
+export type VoiceJob =
+ | { kind: "voice"; inst: Part["inst"]; opts?: Part["opts"]; keys: Played[]; sr: number; n: number; seed: number; ctx: KitCtx; post?: StemPost }
+ | { kind: "piano"; keys: Played[]; pedal: Performance["pedal"]; sr: number; n: number; seed: number; opts?: Part["opts"]; v1?: PianoOpts; post?: StemPost };
+export type VoiceCache = Map<string, VoiceOut>;
+export const runVoiceJob = (j: VoiceJob): VoiceOut => {
+ let out: VoiceOut;
+ if (j.kind === "piano") { const r = j.v1 ? renderPiano(j.keys, j.pedal, j.sr, j.n, j.v1, j.seed) : renderPianoV2(j.keys, j.pedal, j.sr, j.n, j.opts ?? {}, j.seed); out = { L: r.L, R: r.R, halo: r.halo }; }
+ else out = voice({ id: "", role: "accomp", notes: [], inst: j.inst, ...(j.opts ? { opts: j.opts } : {}) }, j.keys, j.sr, j.n, j.seed, j.ctx);
+ const q = j.post; if (!q) return out;
+ if (q.lofi) lofiStem(q.lofi, q.id, out.L, out.R, j.sr, j.keys.map((k) => k.t), q.seed, q.period);
+ if (q.amp !== undefined) ampSim(out.L, out.R, j.sr, q.amp); // a guitar (or any DI) through the amp + cab
+ if (q.transient) out.transientGr = transientGain(out.L, out.R, j.sr, q.transient);
+ return out;
+};
+export const voiceJobKey = (j: VoiceJob) => JSON.stringify(j);
+const copyOut = (v: VoiceOut): VoiceOut => ({ ...v, L: Float32Array.from(v.L), R: Float32Array.from(v.R), ...(v.halo ? { halo: Float32Array.from(v.halo) } : {}) });
+/** A sound-v2 part's job: its voice, then the stem processing (renderV2 and voiceJobs both build jobs here, so their keys agree). */
+const v2Job = (piece: Piece, pt: Part, pi: number, keys: Played[], perf: Performance, sr: number, n: number, o: RenderOpts, ctx: KitCtx): VoiceJob => {
+ const style = STYLES[piece.plan.style], prof = mixProfile(style.id, piece.mix), tempo = o.tempo ?? piece.plan.tempo;
+ 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;
+ // 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, amp = typeof pt.opts?.amp === "number" ? (pt.opts.amp as number) : undefined;
+ const transient = style.master === "gentle" && tg && isStruck(pt.inst, pt.opts) ? tg : undefined;
+ const post: StemPost | undefined = lofi || amp !== undefined || transient ? { id: pt.id, seed: piece.seed, ...(lofi ? { lofi, period } : {}), ...(amp !== undefined ? { amp } : {}), ...(transient ? { transient } : {}) } : undefined;
+ const base = pt.inst === "piano" ? (o.piano ? { kind: "piano" as const, keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, v1: o.piano } : { kind: "piano" as const, keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, opts: pt.opts ?? {} })
+ : { kind: "voice" as const, inst: pt.inst, opts: pt.opts, keys, sr, n, seed: piece.seed * 101 + pi, ctx };
+ return post ? { ...base, post } : base;
+};
+/** Synthesize (or reuse) one voice. The caller owns the returned arrays (it mutates them in place). */
+const synth = (j: VoiceJob, cache?: VoiceCache): VoiceOut => {
+ if (!cache) return runVoiceJob(j);
+ const k = voiceJobKey(j); let v = cache.get(k); if (!v) { v = runVoiceJob(j); cache.set(k, v); }
+ return copyOut(v);
+};
+/** The voice jobs a render will run (for a pool that synthesizes them in parallel into a cache first). Mirrors renderLegacy / renderV2 exactly. */
+export const voiceJobs = (piece: Piece, sr: number, o: RenderOpts = {}): VoiceJob[] => {
+ const expressive = o.expressive ?? true, tempo = o.tempo ?? piece.plan.tempo;
+ const perf = o.perf ?? perform(piece, tempo, { expressive, flatVelocity: o.flatVelocity });
+ const n = o.seconds ? Math.round(o.seconds * sr) : Math.ceil((perf.lastOnset + piece.tail) * sr), jobs: VoiceJob[] = [];
+ const kitCtx: KitCtx = piece.legacy ? { legacy: true } : { 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;
+ if (!piece.legacy) jobs.push(v2Job(piece, pt, pi, keys, perf, sr, n, o, kitCtx));
+ else if (pt.inst === "piano") jobs.push({ kind: "piano", keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, v1: o.piano ?? PIANO_REAL });
+ else jobs.push({ kind: "voice", inst: pt.inst, opts: pt.opts, keys, sr, n, seed: piece.seed * 101 + pi, ctx: kitCtx });
+ });
+ return jobs;
+};
/** 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; transientGr?: number; space?: string } };
@@ -67,7 +127,7 @@ export const renderPiece = (piece: Piece, sr: number, o: RenderOpts = {}): Rende
/** The pre-v2 render, frozen: the shipped launch score renders through this bit for bit. */
const renderLegacy = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
const expressive = o.expressive ?? true, tempo = o.tempo ?? piece.plan.tempo, style = STYLES[piece.plan.style];
- const perf = perform(piece, tempo, { expressive, flatVelocity: o.flatVelocity });
+ const perf = o.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 sends: [Float32Array, Float32Array, number][] = [];
@@ -78,8 +138,8 @@ const renderLegacy = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered =>
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, { legacy: true }); sL = r.L; sR = r.R; }
+ if (pt.inst === "piano") { const r = synth({ kind: "piano", keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, v1: o.piano ?? PIANO_REAL }, o.cache); 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 = synth({ kind: "voice", inst: pt.inst, opts: pt.opts, keys, sr, n, seed: piece.seed * 101 + pi, ctx: { legacy: true } }, o.cache); 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; } }
@@ -125,7 +185,7 @@ const energy = (L: Float32Array, R: Float32Array) => { let e = 0; for (let i = 0
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 perf = o.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
@@ -142,13 +202,10 @@ const renderV2 = (piece: Piece, sr: number, o: RenderOpts = {}): Rendered => {
const keys = perf.parts[pi].keys; if (!keys.length) return;
let sL: Float32Array, sR: Float32Array;
// piano v2 (keysPiano.ts) unless the caller asks for the v1 model by passing RenderOpts.piano
- if (pt.inst === "piano") { const old = !!o.piano, r = old ? renderPiano(keys, perf.pedal, sr, n, o.piano!, piece.seed + pi) : renderPianoV2(keys, perf.pedal, sr, n, pt.opts ?? {}, piece.seed + pi); sL = r.L; sR = r.R; if (old ? o.piano!.pedal : pt.opts?.pedal !== false) 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, 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));
+ // the voice, then its stem processing (lo-fi colour, amp, transient gain): one job (v2Job), cached as a whole
+ const r = synth(v2Job(piece, pt, pi, keys, perf, sr, n, o, kitCtx), o.cache); sL = r.L; sR = r.R;
+ if (pt.inst === "piano" && (o.piano ? o.piano.pedal : pt.opts?.pedal !== false)) for (let i = 0; i < n; i++) haloL[i] += r.halo![i] * db(pt.gainDb ?? 0);
+ if (r.transientGr !== undefined) report.transientGr = Math.max(report.transientGr ?? 0, r.transientGr);
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, after its pan): the EQ is energy-neutral in the audible band
@@ -232,18 +289,33 @@ export const master = (L: Float32Array, R: Float32Array, sr: number, masterMode:
* repeats, pad release) folded back onto the start, then mastered once. Tape wow and flutter are
* snapped to a whole number of cycles per loop so the pitch drift meets itself at the seam.
*/
-export const renderLoop = (piece: Piece, sr: number): Rendered & { loopS: number } => {
+/** What renderLoop renders before it folds the tail: the loop's piece (no ritard, tape cycles snapped) and its length. */
+export const loopSource = (piece: Piece) => {
const beats = piece.plan.sections.reduce((a, s) => a + s.bars * beatsPerBar(piece.plan.meter), 0), loopS = (beats * 60) / piece.plan.tempo;
const snap = (hz: number) => Math.max(1, Math.round(hz * loopS)) / loopS, tp = piece.fx?.tape;
const p: Piece = { ...piece, plan: { ...piece.plan, loop: true, ritard: 1 }, fx: piece.fx && { ...piece.fx, tape: tp && { ...tp, wowHz: snap(tp.wowHz ?? 0.45), flutterHz: snap(tp.flutterHz ?? 6.2) } } };
- const r = renderPiece(p, sr, { seconds: loopS + piece.tail, master: "none" }), n = Math.round(loopS * sr), L = r.L.slice(0, n), R = r.R.slice(0, n);
+ return { p, loopS, opts: { seconds: loopS + piece.tail, master: "none" as const } };
+};
+export const renderLoop = (piece: Piece, sr: number, o: { cache?: VoiceCache; stems?: boolean } = {}): Rendered & { loopS: number } => {
+ const { p, loopS, opts } = loopSource(piece);
+ const r = renderPiece(p, sr, { ...opts, cache: o.cache, stems: o.stems }), n = Math.round(loopS * sr), L = r.L.slice(0, n), R = r.R.slice(0, n);
for (let i = n; i < r.L.length; i++) { L[i - n] += r.L[i]; R[i - n] += r.R[i]; }
const mode = STYLES[piece.plan.style].master, gainDb = master(L, R, sr, mode, !piece.legacy);
return { ...r, L, R, gainDb, masterMode: mode, loopS };
};
-/** The score for a film `seconds` long: the piece's own `refit` first (so it still ends on a phrase), then fitToDuration for the tempo. */
-export const fitScore = (piece: Piece, seconds: number) => fitToDuration(piece.refit ? piece.refit(seconds) : piece, seconds);
+/**
+ * Does the piece as written already fit `seconds`? Its last onset plus its tail lands within
+ * max(0.5 s, one beat) of the length, and the tail keeps at least half its ring. Then a fit keeps
+ * the written tempo and form, and the tail takes up the difference: music composed to picture must
+ * not drift off its hits because the final ritard made the last bar a little longer.
+ */
+export const fitsAsWritten = (piece: Piece, seconds: number) => {
+ const slack = seconds - (perform(piece, piece.plan.tempo, { expressive: true }).lastOnset + piece.tail);
+ return Math.abs(slack) <= Math.max(0.5, 60 / piece.plan.tempo) && piece.tail + slack >= 0.5 * piece.tail;
+};
+/** The score for a film `seconds` long: as written when it already fits (fitsAsWritten), else the piece's own `refit` first (so it still ends on a phrase), then fitToDuration for the tempo. */
+export const fitScore = (piece: Piece, seconds: number) => fitToDuration(!fitsAsWritten(piece, seconds) && piece.refit ? piece.refit(seconds) : piece, seconds);
/** Each part's stem level against the piece's targets (unmastered, pre-room: the mix as the parts were set). */
export const measureStems = (piece: Piece, sr: number, o: RenderOpts = {}, tolDb = 3) => {
@@ -271,6 +343,7 @@ export const limiter = (L: Float32Array, R: Float32Array, sr: number, ceil: numb
export const fitToDuration = (piece: Piece, seconds: number): { piece: Piece; tempo: number; order: number[]; form: string } => {
const st = STYLES[piece.plan.style], T0 = piece.plan.tempo, lo = Math.max(st.tempo[0] * 0.9, T0 * 0.82), hi = Math.min(st.tempo[1] * 1.1, T0 * 1.18);
const secs = piece.plan.sections, want = seconds - piece.tail;
+ if (fitsAsWritten(piece, seconds)) return { piece, tempo: T0, order: secs.map((_, i) => i), form: "full" };
const solve = (p: Piece) => { let tempo = T0; for (let k = 0; k < 5; k++) tempo *= perform(p, tempo, { expressive: true }).lastOnset / want; return tempo; };
const opt = secs.map((s, i) => (s.optional ? i : -1)).filter((i) => i >= 0);
const grp = secs.map((s, i) => (s.repeatable ? i : -1)).filter((i) => i >= 0);
diff --git a/skills/anidoodle/engine/tools/music-unit.mjs b/skills/anidoodle/engine/tools/music-unit.mjs
index 69a711c..bc31ee0 100644
--- a/skills/anidoodle/engine/tools/music-unit.mjs
+++ b/skills/anidoodle/engine/tools/music-unit.mjs
@@ -94,10 +94,16 @@ pass(`${Object.keys(M.VOCAB).length} style vocabularies compose, render (true pe
for (const secs of [70, 90, 110]) {
const f = M.fitScore(M.launchLofi3(), secs), perf = M.perform(f.piece, f.tempo, { expressive: true }), p = f.piece;
assert.equal(p.harmony[p.harmony.length - 1].name, "Cmaj9"); assert.equal(f.form, "full");
- assert(Math.abs(perf.lastOnset - (secs - p.tail)) < 0.05); assert(f.tempo >= 90 * 0.88 && f.tempo <= 90 * 1.12);
+ assert(Math.abs(perf.lastOnset - (secs - p.tail)) < 0.05 || (f.tempo === 90 && M.fitsAsWritten(p, secs))); assert(f.tempo >= 90 * 0.88 && f.tempo <= 90 * 1.12);
pass(`fit ${secs} s: ${p.plan.sections[0].bars} bars at ${f.tempo.toFixed(1)} bpm, last onset ${perf.lastOnset.toFixed(2)} s on Cmaj9`);
}
{ const a = M.filmAudio(M.launchLofi3(), 40)(16000); assert.equal(a[0].length, 40 * 16000); pass("filmAudio: exactly the film's length"); }
+// composed to length: the fit keeps the written tempo (the final ritard must not move the picture's hits); a form that does not fit is still fitted
+{ const p = M.composePiece({ ...M.testMaterial(M.VOCAB.house, { bars: 8 }), tail: 2 }), natural = M.perform(p, p.plan.tempo, { expressive: true }).lastOnset + p.tail, bar = (4 * 60) / p.plan.tempo;
+ const kept = M.fitScore(p, Math.round(natural)), moved = M.fitScore(p, natural + 2 * bar);
+ assert.equal(kept.tempo, p.plan.tempo); assert.equal(M.perform(kept.piece, kept.tempo, { expressive: true }).sec(4 * 4), M.perform(p, p.plan.tempo, { expressive: true }).sec(4 * 4), "bar 5 lands where it was written");
+ assert(moved.tempo !== p.plan.tempo || moved.piece.plan.sections.length !== p.plan.sections.length);
+ pass(`fit: a score composed to ${Math.round(natural)} s keeps ${p.plan.tempo} bpm (natural end ${natural.toFixed(2)} s: the tail takes the difference); ${(natural + 2 * bar).toFixed(1)} s is refitted`); }
// 9. Composer-facing fixes (from the blind composer's report).
{ const t = M.testMaterial(M.VOCAB.playful, { bars: 4 }), sec = t.sections[0];
@@ -143,4 +149,25 @@ for (const secs of [70, 90, 110]) {
const r = M.novelty(quoted, { src: () => src }); assert(!r.pass && r.rows[0].reusedFragments > 0, "a transposed quote must be caught");
pass(`novelty: a transposed 6-note quote of a shipped line fails (${r.rows[0].reusedFragments} reused fragments)`); }
+// 11. Speed without a changed bit: the voice cache and the parallel pool render exactly what a serial render does.
+{ const p = M.composePiece(M.testMaterial(M.VOCAB.lofi, { bars: 4 })), cache = new Map(), fresh = md5(M.renderPiece(p, SR));
+ assert.equal(md5(M.renderPiece(p, SR, { cache })), fresh); assert(cache.size > 3); assert.equal(md5(M.renderPiece(p, SR, { cache })), fresh, "a cache hit must be bit-identical");
+ const r = M.renderPiece(p, SR, { cache }), a = M.maskingCheck(p, SR, { seconds: r.L.length / SR, tempo: r.tempo, perf: r.perf }), pl = M.maskingPlan(p, { seconds: r.L.length / SR, tempo: r.tempo, perf: r.perf, cache });
+ assert.deepEqual(M.maskingFromBands(pl, Object.fromEntries(pl.roles.map((x) => [x, M.roleBandDb(p, SR, pl.opts(x), pl.spans)]))), a, "masking from cached role renders must equal the serial guard");
+ const sel = new Float64Array(Array.from({ length: 999 }, (_, i) => Math.sin(i * 7.3) * 5)), sorted = [...sel].sort((x, y) => x - y); assert.equal(M.kth(Float64Array.from(sel), 999, 899), sorted[899]);
+ pass(`voice cache: a hit renders bit-identically (md5 ${fresh.slice(0, 8)}), masking from cached role renders equals the serial guard; quickselect = sort`); }
+// the master note names where its peak is: a spike planted on the lead at bar 3 beat 2 is found there, the lead named first
+{ const p = M.composePiece(M.testMaterial(M.VOCAB.lofiElectronic, { bars: 4 })), r = M.renderPiece(p, SR, { stems: true }), bpb = M.beatsPerBar(p.plan.meter), at = Math.round(r.perf.sec(2 * bpb + 1) * SR);
+ r.L[at] = 1.5; r.stems.lead[0][at] = 1.5; const pk = M.peakReport(r, SR);
+ assert.equal(pk.bar, 3); assert.equal(pk.beat, 2); assert.equal(pk.parts[0].id, "lead"); assert(pk.parts[0].notes.length > 0);
+ pass(`peak report: bar ${pk.bar} beat ${pk.beat}, ${pk.parts.map((x) => `${x.id} ${x.notes.join(" ")}`).join(", ")}`); }
+{ const { spawnSync } = await import("node:child_process"), { mkdirSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync } = await import("node:fs");
+ const dir = join(import.meta.dirname, "../out/music-unit"); rmSync(dir, { recursive: true, force: true }); mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, "x.wav"), "keep me");
+ const run = spawnSync(process.execPath, [join(import.meta.dirname, "music.mjs"), "render", "lofiNostalgic", join(dir, "x.mp3"), "--seconds", "4", "--verify"], { encoding: "utf8" });
+ const json = JSON.parse(run.stdout.slice(run.stdout.indexOf("{"), run.stdout.indexOf("\n}") + 2));
+ assert.equal(readFileSync(join(dir, "x.wav"), "utf8"), "keep me", "rendering x.mp3 must never touch x.wav");
+ assert(existsSync(join(dir, "x.mp3")) && readdirSync(dir).length === 2, `leftovers: ${readdirSync(dir).join(", ")}`);
+ assert.equal(json.deterministic, true, "the parallel, cached render must equal a serial one bit for bit");
+ pass("render x.mp3 leaves an existing x.wav alone (no temp left behind); the parallel cached render equals a serial one (--verify)"); }
console.log(`music unit: ${ok.length} checks PASS in ${((Date.now() - t0) / 1000).toFixed(0)} s`);
diff --git a/skills/anidoodle/engine/tools/music.mjs b/skills/anidoodle/engine/tools/music.mjs
index 5002272..dafe165 100644
--- a/skills/anidoodle/engine/tools/music.mjs
+++ b/skills/anidoodle/engine/tools/music.mjs
@@ -11,7 +11,8 @@
// node tools/music.mjs craft <piece> [more ...] [--json] # how it is WRITTEN: melody, harmony, rhythm, tension, mood fit (no render, advisory; errors fail)
// node tools/music.mjs novelty <piece> [more ...] # vs every shipped piece (and pairwise when several); FAILS above the threshold or on a reused fragment
// node tools/music.mjs list # shipped demos and fixtures
-// node tools/music.mjs render <piece> <out.wav|out.mp3> [--seconds 45] [--flat] [--tempo 66] [--fit] [--loop] [--stems]
+// node tools/music.mjs render <piece> <out.wav|out.mp3> [--seconds 45] [--flat] [--tempo 66] [--fit] [--loop] [--stems] [--verify]
+// (--verify renders a second time, serially and without the voice cache, and reports whether it is bit-identical)
// node tools/music.mjs stems <piece> [--seconds 45] [--fit] # each part's stem RMS vs its target, flags > 3 dB off
// (--fit uses fitScore: the piece's refit, e.g. a composed score repeats its stretch section so it still ends on its outro;
// --loop renders a seamless loop, automatic for a plan.loop piece. LUFS alone once hid a sub 7-10 dB too hot:
@@ -25,12 +26,71 @@ import { execFileSync } from "node:child_process";
import { writeFileSync, readFileSync, existsSync, mkdirSync, unlinkSync } from "node:fs";
import { dirname, join, resolve, basename } from "node:path";
import { fileURLToPath } from "node:url";
+import { Worker } from "node:worker_threads";
+import { availableParallelism } from "node:os";
const here = dirname(fileURLToPath(import.meta.url));
const SR = 48000;
+let ENGINE_URL = "";
const load = async () => {
const r = await build({ entryPoints: [join(here, "../src/canvas-core/music/index.ts")], bundle: true, write: false, format: "esm", platform: "neutral", target: "es2022", logLevel: "error" });
- return import("data:text/javascript;base64," + Buffer.from(r.outputFiles[0].text).toString("base64"));
+ ENGINE_URL = "data:text/javascript;base64," + Buffer.from(r.outputFiles[0].text).toString("base64");
+ return import(ENGINE_URL);
+};
+
+// ---------------------------------------------------------------- voices in parallel
+// Synthesis is most of a render, and each part's voice is a pure function of its inputs (render.ts
+// VoiceJob). check/render/stems list every voice job they will need (the mix at 48 kHz, each guard
+// role, the stems at 24 kHz), run the distinct ones on a worker pool, and render from that cache:
+// the same code on the same inputs, so the audio is bit-identical to a serial render
+// (tools/music-unit.mjs checks it). ANIDOODLE_THREADS=1 renders serially.
+const WORKER = `const { parentPort, workerData } = require("node:worker_threads");
+import(workerData).then((M) => {
+ parentPort.on("message", ({ id, job, task }) => {
+ if (job) { const o = M.runVoiceJob(job); parentPort.postMessage({ id, o }, [...new Set([o.L.buffer, o.R.buffer, o.halo && o.halo.buffer].filter(Boolean))]); return; }
+ const opts = { ...task.opts, cache: new Map(task.voices) };
+ parentPort.postMessage({ id, out: task.kind === "band" ? M.roleBandDb(task.piece, task.sr, opts, task.spans) : M.measureStems(task.piece, task.sr, opts) });
+ });
+ parentPort.postMessage({ ready: true });
+});`;
+const THREADS = () => Number(process.env.ANIDOODLE_THREADS) || Math.max(1, availableParallelism() - 1);
+/** Run messages on a pool of `threads` workers; resolves to their replies by id, in the order given. */
+const pool = (msgs, threads) => { const queue = msgs.slice(), out = new Map(); return Promise.all(Array.from({ length: Math.min(threads, msgs.length) }, () => new Promise((ok, fail) => {
+ const w = new Worker(WORKER, { eval: true, workerData: ENGINE_URL });
+ const next = () => { const x = queue.shift(); if (!x) { w.terminate(); ok(); return; } w.postMessage(x); };
+ w.on("message", (m) => { if (!m.ready) out.set(m.id, m); next(); }); w.on("error", fail);
+}))).then(() => out); };
+export const prewarm = async (M, cache, jobs) => {
+ const todo = new Map(); for (const j of jobs) { const k = M.voiceJobKey(j); if (!cache.has(k) && !todo.has(k)) todo.set(k, j); }
+ if (Math.min(todo.size, THREADS()) < 2) return; // the render synthesizes on demand
+ const queue = [...todo].sort((a, b) => b[1].n * b[1].keys.length - a[1].n * a[1].keys.length); // biggest first: the slowest voice starts at once
+ for (const [id, m] of await pool(queue.map(([id, job]) => ({ id, job })), THREADS())) cache.set(id, m.o);
+};
+/** What `check` renders besides the mix: the piece the mix plays (a loop's source), its performance, the mix's length, and the masking plan. */
+const checkPlan = (M, piece, tempo, seconds, loop = piece.plan.loop) => {
+ const src = loop ? M.loopSource(piece) : null, p = src ? src.p : piece, o = src ? src.opts : { seconds, tempo };
+ const perf = M.perform(p, o.tempo ?? p.plan.tempo, { expressive: true }), n = src ? Math.round(src.loopS * SR) : o.seconds ? Math.round(o.seconds * SR) : Math.ceil((perf.lastOnset + p.tail) * SR);
+ return { p, o, mask: M.maskingPlan(p, { seconds: n / SR, tempo: o.tempo, perf }) };
+};
+/** Every voice job `check` runs: the mix, each role the masking guard renders from the mix's performance, and the 24 kHz stems. */
+const checkJobs = (M, piece, tempo, seconds, { loop = piece.plan.loop, stems = true } = {}) => {
+ const { p, o, mask } = checkPlan(M, piece, tempo, seconds, loop);
+ return [...M.voiceJobs(p, SR, o), ...(mask ? mask.roles.flatMap((r) => M.voiceJobs(p, SR, mask.opts(r))) : []), ...(stems ? M.voiceJobs(piece, STEM_SR, { tempo, seconds }) : [])];
+};
+/** A piece or a performance as plain data for a worker (its functions stay behind: a refit, the tempo map). */
+const plain = (p) => { const { refit, shortForm, sec, ...rest } = p; void refit; void shortForm; void sec; return rest; };
+/**
+ * The guards' role renders and the stems, started on workers while the caller renders the mix: each
+ * gets the voices it needs from the cache (so nothing is synthesized twice) and returns numbers only.
+ * Resolves to { bands, stems } (either may be undefined: no melody, or stems not asked for).
+ */
+const startGuards = (M, cache, piece, tempo, seconds, { loop = piece.plan.loop, stems = true } = {}) => {
+ if (THREADS() < 2) return Promise.resolve({});
+ const { p, mask } = checkPlan(M, piece, tempo, seconds, loop), voices = (jobs) => jobs.map((j) => { const k = M.voiceJobKey(j); return [k, cache.get(k)]; }).filter(([, v]) => v);
+ const msgs = [];
+ if (mask) for (const r of mask.roles) { const { cache: _c, perf, ...opts } = mask.opts(r); msgs.push({ id: `band:${r}`, task: { kind: "band", piece: plain(p), sr: SR, spans: mask.spans, opts: { ...opts, perf: plain(perf) }, voices: voices(M.voiceJobs(p, SR, mask.opts(r))) } }); }
+ if (stems) msgs.push({ id: "stems", task: { kind: "stems", piece: plain(piece), sr: STEM_SR, opts: { tempo, seconds }, voices: voices(M.voiceJobs(piece, STEM_SR, { tempo, seconds })) } });
+ return pool(msgs, THREADS()).then((out) => ({ bands: mask ? Object.fromEntries(mask.roles.map((r) => [r, out.get(`band:${r}`).out])) : undefined, stems: out.get("stems")?.out }));
};
/** A piece by demo name, or from a module: "path.ts#export" (default export if no #). Material is composed. */
@@ -84,18 +144,20 @@ const onsetsFromData = (rendered) => {
for (const t of ts) if (t - last > 0.03) { c++; last = t; } return c;
};
-const renderOne = (M, spec) => {
+const renderOne = async (M, spec) => {
let piece = spec.make ? spec.make() : (M.DEMOS[spec.piece] ?? M.FIXTURES[spec.piece])();
let tempo = spec.tempo ?? piece.plan.tempo;
let form;
if (spec.fit) { const f = M.fitScore(piece, spec.seconds); piece = f.piece; tempo = f.tempo; form = `${f.form}: ${f.piece.plan.sections.map((x) => x.id).join(", ")}`; }
const opts = { seconds: spec.seconds, tempo };
if (spec.flat) Object.assign(opts, { expressive: false, piano: M.PIANO_FLAT, flatVelocity: 0.6 });
- const loop = spec.loop || piece.plan.loop, go = () => (loop ? M.renderLoop(piece, SR) : M.renderPiece(piece, SR, opts));
- const t0 = Date.now(), r = go(), ms = Date.now() - t0;
- // determinism: a second render must be bit-identical
- const r2 = go(); let same = true; for (let i = 0; i < r.L.length; i += 7) if (r.L[i] !== r2.L[i] || r.R[i] !== r2.R[i]) { same = false; break; }
- return { r, piece, tempo, ms, deterministic: same, form };
+ const loop = spec.loop || piece.plan.loop, go = (cache) => (loop ? M.renderLoop(piece, SR, { cache, stems: true }) : M.renderPiece(piece, SR, { ...opts, cache, stems: true }));
+ const t0 = Date.now(), par = spec.cache && !spec.flat; if (par) await prewarm(M, spec.cache, checkJobs(M, piece, tempo, spec.seconds, { loop, stems: spec.stems }));
+ const guards = par ? startGuards(M, spec.cache, piece, tempo, spec.seconds, { loop, stems: spec.stems }) : Promise.resolve({}); // on workers while the mix renders here
+ const r = go(spec.cache), ms = Date.now() - t0;
+ // determinism (--verify): a second, fully serial render without any cache must be bit-identical
+ let same = null; if (spec.verify) { const r2 = go(undefined); same = r.L.length === r2.L.length && r.L.every((x, i) => x === r2.L[i]) && r.R.every((x, i) => x === r2.R[i]); }
+ return { r, piece, tempo, ms, deterministic: same, form, guards: await guards };
};
const voicing = (M, piece, tempo, seconds, flat) => {
@@ -140,30 +202,36 @@ const main = async () => {
if (cmd === "check") {
const pc = await getPiece(M, args[0]); let piece = pc.make(), tempo = piece.plan.tempo, seconds = val("--seconds", undefined);
if (flag("--fit")) { const f = M.fitScore(piece, seconds); piece = f.piece; tempo = f.tempo; }
- const r = piece.plan.loop ? M.renderLoop(piece, SR) : M.renderPiece(piece, SR, { seconds, tempo });
- const ok = [report(M, r, piece), printStems(M, piece, tempo, seconds)?.ok !== false];
- { const cr = M.craftReport(piece, { centroidHz: M.centroid([r.L, r.R], SR).mean }); console.log(M.craftText(cr)); ok.push(!cr.findings.some((f) => f.level === "error")); }
+ const T = process.env.ANIDOODLE_TIMING ? (l) => console.error(`[timing] ${l} ${((Date.now() - T0) / 1000).toFixed(1)} s`) : () => {}, T0 = Date.now();
+ const cache = new Map(); await prewarm(M, cache, checkJobs(M, piece, tempo, seconds)); T("voices"); // every voice once, in parallel
+ const pending = startGuards(M, cache, piece, tempo, seconds).then((x) => { T("guard renders + stems"); return x; }); // the guards' role renders and the stems, on workers meanwhile
+ const r = piece.plan.loop ? M.renderLoop(piece, SR, { cache, stems: true }) : M.renderPiece(piece, SR, { seconds, tempo, cache, stems: true }); T("mix"); const g = await pending; // stems: the peak note names the parts that pile up
+ const ok = [report(M, r, piece, cache, g.bands), printStems(M, piece, tempo, seconds, cache, g.stems)?.ok !== false];
+ T("report + stems"); { const cr = M.craftReport(piece, { centroidHz: M.centroid([r.L, r.R], SR).mean }); console.log(M.craftText(cr)); ok.push(!cr.findings.some((f) => f.level === "error")); } T("craft");
if (!pc.demo) { const fam = [args[0]]; ok.push(printNovelty(M, args[0], M.novelty(piece, M.DEMOS, fam), "shipped pieces")); }
console.log(ok.every(Boolean) ? "CHECK PASS: now a human listens (an mp3 or wav on a page)" : "CHECK FAIL: fix the items above, then run check again");
if (!ok.every(Boolean)) process.exitCode = 1; return;
}
if (cmd === "render") {
- const [name, out] = args, pc = await getPiece(M, name); const { r, tempo, ms, deterministic, piece } = renderOne(M, { make: pc.make, seconds: val("--seconds", undefined), tempo: val("--tempo", undefined), flat: flag("--flat"), fit: flag("--fit") });
- const mp3 = out.endsWith(".mp3"), wav = mp3 ? out.replace(/\.mp3$/, ".wav") : out;
+ const [name, out] = args, pc = await getPiece(M, name), cache = new Map();
+ const { r, tempo, ms, deterministic, piece, guards } = await renderOne(M, { make: pc.make, seconds: val("--seconds", undefined), tempo: val("--tempo", undefined), flat: flag("--flat"), fit: flag("--fit"), loop: flag("--loop"), cache, verify: flag("--verify"), stems: flag("--stems") });
+ // an mp3 is encoded from a private intermediate next to it (never x.wav: rendering x.mp3 must not touch an x.wav you already have)
+ const mp3 = out.endsWith(".mp3"), wav = mp3 ? join(dirname(resolve(out)), `.${basename(out)}.${process.pid}.tmp.wav`) : out;
if (mp3 && (piece.plan.loop || flag("--loop"))) console.log("NOTE: mp3 is not gapless (encoder padding clicks at the loop point). Ship loops as .wav (or ogg/m4a with gapless metadata).");
writeWavFloat(wav, r.L, r.R);
- if (mp3) { execFileSync("ffmpeg", ["-v", "error", "-y", "-i", wav, "-c:a", "libmp3lame", "-b:a", "256k", out]); unlinkSync(wav); }
+ if (mp3) { try { execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "wav", "-i", wav, "-c:a", "libmp3lame", "-b:a", "256k", out]); } finally { if (existsSync(wav)) unlinkSync(wav); } }
const m = M.measure(decode(out), SR);
console.log(JSON.stringify({ file: out, tempo, renderMs: ms, deterministic, gainDb: r.gainDb, loopS: r.loopS, ...m, shortTerm: undefined }, null, 1));
- let good = report(M, r, piece);
- if (flag("--stems")) good = printStems(M, piece, tempo, val("--seconds", undefined)).ok && good;
+ let good = report(M, r, piece, cache, guards.bands);
+ if (flag("--stems")) good = printStems(M, piece, tempo, val("--seconds", undefined), cache, guards.stems).ok && good;
if (!good) process.exitCode = 1;
return;
}
if (cmd === "stems") {
const [name] = args; let piece = (await getPiece(M, name)).make(), tempo = piece.plan.tempo, seconds = val("--seconds", undefined);
if (flag("--fit")) { const f = M.fitScore(piece, seconds); piece = f.piece; tempo = f.tempo; }
- printStems(M, piece, tempo, seconds); return;
+ const cache = new Map(); await prewarm(M, cache, M.voiceJobs(piece, STEM_SR, { tempo, seconds }));
+ printStems(M, piece, tempo, seconds, cache); return;
}
if (cmd === "meter") { for (const f of args) { const m = M.measure(decode(f), SR), ff = ffmpegEbu(f); console.log(basename(f), `LUFS ${fmt(m.lufs)} (ffmpeg ${fmt(ff.I)}) LRA ${fmt(m.lra)} (ffmpeg ${fmt(ff.LRA)}) TP ${fmt(m.dbtp)} dBTP (ffmpeg ${fmt(ff.TP)}) onsets/s ${fmt(m.onsetsPerS, 2)} centroid ${fmt(m.centroidHz, 0)} Hz (energy-wtd ${fmt(m.centroidEnergyHz, 0)}) dur ${fmt(m.durationS, 2)} lastOnset ${fmt(m.lastOnsetS, 2)}`); } return; }
if (cmd === "score") { console.log(M.scoreText((await getPiece(M, args[0])).make())); return; }
@@ -194,8 +262,8 @@ const main = async () => {
mkdirSync(join(dir, "fixtures"), { recursive: true });
const results = [];
for (const s of set) {
- const { r, piece, tempo, ms, deterministic, form } = renderOne(M, s);
- const g = M.guardReport(r, SR, s.seconds, { masking: !s.noMasking });
+ const cache = new Map(), { r, piece, tempo, ms, deterministic, form } = await renderOne(M, { ...s, verify: true, cache, stems: false });
+ const g = M.guardReport(r, SR, s.seconds, { masking: !s.noMasking, cache });
const wav = join(dir, s.file + ".wav"); writeWavFloat(wav, r.L, r.R);
execFileSync("ffmpeg", ["-v", "error", "-y", "-i", wav, "-c:a", "aac", "-b:a", "192k", join(dir, s.file + ".m4a")]);
const m = M.measure(decode(wav), SR), ff = ffmpegEbu(wav), mm = M.measure(decode(join(dir, s.file + ".m4a")), SR);
@@ -278,14 +346,15 @@ const vocabMarkdown = (M) => {
};
/** Key/mode problems, composer warnings, the master (did the peak cap stop it short?), and the guards. Returns pass. */
-const report = (M, r, piece) => {
+const report = (M, r, piece, cache, bands) => {
let ok = true; const probs = M.planProblems(piece), warn = piece.warnings ?? [];
for (const p of probs) console.log(`PROBLEM ${p}`); ok = ok && !probs.length;
for (const w of warn) console.log(`WARNING ${w}`);
const lu = M.loudness([r.L, r.R], SR).integrated, tp = M.truePeak([r.L, r.R]).dbtp, target = r.masterMode === "dense" ? -14 : r.masterMode === "gentle" ? -16 : NaN;
console.log(`master ${r.masterMode}: ${fmt(lu)} LUFS (target ${fmt(target)}), true peak ${fmt(tp, 2)} dBTP`);
- if (lu < target - 0.5) console.log(`NOTE the master stopped ${fmt(target - lu)} dB short of its target: the true-peak ceiling (-1 dBTP) capped the gain. Peaks are a composing problem: stagger the bass under the loudest downbeat, roll the big chord, don't double the climax note, soften the one loudest hit.`);
- const g = M.guardReport(r, SR, r.L.length / SR);
+ if (lu < target - 0.5) { const pk = M.peakReport(r, SR), who = pk.parts.map((x, i) => `${x.id}${x.notes.length ? ` ${x.notes.join(" ")}` : ""} (${i ? `${fmt(x.db - pk.parts[0].db)} dB` : "loudest"})`).join(", ");
+ console.log(`NOTE the master stopped ${fmt(target - lu)} dB short of its target: the true-peak ceiling (-1 dBTP) capped the gain. The peak is at bar ${pk.bar} beat ${pk.beat} (${fmt(pk.sec, 2)} s)${who ? `, where ${who} pile up` : ""}. Peaks are a composing problem: stagger the bass under that downbeat, roll the big chord, don't double the climax note, soften the one loudest hit.`); }
+ const g = M.guardReport(r, SR, r.L.length / SR, { cache, bands });
console.log(`guards ghost ${g.ghost.pass ? "PASS" : "FAIL"} (${g.ghost.failures}/${g.ghost.windows} windows formless) | reverb ${g.reverb.pass ? "PASS" : "FAIL"} (worst tail ${fmt(g.reverb.worstDb)} dB vs dry) | masking ${g.masking ? `${g.masking.pass ? "PASS" : "FAIL"} (melody clear in ${Math.round(g.masking.shareOfBarsClear * 100)} % of bars, worst ${fmt(g.masking.worstMarginDb)} dB)` : "n/a (no melody)"}`);
if (g.masking && !g.masking.pass) console.log(`FIX masking: the melody must beat every other part in 500 Hz-4 kHz by 3 dB in 80 % of its bars. Guards win over stem targets: raise the lead (levels.lead, up to +${M.LEAD_HEADROOM_DB} dB over its target is allowed), thin or lower what sits in its register, or move it.`);
if (!g.ghost.pass) console.log("FIX ghost: sustained sound with too few onsets and no cadence. Add a rhythmic layer or motif, shorten tails, cadence.");
@@ -302,8 +371,8 @@ const printNovelty = (M, name, v, what) => {
/** Stem balance: each part's unmastered stem level (mid, gated 20 ms blocks: meter.stemRms) vs piece.stemTargets. Measured at 24 kHz, as the targets were. */
const STEM_SR = 24000;
-const printStems = (M, piece, tempo, seconds) => {
- const b = M.measureStems(piece, STEM_SR, { tempo, seconds });
+const printStems = (M, piece, tempo, seconds, cache, measured) => {
+ const b = measured ?? M.measureStems(piece, STEM_SR, { tempo, seconds, cache });
console.log(`stem balance: ${piece.title} (unmastered stem RMS of the mid over the blocks where it plays, vs target, +-${b.tolDb} dB; the lead may sit up to +${b.headroom.lead ?? 0} dB more for the masking guard; targets already include your mood controls' shifts)`);
for (const r of b.rows) console.log(` ${r.id.padEnd(9)} ${fmt(r.rmsDb).padStart(6)} dB ${r.targetDb === null ? "(no target)" : `target ${fmt(r.targetDb).padStart(6)} off ${(r.offDb >= 0 ? "+" : "") + fmt(r.offDb)} ${r.ok ? "ok" : `FLAG: ${r.offDb > 0 ? "too hot" : "too quiet"}`}`}`);
if (!Object.keys(piece.stemTargets ?? {}).length) console.log(" (this piece declares no stemTargets)");