/ music / marimba
Sign in
music / marimba
marimba / js / bar.js
105 lines · 4.2 KBCodeBlameHistory
46256e7Marimba lab: per-key tuned bars, coupled resonator tubes, SoundFont exportJeremy Magland 1// Bar geometry, wood materials, the nominal size of each key's bar, and the resonator
2// and mic placement. Lengths in mm unless a name says otherwise.
3// Axes: x = length (grain, L), y = width (front edge at y = 0, T), z = thickness (up, R).
4// The top face is flat at z = h0; the undercut arch is cut into the bottom face.
6export const AIR = { rho: 1.2, c: 343 };
8export const MATERIALS = {
9 rosewood: { label: 'Honduras rosewood', rho: 1025, EL: 22.0, eta: 0.006 },
10 padauk: { label: 'Padauk', rho: 745, EL: 11.7, eta: 0.01 },
11};
13export const LOW_NOTE = 36; // C2
14export const HIGH_NOTE = 96; // C7
16const NOTE = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
17export const noteName = (k) => NOTE[k % 12] + (Math.floor(k / 12) - 1);
18export const noteFreq = (k) => 440 * 2 ** ((k - 69) / 12);
19export const freqToMidi = (f) => 69 + 12 * Math.log2(f / 440);
20export const isBlack = (k) => [1, 3, 6, 8, 10].includes(k % 12);
22// Orthotropic hardwood stiffness (ratios as for dense hardwoods), scaled by E_L (Pa).
23export function woodStiffness(EL, scale = 1) {
24 const E = EL * scale;
25 const Ex = E, Ey = 0.065 * E, Ez = 0.132 * E; // L, T, R
26 const nuLT = 0.476, nuLR = 0.424, nuRT = 0.774;
27 const S = [
28 [1 / Ex, -nuLT / Ex, -nuLR / Ex],
29 [-nuLT / Ex, 1 / Ey, -nuRT / Ez],
30 [-nuLR / Ex, -nuRT / Ez, 1 / Ez],
31 ];
32 const C = inv3(S);
33 return {
34 E: Ex,
35 c11: C[0][0], c22: C[1][1], c33: C[2][2],
36 c12: C[0][1], c13: C[0][2], c23: C[1][2],
37 gxy: 0.063 * E, gxz: 0.111 * E, gyz: 0.02 * E,
38 };
41function inv3(m) {
42 const [a, b, c] = m[0], [d, e, f] = m[1], [g, h, i] = m[2];
43 const A = e * i - f * h, B = -(d * i - f * g), Cc = d * h - e * g;
44 const det = a * A + b * B + c * Cc;
45 return [
46 [A / det, -(b * i - c * h) / det, (b * f - c * e) / det],
47 [B / det, (a * i - c * g) / det, -(a * f - c * d) / det],
48 [Cc / det, -(a * h - b * g) / det, (a * e - b * d) / det],
49 ];
52/**
53 * A bar design: { L, W, h0 (mm), d: undercut depth (mm), a: undercut length as a
54 * fraction of L, p: arch shape exponent (2 = parabola, larger = flatter bottom) }.
55 */
56export function thickness(b, x) {
57 const u = Math.abs(x - b.L / 2) / ((b.a * b.L) / 2);
58 return u < 1 ? b.h0 - b.d * (1 - u ** b.p) : b.h0;
61export const MIN_THICK = 3; // mm left at the deepest point of the arch
62export const maxDepth = (b) => b.h0 - Math.max(MIN_THICK, 0.1 * b.h0);
64// Width and thickness change smoothly from C2 to C7; the tuner sets length and undercut.
65export function nominalDesign(note, mat) {
66 const t = (note - LOW_NOTE) / (HIGH_NOTE - LOW_NOTE);
67 const W = 72 - 32 * t, h0 = 24 - 4 * t; // typical 5-octave concert ranges
68 // length guess: uniform-bar formula, with the arch lowering f1 more in the bass
69 const c = Math.sqrt((mat.EL * 1e9) / mat.rho);
70 const lower = 1.2 * 2 ** ((HIGH_NOTE - note) / 24);
71 const L = 1000 * Math.sqrt((1.028 * c * h0 * 1e-3) / (noteFreq(note) * lower));
72 const b = { L, W, h0, d: 0, a: 0.45, p: 4 };
73 b.d = 0.65 * maxDepth(b);
74 return b;
77export function validateDesign(b) {
78 const e = [];
79 if (b.d > maxDepth(b) + 1e-9) e.push(`undercut deeper than ${maxDepth(b).toFixed(1)} mm`);
80 if (b.L < 2 * b.W) e.push('bar shorter than twice its width');
81 return e;
84// ---------- resonator and mic ----------
86/**
87 * Resonator: closed-bottom round tube centred under the bar.
88 * res = { on, diam (fraction of W), gap (mm from the bar's bottom plane to the tube top),
89 * detune (cents), loss (× wall loss) }
90 */
91export function tubeGeom(b, res) {
92 const a = (res.diam * b.W) / 2; // radius, mm
93 // end correction: unflanged pipe plus the bar acting as a partial lid over the mouth
94 const delta = 0.61 * a + (0.15 * a * a) / Math.max(2, res.gap);
95 return { a, delta, x: b.L / 2, y: b.W / 2, zTop: -res.gap };
98// Tube length (mm) whose air column, with the end correction, resonates at f (Hz).
99export function tubeLength(f, delta) {
100 const k = (2 * Math.PI * f) / (AIR.c * 1000); // 1/mm
101 return (Math.PI / 2 - Math.atan(k * delta)) / k;
104export const MIC = { front: 300, up: 450 }; // mm in front of and above the bar centre
105export const micPos = (b) => [b.L / 2, b.W / 2 - MIC.front, b.h0 + MIC.up];
↑↓ move↵openescclose