Repository Public
1<!--2ORIX Sonata3File: Documentation/acoustics/strings.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Strings89## The ideal string1011A flexible string of length `L`, tension `T` and linear density `mu`, fixed at both ends,12vibrates at1314 f_n = n / (2 L) * sqrt(T / mu), n = 1, 2, 3, ...1516Its motion is the sum of two waves travelling in opposite directions at `c = sqrt(T / mu)`17(d'Alembert). A digital waveguide stores those travelling waves in delay lines; the round trip18of `2L / c` seconds is `P = fs / f0` samples. Sonata does not ask for `L`, `T` or `mu`: the note19gives `f0` directly, so no dimensionally inconsistent physical parameter can be entered.2021## `plucked_string` and electric `bass`: extended Karplus-Strong2223The loop (`StringLoop` in `engines/common.rs`) is2425 y[n] = delay(D) (4-point Lagrange read)26 l[n] = (1 - b) y[n] + b y[n-1] (loss filter, 0 <= b <= 0.5)27 d[n] = allpass(l[n]; a) (dispersion, a <= 0)28 write( g d[n] + excitation[n] )2930**Tuning.** The loop resonates where its total phase delay is a whole number of samples per31period. The filter delays at the fundamental are computed in closed form and subtracted:3233 D = P - tau_loss(w0) - tau_ap(w0)34 tau_loss(w) = atan2(b sin w, 1 - b + b cos w) / w35 tau_ap(w) = -arg[(a + e^{-jw}) / (1 + a e^{-jw})] / w3637Measured error of the engine: +0.01 to +0.03 cent at MIDI 30, 62 and 94 (the unit test38`string_loop_is_in_tune...` allows 1.5 cents from 82 Hz to 2.1 kHz, with and without dispersion).3940**Decay.** The fundamental must lose 60 dB in `T60` seconds, i.e. a factor41`10^(-3 / (f0 T60))` per period. The loop gain is that factor divided by the loss filter's and42the interpolator's magnitude at `w0`, and never above 1 so that the loop stays passive:4344 g = min(1, 10^(-3 / (f0 T60)) / (|H_loss(w0)| |H_lagrange(w0)|))4546When `g` clips at 1 (strong damping filter on a high note) the note decays faster than47requested. `damping` maps to `T60 = (0.06 / damping) sqrt(220 / f0)` seconds.4849**Excitation.** A pluck releases the string from a triangular displacement with its apex at the50plucking point `p` (fraction of the length). Its harmonic amplitudes are51`sin(n pi p) / n^2`: harmonics with a node at `p` are absent (test: with `p = 0.35`, harmonic 352is weaker than harmonic 2). One period of that triangle (zero mean) is fed into the loop,53blended with its derivative (a rectangular pulse, `1/n` roll-off) for a harder plectrum, then54low-passed according to hardness and velocity. `strike` injects a half-sine velocity pulse and55its inverted reflection from the near end.5657**Stiffness.** A real string is stiff: `f_n = n f0 sqrt(1 + B n^2)`. One first-order allpass58in the loop delays high frequencies less than low ones, which stretches the partials. This59reproduces the *direction* of the effect, not a given `B`; a faithful dispersion filter needs60several allpass sections designed for each note.6162**Body.** `resonance body` adds five two-pole resonators (98, 204, 287, 414, 592 Hz, the order63of magnitude of a guitar's air and top-plate modes). `resonance modal(frequencies: [...])`64replaces them by up to 16 user modes. They are parallel resonators after the string, with no65coupling back into it.6667**Electric bass.** The same loop, then a pickup: a string point at fraction `q` senses the wave68and its reflection, `y[n] - y[n - qP]`, a comb with notches at multiples of `f0 / q`; then a69one-pole tone control and a `tanh` amplifier.7071*Validity.* One polarisation, no string-body coupling, no fret or finger noise, linear string72(no tension modulation, so no pitch glide on hard plucks).7374## `bowed_string`: friction waveguide7576Two delay lines model the string on each side of the bow, at fraction `beta` from the bridge.77With `v_b` the bow velocity and `v_s = v_bridge + v_nut` the incoming string velocity at the bow:7879 dv = v_b - v_s80 mu(dv) = min(1, (|slope (dv + 0.001)| + 0.75)^-4), slope = 5 - 4 pressure81 v_out = dv mu(dv) added to both outgoing waves8283`mu` is 1 while the string sticks to the hair (small `dv`) and falls quickly when it slips: a84memoryless approximation of the friction curve (after McIntyre, Schumacher and Woodhouse, in85the form popularised by the Synthesis ToolKit). The bridge reflects through a one-pole low-pass86with gain -0.95; the nut reflects with -1. The loop delay is `P` minus the low-pass's phase87delay, split `beta : 1 - beta`.8889**Behaviour that is modelled:** the Helmholtz stick-slip motion and its sawtooth-like spectrum;90too little pressure gives a weak surface sound; pressure and speed trade off; `bow.position`91near the bridge brightens the tone.9293**Behaviour that is not:** hysteresis of the friction (no flattening with pressure), torsional94waves, bow-hair compliance, a finite bow width. At high bow speed on short strings the model95jumps to the octave: above MIDI 82 the engine lowers the bow speed, and a measured correction96(`TUNING`) removes a residual sharpness of up to 12 cents. Extreme `bow.pressure` values can97still leave the Helmholtz regime; the output stays finite and bounded (test98`extreme_parameter_values_stay_finite_and_bounded`).99100The body is six parallel resonators scaled by `body.size`; `players` runs up to four101independent strings per note with their own tuning, vibrato phase and pan (a section).102