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
Use sinc for pitch-down recordings and releases
Alex Greenshpunea1fced582b2
diff --git a/skills/anidoodle/engine/src/canvas-core/music/sampler.ts b/skills/anidoodle/engine/src/canvas-core/music/sampler.tsindex a4c4570..77b1b7a 100644--- a/skills/anidoodle/engine/src/canvas-core/music/sampler.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/sampler.ts@@ -87,15 +87,9 @@ export const sampleZones = (bank: SampleBank, keys: Played[], seed: number, peda});};-// No interpolation at unity rate: an in-tune recording keeps its original frames.-const hermite = (a: Float32Array, x: number) => {- const i = Math.floor(x), f = x - i, at = (j: number) => a[Math.max(0, Math.min(a.length - 1, j))];- const y0 = at(i - 1), y1 = at(i), y2 = at(i + 1), y3 = at(i + 2);- return ((0.5 * (y3 - y0) + 1.5 * (y1 - y2)) * f + y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3) * f * f + 0.5 * (y2 - y0) * f + y1;-};/** Blackman-windowed sinc, narrowed before any downsampling (pitch up OR a 24 kHz stem). */const sincTable = (rate: number) => {- const taps = 48, phases = 256, cut = 0.94 / rate, table = new Float64Array(taps * (phases + 1));+ const taps = 48, phases = 256, cut = 0.94 / Math.max(1, rate), table = new Float64Array(taps * (phases + 1));for (let p = 0; p <= phases; p++) {let sum = 0; for (let t = 0; t < taps; t++) { const d = t - taps / 2 + 1 - p / phases, x = Math.PI * d * cut, win = 0.42 + 0.5 * Math.cos(Math.PI * d / (taps / 2)) + 0.08 * Math.cos(2 * Math.PI * d / (taps / 2)), v = Math.abs(d) >= taps / 2 ? 0 : (Math.abs(x) < 1e-12 ? cut : cut * Math.sin(x) / x) * win; table[p * taps + t] = v; sum += v; }for (let t = 0; t < taps; t++) table[p * taps + t] /= sum;@@ -120,7 +114,7 @@ export const samplerVoice = (bank: SampleBank, keys: Played[], pedal: PedalSpan[sorted.forEach((k, idx) => {const { zone: z, channels } = zones[idx], i0 = Math.round(k.t * sr); if (i0 >= n) return;const rate = (48000 / sr) * (z.unpitched ? 1 : Math.pow(2, (k.p - z.midi) / 12 + (k.vary?.cents ?? 0) / 1200));- const table = rate > 1 ? sincTable(rate) : null, v = clamp(k.v, 0, 1);+ const table = rate !== 1 ? sincTable(rate) : null, v = clamp(k.v, 0, 1);// Keep the source's layer levels intact. A normalized source gets all its dynamics from this law.const gain = sampleGain(e, z, v) * db((k.vary?.db ?? 0) + (bank.trim?.find((t) => t.file === z.file)?.correctionDb ?? 0));const fc = sampleCutoff(e, z, k, sr, typeof opts.sampleFilter === "string" ? opts.sampleFilter : undefined), alpha = 1 - Math.exp(-2 * Math.PI * fc / sr);@@ -130,7 +124,7 @@ export const samplerVoice = (bank: SampleBank, keys: Played[], pedal: PedalSpan[if (e.damped) for (let j = idx + 1; j < sorted.length; j++) if (sorted[j].p === k.p && sorted[j].t > k.t) { damp = Math.min(damp, sorted[j].t + 0.004); break; }const tDamp = 0.08 + 0.45 * clamp((60 - k.p) / 36, 0, 1), dampI = Math.round((damp - k.t) * sr), dec = Math.exp(-6.91 / (tDamp * sr));const len = Math.min(n - i0, Math.ceil(z.frames / rate), damp === Infinity ? Infinity : dampI + Math.round(tDamp * 1.6 * sr) + 1), inPedal = e.damped && (pedalAt(k.t) || pedalAt(k.t + 0.25));- const read = (a: Float32Array, i: number) => rate === 1 ? a[i] : table ? sincRead(a, i * rate, table) : hermite(a, i * rate);+ const read = (a: Float32Array, i: number) => table ? sincRead(a, i * rate, table) : a[i];const rel = releases?.[idx], window = Math.max(1, Math.round(0.02 * sr)); let remaining = 0, count = 0;let l = 0, r = 0, env = 1;for (let i = 0; i < len; i++) {@@ -144,8 +138,8 @@ export const samplerVoice = (bank: SampleBank, keys: Played[], pedal: PedalSpan[// Match the release noise to the string's last 20 ms, 15 dB below it. Never extend a dead string.const allowance = k.p === e.range[0] || k.p === e.range[1] ? 3 : 2;if (rel && Number.isFinite(releaseAt) && dampI > 0 && (releaseAt - k.t) * sr < z.frames / rate && count && (rel.zone.unpitched || Math.abs(k.p - rel.zone.midi + (k.vary?.cents ?? 0) / 100) <= allowance)) {- const rz = rel.zone, rr = 48000 / sr * (rz.unpitched ? 1 : Math.pow(2, (k.p - rz.midi) / 12 + (k.vary?.cents ?? 0) / 1200)), rt = rr > 1 ? sincTable(rr) : null;- const readRel = (a: Float32Array, i: number) => rr === 1 ? a[i] : rt ? sincRead(a, i * rr, rt) : hermite(a, i * rr);+ const rz = rel.zone, rr = 48000 / sr * (rz.unpitched ? 1 : Math.pow(2, (k.p - rz.midi) / 12 + (k.vary?.cents ?? 0) / 1200)), rt = rr !== 1 ? sincTable(rr) : null;+ const readRel = (a: Float32Array, i: number) => rt ? sincRead(a, i * rr, rt) : a[i];const size = Math.ceil(rz.frames / rr), ref = Math.min(size, Math.round(0.5 * sr)); let power = 0;for (let i = 0; i < ref; i++) { const a = readRel(rel.channels[0], i), b = readRel(rel.channels[rz.channels - 1], i); power += (a * a + b * b) * 0.5; }const g = power > 0 ? Math.sqrt(remaining / count / (power / ref)) * db(-15) * Math.pow(dec, Math.round((releaseAt - damp) * sr)) : 0, start = Math.round(releaseAt * sr);diff --git a/skills/anidoodle/engine/test/sampler.test.ts b/skills/anidoodle/engine/test/sampler.test.tsindex 93eeb13..d2edd98 100644--- a/skills/anidoodle/engine/test/sampler.test.ts+++ b/skills/anidoodle/engine/test/sampler.test.ts@@ -144,6 +144,24 @@ export const run = (ok: (cond: boolean, label: string) => void) => {const high = bank(); for (let i = 0; i < high.zones[0].channels[0].length; i++) high.zones[0].channels[0][i] = 0.3 * Math.sin(2 * Math.PI * 23000 * i / SR);const upHigh = render(high, [key(62)]), downHigh = render(high, [key()], 24000), center = (x: Float32Array, sr: number) => rms(x.subarray(sr * 0.1, sr * 0.6));ok(center(upHigh.L, SR) < 0.001 && center(downHigh.L, 24000) < 0.001, "sinc removes above-Nyquist energy before pitch-up and 24 kHz downsampling");+ const imagesDb = (x: Float32Array, f: number) => {+ let cc = 0, ss = 0, cs = 0, xc = 0, xs = 0;+ for (let i = 0; i < x.length; i++) { const c = Math.cos(2 * Math.PI * f * i / SR), s = Math.sin(2 * Math.PI * f * i / SR); cc += c*c; ss += s*s; cs += c*s; xc += x[i]*c; xs += x[i]*s; }+ const d = cc*ss-cs*cs, a = (xc*ss-xs*cs)/d, b = (xs*cc-xc*cs)/d;+ let residual = 0, signal = 0;+ for (let i = 0; i < x.length; i++) { const fit = a*Math.cos(2*Math.PI*f*i/SR)+b*Math.sin(2*Math.PI*f*i/SR); residual += (x[i]-fit)**2; signal += fit*fit; }+ return 10*Math.log10(residual/signal);+ };+ const tone = bank([60]); tone.zones[0].channels[0] = Float32Array.from({ length: SR*3 }, (_, i) => 0.3*Math.sin(2*Math.PI*15000*i/SR));+ for (const shift of [-3, -200]) {+ const k = { ...key(), vary: { cents: shift, db: 0, bright: 1, decay: 1, phase: 0, seed: 4 } }, f = 15000*2**(shift/1200);+ const played = render(tone, [k]);+ ok(imagesDb(played.L.slice(SR*0.1, SR*0.3), f) < -65, `${shift} cents: downward humanisation has no audible interpolation images`);+ const relZone = { ...tone.zones[0].zone, art: 'rel' as const, file: 'release.flac' }, withRel = { ...tone, entry: { ...tone.entry, damped: true, zones: [...tone.entry.zones, relZone] }, zones: [...tone.zones, { zone: relZone, channels: tone.zones[0].channels }] };+ const release = render(withRel, [k]), without = render({ ...tone, entry: { ...tone.entry, damped: true } }, [k]);+ const isolated = Float32Array.from(release.L.slice(SR*0.22, SR*0.32), (v, i) => v - without.L[SR*0.22+i]);+ ok(imagesDb(isolated, f) < -65, `${shift} cents: pitch-down release noise uses the same clean sinc path`);+ }const canonical = JSON.stringify([b.entry.kind, b.entry.range, b.entry.layers, null, b.entry.damped, true, b.entry.zones.map((z) => [z.sha256, z.midi, z.layer, z.rr, z.frames, z.channels, false, null])]);ok(bankIdentity(b.entry) === createHash("sha256").update(canonical).digest("hex"), "browser-safe bank identity agrees with SHA-256");const wideGaps = bank([60, 72]); wideGaps.entry.sparse = true; wideGaps.entry.maxShift = 6; wideGaps.zones[0].zone.measuredHz = 260;