简体   繁体   English

使用python-midi库从midi-events获取变量

[英]Get variables from midi-events with the python-midi library

I use the python-midi library to read a midi file into a list. 我使用python-midi库将midi文件读入列表。

variable = midi.read_midifile(source)

The entries look like those: 这些条目如下所示:

   midi.NoteOffEvent(tick=2, channel=10, data=[48, 98]),
   midi.NoteOnEvent(tick=46, channel=10, data=[48, 100]),
   midi.NoteOffEvent(tick=12, channel=10, data=[48, 100]),
   midi.NoteOnEvent(tick=36, channel=10, data=[48, 91]),
   midi.NoteOffEvent(tick=14, channel=10, data=[48, 91]),
   midi.NoteOnEvent(tick=34, channel=10, data=[48, 122]),

Now I'd like to get the parameters of those Events but I don't know how to do that. 现在,我想获取那些事件的参数,但是我不知道该怎么做。 I want to count all ticks, get all different channels and also read the data (so the note and the velocity). 我想计算所有的滴答声,获取所有不同的通道,并读取数据(因此请注意音符和力度)。 I looked into the Github-Repository but I did not find the answer to my question. 我调查了Github存储库,但没有找到问题的答案。 link to repository: https://github.com/vishnubob/python-midi 链接到存储库: https : //github.com/vishnubob/python-midi

Could it be that pyhton-midi is not really made for reading and manipulating midi-files? 可能是pyhton-midi并非真正用于读取和操作midi文件吗?

I don't know anything about the python-midi library but I'm guessing from the source code that read_midifile() method returns a Pattern object. 我对python-midi库一无所知,但我从源代码中猜测read_midifile()方法返回Pattern对象。 The Pattern object contains a list of events. Pattern对象包含事件列表。

Each event will contain a tick , channel and data property. 每个事件将包含tickchanneldata属性。

I'm guessing you can slice a Pattern object like a list, so you should be able to do this: 我猜想您可以像列表一样切片一个Pattern对象,因此您应该能够做到这一点:

pattern = midi.read_midifile(source)
print pattern[0].tick

This will select the first event in the Pattern and print its tick property. 这将在Pattern中选择第一个事件并打印其tick属性。

You could then count the ticks by doing something like: 然后,您可以通过执行以下操作来计算滴答声:

pattern = midi.read_midifile(source)
tick_count = 0
# a unique list
channels = set()

for event in pattern:
    tick_count += event.tick
    channels.add(event.channel)

If you ever need to know what's in an object, you can always use the dir() function. 如果您需要了解对象中的内容,则可以始终使用dir()函数。 Eg 例如

print dir(event)

Good luck with your learning. 祝您学习顺利。 Keep aiming high! 保持高目标!

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

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