The guide
A Song Has More Than One Honest Tempo, and a Good Detector Shows You Its Doubts
Measuring beats per minute from a recording means finding where sounds begin, hunting for the period that repeats, and then admitting that half or double that number is often just as defensible.
- 4 ms step between frames of the onset envelope in the published tracker
- 120 BPM centre of the tempo weighting, taken from how listeners tap
- 0.12 BPM error from rounding a 127 BPM period to whole 4 ms steps
- 118.0 BPM first 8 s of the example loop, after the parabola refines the peak
- 10 percent tempo drift the published tracker held onto with a fixed target
Read the full guide Fold the guide
Ask three drummers for the tempo of the same record and you can get three answers that are all correct. One counts the kick and snare and says 63. Another follows the hi-hat and says 126. A third, used to a slow feel, says the song moves in long bars and hears 42. Nobody misheard anything. They chose different levels of the same rhythm.
That is the first thing to understand about measuring beats per minute from audio. Tempo is defined as the speed or pace of a piece, counted in beats per minute, and performers often bend it on purpose, a practice called rubato. Both halves of that definition cause trouble for a machine. Which pulse counts as the beat is partly a choice, and the speed may not hold still long enough to be a single number.
A detector that prints one figure with no qualification is hiding its doubts. A better one shows the runner-up answers, says how sure it is, and lets you check the result against your own ears.
Finding where sounds begin
Software that detects the beat has to start from something simpler than the beat: the moments when new sounds begin. An onset is the beginning of a musical note or other sound. It is related to a transient but not the same thing, since every note has an onset while not every note starts with a sharp burst of energy.
The obvious approach is to watch the loudness and mark every jump. It works badly. Detecting increases in plain amplitude tends to produce too many false alarms or too many misses, because a held chord can swell without a new note while a soft high note can enter under a loud low one without raising the total much.
Looking at the spectrum helps. The short-time Fourier transform cuts the signal into short overlapping segments, shapes each with a window function to control how energy leaks between frequencies at the cut edges, and measures the frequency content of each slice. Spectral flux then compares each slice with the one before it. For onsets, only increases matter, so many methods keep the bands where energy rose and ignore the ones where it fell.
The method this tool follows comes from Daniel Ellis’s 2007 paper on beat tracking by dynamic programming. The audio is resampled to 8 kHz and cut into 32 ms windows that advance by 4 ms, so the result has 250 values per second. The spectrum of each window is gathered into 40 bands on the Mel scale, a scale of pitches that listeners judge to be equally spaced, usually computed as 2595 times the base 10 logarithm of one plus the frequency over 700 Hz. Each band is converted to decibels.
Decibels matter here. A violin entering softly above a loud piano barely changes the total energy, but in its own bands the level can rise many times over, and a logarithmic scale counts that rise in full. Taking the frame-to-frame difference in every band, setting the negative changes to zero, and adding up what is left gives a single curve that rises wherever something new starts anywhere in the spectrum. The paper then removes slow drift with a high-pass filter near 0.4 Hz, smooths with a Gaussian about 20 ms wide, and divides by the standard deviation so loud and quiet recordings land on the same scale.
The result, the onset strength envelope, is what every later step reads. It no longer looks like music. It looks like a row of spikes of uneven height, and the job is to find the rhythm in them.
Autocorrelation and the octave problem
Autocorrelation is a standard tool for finding a period hidden under noise. Shift a series against a copy of itself, multiply the overlapping values, and add them up. The NIST statistics handbook defines the normalised version for lag k as the sum of the products of deviations from the mean, k steps apart, divided by the sum of the squared deviations. When the shift equals the time between beats, spikes line up with spikes and the sum is large.
So far this sounds decisive, and it is not. If spikes repeat every half second, they also repeat every second and every second and a half. A periodic envelope has peaks at every whole multiple of its period, and those later peaks can be nearly as tall as the first. The hi-hat playing twice per beat adds another peak at half the period. The autocorrelation rarely points at one answer. It offers a family of related answers, and choosing among them is exactly the disagreement between the three drummers.
Ellis handled this with a fact about listeners. When people tap along to music, their choices cluster around 120 BPM, so the paper weights the autocorrelation with a bell curve on a logarithmic time axis centred on half a second. In his test data, listeners tapping the same excerpt split into two groups whose tempos differed by a factor of two or three. A refined version of the method therefore sums the evidence at a period and at two or three times it to find that pair of levels, then ranks the two by the weighted autocorrelation at each, so the bell curve decides which one is named first.
This tool shows that reasoning instead of hiding it. It scores the best tempo and its half, double, two-thirds and three-halves relatives, names the other level of the pair as the alternative, draws the weighting curve, and lets you switch the weighting off.
On 49 steady synthetic test files, 40 came back within 0.1 BPM. The other 9 landed on the other level of the pair: pulses at 160 to 200 BPM were reported at half speed, and drum patterns at 60 and 65 BPM with even hi-hats were reported at double. That is an octave error by the strict count, and it is exactly where a weighting centred on 120 BPM pushes. An independent implementation of the same method chose the same level on every one of those 9 files. Many listeners would tap the same way.
Precision raises a quieter problem. With frames 4 ms apart, the period itself is measured in whole steps. A 127 BPM pulse repeats every 0.4724 s, which is 118.11 steps. Round that to 118 and the answer becomes 127.12 BPM, an error of 0.12 BPM that no amount of music can remove. Fitting a parabola through the peak and its two neighbours finds the summit between the steps. In the tool’s tests the same 127 BPM click returns 127.00 with the parabola and 127.12 without it.
Tracking beats, not counting them
A tempo is a single number. A beat is a time you can point at, and most uses of a tempo measurement really want the beats: a cut on the downbeat, a sample lined up with the groove, a loop trimmed to whole bars.
Ellis’s insight was that beat tracking fits dynamic programming neatly. Each candidate beat time earns points for landing on a strong onset and loses points when the gap from the previous beat strays from the target period, with the penalty growing with the square of the logarithm of the ratio. Because the best path to any moment depends only on the best paths to earlier moments, the search can try every possible beat sequence in a single pass through the envelope. A weight called alpha balances the two goals. The paper used 400 in its tempo experiments, and found that 680 scored best on its 20 training excerpts.
The method has honest limits, and the paper states them. With a fixed target tempo it held onto the correct beats when the true tempo differed by up to about 10 percent, and lost them beyond that. It also fills in beats across silence, which is right for a rest inside a song and wrong for the quiet before the band comes in. The paper suggests watching how fast the score grows to find where the music really starts and stops.
This tool adds two things from that discussion. It measures tempo in 8 second windows every 2 seconds and draws them as a line, so a song that rushes into its chorus shows a slope instead of an average. And its beat grid can follow that local tempo, the remedy the paper itself proposes for music that drifts, at the cost of the strict guarantee that the result is the best possible path.
The built-in example shows why the windows matter. It is a drum loop made in code that plays at 118 BPM for ten seconds, pushes to 126 by twenty-two seconds, and holds there. Analysed as a whole it reads 122.5 BPM, a number that is true of no single bar. The windows rise from 118.0 to 126.0. Set the analysis range to the first 8 seconds and the tool reports 118.0 BPM: the autocorrelation peak sits at 127.13 steps of 4 ms, where the nearest whole step alone would have given 118.11.
Checking by ear
Every number above is an estimate. Music played with rubato, pieces with no percussion, songs that change meter, and recordings where the strongest pulse sits on the offbeat can all mislead an onset envelope. Swing changes the picture too. In swing rhythm, pairs of notes alternate long and short, with ratios running from nearly even to about two to one and beyond, so the notes between the beats no longer split them in half.
The fastest way to catch a wrong answer is to listen. A click track is a series of audio cues used to keep recordings in sync, and it doubles as a metronome laid over the music. Playing short tones at the tracked beat times, on the same audio clock as the recording, turns a column of numbers into something you can judge in seconds. The Web Audio API makes that possible in a browser: a decoded sound plays from a buffer source started at a precise time on the context’s clock, and each click can be scheduled against that same clock.
If the clicks lock to the snare, the measurement is good. If they march steadily but sit between the hits, the grid has the right tempo and the wrong phase. If they gallop at twice the speed of your foot, choose the half-tempo candidate and trust your foot. Tapping along gives a second opinion with numbers attached: the mean gap between your taps, its spread, and which candidate it matches.
A detector worth using does not pretend the question has one answer. It shows the candidates, says how sure it is, lets the tempo move when the music moves, and hands the final call back to the listener.
Sources
Every source consulted for this guide. Links checked October 30, 2026.
- Beat Tracking by Dynamic Programming Daniel P. W. Ellis, LabROSA, Columbia University (author's copy of Journal of New Music Research 36(1), 2007)
- 1.3.5.12. Autocorrelation NIST/SEMATECH e-Handbook of Statistical Methods
- Web Audio API (W3C Recommendation, 17 June 2021) World Wide Web Consortium
- BaseAudioContext: decodeAudioData() method MDN Web Docs
- AudioBufferSourceNode MDN Web Docs
- BaseAudioContext: currentTime property MDN Web Docs
- Tempo - Wikipedia Wikipedia
- Beat detection Wikipedia
- Onset (audio) Wikipedia
- Spectral flux Wikipedia
- Autocorrelation Wikipedia
- Short-time Fourier transform Wikipedia
- Window function Wikipedia
- Mel scale Wikipedia
- Swing time Wikipedia
- Click track Wikipedia