Skip to content
Boris Truyens

Music has a grid

A browser mixer for a dance performance: fetch songs, cut on the beat, and an automatic mode that plans a mix from one sentence in plain language.

The request was simple. For a dance performance in the family, three songs had to be joined: a part of the first up to one particular word, a line from the second, eight bars from the third. Every dancer knows the problem. A cut that lands a fraction late, you hear at once. Even someone who knows nothing about music feels the floor drop for a moment.

The skeleton under the sound

Music seems fluid, but under almost every dance track sits a strict grid. 124 beats per minute means a beat every 0.484 seconds, four beats per bar, phrases of eight bars. Cut on a beat and the transition sounds like a choice. Cut next to it and it sounds like a mistake.

The mixer

It became a timeline in the browser. Songs are fetched, you drag clips onto tracks and set in and out points, fades and volume. Everything is computed in the browser itself: preview, mixing and export to MP3 at 320 kbps, about eight times faster than real time. A separate script renders the same project with ffmpeg, so I can check that the browser and the server give exactly the same result.

At first they did not. The browser’s compressor silently added about 0.6 dB of gain. The mixer now measures that at start-up and cancels it, so preview and export match.

Per clip you can also change the tempo without shifting the pitch, so two songs with different tempos join up.

The automatic mode

Then came the question whether it could do it by itself. You type what you want in plain words: “the first song up to ‘grace’, then ‘What are you looking at?’, then eight bars from ‘come on, vogue’”. A language model turns that sentence into rules. Then:

  1. the songs are fetched and analysed: beats, bars, energy;
  2. speech recognition (Whisper) finds the requested words in the vocals;
  3. every cut is moved to the nearest sensible beat;
  4. the loudness of every clip is matched in LUFS, the measure broadcasters use;
  5. out comes a project you can still edit, and an MP3.

On the first real mix, the automatic version landed within about 30 milliseconds of my own hand-made cut.

What the machine did not know

The most instructive parts were the things that went wrong:

  • Whisper places words in sung vocals 0.1 to 0.2 seconds too early. A sung word stretches; a spoken word does not.
  • Give Whisper the literal lyrics as a hint and it skips those lyrics.
  • On short stretches of silence it invents words. “Thanks for watching” is a classic: it learned from YouTube videos.
  • A beat tracker can lock onto two thirds of the real tempo on syncopated songs.

The fix was always the same: don’t believe one measurement, combine two or three independent ones. That is what a good metrologist does too. One measurement is a number. Three measurements are a finding.

Where this leads

This project shares threads with others. Follow one.