MIDI messages
Table of Contents
:ID: 5741B4DD-B291-4F6D-A33A-EB4CD83792FF
See also MIDI clock
# Channels
- Each MIDI connections can communicate of 16 channels (0-15)
- MIDI devices can switch channels they send messages over.
- Devices listening for message can selectively listen on specific channels, or all channels at once.
# Anatomy of a MIDI message
- MIDI messages are composed of several bytes. Just how many depends on the type of messages
- The first byte is called the
Status Byte
and it has two parts, the message type and the channel information. See also https://midi.org/summary-of-midi-1-0-messages- Message info is the first four bits
- Channel info is the four least significant bits
- The top most bit is always set. This identifies the byte as the one containing the message type and channel info (ie, status)
- The subsequent bytes contain the message data.
- The top bit is unset.
# Midi over USB
These messages will be 4 bytes. There is an extra header byte that comes first which contains the cable and command.
# Hexadecimal Notation
- Send message bytes using Hex notation, prefixed with
0x
. For example,00111100
(60) is sent as0x3c
. - See also https://kb.iu.edu/d/afdl
# Note On / Off
# Status Byte (first byte)
The first byte is the status indicating message type (1001 = 9/Note On) and channel (0100 = 4/midi channel 5)
Message Type | Channel |
---|---|
1 0 0 1 | 0 1 0 0 |
- Top bit is always set
- Message type: 9 = Note On
- Channel: 4 (Channel 5)
- This means the Note On message is in the range of 9..24 (eg, 9 + the channel 0 - 15)
# Bitwise AND
In order to determine if the status byte is On or Off, we just need to look at the first 4 bits. We can mask out the last four bits with a bitwise & operator.
if ((statusByte & 0xF0) == 0x90) { // ON message } if ((statusByte & 0xF0) == 0x80) { // OFF message }
0xF0
in binary is 11110000.- The
&
operation is performed between these two numbers. For example, if the status byte is 10000011 (0x83 in hex, which is a Note Off message on channel 4): 10000011 (status) & 11110000 (0xF0
) is 10000000 (0x80). This operation effectively “masks” the lower 4 bits (which represent the channel) and keeps only the upper 4 bits (which represent the message type). By comparing the result to0x80
or0x90
(10000000 or 10010000), this checks for note Off or On, respectively.
# Data Bytes
The next two bytes are note and velocity data:
Byte | Value | Data |
---|---|---|
0 0 1 1 1 1 0 0 | 60 | Note |
0 1 1 1 1 1 1 1 | 127 | Velocity |
- Top bits are unset indicating these are the message values
- 127 is therefore the maximum value
# Note off alternative
Other than sending a note off message (0x80), another common way is to send an On message for a given note with value of 0
# Note values
Octave | C | C#/Db | D | D#/Eb | E | F | F#/Gb | G | G#/Ab | A | A#/Bb | B |
---|---|---|---|---|---|---|---|---|---|---|---|---|
-1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
-1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
0 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
1 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
2 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
3 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
4 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 |
5 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
6 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 104 | 106 | 107 |
7 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 |
8 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | - | - | - |
# Pitch Bend (LSB and MSB)
The message structure is Status byte, LSB, MSB
- Status byte: Always
0xE0
to0xEF
(224 to 239), where the lower 4 bits represent the MIDI channel (0-15). - LSB (Least Significant Byte): The lower 7 bits of the pitch bend value.
- MSB (Most Significant Byte): The upper 7 bits of the pitch bend value.
The point here is that the pitch bend value is a 14-bit number, ranging from 0 to 16383, where 8192 represents no pitch bend. In other words you get higher resolution than just 0-127
See also https://www.instructables.com/Send-and-Receive-MIDI-with-Arduino/ for a good example.
# Sending and Receiving MIDI notes
# C / C++
# RtMidi
# Arduino Libs
# Midi Jack Wiring
# Midi input
# Midi output
Note the 220ohm resistor is optional and included as a safety precaution against connecting miswired cables and whatnot (eg, current flowing into the TX pin).
# Tools
# SendMIDI
https://github.com/gbevin/SendMIDI Send MIDI from the command line.
# midisnoop
https://github.com/surfacepatterns/midisnoop Receive and print MIDI messages. Use this in conjunction with sendMIDI to try things out. Uses Alsa or JACK midi drivers. See also Mixxx, Ardour, JACK and MIDI