Skip to content

MIDI

In 1983 the makers of synthesisers agreed a way for any keyboard to play any synthesiser: MIDI, the Musical Instrument Digital Interface. It’s still how musical things talk to each other, and underneath it’s the serial port of the last page: a start bit, eight bits, a stop bit. So the computer gets a second copy of the same gateware, set to MIDI’s speed, and the rest of this page is about MIDI’s own parts: how the wire is driven, and what the bytes mean.

MIDI runs at 31,250 baud, an unusual speed chosen in 1983 because it’s 1 MHz divided by 32, easy for the instruments of the time to make. It divides the UPduino’s 12 MHz exactly too: 384 clocks a bit. A byte takes 320 µs, so about 3,100 bytes a second, and a note’s message of three bytes takes about a millisecond.

The second copy of uart.v has its speed fixed at 384 (its divisor registers can’t be written), and two pins of its own: MIDI in and MIDI out. Its registers are the same as the serial port’s, at $8040 instead of $8030.

A serial port between computers sends voltages: high for 1, low for 0, measured against a ground wire the two share. Between instruments that’s a problem. A studio’s synthesisers, amplifiers and computer are plugged into different sockets, and their grounds are never quite the same voltage; join them with a cable and a little current flows round the loop they make, which turns up in the audio as hum.

So MIDI doesn’t send a voltage, and the two ends don’t share a ground. The sender drives a current round a loop: out along one wire, through an LED in the receiver, back along the other. The LED is half of an opto-isolator, a package with an LED shining across a gap onto a light-sensitive transistor. The receiver’s side of the circuit only ever sees light. No wire joins the two instruments’ grounds.

MIDI out to MIDI in. When the sender’s TX pin is 0 (the start bit, or a 0 bit), current flows from its 3.3 V supply through the resistors, the cable and the opto-isolator’s LED, back to the pin; the LED lights, the transistor turns on and pulls the receiver’s RX pin low. When TX is 1, both ends of the loop are at 3.3 V and nothing flows. With the MIDI Association’s 3.3 V values, 33 Ω and 10 Ω at the sender and 220 Ω at the receiver, and the LED’s drop of about 1.2 V, it’s about 8 mA.

A 1 is “no current”, which is also what a disconnected cable gives: the line at rest. The original 5 V instruments used 220 Ω at each end of the loop, for about 5.8 mA; the 3.3 V values keep the current about the same for newer, lower-voltage senders, so either can drive the other. The carrier board has a 5-pin DIN socket each way, the opto-isolator, and these resistors.

MIDI’s bytes come in messages, each a status byte saying what kind, then one or two data bytes. The two are told apart by the top bit: 1 in a status byte, 0 in a data byte. That leaves 7 bits for each data byte, which is why so much of MIDI counts 0 to 127: 128 notes, 128 levels of velocity, 128 controllers. The status byte’s other three top bits say the kind of message, and its low four bits the channel, 1 to 16, so one cable can carry sixteen instruments’ parts.

A MIDI message as bytes. The status byte has its top bit set (bold), then three bits for the kind of message (underlined), then the channel less 1. The data bytes have their top bit clear. Note 60 is middle C; velocity is how hard the key was struck (1 to 127). A note off can be sent as a note on with velocity 0, and most keyboards do exactly that.

The message that matters most: note on, $9n (n the channel less 1), the note, and the velocity. When the key comes up, note off, $8n, or (far more often) note on again with velocity 0. A keyboard sends little else: control changes for its knobs and pedals, pitch bend for the wheel.

Running status saves bytes. If a message has the same status byte as the one before, the sender may leave it out, and the receiver keeps using the last one. A chord of 10 notes is 30 bytes with every status byte, or 21 with running status: 6.7 ms instead of 9.6. And with note off sent as note on with velocity 0, playing a whole melody can be a single $90 followed by nothing but pairs of data bytes. That’s why keyboards do it.

So a receiver has to keep the last status byte, and count data bytes as they arrive: a data byte after a complete message starts another of the same kind. It also has to let real-time bytes through anywhere: $F8 and above (the clock, start, stop) are single bytes that can arrive between any two others, even in the middle of a message, and don’t change the status.

Here’s that receiver in 6502 assembly: a little state machine with three bytes of memory (the last status, how many data bytes so far, and the note), which returns once it has a whole note on or note off.

The computer with MIDI at $8040. Play notes on the keyboard below the panel (or with the computer’s keys: A, S, D and so on are the white notes; or plug in a MIDI keyboard and click Use a MIDI keyboard): they arrive at MIDI in as bytes. Show the notes lights an LED for each note held and shows the last one in hex. An octave up sends every note back out of MIDI out 12 notes higher, to a little synthesiser on the page (switch sound on in the panel). Play from the buttons sends note on and note off as the panel’s buttons are pressed and let go: C, E, G and C. Tick send with running status to send the keyboard’s notes without repeating $90; the programs handle both.

On the board, the same program runs as it is. A MIDI keyboard’s cable goes into the carrier’s MIDI in socket; MIDI out goes to a synthesiser, or (once part 4’s sound chip is in place) the computer plays the notes itself. That’s where this is heading: A MIDI synth on a chip, at the end of the part.

  • MIDI is a serial line at 31,250 baud: the same UART, with a fixed speed that divides 12 MHz exactly.
  • It’s a current loop: the sender drives current through an opto-isolator in the receiver, so the instruments’ grounds are never joined, and there’s no hum.
  • A message is a status byte (top bit 1: the kind and the channel) and data bytes (top bit 0: 0 to 127).
  • Running status leaves out a repeated status byte; with note off sent as velocity 0, a melody is one status byte and pairs of data.
  • The MIDI Association’s MIDI 1.0 Detailed Specification and its 2014 note on 3.3 V signalling, at midi.org (free, with a sign-up).
  • Paul D. Lehrman and Tim Tully, MIDI for the Professional: how MIDI is used, and why it’s built as it is.