// Damping law and the mallet model. // Internal damping: loss factor η(f) = η₀ (f / 1 kHz)^slope; amplitude decay σ = π f η. export function internalDecay(f, eta, slope) { return Math.PI * f * eta * Math.pow(Math.max(f, 1) / 1000, slope); } /** * Yarn-wound mallet: a soft yarn layer over a harder core. * F = K_y δ^P_YARN · (1 + λ δ̇) (yarn, Hunt–Crossley hysteresis) * + K_c (δ - 0.8 t)^1.5 · (1 + λ δ̇) when δ > 0.8 t (core reached through the yarn) * mallet = { yarn: t (mm), Ky: log10 K_y (SI), Kc: log10 K_c (SI), mass (g), v0 (m/s), lam (s/m) } */ export const P_YARN = 2.5; export const MALLETS = { soft: { yarn: 4, Ky: 9.4, Kc: 8.0, mass: 30, lam: 0.25 }, // ~2 ms contact at 1.5 m/s medium: { yarn: 2.5, Ky: 10.6, Kc: 8.6, mass: 30, lam: 0.2 }, // ~1 ms hard: { yarn: 0.6, Ky: 11.6, Kc: 9.5, mass: 28, lam: 0.12 }, // ~0.5 ms bass: { yarn: 6, Ky: 8.6, Kc: 7.6, mass: 45, lam: 0.25 }, // ~4 ms: large, heavy bass mallet }; // Graduated mallets, as players use: below C4 the mallet blends toward the bass mallet, // reaching it at C2. Without this, a 1 ms contact on a low bar excites the 10× partial // far more than the fundamental, and the note sounds two octaves and a third too high. export const GRAD_TOP = 60, GRAD_BOTTOM = 36; export function graduatedMallet(ml, note) { const s = Math.min(1, Math.max(0, (GRAD_TOP - note) / (GRAD_TOP - GRAD_BOTTOM))); if (s === 0) return { ...ml }; const out = { ...ml }; for (const k of ['yarn', 'Ky', 'Kc', 'mass', 'lam']) out[k] = ml[k] + s * (MALLETS.bass[k] - ml[k]); // only ever softer than the chosen mallet out.Ky = Math.min(out.Ky, ml.Ky); out.Kc = Math.min(out.Kc, ml.Kc); out.yarn = Math.max(out.yarn, ml.yarn); return out; } export function malletForce(ml, delta, ddot) { if (delta <= 0) return 0; const t = ml.yarn * 1e-3 * 0.8; let F = 10 ** ml.Ky * Math.pow(delta, P_YARN); if (delta > t) F += 10 ** ml.Kc * Math.pow(delta - t, 1.5); return Math.max(0, F * (1 + ml.lam * ddot)); } // Rough contact duration against a rigid surface (s), to pick the simulation step. export function contactTimeGuess(ml) { const m = ml.mass * 1e-3, v = Math.max(0.05, ml.v0); const K = 10 ** ml.Ky; const d = Math.pow(((P_YARN + 1) * m * v * v) / (2 * K), 1 / (P_YARN + 1)); return Math.max(1e-4, (3 * Math.min(d, ml.yarn * 1e-3 + 1e-3)) / v); }