I have type 0 MIDI file, containing the track with only 269 meta-messages that I'd like to keep the header chunk and the last one for the end of the file.
for i, msg in enumerate(mid.tracks[0]):
if i > 10 or i < len(mid.tracks[0])-1:
if msg.is_meta:
mid.tracks[0].pop(i)
The above code, does not do this. In fact, it removes exactly half of the messages (135 of them), it also removes the beginning and the end and I have to runs it a few times to get it to close to 10 meta messages!
The library says:
pop([index]) → item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
I also tried another way, but again, each time only removes half of that. So I need to runs the loop few times to get it close to size I want:
while len(mid.tracks[0])>16:
for i, msg in enumerate(mid.tracks[0]):
if msg.is_meta and hasattr(msg, 'data'):
if len(msg.data) == 7:
mid.tracks[0].remove(msg)
for i, msg in enumerate(mid.tracks[0]):
if msg.is_meta:
print(i, msg)
What am I doing wrong please?