Concatenating MIDI files #444
-
Given two (type 1) MIDI files mid1 = MidiFile('first.mid')
mid2 = MidiFile('second.mid') how could I go about creating a concat_mid = MidiFile() such that |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
I think I found a solution. I'll share it here for future reference: mid1 = MidiFile('first.mid')
mid2 = MidiFile('second.mid')
# compute mid1 total time
time1 = 0
for track in mid1.tracks:
for message in track:
if message.type == 'note_on':
time1 += message.time
# shift mid2 by time1
for track in mid2.tracks:
for message in track:
if message.type == 'program_change':
message.time += time1
# merge mid1 and shifted mid2
concat_mid = MidiFile()
concat_mid.tracks = mid1.tracks + mid2.tracks
concat_mid.save('concat.mid') |
Beta Was this translation helpful? Give feedback.
-
This would have been better suited for our Discussions area. |
Beta Was this translation helpful? Give feedback.
-
Sorry, didn’t realize there was such a thing. What do you mean by “resolution”? |
Beta Was this translation helpful? Give feedback.
-
Every Standard Midi File (SMF) has a "ticks per quarter note" information in the header. The delta-time used by messages is defined in ticks. Hence the resolution bit. But I've made a mistake stating it wouldn't work since |
Beta Was this translation helpful? Give feedback.
I think I found a solution. I'll share it here for future reference: