The track chunk of a MIDI file is a piece of information in the MIDI file that: 1) identifies itself as track chunk; 2) shows that it has a certain size; and 3) contains the actual MIDI messages.
In other words, the track chunk has the following structure.
|Description||Length in bytes||Starts at byte in the chunk||Value|
|Chunk ID||4 bytes||0x00||The character string "MTrk"|
The "chunk ID" for the track chunk is always the text "MTrk", which signifies that this is a track chunk.
The "size" of the track chunk varies, but it is the number of bytes used to represent the MIDI messages that follow.
The "data" are the track message data, or in other words, the MIDI messages.
There could be one or more tracks in the MIDI file (in principle, between 1 and 65,535 tracks). This is specified by the "number of tracks" bytes of the header chunk of the MIDI file, which are the eleventh and twelfth bytes of the header chunk.
How many tracks there can be in the MIDI file also depends on the format type of the MIDI file, which could be 0, 1, or 2. A value of 0 means that the MIDI file contains one track chunk with all relevant MIDI messages, including meta messages that contain meta information about the MIDI song as well as musical MIDI messages that actually do something. A value of 1 means that the MIDI file contains two or more track chunks. In such a case by convention the first chunk will contain meta messages with the song information and the second and other chunks will contain the musical MIDI messages. A value of 2 also means multiple track chunks, but in this case each track represents a separate MIDI sequence, which may or may not be played simultaneously, and which may contain its own meta and musical messages. The "format type" of the MIDI file is carried by the ninth and tenth byte of the MIDI file header chunk.
MIDI data and MIDI delta time
The data in MIDI tracks consists of MIDI messages with time stamps in the following order.
Time of first message
Time of second message
In a MIDI system where one device transmits a message to another device, the receiving device will react as needed. In such a setup, it is not necessary to tell the device when to react and MIDI messages do not carry time. In a MIDI file, however, specifying time is necessary. In a sequence of MIDI Note On messages stored in a MIDI file, for example, the MIDI device needs to know when each message should be executed and when each note should be played.
The following is an example of the bytes that may occur in a MIDI file.
0x50 0x90 0x26 0x3C
Here, 0x50 is the time as described below. 0x90 0x26 0x3C is the MIDI message.
The 0x50 hexadecimal value translates to the decimal value 80. MIDI devices will interpret this to mean that this message should occur 80 MIDI ticks after the previous message. A MIDI tick is an amount of time. Its length depends, for example, on the time division of the MIDI file. For purposes of this example let us say that one tick is 3,125 microseconds. Then this message will be executed 80 * 3,125 = 250 milliseconds, or a quarter of a second after the previous message.
Note that the MIDI delta time is "delta" time and not "absolute" time. It specifies the time after the previous message and not the time from the beginning of the MIDI sequence.
The three bytes (0x90 0x26 0x3C) that follow the MIDI delta time are the MIDI message.
The number of ticks in the example above was 80 decimal = 0x50 hexadecimal and it was recorded with one byte in the MIDI file. According to the MIDI protocol, the MIDI delta tie is a variable length quantity. It may be carried by one byte as in this example, but it can be carried by several bytes.
Each byte used to record the MIDI delta time contains eight bits. If the topmost bit of the byte is 1, then a MIDI device will expect that there are more delta time bytes to follow. If the top bit is zero, the MIDI device will expect that the delta time is complete and what follows is the MIDI message. The seven remaining bits of each byte are combined to create the actual value for the number of ticks. In the example above, 0x50 hexadecimal translates to the bits 01010000. The top bit of this byte is zero and hence a MIDI device will know that the delta time is complete.