Extract data from a list with some conditions

Basically, I'm trying to add two midi files, and there is no information on the Internet about this, so I'm trying to use my own.

What I have done so far, I have added two midi messages (a type of midi data) and I have a list of both midi messages. This means that I have all the data in which I need to combine the two midi. The problem is that I cannot add data in a specific format.

So my code is:

  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)

I get this output:

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

Now this is fine, but the problem is that I can add tracking messages to 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')

so my question is: how to add each line from this format:

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

into this format

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

in track.append(Message())

because track.append only supports this format:

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

Thanks in advance.

+6
1
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, . , , , .

+2

Source: https://habr.com/ru/post/1016258/


All Articles