/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / lab / voice.ts
80 lines · 3.0 KBBlameHistoryRaw
1// Shared by the labs: one double pendulum as an audio-rate voice (time scaling, strikes that add
2// energy, signal extraction, DC removal). Deterministic math (dmath), as scores require.
3import { cos, hypot, sin, wrap } from "../physics/dmath.ts";
4import { DoublePendulum } from "../physics/doublePendulum.ts";
5import type { Signal } from "./protocol.ts";
7/** ω of the lower small-swing mode (sim units), for the time scaling. */
8export function lowMode(p: DoublePendulum): number {
9 const { m1, m2, l1, l2, g } = p.cfg;
10 const a = (m1 + m2) * l1 * l1, b = m2 * l1 * l2, c = m2 * l2 * l2;
11 const ka = (m1 + m2) * g * l1, kc = m2 * g * l2;
12 const det = a * c - b * b;
13 const tr = (ka * c + kc * a) / det, dt = (ka * kc) / det;
14 const lam = (tr - Math.sqrt(Math.max(0, tr * tr - 4 * dt))) / 2;
15 return Math.sqrt(Math.max(lam, 1e-9));
18/**
19 * Add `add` (absolute) energy: a momentum kick along the current momentum (like pushing a swing
20 * in time, so it always adds), or in a random direction from rest, sized exactly.
21 */
22export function kick(p: DoublePendulum, add: number, rand: () => number) {
23 const y = p.y;
24 if (!(add > 0)) return;
25 let u0 = y[2], u1 = y[3];
26 const pn = hypot(u0, u1);
27 if (pn > 1e-9) {
28 u0 /= pn;
29 u1 /= pn;
30 } else {
31 const a = rand() * 2 * Math.PI;
32 u0 = cos(a);
33 u1 = sin(a);
34 }
35 // energy after p → p + a·u is E + a·(u·ω) + ½a²·(u·M⁻¹u); solve for the positive a
36 const now = p.derived();
37 const b = u0 * now.w1 + u1 * now.w2;
38 const wu = p.derived([y[0], y[1], u0, u1]);
39 const c = u0 * wu.w1 + u1 * wu.w2;
40 const a = (-b + Math.sqrt(Math.max(0, b * b + 2 * c * add))) / c;
41 y[2] += a * u0;
42 y[3] += a * u1;
45/** A state quantity as an audio signal, roughly normalized to ±1. */
46export function signal(p: DoublePendulum, sig: Signal, w1: number, w2: number): number {
47 const { l1, l2, m2 } = p.cfg;
48 const y = p.y;
49 const L = l1 + l2;
50 const vmax = Math.sqrt((2 * p.vTop) / m2) + 1e-9;
51 const s1 = sin(y[0]), c1 = cos(y[0]), s2 = sin(y[1]), c2 = cos(y[1]);
52 switch (sig) {
53 case "x2": return (l1 * s1 + l2 * s2) / L;
54 case "y2": return -(l1 * c1 + l2 * c2) / L;
55 case "vx2": return (l1 * c1 * w1 + l2 * c2 * w2) / vmax;
56 case "vy2": return (l1 * s1 * w1 + l2 * s2 * w2) / vmax;
57 case "x1": return s1;
58 case "vx1": return (l1 * c1 * w1) / vmax;
59 case "θ1": return wrap(y[0]) / Math.PI;
60 case "θ2": return wrap(y[1]) / Math.PI;
61 case "θ2−θ1": return wrap(y[1] - y[0]) / Math.PI;
62 case "ω1": return (w1 * l1) / vmax;
63 case "ω2": return (w2 * l2) / vmax;
64 case "p1": return y[2] / (vmax * (1 + m2) * l1 * l1 + 1e-9);
65 case "p2": return y[3] / (vmax * m2 * l2 * l2 + 1e-9);
66 case "speed2": return hypot(l1 * c1 * w1 + l2 * c2 * w2, l1 * s1 * w1 + l2 * s2 * w2) / vmax;
67 }
70/** One-pole DC blocker. */
71export class DcBlock {
72 private x = 0;
73 private y = 0;
74 run(v: number, r: number): number {
75 const o = v - this.x + r * this.y;
76 this.x = v;
77 this.y = o;
78 return o;
79 }
↑↓ move↵openescclose