Eric's Homemade Stuff

An infrequent blog of stuff I make, including music.

MIDI File Format

Part 2 of a Series

How MIDI files are structured.

A Chunk

MIDI Files are composed of series of blocks of data called chunks. A chunk is a sequence of bytes that start with a chunk header and then a variable number of bytes of data.

MIDI Chunk

  • The Chunk Headers contain a 4-byte ID and a 4-byte Length.
  • They are followed by a block of data bytes that have the length specified in the Length field.
  • The data can be many forms from 2-bytes numbers to long blocks of text, like lyrics.

MIDI Files are Chunks

A MIDI file has several of these chunks. The first chunk is always a header chunk that identifies the file. This header chunk is identified by an ID of 'MThd'. That is followed by a variable number of chunks all with the ID of 'MTrk'. Each of these chunks are called tracks and will contain the MIDI data for an associated set of events. Each track will most likely be a different length depending on the contents of the track.

MIDI File

Core MIDI and MIDI Files

In Core MIDI a MIDI file is represented by a opaque C data structure called a MusicSequence. We can create new MIDI files and load existing ones from the file system with a MusicSequence. There are a set of functions associated with the MusicSequence structure that allow us to parse the data from a MIDI file. We’ll start there with the next entry. Here’s Apple’s documentation for MusicSequence.











Comments