1

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.

  • 1
    Does this answer your question? [Determine the key signature of a MIDI file](https://stackoverflow.com/questions/59630411/determine-the-key-signature-of-a-midi-file) – Himanshu Poddar Aug 20 '22 at 15:45
  • Thank you @Himanshuman for your answer, unfortunately this does not answer my question. I'm looking for a command like f.e. name-of-piece.analyse("key") that get me the key of a piece (in midi). Do you know something like that? – alligator.953 Aug 20 '22 at 15:55
  • unfortunately, I don't understand MIDI and the structure, I can help but will have to first understand everything about it – Himanshu Poddar Aug 20 '22 at 15:57
  • no problem, do you know the module "music21"? as far as I understand, there you can analyze the keys of a MIDI file, but I don't get it how. I would be very grateful for any help/ hint. – alligator.953 Aug 20 '22 at 15:59

1 Answers1

0

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!

Jacob Walls
  • 873
  • 3
  • 15
  • If you need to convert to midi pitches, then forget `.name` and access `.tonic`, which is a `Pitch`, so you can ultimately access `.tonic.midi`. – Jacob Walls Aug 22 '22 at 00:02