MIDI stands for Musical Instrument Digital Interface.
It is a communication protocol which allows electronic musical instruments to communicate with each other, to synchronise timing, for example. It also allows the interface which the musician uses to be separated from the instrument, which makes the sound. This means that a musician can use a single keyboard to play many different electronic instruments.
These are usually synthesisers, but there are examples of mechanical MIDI instruments, such as this xylophone:
These days, many instruments are provided by software running on a PC. The software is known as a DAW, or Digital Audio Workstation. One example of this which is available on all desktop OSs is LMMS. LMMS is capable of producing many synthesised effects which would previously have required many separate pieces of synthesiser equipment.
The advantage of using a MIDI keyboard with a PC is that all of these synthesised instruments become available with a more natural musical interface than a QWERTY keyboard.
How does MIDI work?
Let’s take a look at what MIDI actually is and how it works.
MIDI is a simple serial protocol, which uses an opto-isolated connection between the transmitter and receiver. This means that there is no electrical connection between the transmitter and receiver, which reduces the risk of damage caused by an incorrect connection. The receiver contains an opto-isolator chip, usually a 6N138, which contains an LED (a light-emitting diode) and a photo-transistor (an electrical switch actuated by light) in close proximity, so that the only connection between the two is light transferring between them. Connections are usually carried by 5-pin DIN sockets, as in my keyboard below.
Data is transmitted at 31250 baud (bits per second) with 8 data bits and 1 stop bit. A stop bit is an additional ‘1’ at the end of every byte which is used for synchronisation.
Where a PC provides the instrument sounds, connections between the MIDI keyboard and PCs often use USB, as PCs no longer have MIDI ports of their own. For keyboards and instruments which only have MIDI connections, inexpensive adaptors are available which convert between USB and MIDI.
MIDI Message Format
All MIDI commands consist of a status byte, and may also include data bytes providing further information.
A status byte consists of two nybbles. Yes, half a byte really is called a nybble! The most-significant nybble (the four bits with the highest value) is the command, and the least-significant nybble is usually the channel we want to send that command two.
The channel refers to the instrument we want to play. So we may have many different synthesisers and other instruments in our set-up, and we can play them from one keyboard by assigning a different channel number to each one.
Let’s look at one very simple message, which is actually all that we need to control a simple instrument. This message is called “Note On”.
You may be wondering how we tell the instrument to turn the note off! We will come to that.
Our first “note on” message
The status byte for “Note On” on channel one looks like this:
We prefer hexadecimal to decimal when dealing with these bytes, because each 4-bit nybble converts directly into a single hexadecimal digit.
This status byte has a value of 91 in hex.
So, which note do we want it to play? That is answered in the first of our status bytes for this command, the key number.
In MIDI, middle C is key number 60 (in decimal, or 3C in hexadecimal). The key number can be anything between 0-127, but let’s stick with middle C.
The second status byte tells the instrument how loud to play, and is called the “velocity”. This may be somewhat confusing, as we usually think of velocity as a measure of speed, rather than volume. The reason MIDI uses the word in this way, is that it is the speed with which the key is hit which influences the loudness.
The middle value for velocity is 0x40 (in hex, 64 in decimal) as that also has a range of 0-127.
So, in hexadecimal bytes, our first middle C will be started if we send:
91 3C 40
Turn it off!
If we were connected to an instrument now, and we had sent the above command, it would play the note. And it wouldn’t stop, which would soon become annoying, so I suppose before we start to send messages to a real instrument, we’d better find out how to turn it off!
MIDI does have a “note off” message, but we’re not going to use it, because there is an alternative, and usually it involves sending fewer bytes.
The alternative is to send the same message as “note on” but with a velocity of 0. So:
91 3C 00
If you can count to three, you’re probably thinking that’s just as many bytes as the “note on” message! However, MIDI has a concept called “running status”.
You may have noticed that our data bytes are all less than 128, i.e. they don’t have the most significant bit (MSB) set, while our status byte does. That is not an accident, in fact in MIDI, all status bytes have the MSB set, and no data bytes do, so MIDI can instantly tell when a status byte is sent. So, back to “running status” – what this means is that if a command is sent without a status byte, MIDI will assume the same status byte as the last message we sent.
This means that as long as the last thing we sent was “note on” we can omit the 91 byte from the next note on message we send.
So, to start a note, and then stop it, costs only five bytes:
91 3C 40 (short pause for duration of note) 3C 00
OK, so what else can MIDI do?
MIDI has many more commands, allowing pitch bend, synchronisation, and many other customisable controls. If you have some DAW software running on a PC, and a MIDI keyboard connected which has sliders and other controls, it is possible to control parameters on the DAW, e.g. master volume, individual channel volumes, effects like reverb etc. Many keyboards also have a “split” function, which allows two different parts of the keyboard to play different instruments (channels).
If you want more information, I can recommend the Sparkfun MIDI Tutorial.
For exhaustive detail, the MIDI standard, which is available from the Midi Association, runs to 334 pages!