Skip to content

Concatenating MIDI files #444

Answered by frallebini
frallebini asked this question in Q&A
Discussion options

You must be logged in to vote

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

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by rdoursenaud
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #401 on January 19, 2023 17:52.