/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / README.md
90 lines · 9.3 KBCodeBlameHistory
3An album of electroacoustic compositions. Each composition's sound and image are computed live in the browser from a double pendulum simulation.
5```bash
6npm install
7npm run dev # http://127.0.0.1:8031
8npm run build # static site in dist/ (relative paths, so it works under any subpath)
9```
11## Routes
13- `#/`: the track list
14- `#/<id>`: a composition with its published defaults (what visitors hear)
15- `#/<id>/edit`: author mode. Adds a panel of every parameter, readouts, "start at", and click-to-seek on the progress line.
17## Author workflow
191. Open a composition, click **edit**, and tweak while it plays. Most changes are heard immediately. Instrument choice and the seed apply on the next play.
202. Your edits persist in this browser (localStorage), and only in author mode.
213. **copy config** copies the full parameter set as JSON. Paste it over
22 `src/compositions/<dir>/defaults.json` and rebuild to publish it.
234. **seed** = 0 gives a new variation each play. The readouts show the seed that was used, so you can pin a variation you liked.
25## Layout
27- `src/physics/doublePendulum.ts`: Hamiltonian double pendulum, RK4. Energy is steered to a target by a force `λ·p` (`dE/dt = 2λT`), so it rises and falls smoothly.
28- `src/instruments/`: instrument interfaces (`NoteInstrument`, `DroneInstrument`), placeholders, and the registry. Add real instruments here and they appear in the composition's instrument menus.
29- `src/instruments/modal.ts`: instruments from the sibling **modal-synth** project (`../modal-synth`), imported live through the `@modal-synth` alias (see `vite.config.ts` and `tsconfig.json`). Every built-in modal-synth preset appears as `modal: <name>`. For your own sounds, press **Copy** in modal-synth, choose `modal: custom`, and paste the JSON into **modal custom**. The JSON then becomes part of the composition's config. Building requires `../modal-synth` to be present.
30- `src/music/scales.ts`: scales and quantization (including continuous and harmonic series).
31- `src/core/`: params schema, author panel, audio context, and the composition interface.
32- `src/compositions/`: one folder per track (`schema.ts`, `defaults.json`, `piece.ts`), listed in `index.ts`.
33- `src/compositions/c01-poincare/orbitWorker.ts`: background orbit portrait, run in short work slices that use about `orbitCpu` of one core.
35## 1. poincaré
37The section is θ1 = 0, crossed with dθ1/dt > 0, and plotted at (θ2, p2). Each crossing plays a note:
39- θ2 sets the pitch (signed or absolute, range, curve, scale, root)
40- |p2| sets the loudness
41- θ2 sets the pan
43Notes stop at `duration − finalAt`. The last note is then held for `finalHold` and faded over `finalFade`, all within `tail`.
45Behind the live crossings, a pool of energy-conserving orbits at the current energy keeps drawing the section's structure (islands and chaotic sea). They're reseeded as the energy moves.
47The hum is one held note of a sustained modal-synth preset (bowed, blown, self-oscillating or fed back). The energy sets a timbre value, which drives up to two synth settings (A and B), e.g. bow position and bow force. **normalize loudness** measures the hum's output and compensates slowly, so it holds a fixed pitch and loudness while only its timbre follows the energy. The energy follows an envelope: start → peak (exponential or linear rise) → hold → fall to zero at `duration`.
49Units: energy is in units of the potential energy with both arms up, and p2 is in units of √(m2·l2²·E_top).
5c043e1Add composition 2: orbitsJeremy Magland 51## 2. orbits
53Several pendulums at once (6 by default, up to 20). All follow the same energy envelope from random starts. Only the section is shown: each orbit traces its crossings in its own color (**colors**, cycling). Each orbit plays instrument slot n (cycling through 6 slots), with a register shift per slot. Orbits on the same instrument share one polyphonic instance. The note mapping, hum, and rest-based ending match composition 1. The piece ends when every orbit has come to rest and its last note has faded.
01b86bforbits: show the section portrait and each pendulum's traced orbitJeremy Magland 55Behind the live dots: the grey portrait of the section at the current energy, and each pendulum's *traced orbit* in its color. Each time the portrait refreshes, every pendulum's current state is followed forward with its energy frozen. That draws the orbit it is on right now: a closed loop on an island, a cloud in the chaotic sea. Traces refresh and crossfade with the portrait as the energy moves.
216afddorbits: render instruments ahead in Web WorkersJeremy Magland 57**Render ahead** (on by default): each modal instrument runs in its own Web Worker. Each worker renders with the same modal-synth engine about 1 s ahead of playback and streams audio chunks to a small player worklet (`src/instruments/stream/`). The synthesis uses the device's other cores, and the buffer absorbs momentary slowdowns. The real-time audio thread only copies samples. The simulation runs 1.5 s ahead. After each step, the page sends a *horizon* ("every note before this time has been sent"), and workers never render past it, so a note can't be late; a stall becomes an underrun instead. Final-note holds reach the renderer as timed parameter changes.
59**Device limit**: almost all the cost of this piece is audio. Before the first play on a device, a check estimates how many orbits it can handle. With render ahead, every instrument's worker renders its share of the notes as fast as it can, all in parallel, and the busiest one must stay under 60% of real time. Without it, the instruments play silently on the audio thread and their reported load must stay under 35%. If the device is over budget, fewer orbits are played. The result is cached per device and settings. When orbits are cut, the page says so ("8 of 15 orbits: reduced to what this device can play live"). In author mode, the readouts show the measured load and live **audio load**; **limit to device** turns the limit off.
01b86bforbits: show the section portrait and each pendulum's traced orbitJeremy Magland 61Shared machinery lives in `src/compositions/shared/`: the energy envelope, crossing → note mapping, hum control, section plot with its "fit energy" zoom, and the portrait/trace layer with its throttled worker.
5c043e1Add composition 2: orbitsJeremy Magland 62
bc546ddAdd composition 3: divergenceJeremy Magland 63## 3. divergence
65Twelve pendulums (1–24) are released together from rest at a wide angle (120° by default, varied by up to 3° each play; the seed pins it). Each outer arm is offset by a tiny amount. For each release, the offset is calibrated by bisection so the outermost pendulums visibly separate (bobs 2% of the length apart) at **separate after** (8 s). The rest fan out after them as chaos takes over. Only the pendulums are drawn, from one pivot, each in its own color, with fading bob trails.
d5df914divergence: different chords for left-to-right and right-to-left crossingsJeremy Magland 67A note sounds when a bottom bob crosses the vertical through the pivot, in either direction. Loudness follows the bob's speed, and the note is panned toward the side the bob is heading (two copies of the instrument, panned left and right). **pitch** = "chord" gives each pendulum its own note of a chord spread across the range, so the opening is one chord that breaks into arpeggios. With **two chords** (on by default), left-to-right and right-to-left crossings play two interleaved chords (2n notes spread over the range, alternating between them), so the opening swings alternate between neighbouring chords. "motion" takes the pitch from the bob's speed, so the pendulums start in unison and spread out.
69The pendulums swing freely (energy conserved) for **swinging** (40 s). Then each pendulum's energy follows (1 − u)^curve to zero over **damping** (20 s), with viscous friction settling the last of it. Each pendulum's last note rings out, and the piece ends with the fade. No hum.
f7d91baAdd composition 4: crashJeremy Magland 71## 4. crash
78f914dcrash: tip after 2.5 s (was 5)Jeremy Magland 73One pendulum, balanced straight up. It gets a tiny nudge, calibrated so it visibly tips at **tips after** (2.5 s); each play picks the side and shape at random.
f7d91baAdd composition 4: crashJeremy Magland 74
5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 75- **Height notes**: short piano notes (Piano-ish) mark the outer bob's height. Think of a ladder of levels: every other note of the scale between C4 (bottom) and C7 (top). Each time the height passes a level, going up or down, that level's note plays, with a small margin so hovering doesn't re-trigger it. You hear the fall as a descending run. The notes ramp in over the opening and quiet with the energy at the end.
89519c1crash: extension notes when the arms are fully extendedJeremy Magland 76- **Extension notes**: whenever the pendulum is fully extended (the arms pass through a straight line, at any angle), a short violin note plays at a fixed pitch (G3), with its strength from the bob's speed. The first one is accented. The arms must bend about 3° from straight before the next extension can sound.
77- **Clicks**: when the outer bob crosses the vertical close to the pivot (within **near the pivot**, 25% of the length), a click plays (by default a 0.5 s Viola note at C5). With equal arms, a folded pendulum sweeps its bob right through the pivot.
89519c1crash: extension notes when the arms are fully extendedJeremy Magland 79There's slight friction throughout. Over the damping section it grows smoothly to **final damping**, until the pendulum comes to rest. The last extension note rings out. The instruments render ahead in workers, and a device check limits how many extension notes may ring at once (**overlapping notes**). This matters for heavy instruments like Chaos gong, about 14% of a core per ringing note; the violin is about 3%.
f7d91baAdd composition 4: crashJeremy Magland 80
83The site is published from the `site/` directory of `music/double-pendulum` on vault1.magland.org, served at https://double-pendulum--music.vault1-sites.magland.org/. The build needs `../modal-synth`, so it runs locally, not in CI:
85```bash
86npm run site # build, then copy dist/ to site/
87git add -A && git commit -m "Update site" && git push
88```
90Every push to `main` republishes the site.
↑↓ move↵openescclose