There are many scales with different keys for the same notes.

For example, with the notes C D E F G A B C, we can have: C major, C Ionian, A natural minor, D Dorian, E Phrygian, F Lydian, G Mixolydian, A Aeolian, B Locrian.

I understand that music in these scales have different tonal centers and sound different. I can detect a key by ear or while looking at song notes in my DAW.

But I want to categorize bunch of MIDI files by scales and keys. So i need to find a way to do it without hearing music or viewing notes/sheets.

Maybe some methods are exist? For example statistics of most frequently used note, or note with which most of bars are started, most frequent base of chord etc?

  • 8
    Some pieces are polytonal (or at least bitonal), which throws the biggest wrench ever into determining which key a piece (or even a phrase) is--the music is simultaneously in two or more keys at the same time. Do we have methods good enough to detect polytonality?
    – Dekkadeci
    Commented Jan 2, 2018 at 1:44
  • 1
    Is it really worth the effort? Between modulation, modal vs tonal approach, chords split over different tracks...you may get really poor results. How many files do you need to go through? Are they songs from well known interpreters or personal files?
    – moonwave99
    Commented Jan 2, 2018 at 12:50
  • 2
    I have over 50000 midi files scraped from free resources, the files will be used for machine learning purposes, but I do not plan detect key using NN, but detect a key using some hardcoded algorithm and pass it to NN as one of parameters Commented Jan 2, 2018 at 13:15
  • 2
    Possibly relevant answer on Stack Overflow.
    – nekomatic
    Commented Jan 2, 2018 at 14:30
  • 3
    There are algorithms that do this! A lot of DJ software can automatically detect which key a song is written in. However, this software isn't perfect and will occasionally misidentify a song's key. (The music used in dances is rarely unusual enough to trip these algorithms up, though, so in practice, it isn't a serious issue.) Unfortunately, I'm not sure how these algorithms work, I just know they exist.
    – Kevin
    Commented Jan 2, 2018 at 19:11

6 Answers 6


There's no such thing as a 100% sure identification of what 'the key' of a song is if you aren't taking notation as your reference - sometimes different people hear the same song as being in different keys. But if you have MIDI files, you have most of the same information that someone listening to a song would do - i.e. you have all the notes - so yes, you can make a fairly good go at finding out the key.

Basically all you need to do is work out how you'd work out the key of a piece by ear, and then turn that into an algorithm. How can you find the key of a song by ear? Mentions some relevant information, and you may find other useful techniques for finding the key of the song by ear around this site and the web.

As well as the Krumhansl-Schmuckler algorithm mentioned by Dom, if you're a coder (I see that you have a stack overflow account), this could be a great problem to solve using machine learning, assuming you have a suitable training set of MIDI files with the key already correctly identified. You'd probably want to distil down the data in the song - for example, you could do an analysis that worked out

  • how many times each note of the octave occurs in the piece
  • how many times each note is playing on the strong beats
  • the average midi velocity of each note

...and then pass that in as the inputs to a suitable neural network and training algorithm. You could also consider the algorithm that Dom mentioned when deciding how to slice up the data. You might only end up with a trained NN that generates similar answers to to the Krumhansl-Schmuckler algorithm though!

You might also want to consider how you're going to deal with pieces that identifiably change key over the course of their development.

One more thing - MIDI does have the concept of a Key Signature meta message. This might not often be present or reliable but it could be another data point.

  • 1
    There are lots of other possibly relevant data points to collect. For each note, what one or two or three notes or intervals preceded it? For example, if a file has more than one descending fifths to the same note, we could conclude there is a cadence there and the second note of the descending fifth is possibly the key note. That might help with modulations, especially if you add proximity to other similar intervals. So if half the piece has a few G-C intervals and the other half has D-G intervals and D major chords, you can likely conclude modulation between C and G major. Commented Jan 2, 2018 at 16:44
  • 2
    @ToddWilcox definitely true. With some machine learning models it can be better to throw more data in to the inputs and let the training process work out what's useful. With some types of models you might even to be able to slap all the note data in - I can see some parallels between that approach and the usage of Convolutional Neural Networks as used to recognise patterns in image data. Commented Jan 2, 2018 at 16:49
  • @topomorto yeah, I would throw a RNN at it, with (relatively) raw MIDI as input and a vector of probabilities of keys as output, and let it figure the rest out. Feature engineering is so last decade :) In fact I am tempted to try that, if I could find a suitable dataset.
    – hobbs
    Commented Jan 2, 2018 at 21:21

Seeing the spectrum analysis and frequency of notes alone typically are not enough to figure out the key. You could sometimes get it right for simpler songs, but any type of blues or chromaticism will completely screw with it. Scales used also don't tell you much about the key especially when talking about modes. The reason why is the idea of a key has a tonal center and a typical harmony, but the harmony can be fluid (borrowing, Picardy thirds, ect) and there might be trips to other tonal centers along the way (secondary dominants, modulation, ect).

The other thing to note is midi data is not good for music theory. In this case, because you cannot tell the difference between enharmonic equivalents, you will lose important information that is useful for determining key. Musicxml files are much better at retaining useful music theory data.

One algorithm I know is the Krumhansl-Schmuckler key determination algorithm which is implemented by music21. The Krumhansl-Schmuckler key determination algorithm is based on Bayes' theorem which is statistical determination and while not always accurate is very close.


over 95% of the midi files will be major and minor scale. find the 7 most used notes, look for flatted 3rd. pretty solid way to do it.

not perfect. but, plenty good enough.

  • Right on. Musicians, don't most molodies/songs simply finish/resolve on the note of the key? That would probably be an 80% solution to the issue.
    – Fattie
    Commented Jan 2, 2018 at 16:56
  • Yes, most of the time a piece will end on the tonic. But I think OP wants a foolproof method. Perhaps the algorithm could be weighted with that info.
    – Eric O
    Commented Jan 2, 2018 at 18:03
  • also, keep in mind that the key can change during the piece. called "modulation". having a computer analyze music can often be useless. just analyze the music yourself. you shouldn't care about EVERY midi file out there. Just the ones you're going to bother to play. Which is ONE of the reasons I prefer pop. Everybody likes it. Not too complex. Sounds good. Commented Jan 3, 2018 at 2:36

The problem of key identification isn't necessarily well defined. For music where "key" is important (or else the OP isn't relevant), the cadence in the final key generally determines the key. The final chord is the goal of previous chord progressions (unless the piece just wanders.) The Picardy Third adds a bit of confusion (or perhaps signals a more modal organization.)

Another problem is the question of what a song consists of. Given a simple verse+chorus, the key of the verse and the chorus differ. Another type of song structure is the "rondo" type (like marches and polkas). "Beer Barrel Polks" starts in C (I) and has a trio ending in F (IV). So the term key really ought to apply to parts of a piece (which begs the definition of "part" for that matter.)


The Krumhansl algo is based on perception tests, and is especially suited to diatonic contexts (Ionian mode if you prefer). There are actually two questions in your question. the first is determining the pitch-class set, the second is determining which of the (usually seven) modes is the best candidate. For the first question you could use some recent stuff using Discrete Fourier Transform of the sets of notes, easy to extract from mid files, and look at the directions in the complex plane of the relevant Fourier coefficients (3d and 5th). I do not know if it is worth buying my book Music through Fourier Space but the subject is definitely tackled in Chap. 6 :) The analysis of modes is trickier (not that the first question is easy…), I would try some statistics on the chords used (especially triads). Say if the notes used are CDEFGAB but the most frequents chords are DFA and ACE I suspect Dorian. But as mentioned above, there will usually be some ambiguity which is of no little importance in the charm of the music.

  • 1
    Why would you synthesize music from the midi file and then fourier analyze it? That would be like doing something and then undoing it immediately. The midi file already has the pitches.
    – user9480
    Commented Jan 3, 2018 at 17:01

This may not be an answer in the strictest sense, but I think I can point you in the direction of a software resource published as a tool for answering questions like this by an organization whose mission statement is an academic investigation of the subject.

Check out "Sonic Visualizer" and the "Aubio" music annotator software packages published by Queen Mary School of Digital Music, a school located in London, England.

This is a link to their software downloads page

Sonic Visualizer is designed for plugin extensibility. One of the extension interfaces, VAMP, is often used to implement plugins in Python. Queen Mary has a curated set of VAMP plugins that they maintain as a bundle package. There is a plugin in that package entitled "Key Detection" that you may want to give a try.

I can't speak much for whether or not this is going to fit your needs unfortunately--I've heard of this software enough times to think of it when I saw your question, but I haven't a whole lot of experience with using it for this task.

Last time I used it I did so for tempo-detection and song structure segmenting plugins. It did a decent job at those tasks--not fully automatic, but enough of a head start to make it possible to achieve my goal in less time than a pure manual start would have taken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.