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: ensemble voices v2 (ensemble.ts) - string sections as independent players through measured violin/viola/cello bodies, pizzicato, a bowed-waveguide solo with legato slides, spectral section brass (loudness-driven brightness, lip scoop, bell formants), flute and clarinet, a glottal-source choir with Peterson-Barney formants and vowel morphs, extended-KS guitars (nylon, steel, clean electric with pickup and amp) and harp with commuted bodies, four basses (finger, pick, upright, synth); every note seeded and different; v1 voices kept for Piece.legacy (render routes it) and the shipped owl lesson guitar
Alex Greenshpunc7f6afd11437
diff --git a/skills/anidoodle/engine/src/canvas-core/lessonOwlScore.ts b/skills/anidoodle/engine/src/canvas-core/lessonOwlScore.tsindex aba6c2b..cad5c2e 100644--- a/skills/anidoodle/engine/src/canvas-core/lessonOwlScore.ts+++ b/skills/anidoodle/engine/src/canvas-core/lessonOwlScore.ts@@ -75,7 +75,7 @@ export const owlPiece = (sp: OwlSoundPlan): Piece => {const parts: Part[] = [{ id: "ostinato", inst: "marimba", role: "accomp", notes: ost, gainDb: -6, opts: grid },{ id: "bass", inst: "marimba", role: "bass", notes: bass, gainDb: -3, opts: grid },- { id: "pizz", inst: "guitar", role: "melody", notes: pl, gainDb: 1, opts: grid, pan: -0.15 },+ { id: "pizz", inst: "guitar", role: "melody", notes: pl, gainDb: 1, opts: { ...grid, legacy: true }, pan: -0.15 },{ id: "hook", inst: "marimba", role: "melody", notes: mar, gainDb: -1, opts: grid, pan: 0.15 },{ id: "steps", inst: "celesta", role: "color", notes: ping, gainDb: -5, opts: grid, pan: 0.25 },{ id: "tada", inst: "bell", role: "color", notes: bell, gainDb: -7, opts: grid },diff --git a/skills/anidoodle/engine/src/canvas-core/music/ensemble.ts b/skills/anidoodle/engine/src/canvas-core/music/ensemble.tsnew file mode 100644index 0000000..02247a7--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/music/ensemble.ts@@ -0,0 +1,382 @@+// ENSEMBLE VOICES, v2 (2026-09-28 sound rebuild, track S4): string sections as independent+// players, pizzicato, a bowed-waveguide solo, section brass, flute and clarinet, a glottal-source+// choir, plucked guitars (nylon, steel, clean electric) and harp, and four basses. Every note varies+// (seeded per note), nothing restarts at phase zero, and players drift like players.+// These are CINEMATIC, STYLIZED models: honest physics where we can reach it (plucks, bows, the+// voice source and its formants), careful spectral models where we can't (brass, winds). A human+// listens before any acoustic timbre carries a film (spec 08 DECISION B).+// The old voices stay in instruments.ts / orchestra.ts behind `opts.legacy` (shipped films).+import { type Rng, TAU, clamp, pan, SVF, Biquad, blep } from "./dsp";+import type { Played } from "./perform";+import {+ type Out, type Opts, num, f0, out0, wn, gn, subRng, keysRng, concurrency, Drift, Reson, runBody, VIOLIN_BODY, CELLO_BODY,+ type IRSpec, monoLows, IR_NYLON, IR_STEEL, IR_HARP, IR_UPRIGHT, bodyIR, convolve, pluckExc, ksRender, headRms, FORMANTS, FORMANT_BW, BRASS_FORMANT, bumpDb,+} from "./ensembleCore";++// Output levels, matched to the legacy voices' stem RMS on the same material (tools/ensemble-check.mjs+// calibrate), so the style vocabularies' calibrated gains and stem targets still hold.+const LEVEL = { strings: 0.125, pizz: 0.16, solo: 0.146, cello: 1.13, brass: 0.059, horn: 1.4, flute: 0.119, clarinet: 0.118, choir: 3.07, guitar: 0.066, harp: 0.0757, bass: 0.3, synthBass: 0.3 };++const addStereo = (o: Out, i0: number, y: Float64Array | Float32Array, gl: number, gr: number) => { const m = Math.min(y.length, o.L.length - i0); for (let i = Math.max(0, -i0); i < m; i++) { o.L[i0 + i] += y[i] * gl; o.R[i0 + i] += y[i] * gr; } };+const onsetI = (t: number, sr: number) => Math.max(0, Math.round(t * sr));++// ------------------------------------------------------------------ strings: the section+// Seating (audience view): violins left, violas centre-right, cellos and basses right.+const SEAT = [0.3, 0.12, -0.3];+const BUS_BODY: [typeof VIOLIN_BODY, number][] = [[CELLO_BODY, 1], [VIOLIN_BODY, 0.82], [VIOLIN_BODY, 1]];+const regOf = (p: number, bassRole: boolean) => (bassRole || p < 55 ? 0 : p < 63 ? 1 : 2);++/**+ * String section: each note is played by `players` (default 10, divided across a chord) independent+ * players. Each has its own onset (+-12 ms), tuning (+-6 c), 1/f drift, vibrato (5-6.5 Hz, 10-25 c,+ * delayed 150-400 ms, its rate wandering), bow attack and release, a band-limited sawtooth (the+ * Helmholtz motion) with pulse-synchronous bow noise and a scratch at the attack, and a low-pass that+ * opens with loudness (cutoff ~ amp^0.7). Players sum into register buses (cellos, violas, violins),+ * each through its measured body resonances (ensembleCore). `pizz: true` plucks instead.+ * Options kept from v1: attack, release, width, bright, bass; new: players, vibrato, noise, pizz.+ */+export const strings = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ if (o.pizz === true) return pizzicato(keys, sr, n, o, r0);+ const atk = num(o, "attack", 0.45), rel = num(o, "release", 0.9), w = num(o, "width", 0.8), bright = num(o, "bright", 1), S = Math.round(num(o, "players", 10)), vibAmt = num(o, "vibrato", 1), noiseAmt = num(o, "noise", 1), bassRole = o.bass === true;+ const buses = [out0(n), out0(n), out0(n)], used = [false, false, false], conc = concurrency(keys), short = atk < 0.06;+ keys.forEach((k, ki) => {+ const r = subRng(r0), f = f0(k.p), reg = regOf(k.p, bassRole), P = clamp(Math.round(S / conc[ki]) + 1, 3, S), dur = Math.max(0.03, k.off - k.t), bus = buses[reg]; used[reg] = true;+ const g = (LEVEL.strings * (bassRole ? 1.6 : 1) * Math.min(1, Math.pow(1047 / f, 0.8)) /* above C6 the section thins */ * Math.pow(k.v, 1.2)) / Math.sqrt(P), sig = (short ? 2.5 : 3 + 4 * clamp(atk / 0.4, 0, 1)) / 1000;+ for (let p = 0; p < P; p++) {+ const i0 = onsetI(k.t + clamp(gn(r) * sig, -0.015, 0.015), sr), a = Math.max(0.007, atk * (0.75 + 0.5 * r())), rl = rel * (0.85 + 0.3 * r()), vel = k.v * (0.88 + 0.24 * r());+ const cents0 = clamp(gn(r) * 3, -7, 7), vr = 5 + 1.5 * r(), vd = (10 + 15 * r()) * vibAmt, vDel = 0.15 + 0.25 * r(), dr = new Drift(r, 0.35, sr), vw = new Drift(r, 0.5, sr, 256), ad = new Drift(r, 0.3, sr, 256);+ const [gl, gr] = pan(clamp((bassRole ? 0.12 : SEAT[reg]) * w + (r() - 0.5) * w, -1, 1)), lp = new SVF(sr, 2000, 0.6), cutK = reg === 0 ? 0.7 : 1;+ const len = Math.min(n - i0, Math.ceil((dur + rl * 1.6) * sr)), riseEnd = 1 - Math.exp(-dur / (a / 3)), scratch = (short ? 0.5 : 0.18) * noiseAmt, bowN = (0.05 + 0.07 * (1 - k.v)) * noiseAmt;+ let ph = r(), vph = r() * TAU, nz = 0, env = 0;+ for (let i = 0; i < len; i++) {+ const t = i / sr;+ env = t < dur ? 1 - Math.exp(-t / (a / 3)) : riseEnd * Math.exp(-(t - dur) / (rl / 3));+ const vibR = clamp((t - vDel) / 0.35, 0, 1);+ vph += (TAU * vr * (1 + 0.1 * vw.tick())) / sr;+ const cents = cents0 + 3 * dr.tick() + (vibR > 0 ? vd * vibR * Math.sin(vph) : 0), inc = (f * Math.pow(2, cents / 1200)) / sr;+ ph += inc; if (ph >= 1) ph -= 1;+ const x0 = wn(r); nz += 0.25 * (x0 - nz); // one-pole low-pass of the noise; x0 - nz is its high-passed residue+ const bn = (x0 - nz) * (ph < 0.12 ? 1 : 0.3) * (bowN + scratch * Math.exp(-t / 0.035));+ if ((i & 15) === 0) lp.set(clamp((650 + 7500 * Math.pow(env * vel, 0.7)) * bright * cutK, f * 1.5, sr * 0.45), 0.65);+ const y = lp.tick(2 * ph - 1 - blep(ph, inc) + bn) * env * g * (1 + 0.06 * ad.tick()), j = i0 + i;+ bus.L[j] += y * gl; bus.R[j] += y * gr;+ }+ }+ });+ return mixBuses(buses, used, sr, n, bassRole);+};+const mixBuses = (buses: Out[], used: boolean[], sr: number, n: number, bassRole: boolean): Out => {+ const out = out0(n);+ buses.forEach((b, i) => { if (!used[i]) return; const [body, sc] = BUS_BODY[i]; runBody(b.L, sr, body, sc * 0.97); runBody(b.R, sr, body, sc * 1.03); for (let j = 0; j < n; j++) { out.L[j] += b.L[j]; out.R[j] += b.R[j]; } });+ if (!bassRole) { Biquad.make(sr, "hp", 90, 0.7).run(out.L); Biquad.make(sr, "hp", 90, 0.7).run(out.R); }+ return monoLows(out, sr);+};++/** Pizzicato section: 2-5 players per note, each a finger-plucked string (extended KS) through the same register bodies. */+const pizzicato = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ const w = num(o, "width", 0.8), S = Math.round(num(o, "players", 10)), bassRole = o.bass === true, conc = concurrency(keys);+ const buses = [out0(n), out0(n), out0(n)], used = [false, false, false];+ keys.forEach((k, ki) => {+ const reg = regOf(k.p, bassRole), P = clamp(Math.round(S / conc[ki] / 2) + 1, 2, 5); used[reg] = true;+ for (let p = 0; p < P; p++) {+ const r = subRng(r0), f = f0(k.p) * Math.pow(2, clamp(gn(r) * 3, -6, 6) / 1200), i0 = onsetI(k.t + clamp(gn(r) * 0.007, -0.015, 0.015), sr), v = k.v * (0.85 + 0.3 * r());+ const exc = pluckExc(sr, f, { width: 0.0025 * (1.25 - 0.45 * v), beta: 0.2 + 0.12 * r(), noise: 0.3, lp: f * 2.5 * (0.6 + 0.8 * v) }, r);+ const T60 = clamp(1.1 * Math.pow(262 / f, 0.7), 0.25, 2.6) * (0.9 + 0.2 * r()), len = Math.min(n - i0, Math.ceil(T60 * 1.2 * sr));+ if (len <= 0) continue;+ const y = ksRender(sr, { f, T60, loss: 0.4, exc, len, relI: len, relT60: 1, pol2: { cents: 0.4 + 0.6 * r(), mix: 0.6, t60: 0.5, share: r() } });+ const g = (LEVEL.pizz * Math.pow(v, 1.3)) / headRms(y, sr) / Math.sqrt(P), [gl, gr] = pan(clamp(SEAT[reg] * w + (r() - 0.5) * 0.6 * w, -1, 1));+ addStereo(buses[reg], i0, y, gl * g, gr * g);+ }+ });+ return mixBuses(buses, used, sr, n, bassRole);+};++// ------------------------------------------------------------------ bowed solo: a waveguide string+/**+ * Solo violin / cello: a bowed digital waveguide after STK's Bowed (Cook & Scavone, STK licence,+ * MIT-style; re-implemented, not copied): neck and bridge delay lines around a bow junction whose+ * friction table is (|slope (dv + 0.001)| + 0.75)^-4, a one-pole string loss at the bridge, vibrato+ * as a delay-length change. Touching notes are one bow stroke with a finger slide (legato), so a line+ * sings instead of re-attacking. The bridge signal goes through the measured body (ensembleCore).+ * The loop is tuned exactly: the loss filter's phase delay is taken out of the delay lengths.+ * Options: attack, vibrato (cents), pan, cello; new: pressure (0..1), position (0..1).+ */+export const bowedSolo = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ const out = out0(n), cello = o.cello === true, atk = num(o, "attack", 0.09), vibC = num(o, "vibrato", cello ? 12 : 22), [gl, gr] = pan(num(o, "pan", -0.1));+ const press = num(o, "pressure", 0.5), posn = num(o, "position", 0.5), mono = new Float32Array(n);+ const ks = keys.slice().sort((a, b) => a.t - b.t || a.p - b.p), lines: Played[][] = [];+ for (const k of ks) { const ln = lines.find((l) => { const e = l[l.length - 1]; return e.t < k.t - 0.02 && Math.abs(e.off - k.t) < 0.06; }); if (ln) ln.push(k); else lines.push([k]); }+ const pole = 0.75 - (0.2 * 22050) / sr, sg = 0.95;+ for (const ln of lines) {+ const r = subRng(r0), start = ln[0].t, end = ln[ln.length - 1].off, i0 = onsetI(start, sr), fLo = f0(Math.min(...ln.map((k) => k.p))) * 0.97;+ const M = Math.ceil(sr / fLo) + 8, neck = new Float64Array(M), brid = new Float64Array(M); let wi = 0, sl = 0;+ const tail = cello ? 1.2 : 0.7, len = Math.min(n - i0, Math.ceil((end - start + tail) * sr)); if (len <= 0) continue;+ const beta = 0.027236 + 0.2 * posn * (0.85 + 0.3 * r()), slope0 = 5 - 4 * (0.88 + 0.12 * clamp(press, 0, 1)) /* below ~0.9 the string slips into its octave on high legato notes (tools/ensemble-check.mjs track) */, dr = new Drift(r, 0.3, sr), vw = new Drift(r, 0.6, sr, 256);+ let seg = 0, fCur = f0(ln[0].p), vph = r() * TAU, env = 0, segT = 0, vel = ln[0].v, slope = slope0 * (0.9 + 0.2 * r());+ const vr = 5.3 + 0.7 * r(), tune = ln.map(() => clamp(gn(r) * 2.5, -5, 5)), kEnv = Math.exp(-1 / (Math.max(0.01, atk / 3) * sr)), kRel = Math.exp(-1 / (0.03 * sr)), kGl = Math.exp(-1 / (0.02 * sr)), kVel = Math.exp(-1 / (0.06 * sr));+ const y = new Float64Array(len);+ for (let i = 0; i < len; i++) {+ const t = start + i / sr;+ while (seg + 1 < ln.length && t >= ln[seg + 1].t) { seg++; segT = 0; }+ const k = ln[seg], fT = f0(k.p) * Math.pow(2, tune[seg] / 1200); fCur = fT + (fCur - fT) * kGl; vel = k.v + (vel - k.v) * kVel;+ const on = t < end; env = on ? 1 + (env - 1) * kEnv : env * kRel;+ const vib = vibC * clamp((segT - 0.18) / 0.35, 0, 1), vibc = vib > 0 ? vib * Math.sin(vph) : 0; vph += (TAU * vr * (1 + 0.08 * vw.tick())) / sr; segT += 1 / sr;+ const fr = fCur * Math.pow(2, (vibc + 2 * dr.tick()) / 1200), w = (TAU * fr) / sr, pd = Math.atan2(pole * Math.sin(w), 1 - pole * Math.cos(w)) / w;+ const P0 = sr / fr, base = clamp(P0 - pd + 0.3 + 0.00045 * P0, 4, M - 4) /* measured junction offset, tools/ensemble-check.mjs check */, Dn = base * (1 - beta), Db = base * beta;+ const rn = wi - Dn, rb = wi - Db, rn0 = Math.floor(rn), rb0 = Math.floor(rb), fn = rn - rn0, fb = rb - rb0;+ const nOut = neck[(rn0 + M) % M] * (1 - fn) + neck[(rn0 + 1 + M) % M] * fn, bOut = brid[(rb0 + M) % M] * (1 - fb) + brid[(rb0 + 1 + M) % M] * fb;+ sl = sg * (1 - pole) * bOut + pole * sl;+ const bridgeRefl = -sl, nutRefl = -nOut, strVel = bridgeRefl + nutRefl;+ const bowVel = (0.03 + 0.2 * (0.3 + 0.7 * vel)) * env * (1 + 0.012 * wn(r)), dv = bowVel - strVel;+ const tbl = Math.min(1, Math.pow(Math.abs(slope * (dv + 0.001)) + 0.75, -4)), nv = dv * tbl;+ neck[wi] = bridgeRefl + nv; brid[wi] = nutRefl + nv; wi = wi + 1 === M ? 0 : wi + 1;+ y[i] = bOut;+ }+ const g = LEVEL.solo * (cello ? LEVEL.cello : 1); for (let i = 0; i < len; i++) mono[i0 + i] += y[i] * g;+ }+ const L = Float32Array.from(mono), R = Float32Array.from(mono), body = cello ? CELLO_BODY : VIOLIN_BODY;+ runBody(L, sr, body, 0.99); runBody(R, sr, body, 1.01);+ Biquad.make(sr, "lp", Math.min(9000, sr * 0.45), 0.7).run(L); Biquad.make(sr, "lp", Math.min(9000, sr * 0.45), 0.7).run(R);+ for (let i = 0; i < n; i++) { out.L[i] = L[i] * gl; out.R[i] = R[i] * gr; }+ return out;+};++// ------------------------------------------------------------------ additive core (brass, winds)+/** sum_n w[n] sin(n th) by the Chebyshev recurrence: one sin + one cos per sample, band-limited by construction. */+const harmonics = (th: number, w: Float64Array, N: number) => { const s1 = Math.sin(th), c2 = 2 * Math.cos(th); let sp = 0, sc = s1, acc = w[0] * s1; for (let h = 1; h < N; h++) { const sn = c2 * sc - sp; acc += w[h] * sn; sp = sc; sc = sn; } return acc; };+const BLK = 32;++// ------------------------------------------------------------------ brass+/**+ * Cinematic brass: a spectral model of a lip-reed instrument. Harmonic n's amplitude follows the+ * loudness L as L^(1 + gamma (n-1)) (Risset / Beauchamp: brass brightens with loudness, and the upper+ * harmonics build after the fundamental, the "blat"), times a bell-radiation formant (trumpet,+ * trombone or horn, ensembleCore). A lip scoop of -30..-50 c into each note, breath and a tongued+ * puff, growl at ff, 3 players per section by default with their own timing, tuning and drift.+ * Options: attack, bright, width; new: players, release, vibrato (cents), type (trumpet|trombone|horn).+ */+export const brass = (keys: Played[], sr: number, n: number, o: Opts): Out => {+ const out = out0(n), r0 = keysRng(keys, 0xb7a55), atk = num(o, "attack", 0.06), bright = num(o, "bright", 1), w = num(o, "width", 0.5), rel = num(o, "release", 0.14), vibC = num(o, "vibrato", 8);+ const players = Math.round(num(o, "players", 3)), conc = concurrency(keys), gamma = 0.55 / Math.max(0.3, bright);+ const fixed = typeof o.type === "string" ? String(o.type) : bright < 0.75 ? "horn" : null;+ keys.forEach((k, ki) => {+ const P = clamp(Math.round(players / conc[ki]), 1, players), type = BRASS_FORMANT[fixed ?? (k.p >= 58 ? "trumpet" : "trombone")] ?? BRASS_FORMANT.trumpet, dur = Math.max(0.03, k.off - k.t);+ for (let p = 0; p < P; p++) {+ const r = subRng(r0), i0 = onsetI(k.t + clamp(gn(r) * 0.006, -0.012, 0.012), sr), f = f0(k.p) * Math.pow(2, clamp(gn(r) * 2.5, -5, 5) / 1200), v = clamp(k.v * (0.92 + 0.16 * r()), 0, 1);+ const N = Math.max(1, Math.min(40, Math.floor((0.45 * sr) / (f * 1.04)))), gdb = new Float64Array(N), wA = new Float64Array(N), wB = new Float64Array(N), wC = new Float64Array(N);+ for (let h = 0; h < N; h++) gdb[h] = Math.pow(10, bumpDb((h + 1) * f, type) / 20) * Math.pow(h + 1, -0.35);+ const a = atk * (0.8 + 0.4 * r()), rl = rel * (0.85 + 0.3 * r()), scoop = -(30 + 20 * r()), sT = (0.04 + 0.04 * r()) / 3, dyn = 0.22 + 0.78 * v, dr = new Drift(r, 0.3, sr);+ const itd = Math.round(Math.abs(((k.p - 60) / 30) * w) * 0.0005 * sr + r() * 0.0002 * sr), vr = 5 + r(), vph0 = r() * TAU, growl = Math.max(0, v - 0.75) * 0.6, gph = r() * TAU, bp = new SVF(sr, clamp(f * 3, 700, 3000), 0.8), [gl, gr] = pan(clamp(((k.p - 60) / 30) * w + (r() - 0.5) * 0.9 * w, -1, 1));+ const len = Math.min(n - i0, Math.ceil((dur + rl * 1.5) * sr)), riseEnd = 1 - Math.exp(-dur / (a / 3)), g = (LEVEL.brass * (type === BRASS_FORMANT.horn ? LEVEL.horn : 1) * Math.sqrt(v)) / Math.sqrt(P);+ let th = r() * TAU;+ const weights = (t: number, dst: Float64Array) => { const e = t < dur ? 1 - Math.exp(-t / (a / 3)) : riseEnd * Math.exp(-(t - dur) / (rl / 4)), L = Math.max(1e-4, e * dyn);+ for (let h = 0; h < N; h++) dst[h] = gdb[h] * Math.pow(L, 1 + gamma * h); return e; };+ let e0 = weights(0, wA), e1 = e0;+ for (let i = 0; i < len; i++) {+ const bi = i % BLK; if (bi === 0) { e0 = weights(i / sr, wA); e1 = weights((i + BLK) / sr, wB); }+ const x = bi / BLK; for (let h = 0; h < N; h++) wC[h] = wA[h] + (wB[h] - wA[h]) * x;+ const t = i / sr, e = e0 + (e1 - e0) * x, vibR = dur > 0.5 ? clamp((t - 0.3) / 0.4, 0, 1) : 0;+ const cents = scoop * Math.exp(-t / sT) + 3 * dr.tick() + (vibR > 0 ? vibC * vibR * Math.sin(TAU * vr * t + vph0) : 0);+ th += (TAU * f * Math.pow(2, cents / 1200)) / sr; if (th > TAU) th -= TAU;+ bp.tick(wn(r)); const breath = bp.bp * (0.02 * e * dyn + 0.12 * v * Math.exp(-t / 0.02));+ const am = growl > 0 ? 1 + growl * Math.sin(TAU * 31 * t + gph) : 1, y = (harmonics(th, wC, N) * am + breath) * g, j = i0 + i;+ // a spaced microphone pair: the far channel hears each player a fraction of a millisecond later+ if (gl >= gr) { out.L[j] += y * gl; if (j + itd < n) out.R[j + itd] += y * gr; } else { if (j + itd < n) out.L[j + itd] += y * gl; out.R[j] += y * gr; }+ }+ }+ });+ return monoLows(out, sr);+};++// ------------------------------------------------------------------ woodwinds+// Flute: nearly a sine when soft, the 2nd harmonic strong in the low register, richer when loud+// (Fletcher & Rossing 1998, ch. 16). Clarinet: odd harmonics under the bore cut-off (~1.6 kHz), both+// parities above it (Benade). Shapes voiced by hand.+const FLUTE = [1, 0.45, 0.2, 0.1, 0.05, 0.03, 0.02, 0.012, 0.008, 0.005];+/**+ * Woodwind: flute (jet: a chiff and an overblown octave flicker at the attack, breath noise both+ * broadband and pitched at the jet, amplitude-led vibrato) or `reed: true` clarinet (odd harmonics,+ * reed noise, no vibrato). Slurred notes (touching) glide without re-tonguing. Seeded per note.+ * Options: reed, breath, width; new: vibrato (cents).+ */+export const woodwind = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ const out = out0(n), reed = o.reed === true, breath = num(o, "breath", reed ? 0.02 : 0.08) / (reed ? 0.02 : 0.08), w = num(o, "width", 0.3), vibC = num(o, "vibrato", reed ? 0 : 7);+ const ks = keys.slice().sort((a, b) => a.t - b.t);+ ks.forEach((k, ki) => {+ const r = subRng(r0), prev = ki > 0 ? ks[ki - 1] : null, next = ki + 1 < ks.length ? ks[ki + 1] : null;+ const legato = !!prev && prev.t < k.t - 0.02 && Math.abs(prev.off - k.t) < 0.03, slurOut = !!next && next.t > k.t + 0.02 && Math.abs(k.off - next.t) < 0.03;+ const f = f0(k.p) * Math.pow(2, clamp(gn(r) * 2, -4, 4) / 1200), i0 = onsetI(k.t + (legato ? 0 : clamp(gn(r) * 0.003, -0.006, 0.006)), sr), v = clamp(k.v * (0.93 + 0.14 * r()), 0, 1), dur = Math.max(0.03, k.off - k.t);+ const a = legato ? 0.018 : (reed ? 0.03 : 0.05) * (0.8 + 0.4 * r()), rl = slurOut ? 0.03 : 0.09 * (0.8 + 0.4 * r()), dyn = 0.3 + 0.7 * v, riseEnd = 1 - Math.exp(-dur / (a / 3));+ const N = Math.max(1, Math.min(reed ? 24 : 10, Math.floor((0.45 * sr) / (f * 1.02)))), base = new Float64Array(N), wA = new Float64Array(N), wB = new Float64Array(N), wC = new Float64Array(N);+ for (let h = 0; h < N; h++) { const m = h + 1, fr = m * f;+ base[h] = reed ? (fr < 1600 ? (m % 2 ? 1 : 0.035) * Math.pow(m, -0.75) : Math.pow(m, -0.75) * Math.pow(1600 / fr, 1.2)) : FLUTE[h] * (m === 2 && f < 400 ? 1.5 : 1) * (f > 1000 ? Math.pow(m, -0.5) : 1); }+ const gam = reed ? 1.1 : 0.9, chiff = !legato && !reed, vr = 4.9 + 0.7 * r(), vph = r() * TAU, dr = new Drift(r, 0.3, sr), [gl, gr] = pan(clamp(((k.p - 64) / 30) * w, -1, 1));+ const bpJet = new SVF(sr, Math.min(f, sr * 0.4), 5), bpC = new SVF(sr, Math.min(reed ? 2000 : 2600, sr * 0.4), reed ? 0.7 : 1.2), len = Math.min(n - i0, Math.ceil((dur + rl * 1.6) * sr)), g = LEVEL[reed ? "clarinet" : "flute"];+ let th = r() * TAU, hp = 0;+ const weights = (t: number, dst: Float64Array) => { const e = t < dur ? 1 - Math.exp(-t / (a / 3)) : riseEnd * Math.exp(-(t - dur) / (rl / 4)), L = Math.max(1e-4, e * dyn), ch = chiff ? 1 + 2 * Math.exp(-t / 0.03) : 1;+ for (let h = 0; h < N; h++) dst[h] = base[h] * Math.pow(L, 1 + gam * h) * (h === 1 ? ch : 1); return e; };+ let e0 = weights(0, wA), e1 = e0;+ for (let i = 0; i < len; i++) {+ const bi = i % BLK; if (bi === 0) { e0 = weights(i / sr, wA); e1 = weights((i + BLK) / sr, wB); }+ const x = bi / BLK; for (let h = 0; h < N; h++) wC[h] = wA[h] + (wB[h] - wA[h]) * x;+ const t = i / sr, e = e0 + (e1 - e0) * x, vibR = dur > 0.35 ? clamp((t - 0.22) / 0.3, 0, 1) : 0, vs = vibR > 0 ? Math.sin(TAU * vr * t + vph) : 0;+ const cents = (chiff ? -12 * Math.exp(-t / 0.03) : 0) + 2 * dr.tick() + vibC * vibR * vs;+ th += (TAU * f * Math.pow(2, cents / 1200)) / sr; if (th > TAU) th -= TAU;+ const nz = wn(r); hp += 0.2 * (nz - hp); bpJet.tick(nz); bpC.tick(nz);+ const air = reed ? bpC.bp * (0.012 * breath * e * dyn + 0.05 * v * Math.exp(-t / 0.012)) : (nz - hp) * 0.03 * breath * (0.3 + e * dyn) * e + bpJet.bp * 0.2 * breath * e * dyn + (chiff ? bpC.bp * 0.2 * v * Math.exp(-t / 0.025) : 0);+ const y = (harmonics(th, wC, N) * (reed ? 1 : 1 + 0.07 * vibR * vs) + air) * g, j = i0 + i;+ out.L[j] += y * gl; out.R[j] += y * gr;+ }+ });+ return out;+};++// ------------------------------------------------------------------ choir+/**+ * Choir: singers with a glottal source (Rosenberg pulse, the flow derivative with its closure step+ * band-limited by PolyBLEP; open quotient ~0.62, smaller when loud = brighter), per-period jitter+ * (~0.4 %) and shimmer (~0.4 dB), aspiration during the open phase and a soft "h" on entries, their+ * own vibrato, tuning and drift. Singers sum into bass / tenor / alto / soprano buses, each through a+ * cascade of five formant resonators for the vowel (Peterson & Barney), with F1 raised to follow a+ * high soprano pitch (formant tuning) and L/R vocal tracts 3 % apart. `vowel`: one of a e i o u, or a+ * sequence ("ao", "uoa") that morphs across each note. Options: vowel, attack, width; new: singers,+ * vibrato, breath, release.+ */+export const choir = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ const seq = String(o.vowel ?? "a").split("").filter((c) => FORMANTS[c]), vowels = seq.length ? seq : ["a"];+ const atk = num(o, "attack", 0.35), rel = num(o, "release", 0.7), w = num(o, "width", 0.8), S = Math.round(num(o, "singers", 8)), vibAmt = num(o, "vibrato", 1), breath = num(o, "breath", 1), conc = concurrency(keys);+ const secOf = (p: number) => (p < 52 ? 0 : p < 60 ? 1 : p < 67 ? 2 : 3), SEC_SCALE = [0.97, 1.04, 0.97, 1.04], SEC_PAN = [0.3, 0.1, -0.1, -0.3];+ const buses = [0, 1, 2, 3].map(() => out0(n)), secKeys: Played[][] = [[], [], [], []];+ keys.forEach((k, ki) => {+ const sec = secOf(k.p), P = clamp(Math.round(S / conc[ki]) + 1, 3, S), dur = Math.max(0.05, k.off - k.t), bus = buses[sec]; secKeys[sec].push(k);+ for (let p = 0; p < P; p++) {+ const r = subRng(r0), i0 = onsetI(k.t + clamp(gn(r) * (0.008 + 0.02 * clamp(atk, 0, 1)), -0.03, 0.03), sr), v = clamp(k.v * (0.88 + 0.24 * r()), 0, 1), f = f0(k.p), tune = clamp(gn(r) * 4, -9, 9);+ const a = Math.max(0.02, atk * (0.75 + 0.5 * r())), rl = rel * (0.8 + 0.4 * r()), vr = 4.6 + 1.2 * r(), vd = (18 + 20 * r()) * vibAmt, vDel = 0.25 + 0.25 * r(), vph0 = r() * TAU, dr = new Drift(r, 0.3, sr);+ const oq = clamp(0.64 - 0.12 * v + 0.04 * gn(r), 0.45, 0.78), sq = 2.4 + 0.8 * r(), tp = (oq * sq) / (1 + sq), tn = oq / (1 + sq), kO = Math.PI / (2 * tp), kC = Math.PI / (2 * tn);+ const [gl, gr] = pan(clamp(SEC_PAN[sec] * w + (r() - 0.5) * 0.9 * w, -1, 1)), len = Math.min(n - i0, Math.ceil((dur + rl * 1.6) * sr)), riseEnd = 1 - Math.exp(-dur / (a / 3));+ const g = (LEVEL.choir * Math.pow(v, 0.8)) / Math.sqrt(P), asp = 0.035 * breath, hBurst = 0.25 * breath;+ let ph = r(), jit = 1, shim = 1;+ for (let i = 0; i < len; i++) {+ const t = i / sr, env = t < dur ? 1 - Math.exp(-t / (a / 3)) : riseEnd * Math.exp(-(t - dur) / (rl / 3)), vibR = clamp((t - vDel) / 0.5, 0, 1);+ const cents = tune + 4 * dr.tick() + (vibR > 0 ? vd * vibR * Math.sin(TAU * vr * t + vph0) : 0), inc = (f * jit * Math.pow(2, cents / 1200)) / sr;+ ph += inc; if (ph >= 1) { ph -= 1; jit = 1 + 0.004 * gn(r); shim = 1 + 0.05 * gn(r); }+ let gs = ph < tp ? (kO / kC) * Math.sin(Math.PI * (ph / tp)) : ph < oq ? -Math.sin(Math.PI * ((ph - tp) / (2 * tn))) : 0;+ let tc = ph - oq; if (tc < 0) tc += 1; gs += 0.5 * blep(tc, inc); // the closure step (+1), band-limited+ const y = (gs * shim + wn(r) * (asp * (ph < oq ? 1 : 0.35) + hBurst * Math.exp(-t / 0.05) * (1 - env))) * env * g, j = i0 + i;+ bus.L[j] += y * gl; bus.R[j] += y * gr;+ }+ }+ });+ const out = out0(n), B = 64;+ buses.forEach((bus, sec) => {+ const sk = secKeys[sec].slice().sort((a, b) => a.t - b.t); if (!sk.length) return;+ const tab = sec < 2 ? "m" : "w", bw = FORMANT_BW[tab], rs = [0, 1].map(() => [0, 1, 2, 3, 4].map(() => new Reson()));+ let kIdx = 0, gPrev = -1;+ for (let b0 = 0; b0 < n; b0 += B) {+ const t = b0 / sr; while (kIdx + 1 < sk.length && sk[kIdx + 1].t <= t) kIdx++;+ const k = sk[kIdx], pos = vowels.length > 1 ? clamp((t - k.t) / Math.max(0.2, k.off - k.t), 0, 1) * (vowels.length - 1) : 0, vi = Math.min(vowels.length - 2, Math.floor(pos)), fr = vowels.length > 1 ? pos - Math.max(0, vi) : 0;+ const A = FORMANTS[vowels[Math.max(0, vi)]][tab], Z = FORMANTS[vowels[Math.min(vowels.length - 1, Math.max(0, vi) + 1)]][tab];+ let fmax = 0; for (let q = kIdx; q >= 0 && q > kIdx - 12; q--) if (sk[q].t <= t && sk[q].off + 0.3 > t) fmax = Math.max(fmax, f0(sk[q].p));+ let gg = 0;+ for (let s = 0; s < 2; s++) { const sc = SEC_SCALE[sec] * (s ? 1.015 : 0.985);+ for (let fi = 0; fi < 5; fi++) { let F = (A[fi] + (Z[fi] - A[fi]) * fr) * sc; if (fi === 0) F = Math.max(F, fmax * 1.12); if (fi === 1) F = Math.max(F, (A[0] + (Z[0] - A[0]) * fr) * sc + 250); rs[s][fi].set(sr, F, bw[fi]); }+ if (s === 0) { const F1 = rs[0][0]; let m = 1; const fq = Math.max(fmax * 1.12, (A[0] + (Z[0] - A[0]) * fr) * sc); for (const q of rs[0]) m *= q.mag(sr, fq); gg = 1 / Math.max(1e-6, m); void F1; } }+ if (gPrev < 0) gPrev = gg;+ const end = Math.min(n, b0 + B);+ for (let i = b0; i < end; i++) { const gi = gPrev + ((gg - gPrev) * (i - b0)) / B; let l = bus.L[i], rr = bus.R[i]; for (let fi = 0; fi < 5; fi++) { l = rs[0][fi].tick(l); rr = rs[1][fi].tick(rr); } out.L[i] += l * gi; out.R[i] += rr * gi; }+ gPrev = gg;+ }+ });+ Biquad.make(sr, "hp", 70, 0.7).run(out.L); Biquad.make(sr, "hp", 70, 0.7).run(out.R);+ return monoLows(out, sr);+};++// ------------------------------------------------------------------ plucked strings+type PluckSpec = { width: number; beta: number; noise: number; lpMul: number; loss: number; t60: (f: number) => number; relT60: number; ring: boolean; pol2: number; ir?: IRSpec; pick: number; level: number };+const PLUCK: Record<string, PluckSpec> = {+ nylon: { width: 0.0022, beta: 0.2, noise: 0.25, lpMul: 3.5, loss: 0.3, t60: (f) => clamp(3.4 * Math.pow(196 / f, 0.55), 0.7, 7), relT60: 0.18, ring: false, pol2: 0.6, ir: IR_NYLON, pick: 0, level: LEVEL.guitar },+ steel: { width: 0.0007, beta: 0.13, noise: 0.4, lpMul: 9, loss: 0.16, t60: (f) => clamp(4.5 * Math.pow(196 / f, 0.5), 1, 9), relT60: 0.15, ring: false, pol2: 0.7, ir: IR_STEEL, pick: 0.05, level: LEVEL.guitar },+ electric: { width: 0.0008, beta: 0.12, noise: 0.35, lpMul: 7, loss: 0.14, t60: (f) => clamp(6 * Math.pow(196 / f, 0.45), 1.5, 12), relT60: 0.12, ring: false, pol2: 0.4, pick: 0.035, level: LEVEL.guitar },+ harp: { width: 0.003, beta: 0.36, noise: 0.2, lpMul: 3, loss: 0.14, t60: (f) => clamp(4.2 * Math.pow(262 / f, 0.45), 0.8, 8), relT60: 1, ring: true, pol2: 0.5, ir: IR_HARP, pick: 0, level: LEVEL.harp },+};+/** One plucked note (mono): excitation, commuted body, extended KS, optional pickup comb and pick scrape; RMS-normalised then scaled by velocity. */+const pluckNote = (sr: number, n: number, k: Played, s: PluckSpec, r: Rng, extra: { pickup?: number; glide?: { cents: number; tau: number }; thump?: number } = {}) => {+ const f = f0(k.p) * Math.pow(2, clamp(gn(r) * 1.5, -4, 4) / 1200), v = clamp(k.v * (0.9 + 0.2 * r()), 0.02, 1), T60 = s.t60(f) * (0.92 + 0.16 * r());+ let exc = pluckExc(sr, f, { width: s.width * (1.3 - 0.6 * v) * (0.9 + 0.2 * r()), beta: s.beta * (0.88 + 0.24 * r()), noise: s.noise, lp: f * s.lpMul * (0.6 + 0.8 * v) }, r);+ if (s.ir) exc = convolve(exc, bodyIR(sr, s.ir));+ const dur = Math.max(0.02, k.off - k.t), relI = s.ring ? Infinity : Math.round(dur * sr), len = Math.max(1, Math.min(n, Math.ceil((s.ring ? T60 * 1.1 : Math.min(T60 * 1.1, dur + s.relT60 * 1.5)) * sr)));+ const y = ksRender(sr, { f, T60, loss: s.loss * (0.92 + 0.16 * r()), exc, len, relI: relI === Infinity ? len : relI, relT60: s.relT60, pol2: { cents: 0.3 + 0.7 * r(), mix: s.pol2, t60: 0.45, share: r() }, glide: extra.glide });+ if (extra.pickup) { const D = extra.pickup * (sr / f), Di = Math.floor(D), Df = D - Di; for (let i = len - 1; i >= 0; i--) { const a = i - Di >= 0 ? y[i - Di] : 0, b = i - Di - 1 >= 0 ? y[i - Di - 1] : 0; y[i] -= 0.9 * (a * (1 - Df) + b * Df); } }+ const g = (Math.pow(v, 1.3) * clamp(Math.pow(523 / f, s.ring ? 0.8 : 0.5), 0.3, 1.2)) / headRms(y, sr); // high notes of real plucked strings are quieter+ for (let i = 0; i < len; i++) y[i] *= g;+ if (s.pick > 0 || extra.thump) { let lp = 0; const m = Math.min(len, Math.round(0.02 * sr)), kLp = extra.thump ? 0.03 : 0.5;+ for (let i = 0; i < m; i++) { const nz = wn(r); lp += kLp * (nz - lp); const e = Math.exp(-i / (0.003 * sr)); y[i] += (extra.thump ? lp * extra.thump * 4 : (nz - lp) * s.pick * 3) * e * v; } }+ return y;+};++/**+ * Guitar and harp: extended Karplus-Strong (ensembleCore.ksRender) with a pick/finger excitation,+ * pick-position comb, two polarisations and a commuted body IR (nylon or steel dreadnought, harp+ * soundboard). `kind`: nylon (default) | steel | electric. Electric = no body, a pickup-position comb+ * (`pickup`: neck | bridge), the pickup's resonant low-pass, then a small clean amp (asymmetric tanh+ * `drive`, a mid scoop, a 1x12 cab). Harp notes ring past their release, as harpists let them.+ */+export const pluck = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng, inst: "harp" | "guitar"): Out => {+ const out = out0(n), w = num(o, "width", 0.6), kind = inst === "harp" ? "harp" : String(o.kind ?? "nylon"), s = PLUCK[kind] ?? PLUCK.nylon, elec = kind === "electric";+ const pickup = o.pickup === "bridge" ? 0.08 : 0.25;+ for (const k of keys) {+ const r = subRng(r0), i0 = onsetI(k.t + clamp(gn(r) * 0.002, -0.004, 0.004), sr), y = pluckNote(sr, n - i0, k, s, r, elec ? { pickup } : {});+ const [gl, gr] = pan(clamp(clamp((k.p - 64) / 30, -1, 1) * w + (r() - 0.5) * 0.1, -1, 1)); addStereo(out, i0, y, gl * s.level, gr * s.level);+ }+ if (elec) { const drive = num(o, "drive", 1.6);+ for (const ch of [out.L, out.R]) {+ Biquad.make(sr, "lp", Math.min(o.pickup === "bridge" ? 4300 : 3300, sr * 0.45), o.pickup === "bridge" ? 2 : 1.6).run(ch);+ const tb = Math.tanh(0.15), td = Math.tanh(drive); for (let i = 0; i < n; i++) ch[i] = ((Math.tanh(drive * ch[i] * 2 + 0.15) - tb) / td) * 0.5;+ Biquad.make(sr, "hp", 80, 0.7).run(ch); Biquad.make(sr, "lowshelf", 150, 0.7, 2).run(ch); Biquad.make(sr, "peak", 600, 0.8, -3).run(ch); Biquad.make(sr, "peak", 120, 1, 2).run(ch); Biquad.make(sr, "lp", Math.min(5200, sr * 0.45), 0.9).run(ch);+ } }+ return out;+};++// ------------------------------------------------------------------ bass+const BASS: Record<string, PluckSpec & { pickup?: number; lp: [number, number]; amp: boolean }> = {+ finger: { width: 0.003, beta: 0.2, noise: 0.2, lpMul: 2.5, loss: 0.4, t60: (f) => clamp(3.5 * Math.pow(41 / f, 0.3), 1.2, 4), relT60: 0.08, ring: false, pol2: 0.3, pick: 0, level: LEVEL.bass, pickup: 0.14, lp: [2600, 1.1], amp: true },+ pick: { width: 0.0006, beta: 0.12, noise: 0.4, lpMul: 7, loss: 0.25, t60: (f) => clamp(3.2 * Math.pow(41 / f, 0.3), 1.1, 3.6), relT60: 0.07, ring: false, pol2: 0.3, pick: 0.05, level: LEVEL.bass * 0.85, pickup: 0.1, lp: [4200, 1.3], amp: true },+ upright: { width: 0.004, beta: 0.27, noise: 0.25, lpMul: 2, loss: 0.5, t60: (f) => clamp(1.5 * Math.pow(41 / f, 0.25), 0.6, 2), relT60: 0.12, ring: false, pol2: 0.4, ir: IR_UPRIGHT, pick: 0, level: LEVEL.bass * 1.27, lp: [5000, 0.7], amp: false },+};+/**+ * Bass. `kind`: finger (default: electric, fingered, DI + amp) | pick | upright (commuted body, a+ * tension-modulation pitch settle of +12 c, the finger's thump) | synth (saw + sub square into a+ * 24 dB low-pass with a velocity-keyed envelope, glide on touching notes). Mono, centred.+ * Options: drive (amp saturation, as v1); new: kind.+ */+export const bass = (keys: Played[], sr: number, n: number, o: Opts): Out => {+ const kind = String(o.kind ?? "finger"), r0 = keysRng(keys, 0xba55);+ if (kind === "synth") return synthBass(keys, sr, n, o, r0);+ const s = BASS[kind] ?? BASS.finger, mono = new Float32Array(n);+ for (const k of keys) { const r = subRng(r0), i0 = onsetI(k.t + clamp(gn(r) * 0.002, -0.004, 0.004), sr);+ const y = pluckNote(sr, n - i0, k, s, r, { pickup: s.pickup, glide: kind === "upright" ? { cents: 10 + 6 * k.v, tau: 0.045 } : undefined, thump: kind === "upright" ? 0.12 : undefined });+ const m = Math.min(y.length, n - i0); for (let i = 0; i < m; i++) mono[i0 + i] += y[i] * s.level; }+ if (s.amp) { const drive = num(o, "drive", 1.3), td = Math.tanh(drive); for (let i = 0; i < n; i++) mono[i] = Math.tanh(drive * mono[i]) / td; }+ Biquad.make(sr, "hp", 32, 0.7).run(mono); Biquad.make(sr, "lp", Math.min(s.lp[0], sr * 0.45), s.lp[1]).run(mono);+ return { L: mono, R: Float32Array.from(mono) };+};+const synthBass = (keys: Played[], sr: number, n: number, o: Opts, r0: Rng): Out => {+ const mono = new Float32Array(n), ks = keys.slice().sort((a, b) => a.t - b.t), drive = num(o, "drive", 1.5), td = Math.tanh(drive), cut0 = num(o, "cut", 1.4), envAmt = num(o, "env", 1);+ ks.forEach((k, ki) => {+ const r = subRng(r0), prev = ki > 0 ? ks[ki - 1] : null, glideFrom = prev && prev.off > k.t - 0.01 && prev.t < k.t - 0.02 ? f0(prev.p) : 0, f = f0(k.p), i0 = onsetI(k.t, sr), dur = Math.max(0.02, k.off - k.t);+ const next = ki + 1 < ks.length ? ks[ki + 1] : null, cutEnd = next && next.t < k.off + 0.005 ? next.t - k.t : dur + 0.03;+ const len = Math.min(n - i0, Math.ceil(cutEnd * sr) + 1), la = new SVF(sr, 400, 0.8), lb = new SVF(sr, 400, 0.8), dec = 0.15 * (0.9 + 0.2 * r()), cj = 0.94 + 0.12 * r(), dr = new Drift(r, 0.4, sr);+ let ph = r(), ph2 = r();+ for (let i = 0; i < len; i++) {+ const t = i / sr, fr = (glideFrom ? f + (glideFrom - f) * Math.exp(-t / 0.012) : f) * Math.pow(2, (2.5 * dr.tick()) / 1200), inc = fr / sr, inc2 = inc / 2;+ ph += inc; if (ph >= 1) ph -= 1; ph2 += inc2; if (ph2 >= 1) ph2 -= 1;+ let sq = ph2 < 0.5 ? 1 : -1; sq += blep(ph2, inc2); let q = ph2 - 0.5; if (q < 0) q += 1; sq -= blep(q, inc2);+ const x = 2 * ph - 1 - blep(ph, inc) + 0.45 * sq;+ if ((i & 15) === 0) { const c = clamp((fr * 2.2 * cut0 + 1600 * envAmt * k.v * Math.exp(-t / dec)) * cj, 60, sr * 0.4); la.set(c, 0.75); lb.set(c, 0.9); }+ const e = Math.min(1, t / 0.003) * (0.8 + 0.2 * Math.exp(-t / 0.2)) * (t < dur ? 1 : Math.exp(-(t - dur) / 0.03)) * (t > cutEnd - 0.004 && next && next.t < k.off + 0.005 ? Math.max(0, (cutEnd - t) / 0.004) : 1);+ mono[i0 + i] += (Math.tanh(drive * lb.tick(la.tick(x)) * 0.7) / td) * e * Math.pow(k.v, 1.1) * LEVEL.synthBass;+ }+ });+ Biquad.make(sr, "hp", 30, 0.7).run(mono);+ return { L: mono, R: Float32Array.from(mono) };+};diff --git a/skills/anidoodle/engine/src/canvas-core/music/ensembleCore.ts b/skills/anidoodle/engine/src/canvas-core/music/ensembleCore.tsnew file mode 100644index 0000000..a712d69--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/music/ensembleCore.ts@@ -0,0 +1,187 @@+// Shared machinery for the ensemble voices (ensemble.ts): per-note seeded variation, slow random+// drift, an extended Karplus-Strong string, commuted body impulse responses, formant resonators and+// the "measured physics" tables (body modes, formants, spectra) with their provenance.+// Everything is pure in its inputs: same keys + seed -> the same samples.+import { type Rng, TAU, clamp, Biquad } from "./dsp";+import { rng as mkRng } from "../core";+import type { Played } from "./perform";++export type Out = { L: Float32Array; R: Float32Array };+export type Opts = Record<string, number | boolean | string>;+export const num = (o: Opts, k: string, d: number) => (typeof o[k] === "number" ? (o[k] as number) : d);+export const f0 = (m: number) => 440 * Math.pow(2, (m - 69) / 12);+export const out0 = (n: number): Out => ({ L: new Float32Array(n), R: new Float32Array(n) });+/** Fast unit-variance noise (uniform, sqrt 12 scaled): cheap enough to run per sample per player. */+export const wn = (r: Rng) => (r() - 0.5) * 3.4641016;+export const gn = (r: Rng) => { let u = r(); if (u < 1e-12) u = 1e-12; return Math.sqrt(-2 * Math.log(u)) * Math.cos(TAU * r()); };+/** An independent stream per note, drawn from the part's stream: a note's randomness never shifts its neighbours'. */+export const subRng = (r: Rng): Rng => mkRng(Math.floor(r() * 4294967296));+/** A part stream for voices the renderer hands no rng: a hash of the performed keys (deterministic). */+export const keysRng = (keys: Played[], salt: number): Rng => {+ let h = (2166136261 ^ salt) >>> 0;+ for (const k of keys) { h = Math.imul(h ^ Math.round(k.t * 1000), 16777619); h = Math.imul(h ^ k.p, 16777619); h = Math.imul(h ^ Math.round(k.v * 1000), 16777619); }+ return mkRng(h >>> 0);+};+/** How many keys sound at each key's onset (itself included): a section divides its players across a chord (divisi). */+export const concurrency = (keys: Played[]) => {+ const idx = keys.map((_, i) => i).sort((a, b) => keys[a].t - keys[b].t), c = new Array<number>(keys.length).fill(1);+ for (let a = 0; a < idx.length; a++) { const k = keys[idx[a]]; let m = 0;+ for (let b = a; b >= 0; b--) { const q = keys[idx[b]]; if (q.off > k.t && q.t <= k.t + 0.03) m++; if (k.t - q.t > 12) break; }+ for (let b = a + 1; b < idx.length && keys[idx[b]].t <= k.t + 0.03; b++) m++;+ c[idx[a]] = Math.max(1, m); }+ return c;+};++/**+ * Slow random drift (Ornstein-Uhlenbeck, unit standard deviation), updated every `block` samples+ * and interpolated between, so pitch and level wander the way a player does instead of a sine LFO.+ */+export class Drift {+ private a = 0; private b = 0; private i = 0; private k: number; private s: number;+ constructor(private r: Rng, hz: number, sr: number, private block = 64) { const th = TAU * hz * (block / sr); this.k = Math.exp(-th); this.s = Math.sqrt(1 - this.k * this.k); this.a = gn(r); this.b = this.a * this.k + this.s * gn(r); }+ tick() { if (this.i >= this.block) { this.i = 0; this.a = this.b; this.b = this.b * this.k + this.s * wn(this.r); } return this.a + ((this.b - this.a) * this.i++) / this.block; }+}++/** Klatt-style resonator (unity gain at DC); a cascade of five is a vowel's vocal tract. */+export class Reson {+ private y1 = 0; private y2 = 0; A = 1; B = 0; C = 0;+ set(sr: number, f: number, bw: number) { const T = 1 / sr, fr = clamp(f, 20, sr * 0.46); this.C = -Math.exp(-TAU * bw * T); this.B = 2 * Math.exp(-Math.PI * bw * T) * Math.cos(TAU * fr * T); this.A = 1 - this.B - this.C; }+ tick(x: number) { const y = this.A * x + this.B * this.y1 + this.C * this.y2; this.y2 = this.y1; this.y1 = y; return y; }+ /** |H| at frequency f (for level normalisation) */+ mag(sr: number, f: number) { const w = (TAU * f) / sr, cr = 1 - this.B * Math.cos(w) - this.C * Math.cos(2 * w), ci = this.B * Math.sin(w) + this.C * Math.sin(2 * w); return Math.abs(this.A) / Math.hypot(cr, ci); }+}++/** Mono below `f` Hz (M/S: the side channel high-passed), the mix rule for low end; the highs keep their width. */+export const monoLows = (o: Out, sr: number, f = 160) => { const h1 = Biquad.make(sr, "hp", f, 0.54), h2 = Biquad.make(sr, "hp", f, 1.31); // 4th-order Butterworth on the side+ for (let i = 0; i < o.L.length; i++) { const m = 0.5 * (o.L[i] + o.R[i]), sd = h2.tick(h1.tick(0.5 * (o.L[i] - o.R[i]))); o.L[i] = m + sd; o.R[i] = m - sd; } return o; };+/** A body as a chain of peaking filters (strings): run on a whole bus, one chain per channel. */+export type PeakBody = { peaks: [number, number, number][]; shelf: [number, number]; hp: number };+export const runBody = (buf: Float32Array, sr: number, b: PeakBody, scale = 1) => {+ const ny = sr * 0.45;+ Biquad.make(sr, "hp", Math.min(b.hp * scale, ny), 0.7).run(buf);+ for (const [f, q, g] of b.peaks) if (f * scale < ny) Biquad.make(sr, "peak", f * scale, q, g).run(buf);+ if (b.shelf[0] * scale < ny) Biquad.make(sr, "highshelf", b.shelf[0] * scale, 0.7, b.shelf[1]).run(buf);+};++// ------------------------------------------------------------------ measured-physics tables+// Provenance: mode FREQUENCIES are published measurements; gains, Q and amplitudes are voiced by+// hand to those measurements (no recording was fitted, none ships). Sources:+// violin: Bissinger, "Structural acoustics of good and bad violins", JASA 124(3) 2008 (A0 ~275,+// CBR ~405, B1- ~470, B1+ ~540 Hz; the 2-3 kHz bridge hill); Jansson, "Acoustics for Violin and+// Guitar Makers" (KTH, 2002) ch. 7-8; Duennwald 1991 bands (a dip in the 1.3-1.6 kHz "nasal" band).+// viola = violin modes x0.82 (Rossing, "The Science of String Instruments", 2010, ch. 13).+// cello: Bynum & Rossing in Rossing 2010 ch. 13 (A0 ~100, B1- ~175, B1+ ~210 Hz, bridge hill ~1.6 kHz).+export const VIOLIN_BODY: PeakBody = { hp: 180, shelf: [5200, -9], peaks: [[275, 6, 5], [405, 8, 2], [470, 7, 6], [540, 7, 5.5], [700, 4, 1.5], [1000, 3, 3], [1450, 2, -4], [2500, 1.2, 6], [3500, 2.5, 2]] };+export const CELLO_BODY: PeakBody = { hp: 55, shelf: [4200, -9], peaks: [[100, 5, 5], [150, 6, 2], [175, 6, 6], [210, 6, 5], [340, 4, 2], [600, 3, 2], [900, 2, -3], [1650, 1.2, 5], [2800, 2.5, 1.5]] };++/** A commuted body impulse response: low modes from measurements, a seeded dense modal field above. */+export type IRSpec = { key: string; modes: [number, number, number][]; dense: [number, number, number, number]; len: number; direct: number };+// nylon: Christensen & Vistisen, JASA 68 (1980); Rossing & Fletcher, "The Physics of Musical+// Instruments" (1998) ch. 9: A0 ~98, T(1,1)1 ~204, T(1,1)2 ~240, (1,2) ~385 Hz, then plate modes.+// steel: Rossing 2010 ch. 12 (dreadnought: A0 ~102, T1 ~195, ~225, ~370, ~440 Hz).+// harp: soundboard modes after Le Carrou et al., JASA 2005 (concert harp, 60-700 Hz region).+// upright bass: Askenfelt, "Eigenmodes and tone quality of the double bass" (KTH STL-QPSR 1982): A0 ~57 Hz, ~105, ~130 Hz.+export const IR_NYLON: IRSpec = { key: "nylon", len: 0.09, direct: 1, dense: [800, 5000, 60, 0.35], modes: [[98, 16, 1], [204, 24, 0.9], [240, 28, 0.55], [290, 30, 0.3], [385, 34, 0.45], [455, 38, 0.3], [550, 40, 0.35], [650, 44, 0.25]] };+export const IR_STEEL: IRSpec = { key: "steel", len: 0.08, direct: 1, dense: [700, 7000, 80, 0.45], modes: [[102, 15, 1], [195, 22, 0.8], [225, 26, 0.6], [370, 34, 0.5], [440, 38, 0.4], [540, 40, 0.4], [690, 44, 0.3], [870, 48, 0.25]] };+export const IR_HARP: IRSpec = { key: "harp", len: 0.07, direct: 1.2, dense: [700, 4000, 40, 0.25], modes: [[75, 10, 0.5], [150, 14, 0.6], [250, 20, 0.5], [400, 24, 0.4], [600, 28, 0.3]] };+export const IR_UPRIGHT: IRSpec = { key: "upright", len: 0.11, direct: 0.9, dense: [400, 2500, 40, 0.3], modes: [[57, 9, 0.8], [105, 13, 1], [130, 15, 0.8], [180, 18, 0.5], [250, 22, 0.4], [350, 28, 0.3]] };+const IR_CACHE = new Map<string, Float64Array>();+export const bodyIR = (sr: number, s: IRSpec): Float64Array => {+ const ck = `${s.key}@${sr}`, hit = IR_CACHE.get(ck); if (hit) return hit;+ const n = Math.round(s.len * sr), ir = new Float64Array(n), r = mkRng(0x5eed + s.key.length * 977 + s.key.charCodeAt(0));+ const modes = s.modes.slice(), [lo, hi, cnt, amp] = s.dense;+ for (let i = 0; i < cnt; i++) { const f = lo * Math.pow(hi / lo, r()); modes.push([f, 25 + 35 * r(), amp * Math.sqrt(lo / f) * (0.4 + 0.6 * r())]); }+ for (const [f, q, a] of modes) { if (f > sr * 0.45) continue; const dec = Math.exp(-Math.PI * f / q / sr), w = (TAU * f) / sr, cw = Math.cos(w), sw = Math.sin(w); let c = a, sn = 0;+ for (let i = 0; i < n; i++) { ir[i] += sn; const c2 = (c * cw - sn * sw) * dec; sn = (c * sw + sn * cw) * dec; c = c2; } }+ let e = 0; for (let i = 0; i < n; i++) e += ir[i] * ir[i]; const k = 1 / Math.sqrt(e / 40 + 1e-12); // modal energy relative to the direct pulse+ for (let i = 0; i < n; i++) ir[i] *= k * 0.55; ir[0] += s.direct;+ const fade = Math.round(0.01 * sr); for (let i = 0; i < fade; i++) ir[n - 1 - i] *= i / fade;+ IR_CACHE.set(ck, ir); return ir;+};+export const convolve = (x: Float64Array, h: Float64Array) => { const y = new Float64Array(x.length + h.length - 1);+ for (let i = 0; i < x.length; i++) { const xi = x[i]; if (xi === 0) continue; for (let j = 0; j < h.length; j++) y[i + j] += xi * h[j]; } return y; };++/**+ * A pluck's excitation (bridge force): a raised-cosine contact `width` s long plus a little+ * seeded contact noise, low-passed above `lp` Hz (the 1/n fall of a plucked string's spectrum),+ * then the pick-position comb (harmonics at multiples of 1/beta vanish). No DC by construction.+ */+export const pluckExc = (sr: number, f: number, o: { width: number; beta: number; noise: number; lp: number }, r: Rng) => {+ const W = Math.max(2, Math.round(o.width * sr)), D = o.beta * (sr / f), Di = Math.floor(D), Df = D - Di, n = W + Di + 3, e = new Float64Array(n), c = new Float64Array(n);+ const a = Math.exp((-TAU * Math.min(o.lp, sr * 0.45)) / sr); let y = 0;+ for (let i = 0; i < n; i++) { const x = i < W ? 0.5 * (1 - Math.cos((TAU * (i + 0.5)) / W)) * (1 + o.noise * wn(r)) : 0; y = (1 - a) * x + a * y; e[i] = y; }+ for (let i = 0; i < n; i++) { const j = i - D, j0 = Math.floor(j), fr = j - j0, v = j0 >= 0 ? e[j0] * (1 - fr) + (j0 + 1 < n ? e[j0 + 1] : 0) * fr : j0 === -1 ? e[0] * fr : 0; c[i] = e[i] - v; }+ void Df; return c;+};++export type KSOpts = { f: number; T60: number; loss: number; exc: Float64Array; len: number; relI: number; relT60: number;+ /** second polarisation: detune (cents), level, decay factor vs the first, share of the pluck (0..1) */ pol2?: { cents: number; mix: number; t60: number; share: number };+ /** tension-modulation pitch glide at the attack: +cents decaying with tau s (Lagrange-read line) */ glide?: { cents: number; tau: number } };+/**+ * Extended Karplus-Strong (Jaffe & Smith 1983; Karjalainen, Valimaki & Tolonen 1998): a loop of+ * delay + one-pole loss + Thiran allpass tuning, with the excitation INJECTED over time (so a+ * commuted body can ride on it), two polarisations (a detuned, faster-decaying vertical one gives+ * the two-stage decay and slow beating) and finger damping after release. The loss filter's phase+ * delay is subtracted from the loop so every note is in tune (checked in tools/ensemble-check.mjs).+ */+export const ksRender = (sr: number, o: KSOpts): Float64Array => {+ const out = new Float64Array(o.len);+ const run = (fHz: number, T60: number, share: number, mix: number) => {+ const a = o.loss, w = (TAU * fHz) / sr, pd = Math.atan2(a * Math.sin(w), 1 - a * Math.cos(w)) / w, mag = (1 - a) / Math.sqrt(1 - 2 * a * Math.cos(w) + a * a);+ // the loop's DC gain is g (the one-pole passes DC at unity): keep it below 1 or low frequencies grow without bound+ const g = Math.min(0.99995, Math.pow(10, -3 / (T60 * fHz)) / mag), gRel = Math.min(g, Math.pow(10, -3 / (Math.max(0.02, o.relT60) * fHz)) / mag), L = sr / fHz - pd;+ const kRel = Math.exp(-1 / (0.012 * sr)); let gc = g, lp = 0;+ if (!o.glide) {+ const N = Math.max(2, Math.floor(L - 0.5)), d = L - N, c = (1 - d) / (1 + d), buf = new Float64Array(N); let idx = 0, apx = 0, apy = 0;+ for (let i = 0; i < o.len; i++) {+ if (i >= o.relI) gc = gRel + (gc - gRel) * kRel;+ lp = (1 - a) * buf[idx] + a * lp; const v = gc * lp, ap = c * v + apx - c * apy; apx = v; apy = ap;+ const y = ap + (i < o.exc.length ? o.exc[i] * share : 0); buf[idx] = y; idx = idx + 1 === N ? 0 : idx + 1; out[i] += y * mix;+ }+ } else {+ const M = Math.ceil(L * (1 + o.glide.cents / 1000)) + 8, buf = new Float64Array(M); let wi = 0; const kg = Math.exp(-1 / (o.glide.tau * sr));+ let cents = o.glide.cents;+ for (let i = 0; i < o.len; i++) {+ if (i >= o.relI) gc = gRel + (gc - gRel) * kRel;+ const D = clamp(L / Math.pow(2, cents / 1200), 3, M - 4); cents *= kg;+ let pos = wi - D; if (pos < 0) pos += M; const i1 = Math.floor(pos), x = pos - i1;+ const ym1 = buf[(i1 - 1 + M) % M], y0 = buf[i1 % M], y1 = buf[(i1 + 1) % M], y2 = buf[(i1 + 2) % M];+ const rd = (-x * (x - 1) * (x - 2) / 6) * ym1 + ((x + 1) * (x - 1) * (x - 2) / 2) * y0 - ((x + 1) * x * (x - 2) / 2) * y1 + ((x + 1) * x * (x - 1) / 6) * y2; // 3rd-order Lagrange+ lp = (1 - a) * rd + a * lp; const y = gc * lp + (i < o.exc.length ? o.exc[i] * share : 0); buf[wi] = y; wi = wi + 1 === M ? 0 : wi + 1; out[i] += y * mix;+ }+ }+ };+ const p2 = o.pol2;+ run(o.f, o.T60, p2 ? Math.sqrt(1 - p2.share) : 1, 1);+ if (p2 && p2.mix > 0) run(o.f * Math.pow(2, p2.cents / 1200), o.T60 * p2.t60, Math.sqrt(p2.share), p2.mix);+ return out;+};+/** RMS of the first `sec` seconds (note-level normalisation, so level follows velocity, not period or pluck width). */+export const headRms = (x: Float64Array, sr: number, sec = 0.08) => { const m = Math.min(x.length, Math.round(sec * sr)); let e = 0; for (let i = 0; i < m; i++) e += x[i] * x[i]; return Math.sqrt(e / Math.max(1, m)) + 1e-12; };++// Formants: Peterson & Barney, JASA 24 (1952), average adult men / women F1-F3 (a = /A/ "hod",+// e = /E/ "head", i = /i/ "heed", o = /O/ "hawed" with F1 x0.88 for the rounded sung "oh",+// u = /u/ "who'd"); F4, F5 and the bandwidths are typical values (Sundberg, "The Science of the+// Singing Voice", 1987; F1 bandwidth 60-90 Hz rising to ~200 Hz at F5).+export const FORMANTS: Record<string, { m: number[]; w: number[] }> = {+ a: { m: [730, 1090, 2440, 3350, 3850], w: [850, 1220, 2810, 3950, 4650] },+ e: { m: [530, 1840, 2480, 3350, 3850], w: [610, 2330, 2990, 4000, 4700] },+ i: { m: [270, 2290, 3010, 3400, 3900], w: [310, 2790, 3310, 4100, 4800] },+ o: { m: [500, 840, 2410, 3300, 3800], w: [520, 920, 2710, 3900, 4600] },+ u: { m: [300, 870, 2240, 3300, 3800], w: [370, 950, 2670, 3900, 4600] },+};+export const FORMANT_BW = { m: [70, 80, 110, 140, 180], w: [85, 95, 125, 160, 200] };++// Brass radiation formants in dB bumps on a log-frequency axis [fc, width octaves, dB] plus a+// roll-off corner and slope. Trumpet regions ~1.2 and ~2.5 kHz, trombone ~520 Hz / 1.2 kHz, horn+// ~340-450 Hz with a dark top (Meyer, "Acoustics and the Performance of Music", 2009, ch. 2;+// Luce & Clark, JASA 1967). Shapes voiced by hand.+export const BRASS_FORMANT: Record<string, { bumps: [number, number, number][]; roll: number; slope: number }> = {+ trumpet: { bumps: [[1250, 0.45, 6], [2500, 0.5, 4]], roll: 4200, slope: 12 },+ trombone: { bumps: [[540, 0.5, 5], [1200, 0.6, 3]], roll: 2600, slope: 12 },+ horn: { bumps: [[420, 0.55, 6], [850, 0.6, 1]], roll: 1300, slope: 16 },+};+export const bumpDb = (fr: number, b: { bumps: [number, number, number][]; roll: number; slope: number }) => {+ let g = 0; for (const [fc, wo, db] of b.bumps) { const x = Math.log2(fr / fc) / wo; g += db * Math.exp(-0.5 * x * x); }+ if (fr > b.roll) g -= b.slope * Math.log2(fr / b.roll); return g;+};diff --git a/skills/anidoodle/engine/src/canvas-core/music/instruments.ts b/skills/anidoodle/engine/src/canvas-core/music/instruments.tsindex e2d2e43..03db745 100644--- a/skills/anidoodle/engine/src/canvas-core/music/instruments.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/instruments.ts@@ -2,6 +2,7 @@// presses in seconds. Presets are numbers in one place (the constants at the top of each voice).import { type Rng, TAU, clamp, pan, SVF, Biquad, blep, onePoleCoef, gauss } from "./dsp";import type { Played } from "./perform";+import * as E from "./ensemble";type Out = { L: Float32Array; R: Float32Array };type Opts = Record<string, number | boolean | string>;@@ -66,7 +67,7 @@ export const mallets = (keys: Played[], sr: number, n: number, o: Opts, vibes =* filter for brightness, an allpass for fine tuning, a pick-position comb on the excitation and a* velocity low-pass (a softer pluck is darker). Harp rings long and bright; guitar gets a body.*/-export const pluck = (keys: Played[], sr: number, n: number, o: Opts, r: Rng, kind: "harp" | "guitar"): Out => {+const pluckLegacy = (keys: Played[], sr: number, n: number, o: Opts, r: Rng, kind: "harp" | "guitar"): Out => {const out = { L: new Float32Array(n), R: new Float32Array(n) }, w = num(o, "width", 0.6);for (const k of keys) {const f = f0(k.p), i0 = Math.round(k.t * sr), P = sr / f, N = Math.floor(P - 0.5), frac = P - N - 0.5;@@ -98,7 +99,7 @@ export const pluck = (keys: Played[], sr: number, n: number, o: Opts, r: Rng, ki};/** Soft string ensemble: 5 detuned PolyBLEP saws per note, slow bow attack, delayed vibrato, low-pass, high-pass 150 Hz. */-export const strings = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {+const stringsLegacy = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {const out = { L: new Float32Array(n), R: new Float32Array(n) }, atk = num(o, "attack", 0.45), rel = num(o, "release", 0.9), w = num(o, "width", 0.8), bright = num(o, "bright", 1);const hpL = new SVF(sr, 150, 0.7), hpR = new SVF(sr, 150, 0.7);for (const k of keys) {@@ -218,7 +219,7 @@ export const hat = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Outreturn out;};/** Warm bass: sine + harmonics, a pluck envelope, and a little saturation so phones hear the line (phone rule). */-export const bass = (keys: Played[], sr: number, n: number, o: Opts): Out => {+const bassLegacy = (keys: Played[], sr: number, n: number, o: Opts): Out => {const out = { L: new Float32Array(n), R: new Float32Array(n) }, drive = num(o, "drive", 1.6);for (const k of keys) { const f = f0(k.p), i0 = Math.round(k.t * sr), dur = k.off - k.t, len = Math.min(n - i0, Math.ceil((dur + 0.1) * sr));for (let i = 0; i < len; i++) { const t = i / sr, e = Math.min(1, t * 300) * (0.55 + 0.45 * Math.exp(-t / 0.25)) * (t < dur ? 1 : Math.exp(-(t - dur) / 0.03));@@ -232,3 +233,10 @@ export const vinyl = (keys: Played[], sr: number, n: number, _o: Opts, r: Rng):for (let i = 0; i < len; i++) { const pop = r() < 7 / sr ? (r() - 0.5) * 0.5 * k.v : 0; const hiss = gauss(r) * 0.004 * k.v; zl += lp * (hiss + pop - zl); zr += lp * (hiss * 0.8 + pop - zr); out.L[i0 + i] += zl; out.R[i0 + i] += zr; } }return out;};++// ---------------------------------------------------------------- v2 ensemble voices (ensemble.ts)+// `opts.legacy: true` keeps the v1 voice bit-for-bit (shipped films); everything else gets v2.+const legacy = (o: Opts) => o.legacy === true;+export const pluck = (keys: Played[], sr: number, n: number, o: Opts, r: Rng, kind: "harp" | "guitar"): Out => (legacy(o) ? pluckLegacy(keys, sr, n, o, r, kind) : E.pluck(keys, sr, n, o, r, kind));+export const strings = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => (legacy(o) ? stringsLegacy(keys, sr, n, o, r) : E.strings(keys, sr, n, o, r));+export const bass = (keys: Played[], sr: number, n: number, o: Opts): Out => (legacy(o) ? bassLegacy(keys, sr, n, o) : E.bass(keys, sr, n, o));diff --git a/skills/anidoodle/engine/src/canvas-core/music/orchestra.ts b/skills/anidoodle/engine/src/canvas-core/music/orchestra.tsindex 1dd887b..0fd4254 100644--- a/skills/anidoodle/engine/src/canvas-core/music/orchestra.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/orchestra.ts@@ -4,6 +4,7 @@// DECISION B). Each voice states the physics it borrows.import { type Rng, TAU, clamp, pan, SVF, blep, gauss } from "./dsp";import type { Played } from "./perform";+import * as E from "./ensemble";type Out = { L: Float32Array; R: Float32Array };type Opts = Record<string, number | boolean | string>;@@ -27,7 +28,7 @@ export const organ = (keys: Played[], sr: number, n: number, o: Opts): Out => {};/** Brass: a PolyBLEP saw whose low-pass opens with loudness (the brass "bite"), the pitch rising ~25 cents into the note, vibrato delayed. `attack`, `bright`. */-export const brass = (keys: Played[], sr: number, n: number, o: Opts): Out => {+const brassLegacy = (keys: Played[], sr: number, n: number, o: Opts): Out => {const out = out0(n), atk = num(o, "attack", 0.06), bright = num(o, "bright", 1), w = num(o, "width", 0.5);for (const k of keys) {const f = f0(k.p), i0 = Math.round(k.t * sr), dur = k.off - k.t, len = Math.min(n - i0, Math.ceil((dur + 0.3) * sr)), lp = new SVF(sr, 800, 0.8), [gl, gr] = byPitch(k.p, w); let ph = 0;@@ -39,7 +40,7 @@ export const brass = (keys: Played[], sr: number, n: number, o: Opts): Out => {};/** Woodwind: flute (sine + weak 2nd/3rd + breath noise at the pitch) or `reed: true` clarinet (odd harmonics, darker). Delayed vibrato. */-export const woodwind = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {+const woodwindLegacy = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {const out = out0(n), reed = o.reed === true, breath = num(o, "breath", reed ? 0.02 : 0.08), w = num(o, "width", 0.3);for (const k of keys) {const f = f0(k.p), i0 = Math.round(k.t * sr), dur = k.off - k.t, len = Math.min(n - i0, Math.ceil((dur + 0.2) * sr)), bp = new SVF(sr, f, 4), [gl, gr] = byPitch(k.p, w); let ph = 0;@@ -53,7 +54,7 @@ export const woodwind = (keys: Played[], sr: number, n: number, o: Opts, r: Rng)const VOWELS: Record<string, [number, number, number]> = { a: [700, 1220, 2600], o: [450, 800, 2830], u: [325, 700, 2530], e: [530, 1840, 2480], i: [300, 2200, 3000] };/** Choir: three detuned saws per note through the formants of a vowel (`vowel`: a o u e i), slow attack. Texture and pads; never a lead. */-export const choir = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {+const choirLegacy = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {const out = out0(n), fm = VOWELS[String(o.vowel ?? "a")] ?? VOWELS.a, atk = num(o, "attack", 0.35), w = num(o, "width", 0.8);for (const k of keys) {const f = f0(k.p), i0 = Math.round(k.t * sr), dur = k.off - k.t, len = Math.min(n - i0, Math.ceil((dur + 0.8) * sr));@@ -97,7 +98,7 @@ export const leadSynth = (keys: Played[], sr: number, n: number, o: Opts): Out =};/** Bowed solo (violin / cello): one saw with delayed vibrato and bow noise through body resonances (~280, 450, 2800 Hz), a bowed attack. */-export const bowedSolo = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {+const bowedSoloLegacy = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => {const out = out0(n), atk = num(o, "attack", 0.09), vibC = num(o, "vibrato", 22), [gl, gr] = pan(num(o, "pan", -0.1));const body = o.cello === true ? [140, 300, 1600] : [280, 450, 2800];for (const k of keys) {@@ -108,3 +109,11 @@ export const bowedSolo = (keys: Played[], sr: number, n: number, o: Opts, r: Rng}return out;};++// ---------------------------------------------------------------- v2 ensemble voices (ensemble.ts)+// `opts.legacy: true` keeps the v1 voice bit-for-bit (shipped films); everything else gets v2.+const legacy = (o: Opts) => o.legacy === true;+export const brass = (keys: Played[], sr: number, n: number, o: Opts): Out => (legacy(o) ? brassLegacy(keys, sr, n, o) : E.brass(keys, sr, n, o));+export const woodwind = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => (legacy(o) ? woodwindLegacy(keys, sr, n, o, r) : E.woodwind(keys, sr, n, o, r));+export const choir = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => (legacy(o) ? choirLegacy(keys, sr, n, o, r) : E.choir(keys, sr, n, o, r));+export const bowedSolo = (keys: Played[], sr: number, n: number, o: Opts, r: Rng): Out => (legacy(o) ? bowedSoloLegacy(keys, sr, n, o, r) : E.bowedSolo(keys, sr, n, o, r));diff --git a/skills/anidoodle/engine/src/canvas-core/music/render.ts b/skills/anidoodle/engine/src/canvas-core/music/render.tsindex f9fa296..2934778 100644--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts@@ -66,7 +66,7 @@ export const renderPiece = (piece: Piece, sr: number, o: RenderOpts = {}): Rendeconst 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); sL = r.L; sR = r.R; }+ else { const r = voice((piece as { legacy?: boolean }).legacy ? { ...pt, opts: { ...(pt.opts ?? {}), legacy: true } } : pt, keys, sr, n, piece.seed * 101 + pi); sL = r.L; sR = r.R; } // Piece.legacy (shipped scores) -> the v1 voicesif (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; } }