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