Balian.dev
St. Paul, MN · remote Book a call

Blog

Polyrhythm and polymeter as an interactive lesson, with live sound

Scroll and you hear three against two, then the same numbers as rings on one axle, as meshing gears, as a knot on a torus, and sped up until they become a perfect fifth. Every sound is synthesized in the browser from the numbers that draw the picture, and at the end there is an instrument you can play. The sound is part of the model, and that changed how the lesson had to be built.

Cross Rhythm is a web page that explains polyrhythm with sound. Turn the sound on and one voice hits three times in a bar while another hits twice, and the question the first caption asks is which one is the beat. From there the page takes the idea apart as you scroll: what a pulse and a bar are, how two layers can share a bar (polyrhythm) or share a pulse (polymeter), what a ratio looks like as a knot, what happens when you speed a rhythm up until it turns into a chord, and where these patterns live in real music, from the hemiola in a baroque dance to the bell pattern of a West African ensemble. It ends with an instrument. It is at cross-rhythm.balian.dev, and I am writing it up because making the sound part of the model changed how the whole thing had to be made.

Forty-seven seconds at reading pace through the stage where three against two is sped up until it is a perfect fifth.

A polyrhythm visualizer where the sound and the picture read one clock

Nothing in the page plays an audio file. Each voice, a wood block, a clave, a low drum, a bell and a shaker, is rendered once from a recipe while the page is idle, and every hit you hear is one of those renders scheduled to the sample. The rhythm itself lives in a small pure module with no idea that browsers exist: a pattern is a set of hits on an integer grid, the grid for two layers is the least common multiple of their steps, the tempo is a map from clock time to position in the cycle, and a sequencer walks that grid and hands out onsets. The audio thread and the drawing both read from that module, so the flash on a ring cannot land anywhere but where the sound does.

The hard part was agreeing on what “now” means. A browser plays sound some tens of milliseconds after it is scheduled, more over Bluetooth, and a frame drawn at the moment of scheduling would flash before you hear anything. So the page keeps two clocks. One is the audio output’s own timestamp, corrected for the output latency the browser reports, a Bluetooth term, and a sync offset you can set yourself if your headphones are slow. The picture is drawn at that audible time, which is always a little in the past, and nothing is committed to the audio graph closer than about a tenth of a second ahead, so a scroll can still change its mind before a note is fixed. The stereo follows the drawing too: a hit sits in the mix where its step sits on the ring, right ear for the right half of the dial.

The fast end needed a different engine. Past twenty hits a second a browser cannot start a new sound source per hit, so a small program on the audio thread replays the same renders from the same tempo map, and the stage that speeds a rhythm up into a chord runs on it the whole way, from one hit a second to hundreds. The scroll on that stage multiplies the tempo and never the ratio, so three against two stays exactly 3:2 the whole way and arrives at the just perfect fifth, 702 cents, with the hit rates in the corner to prove it.

Polyrhythm vs polymeter, drawn as rings and as gears

Musicians use the two words loosely, and the page says so, but it draws them as two different machines. In a polyrhythm the layers share the bar and divide it differently, so the picture is rings on one axle: three spokes on one, two on the other, one hand for both, and their first spokes always line up. In a polymeter the layers share the pulse and loop over different lengths, so the picture is meshing gears. Meshing gears must have teeth of the same size, which is the shared pulse, and a three-tooth gear against a four-tooth one brings its marked tooth back under the pointer at the same moment as the other only every twelve pulses, when a hammer that has been rising through the cycle strikes a bell. A stage later, loops of four, five and seven on one pulse meet only every 140 pulses, which is 28 seconds at the speed it plays, and a dial round the gear train counts you there.

The knot is my favourite stage, because it is the ratio itself as a shape. A point travels on a torus, once round the ring between hits of one layer and once round the tube between hits of the other, and in one bar it draws a closed curve. For 3:2 that is the trefoil. A ratio with a common factor, 4:6, closes after half a bar and draws the same trefoil again, which is the geometric way of saying it is 2:3 played twice. Seen edge-on the knot is the Lissajous figure for two tones in the same ratio, and that is the picture the next stage speeds into.

Hemiola, the Rhythmicon and the Euclidean rhythm

The middle of the story is where the page earns its status chips. The stage on hemiola plays six even clicks and accents every third or every second, and the only thing that changes is how you group them. The grouping is exact, how people hear it is measured, and the chip on that stage says so. The stage before it, on meter, rests on Bolton’s 1894 finding that listeners hear uniform clicks in groups with an accent that is not there. The Rhythmicon stage is labelled historical: Theremin finished the instrument for Henry Cowell in 1931, each of its keys played a pulse at a whole-number multiple of the base rate at the same multiple of the base pitch, and the page draws it as a perforated disc with a light turning behind it. The stage on Euclidean rhythms takes Toussaint’s 2005 observation that Bjorklund’s algorithm for spreading k hits as evenly as possible over n steps reproduces timelines from all over the world, and plays the tresillo as E(3,8), the cinquillo as E(5,8) and the West African bell as E(7,12), each checked against the table in his paper before it went in.

Where it simplifies, and says so

The ensemble stage is a minimal model of a twelve-pulse cycle, with the bell, a four, a three and a shaker, and its note says that the music lives in performance and variation that no fixed pattern captures. The Rhythmicon is drawn as a diagram: the real one spun its discs past fixed photocells, and the page turns the light instead so that each flash lands where its step sits on the dial. The torus is a way of drawing a ratio and nothing physical. The practice of running an odd riff over a steady four is described and never played, because the page only uses traditional and public-domain patterns and its own. Every stage has a note like these in its HTML, under a status that is one of exact, measured, convention, tradition, historical or metaphor.

An interactive music lesson with the model underneath

A music theory unit on polyrhythm is usually a page of notation and an audio example, and the student either hears the two layers or does not. In this page the learner changes the ratio and hears it, moves the accent and feels the meter flip, speeds the rhythm up until the clicks become a chord, which is the one demonstration I have never seen done on paper, and taps along to find out how far off the beat they are. Because the readouts and the sound come from one module, a lesson built this way can put a check between stages that asks for the interval and know the answer is right.

What your expert edits

The captions, the “how we know” paragraph, the sources and the status of each stage are plain files a musicologist or a teacher can edit, with the rhythm module and the audio engine untouched underneath. Packaging for an LMS is the same as for the Cosmic History lesson: SCORM or xAPI, LTI for the university platforms, an embed for the rest.

The polyrhythm generator at the end

The last stage is an instrument with up to six layers, each one either dividing the bar or looping over the pulse, with hand-set hits or a Euclidean fill, a voice, a pitch and a pan. The same pattern can be viewed as rings, as polygons, as a knot, as gears or as a score, and the tempo dial runs from a bar every twenty seconds to past the point where the rhythm is a chord. Tap along on the space bar and each tap is drawn on its ring, early or late against the moment the hit reached you, with your mean and spread over the last taps. The whole state lives in the address bar, so a teacher can send a student a link to a 7:5 and the student opens it playing. For a music school it is a polyrhythm metronome with the explanation attached.

Subjects with the same shape

Anything periodic has this shape: gear trains and orbital resonances, shift rotations, the timing of packets on a network, vibration modes in a machine, a heart in arrhythmia. Where two cycles share a clock and meet on a schedule, the rings and the gears draw it, and if there is a sound in it, the sound can be the model too.

What a lesson like this costs now

A lesson with real audio synthesis, 3D, a sample-accurate scheduler and accessibility used to need a studio and a season. This one was built by one engineer in a fraction of that time, because I build with AI, which is what makes the price fit a department’s budget. It is a fixed price for a written scope, and the proposal says what the subject, the number of stages and the LMS connection add up to before anything starts. The AI-assisted engineering page says how I work and what I check.

Tell me the subject your students struggle to hear or see and I will tell you what a lesson like this one would take. The Block Universe and Cosmic History posts cover the other lessons, the demos page has all of them live, and the science visualization page has what the same work looks like for a museum, a publisher or a company.

Want a second opinion on what you are paying for?