Musical Key Detection

Machine Learning
Introduced by
Emilia Gómez
Year
2006
Type
Tonal center estimation
Family
Tonal analysis

What does it do?

Musical key detection estimates the key (tonal centre) and mode (major or minor) of a piece of music from its audio.

The key identifies the tonal centre around which the piece's harmony and melody are organised. The reference this page gives for the task is Gómez (2006), on tonal description of polyphonic audio.

The intuition behind the method

Music tends to return to a tonal centre, and listeners sense this through which pitches recur and where phrases come to rest. An audio-based estimator approximates this by summarising the audio as a pitch-class profile: a 12-bin chromagram with one bin per pitch class, accumulated over the whole piece or over windows. With 12 major and 12 minor keys there are 24 candidate answers. The profile is compared with a template for each key, such as the Krumhansl-Kessler key profiles (listener ratings of how well each pitch class fits a major or a minor key), or with a classifier trained on labelled examples. The key whose template fits best is returned.

History

Work on key detection from audio in the mid-2000s includes the tonal description of polyphonic audio, which this page takes as its reference for the task: Gómez (2006). Noland and Sandler (2007) addresses signal-processing parameters for tonality estimation.

When to use it

Choose key detection when the question concerns the tonal organisation of a piece: transposition, harmonic analysis, or teaching key relationships. It fits recordings with a clear, stable tonal centre within a major/minor framework, such as much common-practice and popular music. Do not use it for atonal music, or for modal and non-Western music whose pitch organisation differs from the major/minor templates. It is also unsuited to pieces that modulate frequently when a single global key is required, and to percussion-only material, which has little pitch content to profile.

Limitations and considerations

  • The method assumes that the piece has one tonal centre. Pieces that modulate or stay tonally ambiguous yield a global key that may fit no section well.
  • Relative major and minor keys share the same seven diatonic pitch classes, so the mode is often the hardest decision to make from pitch content alone.
  • Templates and training data encode the tonal norms they were built from; non-Western or modal music may match them poorly.
  • The notated key signature may differ from the perceived key, so the reliability of the ground truth depends on the annotation convention used.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Gives a single, interpretable tonal label that connects directly to standard music theory vocabulary.
  • Operates on a pitch-class profile, so the estimate is largely independent of octave and of the specific instrument playing each pitch.
  • Supports transposition and key-aware comparison of pieces, because the estimated key can be used to shift material to a common tonic.
  • Template-based versions need no annotated training data, since they rely on fixed key profiles.

Applications

  • Transposing music to a common key for performance or to suit a singer's vocal range.
  • Music education: teaching key relationships and harmonic function.
  • Musicological analysis of tonal structure across a corpus of pieces.
  • Pre-processing for chord recognition and harmonic analysis, which depend on the local tonal context.
  • Comparing or grouping recordings by tonal centre in music recommendation or playlist tools.

How it works

How it works

1

Compute a 12-bin chromagram from a constant-Q transform or mel-spectrogram of the whole piece, or of short windows if key changes are to be tracked. Averaging over the entire piece gives one global profile, while windowed chroma gives a time series of profiles.

2

Templates for the 24 major and minor keys are either taken from music-theoretic profiles, such as the Krumhansl-Kessler key profiles (listener ratings of how well each pitch class fits a major or a minor key), with each profile rotated to its tonic, or learned from annotated audio. Learned approaches train a classifier that maps chroma features to key labels, so the training data determines which keys and styles the model can recognise.

3

Compare the chromagram, averaged or weighted over time, with each key template using a correlation or a classifier score, and output the key with the best match. In template matching, the template for each of the 24 keys is the 12-value profile rotated so that its tonic falls on the candidate tonic pitch class. Relative major and minor keys use the same pitch classes, so the decision between them depends on how the profile weights the pitches that distinguish the two tonal centres.

4

Measure accuracy as the fraction of pieces whose predicted key (tonic and mode) matches the ground-truth annotation. Separating errors into confusions between relative major and minor keys and errors on a different tonic shows more than an exact-match score alone.

Common pitfalls

  • Confusing key with chord. A piece in C major has C as its key, but its individual chords can be any chord in any inversion. The key is the long-term tonal centre; chords are local harmony.
  • Ignoring relative major/minor relationships. C major and A minor share the same pitch classes but have different tonal centres, and distinguishing them requires cues beyond the pitch-class histogram.
  • Assuming the key is fixed. Many pieces modulate, so detection should ideally track key changes rather than report only a global key.
  • Comparing results across studies without checking which ground-truth convention and evaluation metric were used.

Frequently asked questions

What is the difference between key and key signature?

Key signature is a notational convention (sharps or flats at the start of a staff) indicating the key. However, music may modulate (change key) despite a constant key signature, or a key signature may be misleading about the actual perceived key.

Can key detection work on music that modulates?

Standard approaches assume a single global key. Tracking key changes requires dynamic programming or per-frame inference. Many systems output the global key even for modulating pieces, which can make the result inaccurate.

Why is distinguishing major from minor challenging?

C major and A minor (relative major and minor) share the same seven diatonic pitch classes: C, D, E, F, G, A and B. Pitch-class distribution alone therefore does not separate them; harmonic function and structural cues are needed.

How does key detection work on atonal or modal music?

Poorly. Key detection assumes Western tonality (major and minor scales). Atonal music, which has no tonal centre, and modal music, which uses different scale structures, need alternative approaches or specialist models.

Related methods

Sources

  1. Gómez, E. (2006). Tonal description of polyphonic audio for music content processing. In INESC Porto PhD Thesis.

    Open publication reference
  2. Noland, K., & Sandler, M. (2007). Signal processing parameters for tonality estimation. In Proceedings of the International Symposium on Music Information Retrieval.

    Open publication reference
  3. Khadkevich, M., & Omologo, M. (2011). Community structure in networks of classical music composers. Journal of Cultural Economics, 35(4), 307-319.

    Open publication reference