Marco's Blog

All content strictly personal opinions.
en eo

BPM Detection in Linux

2010-09-25 3 min read Electronics Anonymous marco

Doing a lot of cardio workouts, it is really good to have music that beats to your rhythm. The pulsating sound gives you energy and pace, both excellent ways to make a good workout, great, and to make time pass faster. When I get new music on my player (a Sansa Clip+ – the almost perfect player for a Sporty Spice) life is good. An hour of running is gone before I even know it, and when I look at the calorie count, I feel like Michael Phelps freshly crowned with Olympic gold.

At first I would stumble across music that matched the pace. I do lots of different kinds of workouts, so certain songs would work with different segments. I have a running pace, a hiking pace, a mountain climbing pace, a cycling pace, a weight lifting pace, a spinning pace, etc. I would get used to certain songs in certain parts, but that would get old, fast.

Then I got used to counting beats. I would look at the big clock in the gym and count beats for 15 seconds. That would give me a general idea of what I could use a song for. My favorite spinning song, for instance, was “Hazel Eyes,” so anything that had the same beat count would be a good replacement.

Then I started getting bored with this random approach and realized I had a library of hundreds of CDs ripped onto my computers. I just had to detect the beat automatically and I would be able to simply do a lookup search for a specific BPM count and get all possible results.

I started looking for software that would do the automatic detection for me. Windows (and presumably OS X) have lots of different applications for the task, but most of them require you to interact with the software. What I wanted was a typical Linux thing: something that would detect if there was a music file without beat count, and then automatically determined the beat.

After searching for a while, I found two pieces of software that are both open and relatively accurate: soundstretch, which comes in the Ubuntu repositories, and bpmcount, which is available as a binary package from the website.

Both applications work in a very similar way: they process WAV files (not OGG or MP3) and give you a number as output – the BPM count. This number is more or less accurate, depending on a number of factors. Some of it is systemic – if the music doesn’t have a single beat, or if the beat changes, then no software will be able to find a reliable number. In spot tests, I found that soundstretch is much faster, while bpmcount is more reliable and accurate. Both of them don’t admit to failing and give a wrong number if they couldn’t find a beat (180 in the case of bpmcount).

To use them, then, you have to first unpack/decode the file you have at hand to WAV, perform the beat detection, and then store the BPM count back into the file. Both OGG and MP3 have a standard field where to store BPM information, so that’s not an issue.

I wrote a script that takes a number of input files, determines their beat, adjusts gain, cleans up tags, and removes silence from the beginning and end, using a number of utilities readily available. Download link here.