// Sound of a mallet strike: bar modes coupled to the resonator tube, radiated to the mic. // // Bar: mass-normalized modes φₙ (vertical displacement on the top face), so a downward force // F at P drives q̈ₙ + 2σₙq̇ₙ + ωₙ²qₙ = φₙ(P)·F (+ the gap pressure, below). // Tube: closed at the bottom; seen from its mouth it has input impedance // Z_t = Z₀ coth(Γ L_t), Γ = s/c + β√s (wall losses ∝ √f). // The bar's underside pushes volume flow U = -s Σ aₙ q̇ₙ... into the gap over the mouth // (aₙ = ∫ φₙ over the mouth). That flow either enters the tube (Z_t) or escapes around the // rim (Z_e = s M + radiation resistance), so the gap pressure is p_m = Z_t‖Z_e · U and it // pushes back on the bar with generalized force aₙ p_m. The coupling is rank one, so the // coupled response is solved in closed form at each frequency (Sherman–Morrison). // Mic: each top-face cell is a vertical dipole of moment v·V (bar volume + added mass), and // the flow swallowed by the tube removes a monopole at the mouth. // Time signal: contact force from a Hertz-type mallet simulation against the bar modes, // then multiplication by the transfer function and an inverse FFT, with an exponential // window (evaluate at s = γ + iω) so long ringing does not wrap around. // The FFT window is TAIL seconds longer than the output and the force spectrum is sampled // every 2 Hz: interpolating it makes faint copies of the strike every 1/df seconds, and the // copy at negative time would otherwise wrap to the end of the output, where the // exponential window amplifies it enormously (heard as a rattle at the end of some notes). import { AIR, tubeGeom, tubeLength, micPos, noteFreq } from './bar.js'; import { cellGeom, dipoleDepth } from './fe.js'; import { internalDecay, malletForce, contactTimeGuess } from './physics.js'; import { fft, nextPow2 } from './fft.js'; /** * opts: { note, eta, slope, res: { on, diam, gap, detune, loss } } */ export function prepare(model, opts) { const b = model.design; const { nx, ny } = model; const { dx, dy, dA } = cellGeom(model); const mic = micPos(b).map((v) => v * 1e-3); const zTop = b.h0 * 1e-3; const r0 = Math.hypot(mic[0] - (b.L / 2) * 1e-3, mic[1] - (b.W / 2) * 1e-3, mic[2] - zTop); const tg = tubeGeom(b, opts.res); const at = tg.a * 1e-3; // fraction of each cell over the tube mouth (3 x 3 subsamples) const cover = new Float32Array(nx * ny); for (let j = 0; j < ny; j++) { for (let i = 0; i < nx; i++) { let c = 0; for (let u = 0; u < 3; u++) for (let v = 0; v < 3; v++) { const x = (i + (u + 0.5) / 3) * dx - tg.x * 1e-3, y = (j + (v + 0.5) / 3) * dy - tg.y * 1e-3; if (x * x + y * y < at * at) c++; } cover[j * nx + i] = c / 9; } } // per-cell geometry toward the mic const geo = []; for (let j = 0; j < ny; j++) { for (let i = 0; i < nx; i++) { const ex = mic[0] - (i + 0.5) * dx, ey = mic[1] - (j + 0.5) * dy, ez = mic[2] - zTop; const r = Math.hypot(ex, ey, ez); geo.push({ r, cos: ez / r }); } } const V = dipoleDepth(b) * dA; const c = AIR.c, rho = AIR.rho; const modes = model.modes.map((m) => { const w = 2 * Math.PI * m.f; const sig = m.sigRad + internalDecay(m.f, opts.eta, opts.slope); let a = 0, Br = 0, Bi = 0; for (let q = 0; q < nx * ny; q++) { const ph = m.phi[q]; a += ph * cover[q] * dA; // ρ s cosθ (1/r² + s/(c r)) e^{-s (r - r0)/c} / 4π at s = iω, per unit q̇ const { r, cos } = geo[q]; const k = (ph * V * rho * cos) / (4 * Math.PI); const tr = -(w * w) / (c * r), ti = w / (r * r); const ang = (-w * (r - r0)) / c, cr = Math.cos(ang), ci = Math.sin(ang); Br += k * (tr * cr - ti * ci); Bi += k * (tr * ci + ti * cr); } // Causal form for synthesis: |B| e^{-sτ}, with 0 ≤ τ < one period chosen so it matches B at // s = iω. (A constant complex B at all frequencies is non-causal and leaks before t = 0.) const Bmag = Math.hypot(Br, Bi); let ph = -Math.atan2(Bi, Br); if (ph < 0) ph += 2 * Math.PI; return { f: m.f, w, sig, a, Br, Bi, Bmag, Btau: ph / w, phi: m.phi, label: m.label }; }); let tube = null; if (opts.res.on) { const fT = noteFreq(opts.note) * 2 ** ((opts.res.detune || 0) / 1200); const Lt = tubeLength(fT, tg.delta) * 1e-3; const S = Math.PI * at * at; const mouth = [tg.x * 1e-3, tg.y * 1e-3, tg.zTop * 1e-3]; tube = { L: Lt, S, a: at, fT, delta: tg.delta * 1e-3, Z0: (rho * c) / S, // escape path: inertance of the end correction, minus the part the radiation // impedance below already carries (ρ/4πa) M: (rho * (tg.delta * 1e-3 - at / 4)) / S, // passive radiation impedance of a small monopole of radius a: // (ρc/4πa²)·(sa/c)/(1 + sa/c) ≈ sρ/4πa + ρω²/4πc at low ka Rr: (rho * c) / (4 * Math.PI * at * at), ta: at / c, beta: ((opts.res.loss ?? 1) * 3e-5) / (Math.sqrt(Math.PI) * at), rt: Math.hypot(mic[0] - mouth[0], mic[1] - mouth[1], mic[2] - mouth[2]), }; } return { model, modes, tube, r0, nx, ny, dx, dy }; } // Bilinear interpolation of every mode's φ at a top-face point (mm). export function phiAt(prep, sx, sy) { const { nx, ny, dx, dy } = prep; const u = Math.min(nx - 1.0001, Math.max(0, (sx * 1e-3) / dx - 0.5)); const v = Math.min(ny - 1.0001, Math.max(0, (sy * 1e-3) / dy - 0.5)); const i0 = Math.floor(u), j0 = Math.floor(v), fu = u - i0, fv = v - j0; const i1 = Math.min(nx - 1, i0 + 1), j1 = Math.min(ny - 1, j0 + 1); return prep.modes.map((m) => m.phi[j0 * nx + i0] * (1 - fu) * (1 - fv) + m.phi[j0 * nx + i1] * fu * (1 - fv) + m.phi[j1 * nx + i0] * (1 - fu) * fv + m.phi[j1 * nx + i1] * fu * fv); } /** * Mallet against the bar modes (tube ignored over the few ms of contact). * Returns { F (N, downward on the bar), h (s) }. */ export function simulateContact(modes, phiP, ml, hStep = 0) { const T0 = contactTimeGuess(ml); const h = hStep || T0 / 300; const nMax = Math.ceil(Math.max(0.02, 10 * T0) / h); const M = modes.length; const Er = new Float64Array(M), Ei = new Float64Array(M), Gr = new Float64Array(M), Gi = new Float64Array(M); const Pr = new Float64Array(M), Pi = new Float64Array(M), ph = new Float64Array(M); const zr = new Float64Array(M), zi = new Float64Array(M); for (let n = 0; n < M; n++) { const { w, sig } = modes[n]; const wd = Math.sqrt(Math.max(1e-6, w * w - sig * sig)); const pr = -sig, pi = wd; // pole const e = Math.exp(pr * h); Er[n] = e * Math.cos(pi * h); Ei[n] = e * Math.sin(pi * h); // G = r (E - 1)/p, r = 1 + iσ/ω_d const nr = Er[n] - 1, ni = Ei[n], d = pr * pr + pi * pi; const qr = (nr * pr + ni * pi) / d, qi = (ni * pr - nr * pi) / d; const rr = 1, ri = sig / wd; Gr[n] = qr * rr - qi * ri; Gi[n] = qr * ri + qi * rr; Pr[n] = pr / d; Pi[n] = -pi / d; // 1/p ph[n] = phiP[n]; } const m = ml.mass * 1e-3; let xm = 0, vm = ml.v0, wb = 0, vb = 0, apart = 0; const F = []; for (let k = 0; k < nMax; k++) { const f = malletForce(ml, xm - wb, vm - vb); F.push(f); wb = 0; vb = 0; for (let n = 0; n < M; n++) { const g = ph[n] * f; const a = Er[n] * zr[n] - Ei[n] * zi[n] + Gr[n] * g; zi[n] = Er[n] * zi[n] + Ei[n] * zr[n] + Gi[n] * g; zr[n] = a; vb += ph[n] * zr[n]; wb += ph[n] * (zr[n] * Pr[n] - zi[n] * Pi[n]); } vm -= (f * h) / m; xm += vm * h; apart = f > 0 ? 0 : apart + 1; if (k > 20 && apart * h > 3e-4 && vm < vb) break; } return { F: Float64Array.from(F), h }; } /** * X_k = Σ_n F_n h e^{-s_k t_n}, s_k = γ + i k Δω, t_n = (n + ½) h, for k = 0..K-1 (Δω = 2π/(Mh·h)), by the * chirp-z transform (Bluestein): nk = (n² + k² - (k-n)²)/2 turns the sum into a convolution. * Exact, unlike interpolating a coarse spectrum, whose errors become ghost strikes that the * exponential window amplifies. */ function forceSpectrum(F, h, gam, Mh, K) { // Δω = 2π/T and h = T/Mh, so α = Δω·h = 2π/Mh and α m²/2 = π (m² mod 2Mh)/Mh exactly const nF = F.length, dw = (2 * Math.PI) / (Mh * h); const L = nextPow2(nF + K); const ar = new Float64Array(L), ai = new Float64Array(L), br = new Float64Array(L), bi = new Float64Array(L); const chirp = (m) => (Math.PI * ((m * m) % (2 * Mh))) / Mh; for (let n = 0; n < nF; n++) { const x = F[n] * h * Math.exp(-gam * (n + 0.5) * h), c = chirp(n); ar[n] = x * Math.cos(c); ai[n] = -x * Math.sin(c); } for (let m = 0; m < K; m++) { const c = chirp(m); br[m] = Math.cos(c); bi[m] = Math.sin(c); } for (let m = 1; m < nF; m++) { const c = chirp(m); br[L - m] = Math.cos(c); bi[L - m] = Math.sin(c); } fft(ar, ai); fft(br, bi); // product, then inverse FFT via conjugation for (let j = 0; j < L; j++) { const r = ar[j] * br[j] - ai[j] * bi[j], i = ar[j] * bi[j] + ai[j] * br[j]; ar[j] = r; ai[j] = -i; } fft(ar, ai); const Xr = new Float64Array(K), Xi = new Float64Array(K); for (let k = 0; k < K; k++) { const cr = ar[k] / L, ci = -ai[k] / L; const ph = -chirp(k) - (k * dw * h) / 2; // e^{-iα k²/2} and the half-step offset const er = Math.cos(ph), ei = Math.sin(ph); Xr[k] = cr * er - ci * ei; Xi[k] = cr * ei + ci * er; } return [Xr, Xi]; } // complex helpers on [re, im] pairs (kept inline in the hot loop below) function cdiv(ar, ai, br, bi) { const d = br * br + bi * bi; return [(ar * br + ai * bi) / d, (ai * br - ar * bi) / d]; } function csqrt(r, i) { const m = Math.hypot(r, i); const sr = Math.sqrt((m + r) / 2), si = Math.sign(i || 1) * Math.sqrt(Math.max(0, (m - r) / 2)); return [sr, si]; } function ccoth(r, i) { // coth(z) = (sinh 2x - i sin 2y) / (cosh 2x - cos 2y) const den = Math.cosh(2 * r) - Math.cos(2 * i); return [Math.sinh(2 * r) / den, -Math.sin(2 * i) / den]; } /** * Mic pressure (Pa) for a strike at (sx, sy) mm. * ml: mallet (see physics.js) with v0; returns { y: Float32Array, sr, F, h, T, Fm, dur }. */ export function strike(prep, sx, sy, ml, sr, maxDur = 4) { const phiAll = phiAt(prep, sx, sy); // modes below Nyquist take part in the sound const idx = []; prep.modes.forEach((m, n) => { if (m.f < 0.48 * sr) idx.push(n); }); const M = idx.length; const w2 = new Float64Array(M), s2 = new Float64Array(M), aC = new Float64Array(M), phi = new Float64Array(M); const Bm = new Float64Array(M), Bt = new Float64Array(M); let sigMin = Infinity; idx.forEach((n, q) => { const m = prep.modes[n]; w2[q] = m.w * m.w; s2[q] = 2 * m.sig; aC[q] = m.a; phi[q] = phiAll[n]; Bm[q] = m.Bmag; Bt[q] = m.Btau; if (Math.abs(phi[q]) > 1e-9 || (prep.tube && Math.abs(m.a) > 1e-12)) sigMin = Math.min(sigMin, m.sig); }); if (prep.tube) { // the tube's own (wall-loss) decay can be slower than the bar's const t = prep.tube, w = 2 * Math.PI * t.fT; sigMin = Math.min(sigMin, t.beta * Math.sqrt(w / 2) * AIR.c); } const dur = Math.min(maxDur, Math.max(0.4, 7 / (Number.isFinite(sigMin) ? sigMin : 10))); // Exponential window: the part of the ring beyond T wraps to the start, scaled by // e^{-σT}·e^{-γ(T - t)}. Keep that 120 dB down at the end of the output even when the // output is cut while the note still rings (the maxDur cap, light damping). γ is capped // so e^{γ·dur} cannot blow up rounding errors; if the cap binds, the window grows instead. // The window is also at least TAIL longer than the output, so nothing from just before // t = 0 can wrap into it. const TAIL = 0.6, GMAX = 16 / dur, NEED = Math.log(1e6); const sMin = Number.isFinite(sigMin) ? sigMin : 10; let N = nextPow2(Math.ceil((dur + TAIL) * sr)); while (N < 1 << 22 && sMin * (N / sr) + GMAX * (N / sr - dur) < NEED) N *= 2; const T = N / sr; const gam = Math.min(GMAX, Math.max(Math.log(1e4) / T, (NEED - sMin * T) / (T - dur))); // per-mode delay phasors e^{-sτ}: e^{-γτ} fixed, e^{-iωτ} rotated bin by bin const bpR = new Float64Array(M), bpI = new Float64Array(M), brR = new Float64Array(M), brI = new Float64Array(M); for (let q = 0; q < M; q++) { bpR[q] = Bm[q] * Math.exp(-gam * Bt[q]); bpI[q] = 0; const a = (-2 * Math.PI * Bt[q]) / T; brR[q] = Math.cos(a); brI[q] = Math.sin(a); } // force spectrum at s = γ + iω_k on every bin, exactly (chirp-z / Bluestein) // contact step h = T / Mh with Mh an integer, so the chirp phases reduce exactly const Mh = Math.max(1, Math.round(T / (contactTimeGuess(ml) / 300))); const { F, h } = simulateContact(prep.modes, phiAll, ml, T / Mh); let Fm = 0, nC = 0; for (const f of F) { Fm = Math.max(Fm, f); if (f > 0) nC++; } const [Fcr, Fci] = forceSpectrum(F, h, gam, Mh, N / 2 + 1); const tube = prep.tube; const c = AIR.c, rho = AIR.rho; const re = new Float64Array(N), im = new Float64Array(N); const Dr = new Float64Array(M), Di = new Float64Array(M); for (let k = 0; k <= N / 2; k++) { const w = (2 * Math.PI * k) / T; const sR = gam, sI = w; const ssR = sR * sR - sI * sI, ssI = 2 * sR * sI; // bar modes let gR = 0, gI = 0, hR = 0, hI = 0; for (let q = 0; q < M; q++) { const dR = ssR + s2[q] * sR + w2[q], dI = ssI + s2[q] * sI; const dd = dR * dR + dI * dI; const r = dR / dd, i = -dI / dd; Dr[q] = r; Di[q] = i; const ta = aC[q]; gR += ta * phi[q] * r; gI += ta * phi[q] * i; hR += ta * ta * r; hI += ta * ta * i; } // gap pressure per unit force, and the tube's swallowed flow let PmR = 0, PmI = 0, UtR = 0, UtI = 0; if (tube) { const [qR, qI] = csqrt(sR, sI); const GR = sR / c + tube.beta * qR, GI = sI / c + tube.beta * qI; const [cR, cI] = ccoth(GR * tube.L, GI * tube.L); const ZtR = tube.Z0 * cR, ZtI = tube.Z0 * cI; const [radR, radI] = cdiv(tube.Rr * tube.ta * sR, tube.Rr * tube.ta * sI, 1 + tube.ta * sR, tube.ta * sI); const ZeR = tube.M * sR + radR, ZeI = tube.M * sI + radI; const nR = ZtR * ZeR - ZtI * ZeI, nI = ZtR * ZeI + ZtI * ZeR; const [ZpR, ZpI] = cdiv(nR, nI, ZtR + ZeR, ZtI + ZeI); const sZR = sR * ZpR - sI * ZpI, sZI = sR * ZpI + sI * ZpR; // s Z_par const numR = -(sZR * gR - sZI * gI), numI = -(sZR * gI + sZI * gR); const denR = 1 + sZR * hR - sZI * hI, denI = sZR * hI + sZI * hR; [PmR, PmI] = cdiv(numR, numI, denR, denI); [UtR, UtI] = cdiv(PmR, PmI, ZtR, ZtI); // into the tube } // bar dipole radiation: Σ s qₙ Bₙ let aR = 0, aI = 0; for (let q = 0; q < M; q++) { const fR = phi[q] + aC[q] * PmR, fI = aC[q] * PmI; const qR = Dr[q] * fR - Di[q] * fI, qI = Dr[q] * fI + Di[q] * fR; aR += qR * bpR[q] - qI * bpI[q]; aI += qR * bpI[q] + qI * bpR[q]; const t = bpR[q] * brR[q] - bpI[q] * brI[q]; bpI[q] = bpR[q] * brI[q] + bpI[q] * brR[q]; bpR[q] = t; } // times s, times e^{-s r0/c} let pR = sR * aR - sI * aI, pI = sR * aI + sI * aR; { const e = Math.exp((-sR * prep.r0) / c), ang = (-sI * prep.r0) / c; const cR = e * Math.cos(ang), cI = e * Math.sin(ang); [pR, pI] = [pR * cR - pI * cI, pR * cI + pI * cR]; } if (tube) { // monopole -U_t at the mouth: ρ s (-U_t) e^{-s r/c} / (4π r) const e = Math.exp((-sR * tube.rt) / c) / (4 * Math.PI * tube.rt), ang = (-sI * tube.rt) / c; const cR = e * Math.cos(ang), cI = e * Math.sin(ang); const uR = -rho * (sR * UtR - sI * UtI), uI = -rho * (sR * UtI + sI * UtR); pR += uR * cR - uI * cI; pI += uR * cI + uI * cR; } // force on the bar is downward, φ is up const FR = -Fcr[k], FI = -Fci[k]; // smooth roll-off to zero at Nyquist: a hard cut there smears in time and the // exponential window would amplify the smear near the end of the output const fr = k / N, tap = fr < 0.44 ? 1 : Math.cos(((fr - 0.44) / 0.06) * (Math.PI / 2)) ** 2; const YR = tap * (FR * pR - FI * pI), YI = tap * (FR * pI + FI * pR); // inverse transform via a forward FFT of the conjugate re[k] = YR; im[k] = -YI; if (k > 0 && k < N / 2) { re[N - k] = YR; im[N - k] = YI; } if (k === N / 2) im[k] = 0; } fft(re, im); const nOut = Math.min(N, Math.ceil(dur * sr)); const y = new Float32Array(nOut); for (let n = 0; n < nOut; n++) y[n] = (re[n] / T) * Math.exp((gam * n) / sr); // fade out: short if the note has died away, longer if maxDur cut it while still ringing const cut = 7 / (Number.isFinite(sigMin) ? sigMin : 10) > dur + 1e-9; const fade = Math.min(nOut, Math.round((cut ? 0.5 : 0.02) * sr)); for (let n = 0; n < fade; n++) y[nOut - 1 - n] *= 0.5 - 0.5 * Math.cos((Math.PI * n) / fade); return { y, sr, F, h, T: nC * h, Fm, dur }; }