Skip to content
Farshad Jafari edited this page Jul 8, 2026 · 3 revisions

Estimating musical key

This app implements the Krumhansl-Schmuckler key-finding algorithm. It estimates the key of a composition by computing a duration-weighted pitch-class distribution and comparing that distribution with stored major and minor key profiles.

Two top-level functions are provided:

  • key-finding:find-key: estimate the key of a single composition
  • key-finding:find-keys: estimate the keys of all compositions in a dataset and compare the estimates with the keys stored in the database

The function key-finding:find-key takes a dataset-id and a composition-id. These are positional arguments and should be supplied in that order. It returns a list of four values:

  • keysig: the estimated key signature
  • mode: 0 for major or 9 for minor
  • tonic: the estimated tonic as a pitch class
  • coefficient: the correlation coefficient for the best-fitting key profile

The optional :method parameter determines which set of key profiles is used. The default is :temperley. If a value other than :temperley is supplied, the Krumhansl-Kessler profiles are used.

The following is an example:

This estimates the key of composition 0 in dataset 0 and returns the estimated key signature, mode, tonic and correlation coefficient.

CL-USER> (key-finding:find-key 0 0)

The function key-finding:find-keys applies the same procedure to every composition in a dataset. It prints any compositions whose estimated key does not match the key stored in the database, and then prints an overall score.

Useful optional parameters are:

  • method: the key profile set to use (default :temperley)
  • update-db?: if t, update the key signature and mode stored in the database; if nil (the default), only report the estimates

For example:

This processes every composition in dataset 0 using the Temperley profiles, prints any disagreements with the stored keys, and then prints an overall score.

CL-USER> (key-finding:find-keys 0 :method :temperley)

Clone this wiki locally