Music sync and the ECG: measuring a beat
A heart monitor and a music-synced light show solve the same problem: turning a noisy waveform into beats and a rate. Where the two match and where they differ.
My first software job was on ECG machines, and much of that work was taken up by finding each heartbeat in a messy signal and by working out the rate from it. The same problem is found in music, and the beat in songs has been found by DJ software for years, so the idea itself is not new. After that work had been done every day, though, I no longer thought of lights that follow the music as a project for someday. I love live music, and lights that change with the song were what I had always missed at home, so I started building them.
The same basic job is done by a heart monitor and by a music-synced lighting system. A continuous, noisy signal is taken in by each of them, the event that repeats in it is found, and both the moment at which that event happens and the number of times it happens are reported. By a heart monitor this is reported as beats per minute, whereas by a light show the same information is reported in color.
The two systems are compared in these notes. The comparison is a useful one, since decades have been spent by cardiology on learning how heartbeats are found, which means that a name has already been given on the medical side to most of the ways in which the beat can be got wrong by a light show.
The signal
The same shape is repeated by an ECG trace for each heartbeat, namely a small P wave, a sharp spike that is called the QRS complex, and then a rounded T wave. The tallest point of the spike is called the R peak, and since it is the feature that is found most easily, most heart rate measurement is started from it.
In music, the place of the R peak is taken by the kick drum and by other sharp attacks. In a lot of dance and pop music the beat is spotted as easily as an R peak, although in other music it is harder to spot, since no clean spike at all may be contained in a ballad, an ambient track or a live recording. Both kinds of music therefore have to be handled by a good system, without the way in which it behaves being changed from one song to the next.
Rate comes from intervals
Heart rate is never read from a label, because it has to be measured from the signal. The time between two R peaks is called the RR interval, and the rate is found by dividing 60 by that interval in seconds. An interval of 0.8 seconds is equal to 75 beats per minute.
Music tempo is worked out in the same way, so that a beat every 0.5 seconds is equal to 120 BPM. In principle the measurement could be skipped by a lighting system and the tempo read from the song’s details, although in practice those details are usually missing. We checked nearly 600 tracks on SoundCloud for a tempo value and none of them had one, which means that a lighting system, like a monitor, has to measure the rate from the signal itself.
A tall T wave can be counted by an ECG detector as a second R peak, and in that case twice the real heart rate is shown on the monitor. This fault is known as double counting. The same doubling is found in music, although there it is a matter of choice rather than a fault. A song at 70 BPM may equally be counted at 140 BPM, and many tracks that are written in half time sit right on that line, so either of the two counts is correct.
The two side by side
| ECG monitor | Music-synced lights | |
|---|---|---|
| Event | R peak in the QRS complex | Kick drum and other sharp attacks |
| Rate | 60 ÷ RR interval | 60 ÷ beat interval |
| Labelled data | None. Rate is measured | Tempo is usually missing from songs |
| Noise | Movement, muscle, mains hum | Talking, TV, room echo |
| Double counting | Tall T wave read as a beat | Half time vs double time |
| Gaps | Report loss of signal | Rest during breakdowns |
| Output | Fixed sweep speed | Bulbs drop changes sent too fast |
Timing
Some cardiac equipment is required to act on the beat and not after it. In synchronized mode, the shock of a defibrillator is timed to the R wave, because the moment at which the shock is delivered matters.
A milder version of the same requirement is placed on lights. In a system that listens through a microphone, a drum hit is registered only after it has already been heard in the room, which means that the lights of such a system always follow behind the music. Because the music itself is played by FirePod, its lights are able to change on the beat instead.
Output rate
When a monitor loses the signal, the loss has to be reported, and no beats may be drawn in the gap. Songs contain gaps of their own, such as breakdowns, quiet intros and the bar before a drop, and these are placed there on purpose. During such a gap the lights should be held still rather than pulsed to a beat that is absent, which makes the change larger when the kick drum comes back.
Only half of the job is done by measuring the beat, and the other half is done by the display. Its trace is drawn by a monitor at a fixed sweep speed, regardless of what is being done by the heart.
Smart lights also have an upper limit, because some bulbs start to drop changes when they are sent changes faster than they can accept them. For that reason a light change should be made for only some of the beats, since a smaller number of well-timed changes is seen as better in a room than a constant flicker that cannot be kept up with by the bulbs.
Noise and artifacts
Noise is picked up by ECG readings from the patient’s movement, from muscle activity and from the electrical hum of the building’s power supply. Any one of these is able to look like a heartbeat to a detector by which too little care is taken.
The same problem is met by music modes in which a microphone is relied on, although with different sources, such as people talking, a TV in the next room and the echo of the room itself, and quiet songs are hardly registered over that background. The cleanest place at which the beat can be measured is the audio itself, before it reaches a speaker and a room. FirePod sits at that point because it is the speaker, so it has the music before anyone hears it.
FirePod applies these principles to the lights you already own: Philips Hue, WiZ, LIFX, Govee and other brands, all following the same beat. FirePod isn’t on sale yet. See how it works or sign up for updates.
More from Firedance
-
kicad-skew: making sure memory signals arrive together
Memory chips need their signals to arrive together, so the wires have to match in length. kicad-skew checks that on a KiCad board with KiCad's own measurements.
-
share-cards: social images from one HTML file
Every social site wants a different image size. share-cards turns one HTML design into all of them, with each platform's cropping in mind. It's free on GitHub.
-
A Halloween treat: freedcamp-cli and more
Our last open source release of October is freedcamp-cli, for managing Freedcamp projects from the terminal or a script. Plus a look back at the month.