Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.
TypeScript agent-skillsanimationcanvasclaude-codeclaude-pluginclaude-skillscodex-plugincreative-codecreative-codingcreative-toolsdeterministicgenerative-arthand-drawnillustrationlaunch-videomotion-designmotion-graphicspluginprocedural-generationskill
music speed: bit-identical fast paths in the hot DSP (Biquad.run on locals, FFT with planned twiddles, quickselect for the compressor's p90, hoisted reverb constants)
Alex Greenshpunfdfd16ce0192
diff --git a/skills/anidoodle/engine/src/canvas-core/music/dsp.ts b/skills/anidoodle/engine/src/canvas-core/music/dsp.tsindex a482c26..93192b0 100644--- a/skills/anidoodle/engine/src/canvas-core/music/dsp.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/dsp.ts@@ -34,7 +34,12 @@ export class Biquad {bq.b0 = b0 / a0; bq.b1 = b1 / a0; bq.b2 = b2 / a0; bq.a1 = a1 / a0; bq.a2 = a2 / a0; return bq;}tick(x: number) { const y = this.b0 * x + this.b1 * this.x1 + this.b2 * this.x2 - this.a1 * this.y1 - this.a2 * this.y2; this.x2 = this.x1; this.x1 = x; this.y2 = this.y1; this.y1 = y; return y; }- run(buf: Float32Array) { for (let i = 0; i < buf.length; i++) buf[i] = this.tick(buf[i]); return buf; }+ /** tick() over a buffer, with the state in locals: the same operations in the same order, so bit-identical, several times faster */+ run(buf: Float32Array) {+ const { b0, b1, b2, a1, a2 } = this; let { x1, x2, y1, y2 } = this;+ for (let i = 0; i < buf.length; i++) { const x = buf[i], y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2; x2 = x1; x1 = x; y2 = y1; y1 = y; buf[i] = y; }+ this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; return buf;+ }}export const onePoleCoef = (sr: number, f: number) => 1 - Math.exp((-TAU * f) / sr);diff --git a/skills/anidoodle/engine/src/canvas-core/music/index.ts b/skills/anidoodle/engine/src/canvas-core/music/index.tsindex fe96de7..5643ce6 100644--- a/skills/anidoodle/engine/src/canvas-core/music/index.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/index.ts@@ -5,6 +5,7 @@ export * from "./plan";export * from "./perform";export * from "./piano";export * from "./render";+export { kth } from "./mixDsp";export * from "./meter";export * from "./guards";export * from "./score-text";diff --git a/skills/anidoodle/engine/src/canvas-core/music/meter.ts b/skills/anidoodle/engine/src/canvas-core/music/meter.tsindex 5dfed0a..a411eae 100644--- a/skills/anidoodle/engine/src/canvas-core/music/meter.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/meter.ts@@ -4,12 +4,26 @@// truePeak 4x oversampled (windowed-sinc polyphase), dBTP// onsets spectral flux with a max-filtered previous frame (SuperFlux-style), adaptive threshold// centroid magnitude-weighted spectral centroid, averaged over non-silent frames-export const fft = (re: Float64Array, im: Float64Array) => {- const n = re.length;- for (let i = 1, j = 0; i < n; i++) { let bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) { let t = re[i]; re[i] = re[j]; re[j] = t; t = im[i]; im[i] = im[j]; im[j] = t; } }+/** Per size: the bit-reversal swaps and, per stage, the twiddles exactly as the butterfly loop's recurrence makes them (so the tables change speed, not a single bit). */+const FFT_PLAN = new Map<number, { swaps: Int32Array; tw: { r: Float64Array; i: Float64Array }[] }>();+const fftPlan = (n: number) => {+ let p = FFT_PLAN.get(n); if (p) return p;+ const sw: number[] = [];+ for (let i = 1, j = 0; i < n; i++) { let bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) sw.push(i, j); }+ const tw: { r: Float64Array; i: Float64Array }[] = [];for (let len = 2; len <= n; len <<= 1) {- const ang = (-2 * Math.PI) / len, wr = Math.cos(ang), wi = Math.sin(ang);- for (let i = 0; i < n; i += len) { let cr = 1, ci = 0; for (let j = 0; j < len / 2; j++) { const a = i + j, b = a + len / 2, xr = re[b] * cr - im[b] * ci, xi = re[b] * ci + im[b] * cr; re[b] = re[a] - xr; im[b] = im[a] - xi; re[a] += xr; im[a] += xi; const t = cr * wr - ci * wi; ci = cr * wi + ci * wr; cr = t; } }+ const ang = (-2 * Math.PI) / len, wr = Math.cos(ang), wi = Math.sin(ang), h = len / 2, r = new Float64Array(h), im = new Float64Array(h); let cr = 1, ci = 0;+ for (let j = 0; j < h; j++) { r[j] = cr; im[j] = ci; const t = cr * wr - ci * wi; ci = cr * wi + ci * wr; cr = t; }+ tw.push({ r, i: im });+ }+ p = { swaps: Int32Array.from(sw), tw }; FFT_PLAN.set(n, p); return p;+};+export const fft = (re: Float64Array, im: Float64Array) => {+ const n = re.length, { swaps, tw } = fftPlan(n);+ for (let s = 0; s < swaps.length; s += 2) { const i = swaps[s], j = swaps[s + 1]; let t = re[i]; re[i] = re[j]; re[j] = t; t = im[i]; im[i] = im[j]; im[j] = t; }+ for (let len = 2, st = 0; len <= n; len <<= 1, st++) {+ const h = len / 2, twr = tw[st].r, twi = tw[st].i;+ for (let i = 0; i < n; i += len) for (let j = 0; j < h; j++) { const cr = twr[j], ci = twi[j], a = i + j, b = a + h, xr = re[b] * cr - im[b] * ci, xi = re[b] * ci + im[b] * cr; re[b] = re[a] - xr; im[b] = im[a] - xi; re[a] += xr; im[a] += xi; }}};diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.tsindex 9f051ac..9cde7a6 100644--- a/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/mixDsp.ts@@ -115,6 +115,16 @@ export const transientGain = (L: Float32Array, R: Float32Array, sr: number, o: {// ---------------------------------------------------------------- compressorexport 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. */+/** The k-th smallest of a[0..m) (Hoare quickselect, reorders a in place): the value a sort would put at index k. */+export const kth = (a: Float64Array, m: number, k: number) => {+ let lo = 0, hi = m - 1;+ while (lo < hi) {+ const x = a[(lo + hi) >> 1]; let i = lo, j = hi;+ while (i <= j) { while (a[i] < x) i++; while (a[j] > x) j--; if (i <= j) { const t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } }+ if (k <= j) hi = j; else if (k >= i) lo = i; else return a[k];+ }+ return a[k];+};export const compress = (L: Float32Array, R: Float32Array, sr: number, o: CompOpts) => {const n = L.length, W = o.kneeDb ?? 6, slope = 1 / o.ratio - 1;// detector: peak-ish level in dB (fast 1 ms attack, 40 ms release on the absolute value)@@ -123,10 +133,11 @@ export const compress = (L: Float32Array, R: Float32Array, sr: number, o: CompOpif (mx < -80) return { thresholdDb: 0, p90GrDb: 0, maxGrDb: 0 };const gc = (x: number, T: number) => { const ov = x - T; if (2 * ov < -W) return 0; if (2 * Math.abs(ov) <= W) return (slope * (ov + W / 2) ** 2) / (2 * W); return slope * ov; };const ga = Math.exp(-1 / ((o.attackMs / 1000) * sr)), gr = Math.exp(-1 / ((o.releaseMs / 1000) * sr));+ const act = new Float64Array(n + 1);const run = (T: number, out?: Float32Array, step = 1) => {- let g = 0; const act: number[] = [], ga2 = Math.pow(ga, step), gr2 = Math.pow(gr, step);- for (let i = 0; i < n; i += step) { const c = gc(det[i], T); g = c < g ? ga2 * g + (1 - ga2) * c : gr2 * g + (1 - gr2) * c; if (out) out[i] = g; if (det[i] > mx - 30) act.push(-g); }- act.sort((a, b) => a - b); return act.length ? act[Math.floor(act.length * 0.9)] : 0;+ let g = 0, m = 0; const ga2 = Math.pow(ga, step), gr2 = Math.pow(gr, step);+ for (let i = 0; i < n; i += step) { const c = gc(det[i], T); g = c < g ? ga2 * g + (1 - ga2) * c : gr2 * g + (1 - gr2) * c; if (out) out[i] = g; if (det[i] > mx - 30) act[m++] = -g; }+ return m ? kth(act, m, Math.floor(m * 0.9)) : 0; // the 90th percentile: the same value a full sort puts there, in linear time};// bisection on the threshold: p90 GR is monotone in Tlet lo = mx - 40, hi = mx + 1; const step = Math.max(1, Math.floor(sr / 6000));diff --git a/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts b/skills/anidoodle/engine/src/canvas-core/music/mixReverb.tsindex 64e6d12..7ec8375 100644--- a/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/mixReverb.ts@@ -29,7 +29,8 @@ class Line {at(d: number) { return this.buf[(this.w - 1 - d) & this.mask]; } // integer delay d >= 0 (0 = last pushed)frac(d: number) { const i = Math.floor(d), f = d - i, y0 = this.at(i - 1), y1 = this.at(i), y2 = this.at(i + 1), y3 = this.at(i + 2); const c1 = 0.5 * (y2 - y0), c2 = y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3, c3 = 0.5 * (y3 - y0) + 1.5 * (y1 - y2); return ((c3 * f + c2) * f + c1) * f + y1; }}-const hadamard = (h: Float64Array) => { const N = h.length; for (let s = 1; s < N; s <<= 1) for (let j = 0; j < N; j += s << 1) for (let q = j; q < j + s; q++) { const a = h[q], b = h[q + s]; h[q] = a + b; h[q + s] = a - b; } const k = 1 / Math.sqrt(N); for (let j = 0; j < N; j++) h[j] *= k; };+const HAD_K = new Map<number, number>(); // 1 / sqrt(N), computed once per size (the same double every call)+const hadamard = (h: Float64Array) => { const N = h.length; for (let s = 1; s < N; s <<= 1) for (let j = 0; j < N; j += s << 1) for (let q = j; q < j + s; q++) { const a = h[q], b = h[q + s]; h[q] = a + b; h[q + s] = a - b; } let k = HAD_K.get(N); if (k === undefined) { k = 1 / Math.sqrt(N); HAD_K.set(N, k); } for (let j = 0; j < N; j++) h[j] *= k; };// ---------------------------------------------------------------- FDNexport const fdnReverb = (L: Float32Array, R: Float32Array, sr: number, s: Space, rng: Rng): Wet => {@@ -48,13 +49,14 @@ export const fdnReverb = (L: Float32Array, R: Float32Array, sr: number, s: Spaceconst pa = lens.map((_, j) => (gDc[j] - gNy[j]) / (gDc[j] + gNy[j])), pb = lens.map((_, j) => gDc[j] * (1 - pa[j])), zf = new Float64Array(N);const x8 = new Float64Array(DC), fb = new Float64Array(N), out = new Float64Array(N), twoPi = Math.PI * 2;const erG = s.er * 0.75, lateG = s.late * 1.6; // calibrated: at late 0.3 the tail sits where the legacy hall's did (same input, -19.5 dB)+ const w = rate.map((r) => twoPi * r); // twoPi * rate[j], the same product the per-sample expression starts withfor (let i = 0; i < n; i++) {const xl = i >= pd ? inL[i - pd] : 0, xr = i >= pd ? inR[i - pd] : 0;for (let c = 0; c < DC; c++) x8[c] = c % 2 ? xr : xl;- for (const st of dif) { for (let c = 0; c < DC; c++) { st.lines[c].push(x8[c]); x8[c] = st.lines[c].at(st.d[c]) * st.flip[c]; } hadamard(x8); }+ for (let k = 0; k < steps; k++) { const st = dif[k], ln = st.lines, d = st.d, fl = st.flip; for (let c = 0; c < DC; c++) { ln[c].push(x8[c]); x8[c] = ln[c].at(d[c]) * fl[c]; } hadamard(x8); }// early: the diffuser output, alternate channels to each side- const el = x8[0] - x8[2] + x8[4] - x8[6], er = x8[1] - x8[3] + x8[5] - x8[7];- for (let j = 0; j < N; j++) { const md = depth * Math.sin(twoPi * rate[j] * (i / sr) + phase[j]); out[j] = lines[j].frac(lens[j] + md); }+ const el = x8[0] - x8[2] + x8[4] - x8[6], er = x8[1] - x8[3] + x8[5] - x8[7], t = i / sr;+ for (let j = 0; j < N; j++) { const md = depth * Math.sin(w[j] * t + phase[j]); out[j] = lines[j].frac(lens[j] + md); }// Householder: y = x - 2/N sum(x)let sum = 0; for (let j = 0; j < N; j++) sum += out[j]; sum *= 2 / N;for (let j = 0; j < N; j++) {diff --git a/skills/anidoodle/engine/src/canvas-core/music/roomSmall.ts b/skills/anidoodle/engine/src/canvas-core/music/roomSmall.tsindex d5e9878..81bf284 100644--- a/skills/anidoodle/engine/src/canvas-core/music/roomSmall.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/roomSmall.ts@@ -45,9 +45,10 @@ export const smallRoom = (L: ArrayLike<number>, R: ArrayLike<number>, sr: numberconst a = i >= pd ? inL[i - pd] : 0, b = i >= pd ? inR[i - pd] : 0;for (let c = 0; c < N; c++) x[c] = c & 1 ? b : a;// diffuse- for (const st of steps) {- for (let c = 0; c < N; c++) { const buf = st.bufs[c], L0 = buf.length, wi = st.idx[c]; buf[wi] = x[c]; y[c] = buf[(wi + 1) % L0] * st.flip[c]; st.idx[c] = (wi + 1) % L0; }- for (let c = 0; c < N; c++) x[c] = y[st.perm[c]];+ for (let k = 0; k < 4; k++) {+ const st = steps[k], bufs = st.bufs, idx = st.idx, flip = st.flip, perm = st.perm;+ for (let c = 0; c < N; c++) { const buf = bufs[c], L0 = buf.length, wi = idx[c], nx = wi + 1 === L0 ? 0 : wi + 1; buf[wi] = x[c]; y[c] = buf[nx] * flip[c]; idx[c] = nx; }+ for (let c = 0; c < N; c++) x[c] = y[perm[c]];hadamard8(x);}let eL = 0, eR = 0; for (let c = 0; c < N; c++) { if (c & 1) eR += x[c] * (c & 2 ? -1 : 1); else eL += x[c] * (c & 2 ? -1 : 1); }