// From musical intent to physics. Each pendulum's two small-swing partials have a ratio set by its // mass ratio μ = m2/m1 and length ratio λ = l2/l1 (2.414 for equal ones). Keeping λ (its shape), // solve for the μ that gives the wanted interval; then, with one shared time scale, choose its size // so its lower partial sounds the wanted pitch (frequency ∝ 1/√length). /** Small-swing angular frequencies (lower, upper) for m1 = 1, l1 = 1, g = 1. */ export function modes(mu: number, lam: number): [number, number] { const a = 1 + mu, b = mu * lam, c = mu * lam * lam, ka = 1 + mu, kc = mu * lam; const det = a * c - b * b, tr = (ka * c + kc * a) / det, dt = (ka * kc) / det; const disc = Math.sqrt(Math.max(0, tr * tr - 4 * dt)); return [Math.sqrt((tr - disc) / 2), Math.sqrt((tr + disc) / 2)]; } /** μ giving upper/lower partial ratio r for shape λ (the ratio rises with μ). */ export function massForRatio(r: number, lam: number): number { let lo = 1e-4, hi = 1e4; const ratio = (mu: number) => { const [a, b] = modes(mu, lam); return b / a; }; if (ratio(lo) >= r) return lo; if (ratio(hi) <= r) return hi; for (let i = 0; i < 80; i++) { const mid = Math.sqrt(lo * hi); if (ratio(mid) < r) lo = mid; else hi = mid; } return Math.sqrt(lo * hi); } /** "5/4" → 1.25 */ export function parseRatio(s: string): number { const [a, b] = s.split("/").map(Number); return b ? a / b : a; } export interface Built { l1: number; l2: number; m2: number; lower: number; // Hz upper: number; // Hz } /** * One time scale k (sim s per audio s) puts a reference pendulum (l1 = 1, its own μ, λ) at * basePitch × ratio; scaling all lengths by L multiplies frequencies by 1/√L. */ export function build(basePitch: number, ratio: number, partial: number, lam: number, k: number): Built { const mu = massForRatio(partial, lam); const [w1, w2] = modes(mu, lam); // sim rad/s at l1 = 1 const target = 2 * Math.PI * basePitch * ratio; // audio rad/s for the lower partial // audio ω = sim ω · k; sim ω at size L is w1/√L → L = (w1·k / target)² const q = (w1 * k) / target; const L = q * q; return { l1: L, l2: lam * L, m2: mu, lower: basePitch * ratio, upper: basePitch * ratio * (w2 / w1) }; } /** The shared time scale: the largest pendulum (lowest ratio) gets l1 = 1. */ export function timeScale(basePitch: number, lowestRatio: number, partialOfLowest: number, lamOfLowest: number): number { const mu = massForRatio(partialOfLowest, lamOfLowest); const [w1] = modes(mu, lamOfLowest); return (2 * Math.PI * basePitch * lowestRatio) / w1; } export const PITCH_RATIOS = ["1", "16/15", "9/8", "6/5", "5/4", "4/3", "45/32", "3/2", "8/5", "5/3", "9/5", "15/8", "2", "9/4", "12/5", "5/2", "8/3", "3", "16/5", "10/3", "15/4", "4", "9/2", "5", "6", "8"]; export const PARTIAL_RATIOS = ["5/4", "4/3", "3/2", "5/3", "2", "5/2", "3", "4", "5", "6", "8"];