Understand the difference between MIDI file formats 1 and 2
developThe file_format parameter determines how tracks and tempo/time signature events are handled:
Format 1 (Default)
- Uses a separate, internal "tempo" track for tempo and time signature events.
- The
trackparameter inaddTempo()andaddTimeSignature()is ignored. - Track indexing is zero-based for musical tracks. Because the tempo track is managed independently, you can use high track indices for tempo events without creating those tracks (e.g., calling
addTempo(1000, 0, 120)is valid even ifnumTracks=1).
Format 2
- Tempo and time signature events are treated as literal events on the specified track.
- Track indexing is zero-based and strictly follows the
numTrackscount.
# Format 1 example: tempo track is managed independently
MyMIDI = MIDIFile(1, file_format=1)
track = 0
big_track = 1000
MyMIDI.addTempo(big_track, 0, 120) # This works even though track 1000 doesn't exist
MyMIDI.addNote(track, 0, 69, 0, 1, 100)