MIDI

From NURDspace

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.

See class driver specification for more details.