Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an algorithm to get the Scale and Key of a song from a series of notes? [closed]

From a series of MIDI notes stored in array (with MIDI note number), does an algorithm exist to get the most likely key or scale implied by these notes?

like image 465
Amr Hesham Avatar asked Feb 06 '13 17:02

Amr Hesham


3 Answers

If you're using Python you can use the music21 toolkit to do this:

import music21
score = music21.converter.parse('filename.mid')
key = score.analyze('key')
print(key.tonic.name, key.mode)

if you care about specific algorithms for key finding, you can use them instead of the generic "key":

key1 = score.analyze('Krumhansl')
key2 = score.analyze('AardenEssen')

etc. Any of these methods will work for chords also.

(Disclaimer: music21 is my project, so of course I have a vested interest in promoting it; but you can look at the music21.analysis.discrete module to take ideas from there for other projects/languages. If you have a MIDI parser, the Krumhansl algorithm is not hard to implement).

like image 57
Michael Scott Asato Cuthbert Avatar answered Nov 12 '22 23:11

Michael Scott Asato Cuthbert


The algorithm by Carol Krumhansl is the best-known. The basic idea is very straightforward. A reference sample of pitches are drawn from music in a known key, and transposed to the other 11 keys. Major and minor keys must be handled separately. Then a sample of pitches are drawn from the music in an unknown key. This yields a 12-component pitch vector for each of 24 reference samples and one unknown sample, something like:

[ I,        I#,      II,       II#     III,       IV,      IV#,   V,     V#,    VI,     VI#,   VII    ]
[ 0.30,  0.02,  0.10,  0.05, 0.25,  0.20,  0.03, 0.30,  0.05,  0.13, 0.10  0.15]

Compute the correlation coefficient between the unknown pitch vector and each reference pitch vector and choose the best match.

Craig Sapp has written (copyrighted) code, available at http://sig.sapp.org/doc/examples/humextra/keycor/

David Temperley and Daniel Sleator developed a different, more difficult algorithm as part of their (copyrighted) Melisma package, available at http://www.link.cs.cmu.edu/music-analysis/ftp-contents.html

A (free) Matlab version of the Krumhansl algorithm is available from T. Eerola and P. Toiviainen in their Midi Toolbox: https://www.jyu.fi/hum/laitokset/musiikki/en/research/coe/materials/miditoolbox

like image 24
andy_a Avatar answered Nov 12 '22 23:11

andy_a


There are a number of key finding algorithms around, in particular the ones of Carol Krumhansl (most papers that I've seen always cite Krumhansl's methods)

like image 3
the_mandrill Avatar answered Nov 12 '22 22:11

the_mandrill