简体   繁体   中英

How can I reference individual MIDI events using the python-midi module?

I'm using this python module (for python 2) to try and access an individual MIDI event. So far I have

import midi  
pattern = midi.read_midifile('Conquest of Paradise.mid')  
print pattern

This prints a LOT of midi events. But len(pattern) returns the value 13 (which is a LOT less). How can I iterate over pattern to access any midi.NoteOnEvents ? I've tried reading the source code but I guess I don't know nearly enough python.

EDIT: User CL has pointed out the 13 refers to tracks.
So I figure I can iterate over the MIDI file like so:

trackCount = len(pattern)
eventCount = 0

for i in range(trackCount):
    for j in range(i):
        print(pattern[i][j].name)
        eventCount += 1

print(eventCount)

But now this gives eventCount = 78 , when it's definitely a lot more than 78. Also of all the names printed, none of them are NoteOnEvent or NoteOffEvent .

You are iterating the pattern incorrectly in the inner loop, try this:

trackCount = len(pattern)
eventCount = 0

for i in range(trackCount):
    for j in range(len(pattern[i])):
        print(pattern[i][j].name)
        eventCount += 1

print(eventCount)

Or, even better:

eventCount = 0

for p in pattern:
    for event in p:
        print(event.name)
        eventCount += 1

print(eventCount)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM