← Note by Note RU
No MIDI keyboard connected
Computer keyboard: C4–F5
?

A S D F G H J K L are white keys from C, W E T Y U O P are black. Z and X move an octave down and up, C and V play softer and harder. Space is a tap in rhythm exercises. Any keyboard layout works.

Chapter 03 of 21 Sound, synth, MIDI ≈ 40 min 8 exercises

What a MIDI keyboard sends

There is no sound inside a MIDI keyboard. It only reports what you did. In this chapter you catch those reports: the bytes that go down the cable when you press a key or the pedal, or turn a wheel, and you learn to read them.

In this chapter

  • read MIDI messages: note on, note off, the pedal and the wheels
  • turn a note number into a key and a frequency: note 60 is middle C
  • control your velocity and check how evenly you play
  • explain what a channel is and why one stream can play three instruments
  • record a few notes and see what bytes a MIDI file is made of

Plug a MIDI keyboard into your computer, open no programs at all and press a key. Silence. Most MIDI keyboards have no oscillator and no speaker: they are remote controls. The sound comes from a synth, like the one you built in chapter 2, from a program on the computer or from another instrument at the far end of the cable. The keyboard only tells it what happened: which key went down, how hard, and when it came back up.

That raises this chapter's question. If the keyboard makes no sound and only reports, what exactly does it report? You can see the answer quite literally, byte by byte. Below is a monitor that catches everything you play and turns each action into a line.

Catching the messages

Press any key: on a MIDI keyboard, on the computer keyboard (A plays C) or on the screen. Hold it and let go. Then play a few more notes: soft, loud, short, long.

Every action gave a line of three numbers. Pressing a key sent one message, letting go sent another. While a key is simply held down, usually nothing goes down the cable: the keyboard stays quiet as long as nothing changes. (The exception is keyboards that sense how hard you press on a key that is already down. They also send "pressure".) The synth keeps the sound going by itself until it hears that the key is up.

The language the keyboard and the synth speak is called MIDI, short for Musical Instrument Digital Interface. Each line in the monitor is one MIDI message. Your first one is a note on, the second a note off.

To the right of the list is the latest message, broken down byte by byte. Click any line to keep its breakdown on screen. The HEX, DEC and BIN buttons show the same bytes in hexadecimal, decimal and binary: the numbers stay the same, only the way of writing them changes.

The monitor works just as well without a MIDI keyboard. Notes from the computer keyboard and the screen are turned into the bytes a real keyboard would have sent, and those lines are marked with ≈. With a keyboard connected, the lines without the mark are exactly what came down the wire, bit for bit.

Three bytes

Take one line: 90 3C 64. These are three bytes, three numbers from 0 to 255. The monitor writes them in hexadecimal, or hex, which has sixteen digits: 0–9 and A–F, where A is 10 and F is 15. Programmers and sound engineers write bytes this way because one byte is exactly two hex digits. 3C is 3·16 + 12 = 60, and 64 is 6·16 + 4 = 100.

The first byte says what happened. It is called the status byte, and you read it in two halves. The first digit is the type of message: 9 is note on, 8 is note off. The second is the channel number. More on channels later; for now yours is 0, which means channel 1. The remaining bytes are data bytes: the note number and the velocity.

First byteWhat happenedData bytes
9nnote onnote number, velocity
8nnote offnote number, release velocity
Bncontrol change (pedal, mod wheel…)controller number, value
Enpitch bendlow 7 bits, high 7 bits
Cnprogram change (a new sound)program number

Why do data bytes stop at 127 and not 255? Switch the monitor to BIN. The leftmost bit of a status byte is always 1, and of a data byte always 0. Thanks to that bit, a receiver always knows where a new message begins, even if it was switched on in the middle of a stream. That leaves seven bits for the number, which gives 128 values: 0 to 127. This is where every "127" in this chapter comes from.

The messages are short because the wire is slow by today's standards: 31,250 bits a second. Each byte travels with two extra framing bits, so it takes ten bits, and a three-byte message takes 0.96 milliseconds. That is plenty for fingers: a ten-note chord gets down the cable in under 10 ms. Over USB the same MIDI travels faster still, and the bytes stay the same.

Test yourself: here are three bytes, so what did the player do? The decimal hint below will help.

Note 60

The second byte of a note on is the note number. The numbering is simple: each next key, white or black, adds one. Middle C (C4), the C in the middle of a piano, is note 60. The C sharp next to it is 61, D is 62. An octave up adds 12: the next C is 72, the one below is 48. From this chapter on, the course also names notes this way: "note 60".

Move the slider or press keys, and the X-ray shows the number, the note name, its frequency and the message that would go down the cable.

There are 128 numbers, from 0 to 127, which is more than ten octaves. A grand piano has 88 keys: from note 21 (the lowest A, A0) to note 108 (the highest C, C8). A synth will play the numbers below and above as well; a piano simply has no keys for them. Note 0 sounds at 8.18 Hz, below the threshold of hearing, and note 127 at 12,543.85 Hz.

One formula turns a number into a frequency. The A above middle C, note 69, is 440 Hz, and every 12 numbers the frequency doubles:

$$f = 440 \cdot 2^{(n - 69)/12}$$

Press +12 in the X-ray and look at the frequency: it is exactly twice as high. Press +1 and it is multiplied by 1.0595, the twelfth root of two from chapter 1. The synth receives the number and works out the frequency itself. The message carries no frequency at all.

The note number is the same everywhere, but octave labels differ. In this course, as in scientific pitch notation, note 60 is C4. Some programs and instruments, though, label the same note 60 as C3. If the piano roll in your DAW suddenly shows "C3" on middle C, nothing is broken: look at the number, which never lies.

Find notes by their numbers. You have one landmark: 60 is the C labelled on the keyboard. From there, count in steps: every key, white or black, is one.

Now the other way round: a key lights up and you name its number. Two hints: every C is a multiple of twelve (48, 60, 72), and the A above middle C is 69.

MIDI sends a key number, not a sound. Note 60 is middle C (C4), each key adds one, an octave adds 12. The synth works out the frequency from the number.

Velocity

The third byte of a note on is the velocity, from 1 to 127. The word is well chosen, because the keyboard measures speed, not force. Usually each key has two contacts, one slightly above the other, and the keyboard times how many milliseconds the key takes to travel between them. Fast gives a big number, slow a small one. Pressing harder on a key that is already down does nothing: the velocity was sent the moment the key went down.

Play a few notes in different ways. The bars show the velocity of each one.

Press the buttons "16", "40", "64", "96" and "127": the same note at different velocities. On the piano the volume rises and the sound gets brighter. Switch to the synth: here velocity opens the filter, so a soft note is dull and a hard one bright. Now the organ: all five buttons sound the same. The number arrives, but the sound ignores it, just like the keys of a real organ, which don't respond to velocity. What to do with velocity is up to the synth, not the keyboard.

A computer keyboard does not measure velocity: every note goes out with the same number. The C key makes it softer and V louder. On a phone, or with a mouse, play on the velocity pads: the higher up you press, the bigger the number. The pads work in the exercises below too.

Musicians mark loudness with Italian words: p (piano, soft), mf (mezzo-forte, moderately loud), f (forte, loud). MIDI has no such words, only a number, so the exercise below gives each marking its own range of numbers. Play at the velocity it asks for.

Loud and soft are half the job. Playing evenly is harder: eight notes at one velocity, with none of them sticking out. Pianists work on this for years, and MIDI gives you an honest ruler: the spread, in numbers.

And the reverse task, a steady rise: each note a little louder than the one before. Musicians call this a crescendo.

Three markings instead of two is the next step up. Here the middle one, mf, has to land between soft and loud.

Controlling velocity is a skill of the hands, and it grows with repetition. The trainers page has "Soft and loud", with levels of up to five markings. A couple of minutes a day makes your playing noticeably more even.

Letting go is a message too

Back to the second line in the monitor, the note off. It carries the same note number, and its third byte is the release velocity. Few keyboards can measure it, and by the MIDI rules those that can't send 64, the middle of the scale. That is why the monitor without a MIDI keyboard shows 80 3C 40: 40 in hex is 64.

Many keyboards do something else: instead of a note off they send a note on with velocity 0, 90 3C 00. The MIDI 1.0 specification says outright that a receiver must understand both and treat them the same. This saves bytes. Messages of the same type on the same channel can follow each other without repeating the status byte, a trick called running status, and a stream made only of note ons ends up almost a third shorter.

A note in MIDI has no duration. There are two moments, when the key went down and when it came up, and the duration is the gap between them. Play short and long notes: the strip below shows the last six seconds, and each released note is labelled with how many milliseconds it was held.

Now press the pedal, with the button, the Space bar or a real pedal plugged into your keyboard, and play a few notes while you hold it. You let go of the keys and the note offs go out as usual, yet the sound carries on: the strip shows it hatched. The sustain pedal leaves the notes alone and sends a message of its own: B0 40 7F when it goes down, B0 40 00 when it comes up. The synth remembers that the pedal is down and keeps the released notes sounding until the pedal comes up.

This is a different type of message: a control change, or CC for short. The first byte is B0, the second the controller number (64 for the pedal, 40 in hex), the third the value. By the rules, 0–63 means "up" and 64–127 means "down". The line sits in the middle because some pedals send values in between: the synth reads anything from 64 upwards as "down".

The pedal is down. You play note 60 and let go of the key. What does the keyboard send the moment you let go?

The keyboard reports truthfully that your finger has left the key. Holding the sound is the synth's decision: it remembers that the pedal is down and puts off the release until the pedal comes up (controller 64 with a value below 64).

The wheels on the left

To the left of the keys, most MIDI keyboards have two wheels or a joystick. One wheel stays wherever you leave it, the other is sprung and returns to the centre by itself. Try both. If you have no keyboard, drag the wheels on the screen.

The first is the mod wheel, short for modulation. It is controller 1: B0 01 and a value from 0 to 127. What exactly it modulates is up to the synth; most often it is the depth of vibrato, as here. In the synth from chapter 2 you could send it to the filter or to the volume instead. The keyboard does not care: it only sends a number.

The second is the pitch-bend wheel. MIDI has a message type of its own for it, E0, and it is unusual: the value travels in two data bytes of seven bits each, 14 bits in all, which gives 16,384 positions. The centre is 8192 (E0 00 40): the pitch does not change. Watch the bytes under the wheel: the low byte races and the high byte creeps, like the minute and hour hands of a clock.

Why so fine? Switch the range to ±12 semitones: now the wheel bends the note a whole octave up or down. With only 128 positions, each step would be 2400 / 128 = 18.75 cents, and a slow bend would sound like a staircase. With 16,384 positions a step is about 0.15 cent, too small for the ear to notice. How far to bend is up to the synth, not the wheel; the default is often ±2 semitones.

Look at the counter under the wheels, too: one movement of a finger sends dozens of messages. The wheel sends a new value at every shift, and the monitor at the top of the chapter shows them all, line after line.

What other controllers are there?

There are 128 controller numbers, from 0 to 127. The last eight, 120–127, are taken by housekeeping commands such as "all notes off". The rest are for knobs, pedals and faders. Some numbers are fixed by habit and by the standard: 1 is the mod wheel, 7 channel volume, 10 pan (further left or right), 11 expression, 64 the sustain pedal. The knobs on a MIDI controller can usually be set to any free number, and in a DAW you can then link that number to any knob on a synth.

Sixteen channels

That leaves the second half of the status byte. This is the channel: a hex digit from 0 to F, so 16 channels, which people number from 1 to 16. One cable can carry notes for sixteen different instruments, and each one listens only to its own channel. 90 3C 64 is "note 60 on channel 1", 91 3C 64 is the same note on channel 2.

Play a short phrase and listen to it on three channels. The bytes are the same, and only one digit changes: the red one in the first byte.

The piano, the synth and the drums received the same sequence of numbers. The piano and the synth played the tune with different sounds, while the drums play no pitches at all: on channel 10 the note number picks a drum. This was agreed in the General MIDI standard (1991): on channel 10, 36 is the kick, 38 the snare, 42 a closed hi-hat, on any compatible instrument.

Change "Your keys send on channel" and play: in the monitor at the top of the chapter the second digit of the status byte changes. On a real MIDI keyboard the channel is set in its own settings. On stage this lets one keyboard play two synths: the keys are split into two zones, each on its own channel.

MIDI holds no sound. It is a record of what the hands did: which key, when and how hard. Any instrument can play the same stream, and each will play it in its own way.

One cable, rival companies

Until the early 1980s, synths from different makers barely spoke to each other. Each company had its own way of connecting instruments, and playing two synths of different brands from one keyboard took adapters and luck. MIDI began as an agreement between competitors.

The most remarkable thing about this story is that the agreement still holds. A keyboard bought today can control a synth from 1983: the same five-pin DIN connector (or USB), the same three bytes, the same "90 3C 64". A program on a computer, an app on a phone and a synth on stage all understand the same messages.

MIDI 2.0

In January 2020, at the same NAMM show, MIDI 2.0 was unveiled. In it velocity has 16 bits (65,536 values instead of 127), controllers and pitch bend have 32 bits, there are 256 channels (16 groups of 16), and the connection runs both ways: instruments can ask each other what each one can do. If the other side only understands MIDI 1.0, a MIDI 2.0 device falls back to the old language. So everything in this chapter still holds true for new instruments.

The MIDI file

Record messages together with their timing and you get a MIDI file, with the extension .mid, which any DAW can open. There is no sound in it, only messages, and between them the delta time: how many ticks to wait before the next event. A tick is the smallest unit of time in the file; here there are 480 ticks per quarter note.

Record a few notes, up to twelve, and look inside the file. If you don't feel like playing, open the example.

Click the green bytes: these are your note ons, the same three bytes as in the monitor. The red ones are the note offs. The grey bytes before each message are the wait before it. The file starts with the MThd header, then comes a housekeeping track with the tempo and time signature, and finally the track with your notes. The whole file is a couple of hundred bytes at most. A few seconds of sound as a WAV would take hundreds of kilobytes, but a MIDI file stores only what the hands did, and you can play it back with any sound you like.

You send this MIDI file to a friend. They open it in their own program, where the track is set to a string orchestra. What will they hear?

The file holds only note numbers, velocities and the moments each key went down and came up. The sound comes from whichever synth reads the file. That is what makes a MIDI file handy: you can change the sound at any time without playing the part again.

The last check: any message from this chapter, with no decimal hint and on different channels. Read the first digit of the first byte. It tells you the type, and the rest is routine.

Next

Now you know what leaves the keyboard: a note on with a number and a velocity, a note off, the pedal and the wheels as controllers, all of it on one of sixteen channels. You can read these bytes, play soft and loud on demand and check how evenly you play. And you have a measure for any note: its number. Note 60 is middle C.

For now, though, you still have to count from 60: plus one, plus two, plus twelve. A pianist doesn't do that. They see the whole keyboard at once. Note 60, note 62, note 65: where are they, and how do you land on the right key straight away, without counting? That is chapter 4: the map of the keyboard.

From this chapter on, the Notebook records what you play: note numbers, velocities and the moments each key goes down and comes up, the same messages you saw in the monitor. Press "Record", play something of your own and listen back. In chapter 4 the Notebook will show you which keys you played.

Chapters

  1. 00 What your ear knows
  2. 01 String and octave
  3. 02 The synth
  4. 03 MIDI
    1. Catching the messages
    2. Three bytes
    3. Note 60
    4. Velocity
    5. Letting go is a message too
    6. The wheels on the left
    7. Sixteen channels
    8. One cable, rival companies
    9. The MIDI file
    10. Next
  5. 04 Keyboard
  6. 05 Note names
  7. 06 The pulse
  8. 07 Note values
  9. 08 Meters
  10. 09 Groove
  11. 10 The staff
  12. 11 Sharps and flats
  13. 12 Bass clef
  14. 13 Intervals
  15. 14 Major and minor
  16. 15 Chords
  17. 16 Sight-reading
  18. 17 Playing by ear
  19. 18 Motif and phrase
  20. 19 Harmony and bass
  21. 20 Arrangement
  22. 21 Your own track
  23. 🎯Trainers
  24. 📓Notebook
  25. 📖Glossary