简体   繁体   English

如何使用python-midi模块引用单个MIDI事件?

[英]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. 我正在使用此python模块 (对于python 2)尝试访问单个MIDI事件。 So far I have 到目前为止,我有

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

This prints a LOT of midi events. 这将打印很多Midi事件。 But len(pattern) returns the value 13 (which is a LOT less). 但是len(pattern)返回值13(少了很多)。 How can I iterate over pattern to access any midi.NoteOnEvents ? 我如何遍历pattern以访问任何midi.NoteOnEvents I've tried reading the source code but I guess I don't know nearly enough python. 我已经尝试阅读源代码,但是我想我几乎不了解python。

EDIT: User CL has pointed out the 13 refers to tracks. 编辑:用户CL指出了13指轨道。
So I figure I can iterate over the MIDI file like so: 所以我认为我可以像这样遍历MIDI文件:

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 . 但这现在使eventCount = 78 ,绝对比78多得多。此外,在所有打印的名称中,没有一个是NoteOnEventNoteOffEvent

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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