/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c05-ensemble / harmony.ts
66 lines · 2.9 KBCodeBlameHistory
b7d8818Add "ensemble" as track 5; louder piano in descentJeremy Magland 1// From musical intent to physics. Each pendulum's two small-swing partials have a ratio set by its
2// mass ratio μ = m2/m1 and length ratio λ = l2/l1 (2.414 for equal ones). Keeping λ (its shape),
3// solve for the μ that gives the wanted interval; then, with one shared time scale, choose its size
4// so its lower partial sounds the wanted pitch (frequency ∝ 1/√length).
6/** Small-swing angular frequencies (lower, upper) for m1 = 1, l1 = 1, g = 1. */
7export function modes(mu: number, lam: number): [number, number] {
8 const a = 1 + mu, b = mu * lam, c = mu * lam * lam, ka = 1 + mu, kc = mu * lam;
9 const det = a * c - b * b, tr = (ka * c + kc * a) / det, dt = (ka * kc) / det;
10 const disc = Math.sqrt(Math.max(0, tr * tr - 4 * dt));
11 return [Math.sqrt((tr - disc) / 2), Math.sqrt((tr + disc) / 2)];
14/** μ giving upper/lower partial ratio r for shape λ (the ratio rises with μ). */
15export function massForRatio(r: number, lam: number): number {
16 let lo = 1e-4, hi = 1e4;
17 const ratio = (mu: number) => {
18 const [a, b] = modes(mu, lam);
19 return b / a;
20 };
21 if (ratio(lo) >= r) return lo;
22 if (ratio(hi) <= r) return hi;
23 for (let i = 0; i < 80; i++) {
24 const mid = Math.sqrt(lo * hi);
25 if (ratio(mid) < r) lo = mid;
26 else hi = mid;
27 }
28 return Math.sqrt(lo * hi);
31/** "5/4" → 1.25 */
32export function parseRatio(s: string): number {
33 const [a, b] = s.split("/").map(Number);
34 return b ? a / b : a;
37export interface Built {
38 l1: number;
39 l2: number;
40 m2: number;
41 lower: number; // Hz
42 upper: number; // Hz
45/**
46 * One time scale k (sim s per audio s) puts a reference pendulum (l1 = 1, its own μ, λ) at
47 * basePitch × ratio; scaling all lengths by L multiplies frequencies by 1/√L.
48 */
49export function build(basePitch: number, ratio: number, partial: number, lam: number, k: number): Built {
50 const mu = massForRatio(partial, lam);
51 const [w1, w2] = modes(mu, lam); // sim rad/s at l1 = 1
52 const target = 2 * Math.PI * basePitch * ratio; // audio rad/s for the lower partial
53 // audio ω = sim ω · k; sim ω at size L is w1/√L → L = (w1·k / target)²
54 const L = ((w1 * k) / target) ** 2;
55 return { l1: L, l2: lam * L, m2: mu, lower: basePitch * ratio, upper: basePitch * ratio * (w2 / w1) };
58/** The shared time scale: the largest pendulum (lowest ratio) gets l1 = 1. */
59export function timeScale(basePitch: number, lowestRatio: number, partialOfLowest: number, lamOfLowest: number): number {
60 const mu = massForRatio(partialOfLowest, lamOfLowest);
61 const [w1] = modes(mu, lamOfLowest);
62 return (2 * Math.PI * basePitch * lowestRatio) / w1;
65export 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"];
66export const PARTIAL_RATIOS = ["5/4", "4/3", "3/2", "5/3", "2", "5/2", "3", "4", "5", "6", "8"];
↑↓ move↵openescclose