Realign build log - WordPad
✕
C:\ar9av\builds\REALIGN.TXT
  1. Desktop
  2. Build Log
  3. Music & Audio

Build log · Music & Audio

How I built Realign

24 bead rings strike marimba bars, drift apart, and all land back home at 3:16

Screenshot of Realign running in the portfolio
Realign, running on the desktop. Open it yourself →

1.Why I built it

I built Realign to recreate a polyrhythm reel where 24 beads seem to play a song by themselves. Each bead runs along a half-ring and hits a bar at either foot, and because every ring loops a little slower than the one inside it, the beads spread out into waves and patterns before they line up again.

I wanted the whole thing to be made in code, sound included, and I wanted the moments where the rings line up to be readable. So the app counts down to the next realignment, shows which fraction of the cycle you are at, and adds a backing track whose structure is built around those same moments.

2.How it works

Choosing frequencies that come back together

The cycle length is T = 196 seconds, which is 3:16. Ring i makes 63 - i round trips per cycle, so the innermost ring does 63 and the outermost does 40. Since every ring makes a whole number of round trips in T, all 24 are guaranteed to be back at their starting foot when the cycle wraps. The bead angle is just a triangle wave of k * t / T, mapped from pi (left foot) to 0 (right foot) and back.

A ring strikes 2k times per cycle, once at each foot. At a fraction p/q of the cycle (in lowest terms), ring i is on a foot exactly when q divides 2k. I precompute every such moment for q up to 8 with a gcd check and count the rings. That gives 6 rings at 1/8, 12 at 1/4 and all 24 at 1/2, which is what the fraction HUD shows. Picking integer frequencies off a common period is the general trick behind pendulum waves, and it makes the realignment exact instead of approximate.

A marimba from three sine waves

Each strike is additive synthesis: a sine at the fundamental plus partials at 3.93x and 9.2x, roughly where a real marimba bar's overtones sit. Each partial gets a 3 ms linear attack and an exponential decay, and the upper partials decay four and twelve times faster than the fundamental. A 30 ms burst of bandpassed noise tuned near six times the pitch adds the mallet click.

Notes come from D major pentatonic starting at D2, with the outermost ring on the lowest bar and higher bars decaying faster. Strikes are panned left or right depending on which foot the bead hits, wider for outer rings, and everything feeds a convolver whose impulse response is decaying stereo noise generated at startup, so there is no reverb file to load.

Scheduling on the audio clock

Timing runs through a lookahead scheduler. A setInterval fires every 25 ms and schedules every strike that falls in the next 0.15 seconds of cycle time, converting cycle time to AudioContext.currentTime with the current playback speed. Strike times are computed directly as multiples of T / (2k), so nothing accumulates drift. If the scheduler falls far behind (a hidden tab, a scrub), it resets to the current position instead of firing a burst of old notes.

At higher speeds the strikes get quieter, from 0.5 velocity at 1x down to 0.16 at 8x, so the sound doesn't turn into noise. The visuals do their own strike detection each frame by watching for the half-cycle index of each ring to change, which triggers bar flashes and floor ripples without having to talk to the audio thread.

A backing track locked to the cycle

At 120 BPM a sixteenth note is 0.125 seconds, so the 196 second cycle is exactly 1568 steps or 98 bars. That made it possible to arrange a trance or ambient track against the bead cycle: a Bm, G, D, A progression with two bars per chord, a riser starting at bar 45, a crash and drop at bar 49 (the half-way realignment), and a build into bar 94 that resolves on the 3:16 return.

The sidechain pump is one gain node that every music layer passes through. On each kick its gain is set to 0.25 and ramped linearly back to 1 over three sixteenths. The arp runs through a dotted-eighth delay with feedback, and the music only plays at 1x speed so speeding up the beads never warps the track.

A hand-rolled 3D camera on Canvas 2D

There is no 3D library. A small project(x, y, z) function applies a yaw rotation, a pitch rotation and a perspective divide, and every ring, bead trail, bar and ripple goes through it. The camera drifts on a couple of slow sine waves unless you drag, and both the automatic and user offsets are eased toward their target each frame.

The mirror floor is the same scene drawn twice: first with the y axis flipped and faded, then normally, using the lighter composite mode so the glowing rings add together. A gradient over the floor fades the reflection out with distance.

3.Try it

Drag to orbit the camera, scroll to zoom, and use the toolbar to change speed up to 8x, scrub the cycle, pick a backing track and set bead and music volume separately.

More from the build log

Realign · 4 min readAll 29 builds