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.

launchTemplate: the bed is the score fitted to the film (fitScore), not cut and faded; limit: true option; render reports loudness, the peak ceiling and the fitted tempo vs the grid

Alex Greenshpun4d133a689d2d
diff --git a/skills/anidoodle/engine/src/canvas-core/film.ts b/skills/anidoodle/engine/src/canvas-core/film.ts
index 64a8602..0dac60c 100644
--- a/skills/anidoodle/engine/src/canvas-core/film.ts
+++ b/skills/anidoodle/engine/src/canvas-core/film.ts
@@ -18,6 +18,8 @@ export type Film = { meta: {
holds?: [from: number, to: number][];
onTwos?: boolean;
step?: 1 | 2 | 3;
+ // the score as fitted to this film (fitScore): its tempo and form; render prints it next to the grid's bpm
+ score?: { tempo: number; form: string };
}; assets: Assets; shots: Shot[]; audio?: (sampleRate: number) => [Float32Array, Float32Array] };
// Cheap structural checks every adapter runs before frame 0. (The full gate is Phase 2.)
diff --git a/skills/anidoodle/engine/src/canvas-core/launchTemplate.ts b/skills/anidoodle/engine/src/canvas-core/launchTemplate.ts
index 8a32921..b053245 100644
--- a/skills/anidoodle/engine/src/canvas-core/launchTemplate.ts
+++ b/skills/anidoodle/engine/src/canvas-core/launchTemplate.ts
@@ -24,7 +24,7 @@ import {
} from "./launchKit";
import { beatGrid, bloomFrame, bloomRadius, makeCut, pic, type, typeFrame, type Seg, type TypeLine } from "./launchCut";
import { measure, writeOn } from "./kinetic";
-import { renderPiece } from "./music/render";
+import { fitScore, limiter, renderPiece } from "./music/render";
import { loudness, truePeak } from "./music/meter";
import * as families from "./music/pieces/families";
import * as launchPieces from "./music/pieces/launch";
@@ -61,6 +61,7 @@ export type LaunchSpec = {
// A Material (what compose.md writes) is composed here; a finished Piece is used as is.
score: (() => Piece | Material) | null;
audio?: Film["audio"]; // or your own finished mix; wins over score
+ limit?: boolean; // let a look-ahead limiter take the score's last peaks so it reaches -14 LUFS (default off: the peak ceiling wins)
};
const W = 1920, H = 1080;
@@ -207,24 +208,39 @@ export const makeLaunchFilm = (spec: LaunchSpec): Film & { cut: ReturnType<typeo
holds.push([cut.STARTS[k] + written + 1, cut.STARTS[k] + open + 1]);
});
holds.push([cut.N - END + 53, cut.N]); // the end card, all on: read it, screenshot it
+ // the score, fitted to the film's exact length so it ends on its phrase (fitScore: its stretch section
+ // repeated or dropped, then the tempo trimmed); the fitted tempo goes in meta so render prints it
+ // beside the grid's bpm (compose at the film's bpm and they stay within a few percent)
+ const bed = !spec.audio && spec.score ? scoreFit(asPiece(spec.score), cut.N, fps) : null;
return {
- meta: { title: `${spec.title} · launch`, W, H, fps, bpm: spec.bpm, durationFrames: cut.N, raster: "cpu", kind: "launch", holds },
+ meta: { title: `${spec.title} · launch`, W, H, fps, bpm: spec.bpm, durationFrames: cut.N, raster: "cpu", kind: "launch", holds, ...(bed ? { score: { tempo: bed.fit.tempo, form: bed.fit.form } } : {}) },
assets: { images: {} },
shots: [{ id: "cut", start: 0, end: cut.N, draw }],
- audio: spec.audio ?? (spec.score ? musicBed(asPiece(spec.score), cut.N, fps) : undefined),
+ audio: spec.audio ?? (bed ? musicBed(() => bed.fit, cut.N, fps, -14, { limit: spec.limit }) : undefined),
cut,
};
};
-// A music bed: the first `frames` of a composed piece, faded out over the last 1.2 s, then set to
-// `lufs` integrated (default -14, the one published cross-platform target) with the true peak held
-// at or under -1 dBTP. Compose the piece for this film, at the film's bpm, so the cuts sit on its downbeats.
-export const musicBed = (piece: () => Piece, frames: number, fps = 30, lufs = -14) => (sr: number): [Float32Array, Float32Array] => {
- const n = Math.round((frames / fps) * sr), L = new Float32Array(n), R = new Float32Array(n), m = renderPiece(piece(), sr);
- for (let i = 0; i < n && i < m.L.length; i++) { L[i] = m.L[i]; R[i] = m.R[i]; }
- const fade = Math.round(1.2 * sr); for (let i = 0; i < fade && i < n; i++) { const g = i / fade; L[n - 1 - i] *= g; R[n - 1 - i] *= g; }
+type Fit = ReturnType<typeof fitScore>;
+const scoreFit = (piece: () => Piece, frames: number, fps: number) => ({ fit: fitScore(piece(), frames / fps) });
+
+// A music bed: the piece FITTED to the film (fitScore, exactly as filmAudio: its stretch section
+// repeated or dropped so it lands on its outro, the tempo trimmed so the tail rings out on the last
+// frame; never cut and faded), then set to `lufs` integrated (default -14, the one published
+// cross-platform target) with the true peak held at or under -1 dBTP. A dynamic piece stops at the
+// peak ceiling first (render prints how far short); `limit: true` lets a look-ahead limiter take
+// those few peaks instead so the bed reaches the target. Compose the piece for this film at the
+// film's bpm, so the cuts sit on its downbeats. `piece` may return a Piece or a fitScore result.
+export const musicBed = (piece: () => Piece | Fit, frames: number, fps = 30, lufs = -14, o: { limit?: boolean } = {}) => (sr: number): [Float32Array, Float32Array] => {
+ const seconds = frames / fps, x = piece(), fit = "order" in x ? x : fitScore(x, seconds);
+ const m = renderPiece(fit.piece, sr, { seconds, tempo: fit.tempo }), n = Math.round(seconds * sr);
+ const L = new Float32Array(n), R = new Float32Array(n); L.set(m.L.subarray(0, n)); R.set(m.R.subarray(0, n));
+ const gain = (dB: number) => { const g = Math.pow(10, dB / 20); for (let i = 0; i < n; i++) { L[i] *= g; R[i] *= g; } };
const now = loudness([L, R], sr).integrated, peak = truePeak([L, R]).dbtp;
- const gainDb = Math.min(lufs - now, -1 - peak), g = Math.pow(10, gainDb / 20); // loudness first, never past -1 dBTP
- for (let i = 0; i < n; i++) { L[i] *= g; R[i] *= g; }
+ if (!o.limit || lufs - now <= -1 - peak) { gain(Math.min(lufs - now, -1 - peak)); return [L, R]; } // loudness first, never past -1 dBTP
+ gain(lufs - now); limiter(L, R, sr, Math.pow(10, -1.3 / 20)); // the limiter takes the peaks
+ const again = lufs - loudness([L, R], sr).integrated; if (again > 0) { gain(Math.min(again, 1)); limiter(L, R, sr, Math.pow(10, -1.3 / 20)); }
+ const tp = truePeak([L, R]).dbtp; if (tp > -1) gain(-1.05 - tp); // an inter-sample overshoot: a last static trim
return [L, R];
};
+
diff --git a/skills/anidoodle/engine/src/canvas-core/music/render.ts b/skills/anidoodle/engine/src/canvas-core/music/render.ts
index d14a4fb..f9fa296 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts
@@ -141,8 +141,8 @@ export const measureStems = (piece: Piece, sr: number, o: RenderOpts = {}, tolDb
return stemBalance(r.stems, piece.stemTargets ?? {}, tolDb);
};
-/** Look-ahead peak limiter (1.5 ms look-ahead, 120 ms release), used by dense styles only. */
-const limiter = (L: Float32Array, R: Float32Array, sr: number, ceil: number) => {
+/** Look-ahead peak limiter (1.5 ms look-ahead, 120 ms release), used by dense styles only; `ceil` is linear. */
+export const limiter = (L: Float32Array, R: Float32Array, sr: number, ceil: number) => {
const n = L.length, la = Math.round(0.0015 * sr), rel = Math.exp(-1 / (0.12 * sr)), need = new Float32Array(n);
for (let i = 0; i < n; i++) { const a = Math.max(Math.abs(L[i]), Math.abs(R[i])) * 1.12; need[i] = a > ceil ? ceil / a : 1; } // 1.12: inter-sample margin
const g = new Float32Array(n); let cur = 1;
diff --git a/skills/anidoodle/engine/test/launchTemplate.test.ts b/skills/anidoodle/engine/test/launchTemplate.test.ts
index 446e5d6..4955006 100644
--- a/skills/anidoodle/engine/test/launchTemplate.test.ts
+++ b/skills/anidoodle/engine/test/launchTemplate.test.ts
@@ -2,7 +2,7 @@
// compose.md writes) or a Piece. anidoodle's own pieces are refused by identity, by title and by
// content, so a retitled or transposed copy of our launch score cannot ship as a user's film.
import { makeLaunchFilm } from "../src/canvas-core/launchTemplate";
-import { chiptunePlayful, daylightCopy, launchLofi3, launchLofi3Material, type Material } from "../src/canvas-core/music";
+import { chiptunePlayful, composePiece, daylightCopy, filmAudio, fitScore, launchLofi3, launchLofi3Material, loudness, perform, truePeak, type Material } from "../src/canvas-core/music";
import type { Film } from "../src/canvas-core/film";
export const name = "launchTemplate";
@@ -26,4 +26,24 @@ export const run = (ok: (cond: boolean, label: string) => void) => {
ok(refused(() => ({ ...launchLofi3Material(), title: "Mine" })), "refuses retitled launch material");
ok(refused(daylightCopy), "refuses the transposed copy of our launch score (the Daylight fixture)");
ok(refused(undefined, /required/), "a missing score is an error, never a default");
+
+ // The bed is the score FITTED to the film (fitScore via filmAudio), never the first N seconds cut
+ // and faded: it is filmAudio's render exactly, times one gain, and its last note lands so the tail
+ // rings out on the last frame.
+ const two = { ...base, asks: [base.asks[0], { prompt: "again", plate, label: "l" }], askBeats: 16, endBeats: 12, score: mine };
+ const bedFilm = makeLaunchFilm(two), secs = bedFilm.meta.durationFrames / bedFilm.meta.fps, SR = 16000;
+ const [L, R] = bedFilm.audio!(SR), [fL] = filmAudio(composePiece(mine()), secs)(SR);
+ let k = 0; for (let i = 0; i < fL.length; i++) if (Math.abs(fL[i]) > Math.abs(fL[k])) k = i;
+ const g = L[k] / fL[k]; let dev = 0; for (let i = 0; i < L.length; i++) dev = Math.max(dev, Math.abs(L[i] - g * fL[i]));
+ ok(L.length === Math.round(secs * SR) && dev < 1e-5, `the bed is filmAudio (fitScore) at one gain, exactly the film's ${secs} s (max deviation ${dev.toExponential(1)})`);
+ const sc = bedFilm.meta.score, f = fitScore(composePiece(mine()), secs), end = perform(f.piece, f.tempo, { expressive: true }).lastOnset + f.piece.tail;
+ ok(!!sc && sc.form === f.form && Math.abs(sc.tempo - f.tempo) < 1e-9, `meta.score reports the fit (${sc?.tempo.toFixed(1)} bpm, ${sc?.form}) for render to print beside the grid's 120 bpm`);
+ ok(f.piece.arrangement?.at(-1)?.kind === "outro" && Math.abs(end - secs) < 0.25, `it ends on its outro: last note + tail = ${end.toFixed(2)} s of ${secs} s`);
+ const lu = loudness([L, R], SR).integrated, tp = truePeak([L, R]).dbtp;
+ ok(tp <= -0.99 && (Math.abs(lu + 14) < 0.3 || tp > -1.05), `bed at -14 LUFS or held by the -1 dBTP ceiling (${lu.toFixed(2)} LUFS, ${tp.toFixed(2)} dBTP)`);
+ // a dynamic bed stops at the peak ceiling; limit: true lets the limiter take those peaks instead
+ const long = { ...two, askBeats: 24, endBeats: 16 }, [cL, cR] = makeLaunchFilm(long).audio!(SR), [lL, lR] = makeLaunchFilm({ ...long, limit: true }).audio!(SR);
+ const cu = loudness([cL, cR], SR).integrated, lu2 = loudness([lL, lR], SR).integrated, tp2 = truePeak([lL, lR]).dbtp;
+ ok(cu < -14.5 && truePeak([cL, cR]).dbtp > -1.05, `without limit the ceiling wins and says so in the loudness (${cu.toFixed(2)} LUFS)`);
+ ok(Math.abs(lu2 + 14) < 0.3 && tp2 <= -1, `limit: true reaches -14 LUFS under -1 dBTP (${lu2.toFixed(2)} LUFS, ${tp2.toFixed(2)} dBTP)`);
};
diff --git a/skills/anidoodle/engine/tools/render.mjs b/skills/anidoodle/engine/tools/render.mjs
index 8122217..fc6990f 100644
--- a/skills/anidoodle/engine/tools/render.mjs
+++ b/skills/anidoodle/engine/tools/render.mjs
@@ -72,6 +72,18 @@ if (ranged) console.log(`range render: frames [${FROM}, ${TO}) of ${N}, silent b
// audio: pure JS in the page -> WAV here (skipped for a range render: the score would not line up)
mkdirSync(resolve(".tmp"), { recursive: true }); const wav = resolve(`.tmp/${film}.wav`), a = ranged ? null : await session.audio(48000);
if (a) writeFileSync(wav, float32Wav(a));
+if (a) { // the audio report: loudness and true peak, and the score's fit against the picture's grid
+ const { build } = await import("esbuild"), js = (await build({ entryPoints: [resolve("src/canvas-core/music/meter.ts")], bundle: true, write: false, format: "esm", platform: "neutral", logLevel: "error" })).outputFiles[0].text;
+ const { loudness, truePeak } = await import("data:text/javascript;base64," + Buffer.from(js).toString("base64"));
+ const pcm = new Float32Array(Uint8Array.from(Buffer.from(a.float32, "base64")).buffer), L = new Float32Array(a.frames), R = new Float32Array(a.frames);
+ for (let i = 0; i < a.frames; i++) { L[i] = pcm[2 * i]; R[i] = pcm[2 * i + 1]; }
+ const lu = loudness([L, R], a.sampleRate).integrated, tp = truePeak([L, R]).dbtp;
+ console.log(`audio: ${lu.toFixed(1)} LUFS integrated, ${tp.toFixed(1)} dBTP true peak`);
+ if (lu < -14.5 && tp > -1.1) console.log(` NOTE the -1 dBTP ceiling held the gain ${(-14 - lu).toFixed(1)} dB under -14 LUFS (the cross-platform target): peaks are a composing problem (stagger the bass under the loudest downbeat, roll the big chord); a launch template bed takes \`limit: true\``);
+ if (meta.score) { const off = (meta.score.tempo / meta.bpm - 1) * 100;
+ console.log(`score: fitted to the film at ${meta.score.tempo.toFixed(1)} bpm (${meta.score.form}); the picture's grid is ${meta.bpm} bpm (${off >= 0 ? "+" : ""}${off.toFixed(1)} %)`);
+ if (Math.abs(off) > 2) console.log(" NOTE past 2 % the cuts drift off the score's downbeats: a 1-bar stretch section, or the Material's `tail`, lets the fit land nearer the grid"); }
+}
mkdirSync(join(out, ".."), { recursive: true });
const input = ["-y", "-loglevel", "error", "-f", "image2pipe", "-framerate", String(meta.fps), "-c:v", "png", "-i", "-"];
diff --git a/skills/anidoodle/references/workflows/launch-video-kit.md b/skills/anidoodle/references/workflows/launch-video-kit.md
index 6f656b7..1f55174 100644
--- a/skills/anidoodle/references/workflows/launch-video-kit.md
+++ b/skills/anidoodle/references/workflows/launch-video-kit.md
@@ -46,9 +46,13 @@ muted), the camera easing out for the press, an ink drop arcing from Generate in
and blooming open into a card where the plate draws itself live, the thread keeping earlier
answers and scrolling, a gentle lean onto each new card, full-frame word pages between asks,
and the end card blooming open and holding. The corner mark (your name, hand-lettered) steps
-aside before any lean. Real holds are declared, so the gate passes; the score is set to -14
-LUFS with the true peak at or under -1 dBTP (`musicBed`; a very dynamic piece stops at the
-peak ceiling first).
+aside before any lean. Real holds are declared, so the gate passes. The score is fitted to the
+film (`fitScore`: its stretch section repeated or dropped, the tempo trimmed) so it ends on its
+outro at the last frame, then set to -14 LUFS with the true peak at or under -1 dBTP
+(`musicBed`). Render prints the loudness and the fitted tempo beside the grid's bpm: compose at
+the film's bpm with a 1-bar stretch section (or set the Material's `tail`) so they stay within
+2 %, or the cuts drift off the downbeats. A dynamic piece stops at the peak ceiling first and
+render says how far short; `limit: true` lets a look-ahead limiter take those peaks instead.
**The plates and the score are made for this product, every time.** A style is a recipe applied
to the user's subject (`references/styles.md`): draw their dashboard, their mascot, their