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.

keys (S3): mallet pulse-zero blow-up fixed (contact scales with pitch), DX bark kept under Nyquist, felt hammer noise darker, anti-MIDI metrics tool

Alex Greenshpunbfb5bf9856fc
diff --git a/skills/anidoodle/engine/src/canvas-core/music/keysEP.ts b/skills/anidoodle/engine/src/canvas-core/music/keysEP.ts
index 7454b28..4379b24 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/keysEP.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/keysEP.ts
@@ -43,7 +43,7 @@ export const electricPiano = (keys: Played[], sr: number, n: number, o: Opts, se
const contact = Math.max(2, Math.round(((model === "wurli" ? 2.4 : 3) - 1.8 * v) / 1000 * sr));
if (model === "dx") {
// three stacks, phase modulation; the carriers start at zero phase, the modulators free
- const I1 = 1.1 + 2.3 * v * v, I2 = 0.2 + 3.2 * Math.pow(v, 1.6), I3 = 0.5 + 0.7 * v;
+ const I1 = 1.1 + 2.3 * v * v, I3 = 0.5 + 0.7 * v, I2 = Math.min(0.2 + 3.2 * Math.pow(v, 1.6), Math.max(0, (0.42 * sr / f - 1) / 14 - 1)); // the 14:1 bark's sidebands stay under Nyquist (no aliasing up high)
const tb = 0.02 + 0.04 * (1 - v), w = TAU * f / sr, d2 = Math.pow(2, 2 / 1200), d3 = Math.pow(2, -3 / 1200);
let pm2 = nv.phase(), pm3 = nv.phase(), fb = 0;
const e1 = Math.exp(-sig / sr), e1i = Math.exp(-1 / (0.55 * sr)), e2 = Math.exp(-1 / (0.28 * sr)), e2i = Math.exp(-1 / (tb * sr)), e3 = Math.exp(-(sig * 0.7) / sr), e3i = Math.exp(-1 / (1.5 * sr));
diff --git a/skills/anidoodle/engine/src/canvas-core/music/keysMallets.ts b/skills/anidoodle/engine/src/canvas-core/music/keysMallets.ts
index 076b6fd..ea66f15 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/keysMallets.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/keysMallets.ts
@@ -42,7 +42,7 @@ export const bars = (keys: Played[], sr: number, n: number, o: Opts, seed: numbe
const i0 = Math.round(k.t * sr); if (i0 >= n) return;
const nv = noteVar(seed, idx, k, { cents: 1.0, db: 0.5, bright: 0.1, decay: 0.06 }, strike(k.p));
const v = clamp(k.v, 0.02, 1), f0 = mtof(k.p) * Math.pow(2, nv.cents / 1200);
- const tc = ((S.tcMs[0] + (S.tcMs[1] - S.tcMs[0]) * Math.pow(v, 0.8)) / 1000) * (1 + 0.1 * clamp(gauss(nv.r), -2, 2)) / nv.bright;
+ const tc = ((S.tcMs[0] + (S.tcMs[1] - S.tcMs[0]) * Math.pow(v, 0.8)) / 1000) * clamp(Math.pow(262 / f0, 0.6), 0.15, 2) * (1 + 0.1 * clamp(gauss(nv.r), -2, 2)) / nv.bright; // shorter, stiffer bars up high: shorter contact
let damp = Infinity;
if (S.damped) damp = Math.min(k.off, nextSameKey(sorted, idx));
else { const nx = nextSameKey(sorted, idx); if (nx < Infinity) damp = nx + 0.003; } // a restrike stops the old ring
@@ -50,11 +50,11 @@ export const bars = (keys: Played[], sr: number, n: number, o: Opts, seed: numbe
const len = Math.min(buf.length, n - i0); if (len <= 0) return;
const b = buf.subarray(0, len); b.fill(0);
const rb = resBuf.subarray(0, len); rb.fill(0);
- const W0 = pulseW(f0, tc);
+ const W0 = Math.max(0.3, pulseW(f0, tc)); // never divide by a spectral zero of the pulse (contact ~1.5 periods): that blew a mode up 20 dB
for (const [ratio, db, t60ref] of S.modes) {
const f = ratio * f0; if (f > sr * 0.45) continue;
const t60 = clamp(t60ref * (S.t60Mul ?? 1) * Math.pow((ratio * S.refHz) / f, S.alpha), 0.02, 60) * nv.decay;
- const a = dbA(db + (ratio === S.modes[0][0] ? 0 : 2 * (ratio < 5 ? nv.seq2 : -nv.seq2)) + 0.6 * clamp(gauss(nv.r), -2, 2)) * (pulseW(f, tc) / W0); // strike position moves the upper modes against each other
+ const a = dbA(db + (ratio === S.modes[0][0] ? 0 : 2 * (ratio < 5 ? nv.seq2 : -nv.seq2)) + 0.6 * clamp(gauss(nv.r), -2, 2)) * (Math.max(0.02, pulseW(f, tc)) / W0); // strike position moves the upper modes against each other
if (a < 1e-4) continue;
const ph = 0.35 * nv.seq2 + 0.1 * clamp(gauss(nv.r), -2, 2);
if (ratio === S.modes[0][0] && S.bloom > 0) {
diff --git a/skills/anidoodle/engine/src/canvas-core/music/keysPiano.ts b/skills/anidoodle/engine/src/canvas-core/music/keysPiano.ts
index f48086b..f351f31 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/keysPiano.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/keysPiano.ts
@@ -26,7 +26,7 @@ import { GRAND_ENV, GRAND_LAYER_V, GRAND_DECAY, PIANO_BANDS } from "./keysTables
import { partial, noiseBurst, dbA, strikeCounter, type Opts } from "./keysCore";
export type PianoVariant = "grand" | "felt" | "upright" | "honky";
-type Voicing = { bMul: number; promptMul: number; afterMul: number; detuneC: [number, number]; tilt: (f: number, v: number) => number; contactMs: (v: number) => number; knock: number; noise: number; board: number; width: number; level: number };
+type Voicing = { noiseHz: number; bMul: number; promptMul: number; afterMul: number; detuneC: [number, number]; tilt: (f: number, v: number) => number; contactMs: (v: number) => number; knock: number; noise: number; board: number; width: number; level: number };
const LOG_BANDS = PIANO_BANDS.map(Math.log);
const lerp = (a: number, b: number, x: number) => a + (b - a) * x;
@@ -77,13 +77,13 @@ export const measuredB = (m: number) => {
};
const VOICINGS: Record<PianoVariant, Voicing> = {
- grand: { bMul: 1, promptMul: 1, afterMul: 1, detuneC: [0.3, 1.8], tilt: () => 0, contactMs: (v) => 4.2 - 3.3 * v, knock: 1, noise: 1, board: 1, width: 0.55, level: 1 },
+ grand: { noiseHz: 2200, bMul: 1, promptMul: 1, afterMul: 1, detuneC: [0.3, 1.8], tilt: () => 0, contactMs: (v) => 4.2 - 3.3 * v, knock: 1, noise: 1, board: 1, width: 0.55, level: 1 },
// felt strip: the felt eats the hammer's highs (-12 dB/oct above ~0.6-1.4 kHz), a soft, slow contact, and the
// mechanism right in the microphone
- felt: { bMul: 1, promptMul: 0.9, afterMul: 0.85, detuneC: [0.3, 1.6], tilt: (f, v) => -12 * Math.max(0, Math.log2(f / (600 + 800 * v))), contactMs: (v) => 8 - 3 * v, knock: 2.6, noise: 3.2, board: 1.2, width: 0.35, level: 1.25 },
+ felt: { noiseHz: 650, bMul: 1, promptMul: 0.9, afterMul: 0.85, detuneC: [0.3, 1.6], tilt: (f, v) => -12 * Math.max(0, Math.log2(f / (600 + 800 * v))), contactMs: (v) => 8 - 3 * v, knock: 2.6, noise: 3.2, board: 1.2, width: 0.35, level: 1.25 },
// shorter, stiffer strings on a smaller board: more inharmonic, less bass, a boxy 1-2 kHz, a shorter ring
- upright: { bMul: 1.8, promptMul: 0.75, afterMul: 0.5, detuneC: [0.8, 3], tilt: (f) => -6 * Math.max(0, Math.log2(110 / f)) + 3 * Math.exp(-Math.pow(Math.log2(f / 1400), 2) * 2), contactMs: (v) => 3.4 - 2.6 * v, knock: 1.4, noise: 1.4, board: 1.5, width: 0.3, level: 1.1 },
- honky: { bMul: 2, promptMul: 0.7, afterMul: 0.45, detuneC: [6, 14], tilt: (f) => -6 * Math.max(0, Math.log2(110 / f)) + 4 * Math.exp(-Math.pow(Math.log2(f / 2000), 2) * 1.5), contactMs: (v) => 2.2 - 1.6 * v, knock: 1.6, noise: 1.5, board: 1.5, width: 0.3, level: 1.05 },
+ upright: { noiseHz: 1800, bMul: 1.8, promptMul: 0.75, afterMul: 0.5, detuneC: [0.8, 3], tilt: (f) => -6 * Math.max(0, Math.log2(110 / f)) + 3 * Math.exp(-Math.pow(Math.log2(f / 1400), 2) * 2), contactMs: (v) => 3.4 - 2.6 * v, knock: 1.4, noise: 1.4, board: 1.5, width: 0.3, level: 1.1 },
+ honky: { noiseHz: 2600, bMul: 2, promptMul: 0.7, afterMul: 0.45, detuneC: [6, 14], tilt: (f) => -6 * Math.max(0, Math.log2(110 / f)) + 4 * Math.exp(-Math.pow(Math.log2(f / 2000), 2) * 1.5), contactMs: (v) => 2.2 - 1.6 * v, knock: 1.6, noise: 1.5, board: 1.5, width: 0.3, level: 1.05 },
};
const stretchCents = (m: number) => (m > 60 ? 0.012 * Math.pow(m - 60, 1.9) : -0.02 * Math.pow(60 - m, 1.6));
@@ -187,7 +187,7 @@ export const renderPianoV2 = (keys: KeyPress[], pedal: PedalSpan[], sr: number,
const ml = Math.min(mech.length, len); mech.fill(0);
const thumpF = 70 + 40 * nr(), thumpW = (TAU * thumpF) / sr;
for (let i = 0; i < Math.min(ml, Math.round(0.05 * sr)); i++) mech[i] += Math.sin(thumpW * i) * Math.exp(-i / (0.014 * sr)) * 0.012 * V.knock * Math.pow(v, 1.3);
- noiseBurst(mech, 0, Math.round((0.0015 + 0.0015 * (1 - v)) * sr), 2200 + 2000 * v, 0.7, 0.02 * V.noise * v * v, nr, sr);
+ noiseBurst(mech, 0, Math.round((0.0015 + 0.0015 * (1 - v)) * sr), V.noiseHz * (1 + 0.9 * v), 0.7, 0.02 * V.noise * v * v, nr, sr);
const bw = 0.05 * V.board * Math.pow(v, 1.2) * (0.8 + 0.4 * nr()) * (m < 48 ? 1.4 : m > 84 ? 0.7 : 1), hard = clamp(v * v, 0, 1), sh = Math.round(nr() * 40);
for (let i = 0; i < ml && i + sh < irHard.length; i++) mech[i] += (irHard[i + sh] * hard + irSoft[i + sh] * (1 - hard)) * bw * Math.min(1, i / 24);
for (let i = 0; i < ml; i++) buf[i] += mech[i];
diff --git a/skills/anidoodle/engine/tools/keys-metrics.py b/skills/anidoodle/engine/tools/keys-metrics.py
new file mode 100644
index 0000000..d4088a2
--- /dev/null
+++ b/skills/anidoodle/engine/tools/keys-metrics.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""Anti-MIDI metrics for the keys phrases (research sec. 9.2): per-band inter-channel correlation
+(< 0.9 above 500 Hz = real width; ~1 below 120 Hz = mono lows), click detector (sample steps far above the
+local signal), spectral flux in sustains (a static oscillator is ~flat). numpy only.
+ python3 tools/keys-metrics.py <dir with before-*.wav / after-*.wav>
+"""
+import os, sys, wave, numpy as np
+
+def load(p):
+ w = wave.open(p); x = np.frombuffer(w.readframes(w.getnframes()), dtype="<i2").astype(float).reshape(-1, 2) / 32768
+ return x[:, 0], x[:, 1], w.getframerate()
+
+def band(x, sr, lo, hi):
+ X = np.fft.rfft(x); f = np.fft.rfftfreq(len(x), 1 / sr); X[(f < lo) | (f >= hi)] = 0; return np.fft.irfft(X, len(x))
+
+def corr(a, b):
+ return float(np.dot(a, b) / (np.sqrt(np.dot(a, a) * np.dot(b, b)) + 1e-20))
+
+def clicks(x, sr):
+ """Impulsive steps: |2nd difference| more than 12x its own local RMS (10 ms). Steady bright tones do not count."""
+ d2 = np.abs(np.diff(x, 2)); w = int(0.01 * sr)
+ loc = np.sqrt(np.convolve(d2 * d2, np.ones(w) / w, mode="same")) + 1e-6
+ hits = np.where((d2 > 12 * loc) & (d2 > 1e-3))[0]
+ return int(np.sum(np.diff(hits) > int(0.005 * sr)) + (1 if len(hits) else 0))
+
+def flux(x, sr):
+ n = 2048; fr = [np.abs(np.fft.rfft(x[i:i + n] * np.hanning(n))) for i in range(0, len(x) - n, 1024)]
+ fr = np.array(fr); fr /= fr.sum(axis=1, keepdims=True) + 1e-12
+ return float(np.mean(np.abs(np.diff(fr, axis=0)).sum(axis=1)))
+
+d = sys.argv[1]
+print(f"{'phrase':28} {'ICC<120':>8} {'ICC.5-2k':>9} {'ICC>2k':>8} {'clicks':>7} {'flux':>6}")
+for fn in sorted(os.listdir(d)):
+ if not fn.endswith(".wav") or not (fn.startswith("before-") or fn.startswith("after-")): continue
+ L, R, sr = load(os.path.join(d, fn))
+ row = [corr(band(L, sr, a, b), band(R, sr, a, b)) for a, b in [(20, 120), (500, 2000), (2000, 16000)]]
+ print(f"{fn[:-4]:28} {row[0]:8.2f} {row[1]:9.2f} {row[2]:8.2f} {clicks((L + R) / 2, sr):7d} {flux((L + R) / 2, sr):6.3f}")