gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:13:08 26 4
gpt4 key购买 nike

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

提前致谢。

最佳答案

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 对象,因此您不需要再次构造它们。尝试此代码,如果它不起作用,请复制您返回的完整错误消息。

关于python - 在某些条件下从列表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com