python - Python:从 type 0 转换为 type 1 midi
问题描述
0 型 midi 文件(示例)将所有乐器都挤在一个轨道上。
Type 1 MIDI 文件将乐器分离到不同的轨道中。
有没有从0型转换为1型的好方法?如果有任何资源可以运行此转换,我很想听听它们!
解决方案
这就是我如何处理这个问题的。重要的见解是,channel
在 MIDI 文件中指定发送某些信息的设备端口(例如某些 MIDI 键盘输入),而program
乐器声音(例如锯齿导音、cajun banjo...)。
这意味着可以创建一个字典,每个语音一个键,以及一个包含该语音中播放的音符的值列表。时间最初应该存储在全局坐标中(如在 type-0 文件中,相对时间坐标在所有声音中表示,但我们现在将声音分成不同的音符列表)。
然后可以转换回相对时间单位,存储输入类型 0 音轨中的 bpm 和时间分辨率值,然后哇哦——这是你的类型 1 MIDI。
from collections import defaultdict
import mido, os
def subdivide_midi_tracks(path):
'''Convert a type 0 midi file to a type 1 midi file'''
m = mido.MidiFile(path) # load the original type-0 midi file
messages = [] # a list of message dicts, one per track
for track in m.tracks:
time = 0 # store time in aggregate units
track_messages = []
for idx, i in enumerate(track):
i = i.dict()
if i.get('time', None): time += i.get('time')
i['time'] = time
track_messages.append(i)
messages.append(track_messages)
# build a dictionary of the events for each channel
d = defaultdict(list) # d[channel_id] = [notes]
for track_idx, track in enumerate(messages):
for i in track:
channel = i.get('channel', -1)
d[channel].append(i)
# covert time units in each program back to relative units
for channel in d:
total_time = 0
for i in sorted(d[channel], key=lambda i: i['time']):
t = i['time']
i['time'] = t - total_time
total_time = t
# create a midi file and add a track for each channel
m2 = mido.MidiFile()
for channel in sorted(d.keys()):
track = mido.midifiles.tracks.MidiTrack()
# add the notes to this track
for note in d[channel]:
note_type = note['type']
del note['type']
# if this is a meta message, append a meta message else a messaege
try:
track.append(mido.MetaMessage(note_type, **note))
except:
track.append(mido.Message(note_type, **note))
m2.tracks.append(track)
# ensure the time quantization is the same in the new midi file
m2.ticks_per_beat = m.ticks_per_beat
return m2
m = midi.load("data/nes/'Bomb_Man's_Stage'_Mega_Man_I_by_Mark_Richardson.mid")
m2 = subdivide_midi_tracks(m.path)
m2.save('whoop.mid')
os.system('open whoop.mid')
推荐阅读
- java - 填充给定形状的选项数
- html - 在鼠标悬停之前隐藏 Blazor HTML 链接
- python - 如何使用 regix (re) 模块从字符串中提取数字
- google-apps-script - 如何将 Google 表格作为电子邮件附件发送?
- c++ - _mm256_load_ps 在调试模式下导致 google/benchmark 出现分段错误
- angular - Angular - 在按钮点击时切换两个类
- c# - 如何通过 OPC 基础 UA-.net 标准库正确使用 OPC UA 中的 TypeDefinition 和 Object 实例
- javascript - 如何使用 setimage discord.js 使图像更大
- c# - 在 GLSL 中复制 GL 中间模式光照模型
- zsh - 杀死特定屏幕