1# Marimba
3Interactive marimba, C2–C7. Every key has its own rosewood bar, tuned by its undercut, with its own resonator tube underneath. Sound comes from modal synthesis of the bar coupled to the tube.
5```bash
6npm run dev # http://127.0.0.1:8021
7```
9The FE model, the tuner and the sound run in any modern browser. The optional 3D FDTD check needs WebGPU (Chrome, Safari 26+). three.js loads from jsDelivr (pinned 0.169.0).
11## Model
13- **Bars** (`js/bar.js`): width 72→40 mm and thickness 24→20 mm from C2 to C7. The top is flat. The bottom has an arch, `h(x) = h0 − d·(1 − |u|^p)` over the central fraction `a` of the length. Wood is orthotropic, with hardwood ratios scaled by E_L. Material presets are Honduras rosewood (default) and padauk.
14- **FE modes** (`js/fe.js`): a Timoshenko beam with varying section models vertical bending, and Saint-Venant theory models torsion. Torsional stiffness uses the torsion constant of an orthotropic rectangle. Eigenvalues come from Sturm bisection on banded LDLᵀ factors, so tuning takes milliseconds. Radiation decay treats each top-face cell as a vertical dipole of bar volume plus added mass, and includes the mutual radiation between cells.
15- **Tuner** (`js/tune.js`): adjusts length, undercut depth and undercut length (Levenberg–Marquardt) until f1 is the note and f2 : f3 = 4 : 10 × f1. Partials whose target is above the cutoff (9 kHz by default) are left free, so the top bars tune f1 and f2 only. A flat-bottomed arch (`p ≈ 4`) is needed to reach 1 : 4 : 10; a parabola cannot.
16- **Resonator + sound** (`js/synth.js`): the tube is closed at the bottom and modelled as a 1D waveguide, with wall losses ∝ √f. Its end correction includes the bar acting as a partial lid. Flow pushed by the bar's underside over the mouth either enters the tube or escapes around the rim. The pressure in the gap pushes back on the bar. That coupling is rank one and is solved exactly at every frequency. The mic picks up the bar's dipole field plus the monopole of the flow the tube takes in. The tube length is set so the air column resonates at the note (adjustable in cents).
17- **Mallet** (`js/physics.js`): yarn over a core. The yarn force is `K_y δ^2.5`. Once the yarn is compressed to 80 % of its thickness, the core adds `K_c δ^1.5`. Hunt–Crossley hysteresis applies to both. The contact is simulated against the bar modes. The mic signal is the contact force times the transfer function, transformed back to time with an exponentially windowed inverse FFT so long rings don't wrap around. Because that window amplifies anything that wraps to the end of the FFT (by up to ~10⁷), every part of the transfer function is kept causal and passive:
18 - the force spectrum is computed exactly at every bin with a chirp-z (Bluestein) transform, not interpolated;
19 - each mode's mic transfer is a magnitude plus a delay, not a constant complex factor;
20 - the mouth's radiation load is the passive impedance of a small monopole;
21 - the spectrum rolls off smoothly to zero at Nyquist;
22 - the window is at least 0.6 s longer than the output, and grows when the note is still ringing at the cut.
24 Against an 8× longer reference, the error over random strikes is below −116 dB of the peak. Earlier versions had a rattle at the end of some notes; these measures removed it. At 1.5 m/s on mid bars, the presets give contacts of about 2 / 1 / 0.5 ms for soft / medium / hard. **Graduated mallets** (on by default) blend the chosen mallet toward a large, heavy bass mallet (about 4 ms contact) from C4 down to C2, as players do. With a 1 ms contact, a low bar's 10× partial is excited far more than its fundamental (16 dB more at C2), so the note sounds two octaves and a third too high.
25- **Damping**: internal loss factor `η(f) = η₀ (f / 1 kHz)^slope`, plus the radiation decay and the tube's own losses.
27## FDTD check
29**Analyze bar (FDTD)** voxelizes the selected bar in air. It reuses woodblock2's WebGPU velocity–stress solver and runs two force-impulse simulations. Modes are fitted from the top-face velocities, matched to the FE modes by shape, and compared in a table. **Sound from → FDTD modes** plays them through the same tube and mic model.
31The grid makes thin wood too soft, so FDTD frequencies come out low and approach the FE values as the grid is refined. Example, C4 bar:
33| grid | bar | f1 FE | f1 FDTD |
34|---|---|---|---|
35| 3 mm | uniform (no undercut) | 698 Hz | 646 Hz (−135 ¢) |
36| 3 mm | tuned (undercut 3 cells thick) | 262 Hz | 151 Hz (−951 ¢) |
37| 2 mm | tuned | 262 Hz | 219 Hz (−312 ¢) |
39Higher torsion modes come out above the FE values even at 3 mm, which points to the FE torsion model (no warping) being too soft there.
41## Controls
43- **Keys**: the strip at the bottom selects and plays a bar; ← / → step through the keys. Edited bars get a dot.
44- **Live**: mallet (preset or sliders, optionally graduated toward the bass), strike velocity, strike point (click the bar), resonator (on/off, diameter, gap, tuning, wall loss) and damping.
45- **Bar**: length, width, thickness, undercut depth, length and flatness. **Auto-tune** retunes from the current shape. **Reset to auto** discards your edits.
46- **All bars**: tuning targets and material. Changing them retunes every bar you haven't edited.
48## SoundFont export
50**Export .sf2** samples keys every 1–6 semitones. Each sampled key renders its own bar and tube (your edited design if there is one, otherwise the auto-tuned bar), so register changes are physical, not pitch-shifted. There are 4 velocity layers; softer layers use a slower strike and slightly softer yarn. SF2 has no round-robin, so each layer is split into 3 velocity bands struck at nearby points. One global gain keeps the physical loudness of keys and layers. The preset is GM program 13 (Marimba), bank 0. With the defaults (C2–C7, every 3 semitones) the file is about 23 MB and renders in about 11 s.