6 python extract list machine-learning python-3.x
基本上,我正在尝试添加两个midi文件,互联网上没有太多关于它的信息,所以我正在尝试自己的.
到目前为止我所做的是我添加了两个midi的消息(midi的数据类型),我有两个midi消息的列表.这意味着我现在需要合并两个midi的所有数据.问题是我无法以特定格式添加数据.
所以我的代码是:
from mido import MidiFile, MidiTrack
mid = MidiFile('har.mid')
mid2 = MidiFile('har2.mid')
l = [msg for track in mid.tracks for msg in track]
l.pop()
ka = [msg for track in mid2.tracks for msg in track]
ka.pop()
result = l + ka
for messagess in result:
print(messagess)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
note_on channel=0 note=59 velocity=40 time=0
note_on channel=0 note=60 velocity=40 time=0
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=59 velocity=0 time=55
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=52 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_off channel=0 note=67 velocity=0 time=55
note_on channel=0 note=57 velocity=40 time=0
note_off channel=0 note=52 velocity=0 time=55
note_off channel=0 note=57 velocity=0 time=0
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=55
note_off channel=0 note=57 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_off channel=0 note=67 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=110
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=57 velocity=0 time=55
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=62 velocity=0 time=55
note_on channel=0 note=57 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=57 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=60 velocity=40 time=0
note_on channel=0 note=62 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_on channel=0 note=60 velocity=40 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_off channel=0 note=64 velocity=0 time=0
note_off channel=0 note=67 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_on channel=0 note=60 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=62 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_on channel=0 note=60 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=48 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=48 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=55
note_on channel=0 note=60 velocity=40 time=0
Run Code Online (Sandbox Code Playgroud)
from mido import Message, MidiFile, MidiTrack
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
track.append(Message('program_change', program=12, time=0))
track.append(Message('note_on', note=64, velocity=64, time=32))
track.append(Message('note_off', note=64, velocity=127, time=32))
mid.save('new_song.mid')
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何从这种格式附加每一行:
note_off channel=0 note=62 velocity=0 time=0
Run Code Online (Sandbox Code Playgroud)
这种格式
'note_off', note=62, velocity=0, time=0
在 track.append(Message())
因为track.append仅支持这种格式方法:
track.append(Message('note_off', note=62, velocity=0, time=0))
Run Code Online (Sandbox Code Playgroud)
提前致谢.
from mido import MidiFile, MidiTrack
mid = MidiFile('har.mid')
mid2 = MidiFile('har2.mid')
l = [msg for track in mid.tracks for msg in track]
l.pop()
ka = [msg for track in mid2.tracks for msg in track]
ka.pop()
result = l + ka
mid3 = MidiFile()
track = MidiTrack()
mid3.tracks.append(track)
for m in result:
track.append(m)
mid3.save('new_song.mid')
Run Code Online (Sandbox Code Playgroud)
中的对象result应该已经是Message对象,因此您不需要再次构造它们。尝试此代码,如果它不起作用,请复制您收到的完整错误消息。
| 归档时间: |
|
| 查看次数: |
205 次 |
| 最近记录: |