// String physics: per-string constants from the parameters, the fingering of a note (which string, // how far up), intonation (the stopped length that puts the stiff string's first partial exactly on // pitch), and per-mode damping (Valette's model: air friction + viscoelastic + a constant floor). import type { Params } from "./params.ts"; import { N_STRINGS, STRING_OPEN_MIDI } from "./types.ts"; export const midiToHz = (m: number) => 440 * Math.pow(2, (m - 69) / 12); export interface StringConst { index: number; /** tension N, linear density kg/m, bending stiffness N m^2, axial stiffness N */ T: number; mu: number; EI: number; EA: number; /** outer diameter m */ d: number; } /** Physical constants of string i, with mu chosen so the open string's first partial is in tune. */ export function stringConst(p: Params, i: number): StringConst { const s = p.strings.s[i]; const L = p.strings.scaleLength; const f1 = midiToHz(STRING_OPEN_MIDI[i]); const dc = s.coreMm * 1e-3; const EI = s.stiffFactor * p.strings.coreE * Math.PI * dc ** 4 / 64; const EA = p.strings.coreE * Math.PI * dc * dc / 4; // f1 = (1/2L) sqrt(T/mu) sqrt(1 + B), B = pi^2 EI / (T L^2) const B = (Math.PI ** 2 * EI) / (s.tension * L * L); const f0 = f1 / Math.sqrt(1 + B); const mu = s.tension / (2 * L * f0) ** 2; return { index: i, T: s.tension, mu, EI, EA, d: s.diameterMm * 1e-3 }; } export interface Fingering { string: number; /** semitones above the open string (negative = on the extension, below the open E) */ shift: number; /** vibrating length (stop to bridge), m */ length: number; /** stopped by a finger (true) or open (nut / extension mechanism) */ stopped: boolean; } /** * The string a bassist would use: the highest string whose open pitch is at or below the note * (lowest position), with notes below E1 on a low-C extension (a longer vibrating length, stopped * by the extension's mechanical fingers, treated as open). The stopped length is intonated so the * stiff string's first partial is exactly on pitch. */ export function fingering(p: Params, note: number): Fingering { let i = N_STRINGS - 1; while (i > 0 && STRING_OPEN_MIDI[i] > note) i--; const shift = note - STRING_OPEN_MIDI[i]; const sc = stringConst(p, i); const target = midiToHz(note); // solve f1(L) = target with f1 = (1/2L) sqrt(T/mu) sqrt(1 + pi^2 EI / (T L^2)) const c = Math.sqrt(sc.T / sc.mu); let L = p.strings.scaleLength * Math.pow(2, -shift / 12); for (let k = 0; k < 30; k++) { const B = (Math.PI ** 2 * sc.EI) / (sc.T * L * L); L = (c / (2 * target)) * Math.sqrt(1 + B); } return { string: i, shift, length: L, stopped: shift > 0 }; } export interface StringModes { /** angular frequencies (rad/s) and decay rates (1/s) of the fixed-fixed stiff string */ w: Float64Array; sigma: Float64Array; } /** Valette damping of mode frequency f (Hz): returns 1/Q. */ export function valetteQinv(p: Params, sc: StringConst, f: number): number { const eta = p.strings.airEta, rhoA = p.strings.airRho; const R = 2 * Math.PI * eta + 2 * Math.PI * sc.d * Math.sqrt(Math.PI * eta * rhoA * f); const qAir = R / (2 * Math.PI * sc.mu * f); const qVe = (4 * Math.PI ** 2 * sc.mu * sc.EI * p.strings.deltaVe * f * f) / (sc.T * sc.T); return qAir + qVe + p.strings.qTeInv; } /** Modes of a stiff string of length L up to fmax (Hz), with Valette damping plus extra sigma. */ export function stringModes(p: Params, sc: StringConst, L: number, fmax: number, extraSigma = 0): StringModes { const c = Math.sqrt(sc.T / sc.mu); const B = (Math.PI ** 2 * sc.EI) / (sc.T * L * L); const f0 = c / (2 * L); const w: number[] = [], sig: number[] = []; for (let n = 1; ; n++) { const f = n * f0 * Math.sqrt(1 + B * n * n); if (f > fmax) break; w.push(2 * Math.PI * f); sig.push(Math.PI * f * valetteQinv(p, sc, f) + extraSigma); } return { w: Float64Array.from(w), sigma: Float64Array.from(sig) }; } /** Release force (N) and slip time (s) of the pluck for a MIDI velocity. */ export function pluckForVelocity(p: Params, vel: number): { force: number; slip: number } { const v = Math.max(1, Math.min(127, vel)) / 127; const force = p.pluck.forceMin + (p.pluck.forceMax - p.pluck.forceMin) * Math.pow(v, p.pluck.velCurve); const slip = (p.pluck.slipMs * 1e-3) * Math.pow(10 / force, p.pluck.slipExp); return { force, slip }; }