首页 > 解决方案 > 使用 mido 库的绝对 MIDI 刻度值

问题描述

我正在使用Mido 库在 python 中读取一个简单的 MIDI 文件。

我的 MIDI 文件如下:https://www.dropbox.com/s/t80kg9l2k525g0h/file.mid?dl=0

这只是一个我用基本音符创建的虚拟 MIDI 文件。

我用 Mido 库打开它并打印它的内容:

from mido import MidiFile
mid = MidiFile('file.mid')
for i, track in enumerate(mid.tracks):
print('Track {}: {}'.format(i, track.name))
for msg in track:
    print(msg)

这就是我得到的:

Track 0: 
<meta message track_name name='\x00' time=0>

<meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0>
<meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0>
note_on channel=0 note=60 velocity=100 time=0
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=60 velocity=64 time=384
note_on channel=0 note=62 velocity=100 time=0
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=62 velocity=64 time=384
note_off channel=0 note=64 velocity=64 time=0
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=64 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_on channel=0 note=66 velocity=100 time=384
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=66 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=384
note_on channel=0 note=69 velocity=100 time=0
note_off channel=0 note=69 velocity=64 time=384
note_on channel=0 note=71 velocity=100 time=0
note_on channel=0 note=60 velocity=100 time=384
note_off channel=0 note=71 velocity=64 time=0
note_off channel=0 note=60 velocity=64 time=384
note_on channel=0 note=62 velocity=100 time=0
note_off channel=0 note=62 velocity=64 time=384
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=64 velocity=64 time=375
note_on channel=0 note=67 velocity=100 time=9
note_on channel=0 note=66 velocity=100 time=384
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=66 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=384
note_on channel=0 note=69 velocity=100 time=0
note_off channel=0 note=69 velocity=64 time=384
note_on channel=0 note=71 velocity=100 time=0
note_off channel=0 note=71 velocity=64 time=384
<meta message end_of_track time=0>

做一些实验我有点明白时间是用相对于前一个事件(note_on - note_off)的刻度表示的。

如何使用绝对时间参考(以滴答声)重新排序笔记?

我想有一个我的笔记的绝对时间线,但我不知道如何从我拥有的数据中“提取”它。

有没有其他库已经实现了这个功能?我看到了这个库:Python-midi,但不幸的是它只适用于 Python 2。

标签: pythonmidimido

解决方案


增量时间与相应的音符开事件无关,而是与同一轨道中的前一个事件相关。

只需按顺序将所有增量时间相加即可。


推荐阅读