Search code examples
pythonaudioanalysismusic21

music21: get keys of midi file?


I'm trying to analyze a midi file with music21 to get the keys of that file. Does anyone know the command for that or where to find an example for this?

I'm new to this. Thank you a lot in advance.


Solution

  • Assuming you need to analyze with a key-finding algorithm (as opposed to just reading the key signature provided by the encoder, if present), then create a music21.stream.Score and call analyze("key"):

    my_score: music21.stream.Score = music21.converter.parse('path.mid')
    k = my_score.analyze('key')
    print(k.name)
    

    Some other fun stuff like alternateInterpretations and correlationCoefficient are described in the User's Guide. Enjoy!