Is there way to do it with programming? In theory, or may be is there some programming library written in java (perfect variant)?


3 Answers 3


The simple answer is yes. Applying the rules of music theory, you can certainly program something to do this.

The problem is that not all music follows conventional patterns and there is sometimes debate over if a voicing is actually one chord or another. This could change the key of the song and affect the way your program interprets the music. The amount of exception logic required to do this would suck. You basically need to programmatically identify the tonic and work from there. It would not be easy to do for sure.

Is it possible, certainly. Does it already exist, I haven't seen a library that can do it. Is it worth it, probably not. Most people interested would rather do the analysis themselves anyway.

I studied classical guitar at university for 2 years before withdrawing and completing my studies in computer science. I have contemplated making a program/library like this, but as I said, the people who would actually use this info, actually enjoy analyzing the music.

  • Do you think someone would enjoy analyzing, say, 10 000 songs? :) For a database of musical material, automatic analysis is the only practical way. For audio material, DJs use automatic tempo and key analysis to find suitable song candidates. There's commercial software people pay money for, and there's also high-quality free open-source software. Check out KeyFinder ibrahimshaath.co.uk/keyfinder Jan 10, 2019 at 20:02

Unfortunately, even when a song has a clear tonal centre and could be usefully described as being 'in the key of x' it very likely won't confine itself to the diatonic notes and chords of that key. A key-detection program would need a long list of special cases and exceptions.

For instance. This is indisputedly in D major. Despite the C#7 chords and Fnat notes.

enter image description here

And this is firmly in A major.

enter image description here

Good luck!

  • Love those examples! Jan 9, 2019 at 18:52
  • 1
    Not really. :) I actually tried your example songs with the music21 library. I converted Jailhouse Rock (in Eb) and Time Warp from Youtube audio to MIDI notes using Ableton Live's "Convert Harmony" function, with no manual editing. Jailhouse Rock was identified as being in Eb minor, and Time Warp in A major. For Jailhouse Rock the major/minor confusion is very understandable, because that's what Ableton's audio-to-MIDI conversion sounded like. The tonic note was correct in both cases. I must say, I was impressed and surprised by the results. Jan 9, 2019 at 20:31
  • Actually, I think your description of what you presume a key-detection algorithm would have to do is analogous to what I think is often wrong with people's expectations of music theory, at least when looking at many questions on this site. They seek logical rules, lists of exceptions and special cases, as if being a musician was like being a lawyer. ;) When instead they should primarily strive to get to feel music and its elements, like with statistical or signal-processing methods. And so the best answers tend to be how-to instructions on getting to feel the elements. :) Jan 9, 2019 at 21:14
  • @piiperi, I think Laurence was trying to make this same point about feeling the elements. In the case of Jailhouse Rock, the F nat. might throw off an algorithm, whereas an experienced musician instantly sees a blue note. If I understand what you told about your test, that is exactly what happened. The blue note was misinterpreted to indicate the music was in minor! Jan 9, 2019 at 21:23
  • @MichaelCurtis The major/minor error was due to my using Ableton's automatic harmony conversion, which isn't really even meant for full mixes. Nobody would make a Jailhouse Rock MIDI arrangement like that. I tried playing some rock'n roll piano with lots of blue notes myself as a test, and the Krumhansl algorithm figured out that it's in a major key. Laurence is of course one of the wise men on this site who emphasize feeling over rules. "Theory describes, it does not command." Jan 9, 2019 at 21:36

Yes, there are ways to algorithmically detect the tonality from MIDI notes, and the algorithms seem to work surprisingly well. If you can use Python, there's a library called "music21" that's easily available and very easy to use. If Python is a no-go, there are implementations for other languages, or you could even try rolling your own.

Look at the answers to this question: https://stackoverflow.com/questions/14734644/is-there-an-algorithm-to-get-the-scale-and-key-of-a-song-from-a-series-of-notes

The best-working algorithm in my very short testing of about five songs with the music21 library was "Krumhansl", which I think is the Krumhansl-Schmuckler algorithm. http://rnhart.net/articles/key-finding/

However, it might be worth knowing that many songs don't stay in one single tonality all the way from beginning to end. You could try splitting the song to shorter segments to see if they're detected as being in different keys. Or perhaps you could do a "sliding window" approach to see if there are modulations. Here are some powerpoint slides found on the web, "Visualizing Harmony" by Craig Stuart Sapp https://ccrma.stanford.edu/~craig/papers/01/icmc01-harmony-6up.pdf

(sliding windows seem to be mentioned - so much for my clever idea)

Your Answer

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

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