MIDI serial protocol summary
MIDI is a stream of bytes requiring stateful decoding. Bytes are classified into:
00-7F data bytes 80-FF status bytes, subclassified into: 8n-En channel messages (n = channel - 1) F0-F7 system messages* F8-FF real-time messages
(* this use of the term slightly deviates from the standard, which includes real-time messages under "system messages", but the standard terminology is pointlessly verbose.)
Data bytes sent after power-on and excess data bytes after a system message should be ignored.
Real-time messages are isolated single-byte messages that can be interjected at any time without affecting the parser state (except FF which resets the state):
F8 MIDI clock tick, 6 per MIDI beat while playing F9 reserved FA Start playing from song position 0 FB Continue playing from current song position FC Stop playing FD reserved FE "Active sensing" FF Reset all state to power-on defaults.
System messages are not channel-specific.
F0 ... SysEx message (manufacturer, variable data) F1 xx MTC quarter-frame (weird format) F2 nn nn Song position (MIDI beats) F3 ss Song select (song number) F4 ... reserved F5 ... reserved F6 "Tune request", request oscillator calibration F7 End of SysEx message
Channel messages encode the channel into the status byte, and are followed by one or two data bytes. Consecutive channel messages with the same status byte only need to send it once (this is known as "running status"). For example, a string of note-ons to channel 2 can be sent as 81 kk vv kk vv kk vv
8n kk vv Note-on (key, velocity > 0) 8n kk 00 Note-off (key) without release-velocity info 9n kk vv Note-off (key, velocity) An kk pp Aftertouch (key, pressure), aka "Key pressure" Bn cc vv Control change (controller, value) Cn pp "Program change" (program) Dn pp "Channel pressure" (pressure) = Aftertouch for all pressed keys En xx xx Pitch bend
Key 60 is C4 (central C), key 64 is typically tuned to 440 Hz.
Pitch bend is 14-bit unsigned little-endian. The center position is 0x2000 (encoded as 00 40).
MIDI USB class
Uses 32-bit packets containing the 4-bit "virtual cable" number, 4-bit packet type, and up to 3 bytes of data. MIDI messages other than SysEx are sent as single packets, while SysEx is split into 3-byte fragments (which may be interleaved with packets for other virtual cables). It is also possible to send raw MIDI data (e.g. to pass unparseable data unchanged), but only a single byte per packet.
(j = virtual cable) channel messages: j8 8n kk vv Note-on j9 9n kk vv Note-off jA An kk vv Aftertouch / Poly-KeyPress jB Bn cc vv Control change jC Cn pp 00 Program change jD Dn vv 00 Channel pressure jE En vv vv Pitch bend real-time and system messages except sysex: j5 Fx 00 00 single-byte j2 Fx xx 00 two-byte j3 Fx xx xx three-byte sysex message: j4 xx xx xx fragment if four or more bytes left j5 xx 00 00 last fragment if one byte left j6 xx xx 00 last fragment if two bytes left j7 xx xx xx last fragment if three bytes left special messages: jF xx 00 00 raw MIDI byte j1 xx xx xx reserved for cable events j0 xx xx xx reserved
See class driver specification for more details.
MIDI beat clock
MIDI beat = resolution of song position counter = 1/16th note = 1/4 of what most people would call a beat.
Six MIDI clock messages (ticks) are sent per MIDI beat during playback. The first tick after sending a Start or Continue message is the beat corresponding to the current song position and starts playback. When playback is stopped and continued, playback resumes from the song position counter, i.e. it repeats the most recent MIDI beat and continues from there.
The Start message is equivalent to song position 0 + Continue.
Setting the song position is not permitted during playback.
Example:
FA start F8 0.0 F8 0.1 F8 0.2 FC stop F2 03 00 song position 3 FB continue F8 3.0 FC stop FB continue F8 3.0 F8 3.1 F8 3.2 F8 3.3 F8 3.4 F8 3.5 F8 4.0 F8 4.1 F8 4.2 F8 4.3 F8 4.4 F8 4.5 FC stop FB continue F8 4.0 F8 4.1
MIDI Time code
See wikipedia.