087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 1// String physics: per-string constants from the parameters, the fingering of a note (which string,
2// how far up), intonation (the stopped length that puts the stiff string's first partial exactly on
3// pitch), and per-mode damping (Valette's model: air friction + viscoelastic + a constant floor).
5import type { Params } from "./params.ts";
6import { N_STRINGS, STRING_OPEN_MIDI } from "./types.ts";
8export const midiToHz = (m: number) => 440 * Math.pow(2, (m - 69) / 12);
10export interface StringConst {
11 index: number;
12 /** tension N, linear density kg/m, bending stiffness N m^2, axial stiffness N */
13 T: number;
14 mu: number;
15 EI: number;
16 EA: number;
17 /** outer diameter m */
18 d: number;
19}
21/** Physical constants of string i, with mu chosen so the open string's first partial is in tune. */
22export function stringConst(p: Params, i: number): StringConst {
23 const s = p.strings.s[i];
24 const L = p.strings.scaleLength;
25 const f1 = midiToHz(STRING_OPEN_MIDI[i]);
26 const dc = s.coreMm * 1e-3;
27 const EI = s.stiffFactor * p.strings.coreE * Math.PI * dc ** 4 / 64;
28 const EA = p.strings.coreE * Math.PI * dc * dc / 4;
29 // f1 = (1/2L) sqrt(T/mu) sqrt(1 + B), B = pi^2 EI / (T L^2)
30 const B = (Math.PI ** 2 * EI) / (s.tension * L * L);
31 const f0 = f1 / Math.sqrt(1 + B);
32 const mu = s.tension / (2 * L * f0) ** 2;
33 return { index: i, T: s.tension, mu, EI, EA, d: s.diameterMm * 1e-3 };
34}
36export interface Fingering {
37 string: number;
38 /** semitones above the open string (negative = on the extension, below the open E) */
39 shift: number;
40 /** vibrating length (stop to bridge), m */
41 length: number;
42 /** stopped by a finger (true) or open (nut / extension mechanism) */
43 stopped: boolean;
44}
46/**
47 * The string a bassist would use: the highest string whose open pitch is at or below the note
48 * (lowest position), with notes below E1 on a low-C extension (a longer vibrating length, stopped
49 * by the extension's mechanical fingers, treated as open). The stopped length is intonated so the
50 * stiff string's first partial is exactly on pitch.
51 */
52export function fingering(p: Params, note: number): Fingering {
53 let i = N_STRINGS - 1;
54 while (i > 0 && STRING_OPEN_MIDI[i] > note) i--;
55 const shift = note - STRING_OPEN_MIDI[i];
56 const sc = stringConst(p, i);
57 const target = midiToHz(note);
58 // solve f1(L) = target with f1 = (1/2L) sqrt(T/mu) sqrt(1 + pi^2 EI / (T L^2))
59 const c = Math.sqrt(sc.T / sc.mu);
60 let L = p.strings.scaleLength * Math.pow(2, -shift / 12);
61 for (let k = 0; k < 30; k++) {
62 const B = (Math.PI ** 2 * sc.EI) / (sc.T * L * L);
63 L = (c / (2 * target)) * Math.sqrt(1 + B);
64 }
65 return { string: i, shift, length: L, stopped: shift > 0 };
66}
68export interface StringModes {
69 /** angular frequencies (rad/s) and decay rates (1/s) of the fixed-fixed stiff string */
70 w: Float64Array;
71 sigma: Float64Array;
72}
74/** Valette damping of mode frequency f (Hz): returns 1/Q. */
75export function valetteQinv(p: Params, sc: StringConst, f: number): number {
76 const eta = p.strings.airEta, rhoA = p.strings.airRho;
77 const R = 2 * Math.PI * eta + 2 * Math.PI * sc.d * Math.sqrt(Math.PI * eta * rhoA * f);
78 const qAir = R / (2 * Math.PI * sc.mu * f);
79 const qVe = (4 * Math.PI ** 2 * sc.mu * sc.EI * p.strings.deltaVe * f * f) / (sc.T * sc.T);
80 return qAir + qVe + p.strings.qTeInv;
81}
83/** Modes of a stiff string of length L up to fmax (Hz), with Valette damping plus extra sigma. */
84export function stringModes(p: Params, sc: StringConst, L: number, fmax: number, extraSigma = 0): StringModes {
85 const c = Math.sqrt(sc.T / sc.mu);
86 const B = (Math.PI ** 2 * sc.EI) / (sc.T * L * L);
87 const f0 = c / (2 * L);
88 const w: number[] = [], sig: number[] = [];
89 for (let n = 1; ; n++) {
90 const f = n * f0 * Math.sqrt(1 + B * n * n);
91 if (f > fmax) break;
92 w.push(2 * Math.PI * f);
93 sig.push(Math.PI * f * valetteQinv(p, sc, f) + extraSigma);
94 }
95 return { w: Float64Array.from(w), sigma: Float64Array.from(sig) };
96}
98/** Release force (N) and slip time (s) of the pluck for a MIDI velocity. */
99export function pluckForVelocity(p: Params, vel: number): { force: number; slip: number } {
100 const v = Math.max(1, Math.min(127, vel)) / 127;
101 const force = p.pluck.forceMin + (p.pluck.forceMax - p.pluck.forceMin) * Math.pow(v, p.pluck.velCurve);
102 const slip = (p.pluck.slipMs * 1e-3) * Math.pow(10 / force, p.pluck.slipExp);
103 return { force, slip };
104}