如何在python中合并或连接两个midi文件

时间:2017-04-02 20:09:14

标签: python python-3.x python-2.7 midi

我正在开发一个产生midi文件作为输出的项目。但是这些midi文件非常短,我想合并或连接它们以生成一个长的midi文件。我正在使用python-midi library,并且没有关于此库的信息或文档。我尝试了这个程序,但它给出了错误:

import midi
pattern=midi.read_midifile("kl.mid")
track=midi.Track()
pattern2=midi.read_midifile("oi.mid")
pattern.append(pattern2)
midi.write_midifile("aaka.mid",pattern)

错误:

    Traceback (most recent call last):
  File "lp.py", line 6, in <module>
    midi.write_midifile("aaka.mid",pattern)
  File "/home/userdf/.local/lib/python2.7/site-packages/midi/fileio.py", line 152, in write_midifile
    return writer.write(midifile, pattern)
  File "/home/userdf/.local/lib/python2.7/site-packages/midi/fileio.py", line 102, in write
    self.write_track(midifile, track)
  File "/home/userdf/.local/lib/python2.7/site-packages/midi/fileio.py", line 116, in write_track
    buf += self.encode_midi_event(event)
  File "/home/userdf/.local/lib/python2.7/site-packages/midi/fileio.py", line 125, in encode_midi_event
    ret += write_varlen(event.tick)
AttributeError: 'Track' object has no attribute 'tick'

我google了很多但没有找到通过python添加两个midi文件的方法。 让我知道我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:0)

读取midi文件并保存模式

pattern1 = midi.read_midifile(file1)
pattern2 = midi.read_midifile(file2)

然后从每个模式读取每个轨道

pattern = midi.Pattern()

for track in pattern1:
    pattern.append(track)

for track in pattern2:
    pattern.append(track)

最终以新模式保存文件

midi.write_midifile('sound.mid', pattern)