Step 1: turn sound into “where do new notes start?”
A tempo is the rate of the beat, and a beat is something you hear as a series of events: a kick drum, a strummed chord, a hand clap. So the first job is to find events, not to look at the raw waveform. The waveform of a mixed song is far too busy: sustained bass notes, reverb tails and vocals all wiggle the signal without starting anything new.
The standard tool is an onset strength envelope. The audio is cut into short overlapping windows — here 512 samples at about 22 kHz, roughly 23 ms each, moved forward by about 6 ms at a time, giving around 172 measurements per second. Each window is turned into a spectrum with a fast Fourier transform, and the loudness of every frequency band is put on a logarithmic scale, because our ears hear loudness that way.
Then comes the key idea, spectral flux: for each band, compare this window with the previous one and keep only the increases. Add the increases up across all bands and you have a single number that is large when something new starts and close to zero when sound is merely continuing or fading. A kick drum lights up the low bands, a hi-hat the high bands, a piano chord many bands at once — all of them register as onsets.
Finally the slowly changing part is subtracted (a moving average over a quarter of a second) so that a song getting louder doesn’t look like one long onset, and the result is normalised. The picture below shows the envelope of our 124 BPM demo loop: tall spikes for kicks and snares, small ones for hi-hats.
Step 2: find the period that repeats
A tempo is a repetition, and the classic way to measure repetition is autocorrelation. Take the onset envelope, slide a copy of it along by some delay (the “lag”), multiply the two point by point and add everything up. When the lag equals one beat, kicks line up with kicks and snares with snares, so the sum is large. When the lag is a beat and a bit, spikes land on gaps and the sum is small.
Plotting that sum for every lag from about 0.1 to 6 seconds gives a curve with peaks at one beat, two beats, four beats and so on. Converting lag to tempo is simple: BPM = 60 ÷ lag in seconds. A peak at 0.484 s means 124 BPM.
Step 3: choose the right octave
Here is the catch that every tempo detector has to deal with. The autocorrelation of a 124 BPM song also peaks at 62 BPM (every two beats — the kick-snare pattern repeats there) and often at 248 BPM (eighth-note hi-hats). Mathematically, all three are valid periodicities. Musically, only one is the tempo people would tap.
The detector handles this in three ways. First, each candidate tempo is scored not only by the correlation at one beat but also at two beats and at four beats (a bar), with weights 1, ½ and ¼, which rewards grids that keep lining up with the bar. Second, the score is multiplied by a gentle preference for tempos near 128 BPM, the geometric middle of the 90–180 BPM octave where most music is felt; studies of tapping behaviour find a similar preference in listeners. Third, if a rival that is not an octave away — two-thirds or one-and-a-half times the winner — scores almost as well, both beat grids are laid over the track and the one that lands on stronger onsets wins. That last check stops a two-step drum & bass beat being reported as 116 instead of 174 or 87.
Even so, the octave is a judgement, not a measurement, which is why the results show the top candidates and half/double buttons. Our guide to half-time and double-time BPM explains when each reading is “right”.
Step 4: refine to a fraction of a BPM with a comb
Autocorrelation works on whole frames of about 6 ms, which limits precision: at 128 BPM, one frame of lag is worth about 1.7 BPM. To get closer, the detector does a fine search with a comb: for each tempo within 1.5 BPM of the estimate, in steps of 0.02 BPM, and for every possible starting point, it lays a grid of evenly spaced “teeth” over the onset envelope and averages the onset strength under the teeth. The tempo and phase with the highest average win. Over a three-minute track, a tempo that is wrong by just 0.1 BPM drifts more than a tenth of a beat off the music by the end, so the comb pins the tempo down very precisely — our test suite checks 90, 120, 128 and 174 BPM patterns to within ±1 BPM, and in practice they land within 0.05.
The winning phase gives the beat grid: the time of the first beat and every beat after it. Clicking the waveform plays the music with a metronome click on that grid, which is the quickest way to check the result by ear.
Step 5: check the tempo over time
One number is a summary. To see whether it is a fair one, the detector repeats the comb search on overlapping 8-second windows every 2 seconds, allowing ±12 % around the overall tempo so that octave flips don’t show up as fake drift. A programmed track gives a flat line; a band playing live wanders up and down by a few BPM, and a DJ mix shows steps where tracks change. The chart and its summary sentence tell you which you have. For more on this, see finding the tempo of live recordings.
Confidence: how sure is it?
The confidence percentage combines two things. One is how far the winning tempo stands out from the rest of the tempo curve: a sharp, isolated peak is convincing, a broad hump is not. The other is how hard the final beat grid hits the onsets compared with a random grid. Steady electronic music scores 90 % or more; a solo voice or a rubato piano piece may score under 50 %, which is a hint to use the tap button instead.
What it can’t do
Automatic detection assumes a steady pulse. It will give an average for music that changes tempo deliberately (a ritardando, a classical piece with tempo changes), and it can’t know your intent when a track is ambiguous — a 6/8 ballad might be felt at 60 or 180. It also cannot tell the time signature from tempo alone; see BPM vs time signature. When in doubt, listen to the click grid: your ears are the final judge.