MIDI tutorial for programmers

ThisMIDI tutorial will help you to understand how you can use the MIDI

language to control any device that uses the MIDI protocol.

MIDI Tutorial Part 1 - MIDI Messages

TheMIDI language is used to transmit real time information for the playback

of a piece of music.

"Realtime" means that each message is sent exactly at the moment it

must be interpreted by the target synthesizer (which can be a hardware

synthesizer or software synthesizer).

Variousmessages are defined to transmit the information needed to perform the

playback of music.

Theimportant point is that the MIDI language does not define the sound

itself, but only the sequence of instructions to create the sound in the

target synthesizer.

TheMIDI messages are sent as a time sequence of one or more bytes (8 bits).

The first byte is a STATUSbyte, often followed by DATAbyteswith additional parameters. A STATUSbyte has bit 7 set

to 1 and aDATA byte hasbit 7 set to 0.

The STATUS byte determinesthe type of the message. The number of DATA bytes that follow depend on

the type of the message.

Exceptfor some system MIDI messages, the STATUSbyte contains theMIDIchannel number.

There are 16 possible MIDI channels, numbered from 0 to 15 in

hexadecimal. In practice, musicians and software refer to the MIDI

channels by counting them from 1 to 16, so that there is a difference of

1 when you program them in hexadecimal (channel "1" is coded "0",

channel "10" is coded "9" and channel 16 is coded "F").

Inthe same MIDI cable, up to 16 MIDI channels may be used to control up to

16 different instruments playing independently.

MIDI RUNNING STATUS Whilereading bytes coming from a MIDI message, you must know

that the STATUS byte can in fact be omitted (except in the

first message of that type).

In such a case, you can receive a message that only has

DATA bytes. The STATUS byte is then supposed to be the

same as the last STATUS byte received.

This is called MIDI RUNNING STATUS. It is useful for

instance to optimize transmission when a long series of

the same messages are sent. An example would be a pitch

bend or crescendo volume curve.

You can also use this MIDI RUNNING STATUS when you

generate MIDI messages, but you should care about how the

target synthesizer or software will receive it, to be sure

it is well interpreted.

MIDI Tutorial Part 2 - NOTE Messages

The

main messages are the NOTE

ON and NOTE

OFF messages.

The NOTE ON message

is sent when the performer hits a key of the music keyboard. It contains

parameters to specify the pitch of the note as well as the velocity

(intensity of the note when it is hit).

When

a synthesizer receives this message, it starts playing that note with

the correct pitch and force level.

When

the NOTE OFF message is received, the

corresponding note is switched off by the synthesizer.

Every NOTE ON message requires its corresponding NOTE

OFF message, otherwise the note will play forever. The

only exception is for percussion instruments, where it can

happen that only the NOTE ON is sent, as the percussion

note stops by itself automatically. But it is better

practice to send the NOTE OFF in every case, because you

are not sure how it could be interpreted by the synthesize

receiving it.

The NOTE ON message is structured as

follows:

Status byte : 1001 CCCC

Data byte 1 : 0PPP PPPP

Data byte 2 : 0VVV VVVV

"CCCC" is the MIDI channel (from 0 to 15)

"PPP PPPP" is the pitch value (from 0 to 127)

"VVV VVVV" is the velocity value (from 0 to 127)

The pitch value determines the frequency of the note to be played. It

goes from 0 to 127, with the middle C note being represented by the

value of 60:

The value is represented in half steps, so that C# will be 61, D will

be 62,...

To transpose a note one octave higher, add 12 to its pitch value. By

using MIDI, transposition is very simple as it is done simply by

adding or subtracting a fixed value.

Be cautious however about the range of MIDI notes that goes from 0 to

127. By adding for instance 4 octaves (+48) to a note of value 96, the

total is 144, which is outside the range and may be truncated to 16

(144 - 128) so that a very low note will result.

The velocity value normally goes from 1 to 127, covering the range

from a practically inaudible note up to the maximum note level. It

basically corresponds to the scale of nuances found in music notation,

as follows (it is more indicative than exact values):

In basic synthesizers, the velocity value is used only to determine

the force with which the note is played, the only effect being a note

that is louder or softer in volume.

In more sophisticated synthesizer, this value will also affect the

sound quality. Indeed, on a real piano, hitting a note harder will not

only affect its loudness but also the quality of the sound itself, the

timber. This is practically the case with any real instrument.

There is a special case if the velocity is set to zero. The NOTE

ONmessage then has the same meaning as a NOTE

OFF message,

switching the note off.

The NOTE OFF message is structured as

follows:

Status byte : 1000 CCCC

Data byte 1 : 0PPP PPPP

Data byte 2 : 0VVV VVVV

where CCCC and PPPPPPP have the same meaning as above. The VVVVVVV is

the release velocity, which is very rarely used. By default, set it to

zero.

MIDI Tutorial Part 3 - Playing notes

and chords

When

you send a NOTE ON message to a synthesizer,

this note starts playing. Meanwhile, you can send other NOTE

ON messages, with

different note pitches, so as to hear a chord. However, you need to keep

track of the notes that are playing, so that you can send a

correspondingNOTE OFF for

each note, otherwise there will be stuck notes playing forever.

Let's

take an example. What are the MIDI messages needed to play the following

measure?

As

the time dimension must be present to hear the music, here is the time

sequence of the MIDI messages that you need to send to a synthesizer to

have it play the above music on channel 1 (remember, coded as 0), with a

velocity of 64 (mezzo forte), in hexadecimal (0x means

hexadecimal notation):

t=0 : 0x90 - 0x40 - 0x40 (Start of E3 note, pitch

= 64)

t=0 : 0x90 - 0x43 - 0x40 (Start of G3 note,

pitch= 67)

t=1 : 0x80 - 0x43 - 0x00 (End of G3 note,

pitch=67)

t=1 : 0x90 - 0x45 - 0x40 (Start of A3 note,

pitch=69)

t=2 : 0x80 - 0x45 - 0x00 (End of A3 note,

pitch=69)

t=2 : 0x80 - 0x40 - 0x00 (End of E3 note,

pitch=64)

t=2 : 0x90 - 0x3C - 0x40 (Start of C3 note, pitch

= 60)

t=2 : 0x90 - 0x47 - 0x40 (Start of B3 note,

pitch= 71)

t=3 : 0x80 - 0x47 - 0x00 (End of B3 note,

pitch= 71)

t=3 : 0x90 - 0x48 - 0x40 (Start of C4 note,

pitch= 72)

t=4 : 0x80 - 0x48 - 0x00 (End of C4 note,

pitch= 72)

t=4 : 0x80 - 0x3C - 0x40 (End of C3 note, pitch =

60)

"t"

represents the time in seconds. The score plays at 60 beats per minute,

so each quarter note is 1 second.

MIDI Tutorial Part 4 - Selecting

Instruments

Up to now, there is no information to tell the synthesizer what sound

must be used to play the notes. The synthesizer would probably use the

piano or its default instrument.

Post a Comment (0)
Previous Post Next Post