Music Tech3 min read

How AI Detects Key and Tempo in Audio

The signal processing behind automatic key and BPM detection — onset detection, chroma features, and why detection fails on some tracks.

Automatic key and tempo detection is one of the most useful things in a modern DAW, and understanding how it works tells you when to distrust it.

Tempo detection

Step 1: onset detection. The audio is analysed for sudden increases in energy — the moments where a drum hits or a note starts. This produces an "onset strength" curve over time, spiking where events occur.

Step 2: find periodicity. The system looks for repeating intervals in that curve, usually via autocorrelation. If onsets cluster every 0.5 seconds, that suggests 120 BPM.

Step 3: pick the right multiple. This is where it goes wrong. A pattern at 0.5s intervals is consistent with 120 BPM, or 60 BPM (every other onset), or 240 BPM. Systems use heuristics — most music sits between 70 and 180 BPM — but half-time and double-time errors are the classic failure, especially in drum and bass or slow hip-hop.

Step 4: track drift. Live performances speed up and slow down. Better systems produce a tempo map rather than a single number.

Fails on: rubato and freely-played material, sparse ambient tracks with no clear onsets, and heavily swung rhythms.

Key detection

Step 1: chroma features. The audio is transformed into frequency content, then folded into 12 pitch classes — all the C's across every octave sum into one "C" value. The result, a chromagram, shows how much of each of the 12 notes is present over time.

Step 2: match against profiles. The averaged chroma vector is compared with template profiles for each of the 24 keys. These profiles come from music theory and from analysing large corpora — in C major, C, G, and E should be prominent.

Step 3: pick the best match. Highest correlation wins.

Fails on: relative major/minor (A minor and C major contain identical notes — only context distinguishes them, and averaged chroma discards context), modulating songs, heavily distorted material where harmonics swamp fundamentals, and atonal or percussion-only tracks.

Why it matters practically

Detection is the foundation for a lot of useful automation: fitting a generated bassline to your track's key, beat-matching, warping, and suggesting harmonically compatible material.

When a tool like Veena generates a part that fits your project, key and tempo detection on the existing audio is the first step — which is why it works better on a clean, clearly-tonal recording than on a distorted, rubato one.

Always sanity-check. If the detected key sounds wrong, try the relative major or minor first — that's the error 80% of the time.

Related reading: how stem separation works, tempo and groove, and how to change the key of a song.

Frequently asked questions

How does software detect the BPM of a song?

It finds onsets — sudden increases in energy that mark note or drum hits — then looks for periodicity in their spacing. The interval that best explains the pattern becomes the tempo estimate.

Why does key detection get it wrong?

The most common errors are relative major and minor confusion, because they share the same notes, and modulating songs, where a single key label cannot describe the whole track. Heavy distortion and atonal material also degrade detection.

Can key detection handle a song that changes key?

Basic detection reports one key for the whole track and will simply be wrong for part of it. Better implementations analyse in windows and report key changes over time.

Start making music in Veena

Free, browser-based, no downloads required.

Try Veena Free