pythonmidimusic21

Showing midi pitch numbers from Mid file with music21


I am using music21 to extract the midi pitch numbers (in order) for a bunch of midi files.

I have been reading through the documentation and I can load one file like this:

from music21 import *
sBach = corpus.parse('bach/bwv7.7')

Now how do I show a sequence of midi numbers? I am sure this is possible but I can't find the function in the documentation.

And is there a way to do it for multiple files at the same time?


Solution

  • from music21 import *
    sBach = corpus.parse('bach/bwv7.7')
    for p in sBach.parts:
        print("Part: ", p.id)
        for n in p.flat.notes:
            print(n.pitch.midi)
    

    Note that .notes includes Chord objects, which don't have a .pitch property. So for complex scores you may need to separate out the chords from notes or iterate over p.pitches. I think you'll want to go through the music21 User's Guide a bit more before continuing.