How key detection works
A key detector takes the song's spectrogram and computes a chromagram — a 12-dimensional vector that says how much of each pitch class (C, C♯, D, ..., B) is present in the music.
The 12 values are summed over the whole track (or weighted by harmonic salience). The resulting vector is matched against 24 templates — one for every major and minor key — and the closest match wins.
More sophisticated detectors weight by section (verse vs chorus often share a key but bridges sometimes modulate), filter out percussion, and use machine learning to refine the templates.
How accurate is it?
Modern key detectors reach 80-90% top-1 accuracy on the MIREX academic benchmark, and 95% top-2 accuracy (i.e. the correct key is in the top two guesses).
Common error modes: confusing relative minor / major (C major and A minor share all 7 notes), reporting the wrong tonic when the song spends a lot of time on a chord that isn't the tonic.
Modal music (Dorian, Phrygian) and music in non-Western tunings get approximated to the closest major or minor.
Why people detect keys
Key detection is one of the most-used features of any music analysis tool. Common uses:
- DJs — building harmonic sets via the Camelot Wheel.
- Producers — finding sample loops that fit a track's key.
- Singers — transposing a song to fit their vocal range.
- Music teachers — annotating pieces for students.
- Sync editors — matching a music cue to a scene's emotional tone.
- Music streamers — building "songs in C minor" recommendation playlists.
Verifying a detected key
If you suspect a detected key is wrong, the simplest verification is to play the root note over the song. If the song is in C major, holding C while the song plays should sound consonant — no clash, no tension.
Try the relative key as well. C major's relative minor is A minor. If C feels off but A feels right, the song is probably in A minor instead.
Frequently asked
Why do two key detectors sometimes disagree?
Most often relative-key confusion (C major vs A minor — same notes, different tonic). One algorithm picks one, the other picks the other. Both are "reasonable" given the data.
Can a song be in two keys?
Yes — many songs modulate (the bridge of Bon Jovi's "Living on a Prayer" is famous for it). Detectors usually report the dominant key across the song.
Does key detection work on instrumentals?
Yes — and often better than full mixes, because there's no vocal sliding around tonally.