在某些条件下从列表中提取数据

时间:2017-04-02 23:54:23

标签: python list python-3.x machine-learning extract

基本上,我正在尝试添加两个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)

我收到了这个输出:

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

现在可以了,但问题是我可以在this format :

中添加要跟踪的消息
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')

所以我的问题是如何从这种格式中追加每一行:

note_off channel=0 note=62 velocity=0 time=0

这种格式

'note_off', note=62, velocity=0, time=0

track.append(Message())

中的

因为track.append仅支持这种格式化方法:

track.append(Message('note_off', note=62, velocity=0, time=0))

提前致谢。

1 个答案:

答案 0 :(得分:2)

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')

result中的对象应该已经是Message个对象,因此您不需要再次构建它们。试试这段代码,如果它不起作用,请复制你收到的完整错误信息。