MIDI
Edward Chow
The materials presented here are adapted
from MS Multimedia Development Kit, Mutlimedia Authoring Guide, Multimedia
Programmer's Guide. "MIDI: A Comprehensive Introduction", by Joseph Rothstein.
Which one is correct?
-
A stress signal?
-
A measurement somewhere between MINI and
MAXI?
-
Musical Instrument Digital Interface?
-
A standard protocol for communication between
musical instrument and computers?
Related MIDI hyperlinks
MIDI Manufacturer's Association
Tutorial
on MIDI
MIDI File
Format
MIDISCAN: converts
printed sheet music into multi-track MIDI files.
Summary
of Note Numbers by Octave
MIDI archive
MIDI Hardware
MIDI
ports
MIDI cable
Example
of MIDI Connections
MIDI Thru Box
MIDI Recording and Playback (Sequencing)
Sequencer: A
“box”, a feature built into an instrument (keyboard, synthesizer), or a
software program for recording/playing back songs stored as MIDI files
(not sound).
When a sequencer plays MIDI files, it
sends MIDI data(messages) to a MIDI synthesizer.
-
can play back the music with a different
sound than you used during recording
-
can change the speed (tempo) of the playback.
-
can enter notes one at a time (step-time
recording vs. real-time recording)
-
can edit what you have recorded.
-
much smaller file size compared with that
of digitizing sound.
MIDI messages
There are two types of MIDI messages: channel
and system messages
MIDI Message Syntax
Status byte, first data byte, [second data
byte]
example: 0x93, 0x60, 0x7F
0xC0, 0x10
Note On on Channel 3, play note 96 with velocity (how hard) 127.
-
Status byte has two nibbles, the high nibble
represents the message type, the low nibble represents the channel number
(0-15).
-
For Note on message, the first data byte
specifies the note number (0-127)
-
Piano keyboard has 88 notes (21-108 on
MIDI note number). Middle C is 60.
-
For Note on message, the second data byte
specifies the velocity (1-127).
-
Velocity indicates how hard or how fast
you press the instrument.
For velocity insensitive instrument,
64 is transmitted.
Note on with velocity 0 is equivalent
to Note off.
-
Running status - multiple MIDI channel
message with the same status byte, the status byte only need to be sent
once.
-
Note that the 7th bit of status byte is
set to one, and data byte has 7th bit set to 0.
Pressure (After-Touch)
-
Some keyboards respond to pressure on keys
after notes was played.
-
MIDI guitars usually use a variable pedal.
-
MIDI wind instruments usually use breath
pressure.
-
Two kinds of pressure messages:
-
Channel (monophonic)-aftertouch (0xD0-0xDF)-
affect all notes playing on that channel.
-
Polyphonic-key-aftertouch (0xA0-0xAF) -
pressure transmitted separately for each key. (more versatile but more
traffic, less commonly use)
Control Change (0xB0-0xBF)
provide control similar to knobs and switches.
There are 128 possible MIDI controller
numbers (0-127)
-
Number 0-31 are continuous controller (MIDI
knobs)
may take physical form of slider, wheels,
variable pedal, breath controller.
-
Number 64-95 are on/off controller (MIDI
switches)
there are buttons or pedals
-
Number 1 is Modulation Controller
-
Number 2 is Breath Controller
-
Number 7 is Volume. Use value of 80 (0x50)
for main volume controller for normal listening.
-
Number 64 is Sustain Pedal.
Program Change (Patch Select) 0xC0-0xCF
-
Most MIDI instrument offer more than one
sound for you to play.
-
Such sounds are often called programs or
patches.
-
Selecting a program on a MIDI instrument
will cause the instrument to transmit a Program Change message. This allows
the slave units to change programs accordingly.
-
It is the number of the program that is
transmitted not the sound.
-
Some instruments allow you to map the one
program number to other.
-
See the Standard MIDI Patch Assignments
next page.
-
They are based on the MIDI Manufactures
Association (MMA) General MIDI Mode specification.
Pitch-bend-change (0xE0-0xEF)
Bend the pitch up or down from whatever
the “normal” pitch is.
On keyboards, a special wheel, lever,
or joy stick near the left end is used for pitch bend.
Pitch bend, continuous controllers,
and pressure are called continuous messages.
They are often transmitted in long
streams to reflect the changing position of physical controller involved.
There are MIDI modes related to how
an instrument responds to channel messages.
Omni on - listen to all channels.
Omni off - listen to specific channels.
Poly on - play more than one note at
a time.
Mono on - play only one note at a time.
-
Mode 1: Omni On, Poly.
-
Mode 2: Omni On, Mono. (rarely used)
-
Mode 3: Omni Off, Poly.
-
Mode 4: Omni Off, Mono. (multi mode)
System Messages (0xF0, 0xFF)
There are three types of system messages:
-
System real-time
messages: for synchronizing devices, sequencer, drums.
-
MIDI Timing Clock message - transmitted
24 times every quarter note.
-
Active Sensing message - “I am still here”
message.
-
System Reset message.
There are all one-byte message (time is
critical).
Can be in the middle of another message,
in the MIDI data stream.
-
System common messages:
covers miscellaneous functions.
-
Song Position Pointer message (followed
by Continue message to synchronize the playback).
-
Song Select message.
-
Tune Request message.
-
MIDI Time Code message - give absolute
time to synchronize with external world (film, video).
-
System exclusive
messages: for certain brand or model of instrument (as an extension
to standard MIDI message).
MIDI Mapper and Windows
A part of Window Multimedia Extension system
software.
It uses a MIDI setup map to translate
or redirect MIDI messages.
MIDI Mapper Architecture
Examples of Channel/Patch/Key Mapping
Windows MIDI Authoring Guidelines
-
MIDI 1.0 Detailed Specification does not
define any standard patch assignment for synthesizer. Windows provides
MIDI authoring guideline to facilitate the representing music in multimedia
computing.
-
Use MARKMIDI src-filename dest-filename
to mark a MIDI file which conforms to the guideline.
Window MIDI authoring Guidelines
Follow these guidelines to author device-independent
MIDI files for Windows:
-
Author for both base-level and extended
synthesizer setups.
-
Use MIDI channels 13 through 16 for base-level
synthesizer data (reserve channel 16 for key-based percussion instruments).
-
Use MIDI channels 1 through 10 for extended
synthesizer data (reserve channel 1O for key-based percussion instruments).
-
Prioritize MIDI data by putting crucial
data in the lower-numbered channels.
-
Limit the polyphony of non-percussive channels
to a total of 6 notes for base-level data and 16 notes for extended data.
-
Limit the polyphony of percussive channels
to a total of 3 notes for base-level data and 16 notes for extended data.
-
Use the standard MIDI patch assignments
and key assignments.
-
Always send a program-change message to
a channel to select a patch before sending other messages to that channel.
For the two percussion channels (10 and 16), select program number 0.
-
Always follow a MIDI program-changc message
with a MIDI main-volume-controller message (controller number 7) to set
the relative volume of the patch.
-
Use a value of 80 (0x50) for the main volume
controller for normal listening levels. For quieter or louder levels,
you can use lower or higher values.
-
Use only the following MIDI messages: note-on
with velocity, note-off, program change, pitch bend, main volume (controller
7), and damper pedal (controller 64). Internal synthesizers are required
to respond to these messages and most MIDI Musical lnstruments will respond
to them as well.
-
Use the MARKMIDI utility to mark MIDI files
authored for Windows.
Standard MIDI Patch Assignments
Standard MIDI Key Assignments
Table 10.4: Note numbers for Piano. The entries are expressed in decimal format. (From Mandal text) |
Octave |
C |
C# |
D |
D# |
E |
F |
F# |
G |
G# |
A |
A# |
8 |
-2 |
00 |
01 |
02 |
03 |
04 |
05 |
06 |
07 |
08 |
09 |
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 |
105 |
106 |
107 |
7 |
108 |
109 |
110 |
III |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
8 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
|
|
|
|
On MIDI Channel 10, each MIDI Note number
("Key#") corresponds to a different drum sound
SMF: Standard MIDI Files
-
1988 adopted by MDIDI Manufacturer's Assocication
-
SMF consists of groups of data called chunks.
-
Each chunk consists of
-
4-character ID, ('MThd', or 'MTrk')
-
4 byte length field, indicating how many
bytes in the chunk,
-
the actual chunk data.
-
Two chunk types:
-
header chunk (at the beginning of the file)
ID='MThd'
-
track chunk ID='MTrk'
-
Header data include the file
format, # of tracks, and divison.
File Format
-
Format 0: single multichannel track.
-
Format 1: several tracks, played back
simultaneously, each has its own meters and tempos
The first track consists of the tempo
map to be used for all subsequent tracks. Most sequencer use format 1 for
their implementation of SMF.
-
Format 2: any number of independent tracks,
each with its own unique meters and tempos. Not common.
Division
The division value is interpreted according
to the most significant bit.
Bit value =0: the rest 15 bits are
resolution in ticks per quarter note.
Bit value =1: the next 7 bits specify
the SMPTE (Society of Motion Picture & Television Engineers) frame
rate (24, 25, 29, 30)
the last 8 bits specify the frame division (how many ticks per SMPTE frame)
Track Chunk
-
4 byte ID='MTrk'
-
4 byte length field (how many byte of chunk
data)
-
Chunk data
-
Each chunk data element = (Delta
Time, MIDI messages) = track event
Delta Time
-
The elapsed time since the preceding track
event.
-
Variable length (Max=4). Most Significant
bit used as signal.
-
MSb=1 indicates more bytes follow. MSb=0
indicates this is the last byte.
Meta Event
Describe the track names, lyric, key signature,
instrument names.
After delta time, start with 0xFF,
followed by meta event type, length of the data, data itself.
Meta Event Type
Interpretation
=============
===============================
01
Copyright
03
Instrument name
51
Tempo
58
Time signature
59
Key signature
Example of Format 0 SMF files
Another example from Mandal's text
Write MIDI content using Matlab
Here is the matlab code for generating a midi file given the midi binary content in hexadecimal. The file content is similar to the one in Figure 2.12 above, Only longer.
data=hex2dbytes('4D546864000000060001000100784D54726B0000002101C30401935B44505B00005B40505B00005B40505B000057508170570000FF2F00');
fid=fopen('Z:\cs525\Examp10_5ggge2.mid','wb');
fwrite(fid,data);
fclose('all');
Here is the link to the mid generated http://cs.uccs.edu/~cs525/midi/Examp10_5ggge2.mid
Is this four note sequence familiar? Who is the author?
Using Matlab with rats1 and rats2.eas.uccs.edu
hex2dbytes was not built in as part of the matlab r2007. I have imported a file containing the fuction to I:\Courses\CS\cs525\MATLAB\sound\hex2dbytes.m to allow the above matlab code to use the function. We need to include the file in the search path for matlab. Here are steps:
- Click File | SetPath menuitem on the matlab 7.5 as shown:
- Click "Add folder to path" and it will open up a file browser where we would like to select the folder in I:\Courses\CS\cs525\MATLAB\sound
- It is in "My computer, I drive ...
- Once select the sound folder with hex2dbytes, click Save button and the Close.
- We can then test if matlab can find it by typing "which hex2dbytes" command in the command window. If it finds it, it will echo the path as follows. Now we can run our matlab code to generate the mid file.
>> which hex2dbytes
I:\Courses\CS\cs525\MATLAB\sound\hex2dbytes.m
>> data=hex2dbytes('4D546864000000060001000100784D54726B0000002101C30401935B44505B00005B40505B00005B40505B000057508170570000FF2F00');
fid=fopen('H:\cs525\Examp10_5gggeTest.mid','wb');
fwrite(fid,data);
fclose('all');
>>
Ballade.mid
LOC 0: 4d 54 68 64 00 00 00 06 00 01 00 10 00 c0
4d 54 "MThd..........MT??"
LOC 16: 72 6b 00 00 00 7c 00 ff 7f 03 00 00 41 00 ff
58 "rk...|......A..X??"
LOC 32: 04 04 02 18 08 00 ff 51 03 0a 49 6d 00 ff 59
02 ".......Q..Im..Y.??"
LOC 48: 00 00 00 ff 01 2a 43 6f 70 79 72 69 67 68 74
20 ".....*Copyright ??"
LOC 64: 28 43 29 20 31 39 39 30 20 62 79 20 56 6f 79
65 "(C) 1990 by Voye??"
LOC 80: 74 72 61 20 54 65 63 68 6e 6f 6c 6f 67 69 65
73 "tra Technologies??"
LOC 96: 00 ff 01 2a 43 6f 70 79 72 69 67 68 74 20 28
43 "...*Copyright (C??"
LOC 112: 29 20 31 39 39 31 20 62 79 20 56 6f 79 65 74 72
") 1991 by Voyetr??"
LOC 128: 61 20 54 65 63 68 6e 6f 6c 6f 67 69 65 73 00 ff
"a Technologies..??"
LOC 144: 2f 00 4d 54 72 6b 00 00 09 61 00 ff 21 01 01 00
"/.MTrk...a..!...??"
LOC 160: ff 03 14 50 69 61 6e 6f 20 54 72 65 62 6c 65 20
"...Piano Treble ??"
LOC 176: 20 20 20 20 20 20 20 00 c1 02 00 b1 07 61 00 b1
" ......a..??"
LOC 192: 0a 7f 00 91 37 7f 60 39 7f 60 3c 7f 5e 41 7f 81
"....7.`9.`<.^A..??"
LOC 208: 40 43 7f 10 81 41 40 81 30 91 41 7f 06 81 3c 40
"@C...A@.0.A...<@??"
LOC 224: 0c 37 40 4e 91 37 7f 2e 81 39 40 32 91 39 7f 60
".7@N.7...9@2.9.`??"
LOC 240: 3c 7f 0c 81 41 40 04 43 40 50 91 43 7f 60 41 7f
"<...A@.C@P.C.`A.??"
...
4d 54 68 64 4-character chunk ID: "MThd"
00 00 00 06 length of the header: 6 bytes
00 01
file format: format
1
00 10
# of tracks: 16 tracks
in this file
00 c0
MSb=0, resolution per quarter note: 192 ticks per quarter note
4d 54 72 6b 4-character chunk ID: "MTrk"
00 00 00 7c length of the track:
124 Bytes
delta time track event
Intepretation
========= ===========
=====================================
00
ff 7f 03 00 00 41 meta event, (?) 3
bytes
00
ff 58 04 04 02 18 08 Time signature meta event, 4/4
meter?
00
ff 51 03 0a 49 6d Tempo meta event,
674157 msec/quarternote
00
ff 59 02 00 00 Key
signature meta event, C major
00
ff 01 2a 43 6f ... Copyright meta event?
4d 54 72 6b 4-character chunk ID:
"MTrk"
00 00 09 61 length of the chunk data: 2401
bytes
delta time track event
Interpretation
========= ===========
=====================================
00
ff 21 01 01 meta event?
00
ff 03 14 50 ... instrument name, 20 bytes, Piano Treble
00
c1 02 Program
change on channel 1, patch #2
00
b1 07 61 Control change on channel
1, volume level 91
(loud, 80 normal)
00
b1 0a 7f continuous controller(MIDI
knob), max. postion(127)
00
91 37 7f Note on channel 1,note=55(G,octave3),
vel=127(loud)
60
39 7f note=57,
vel=127(loud) after 96/192 quarternote
(A, octave3), running status without command
60
3c 7f note=60(C,
octave4), vel=127 after eighth note
5e
41 7f note=65(F,
octave4), vel=127 after 94/192 qnote
8140 43 7f
note=67(G, octave4), vel=127 after 192/192 qnote
10
81 41 40 Note off, note=65, vel=64(med.)
after 16/192 qnote
8130 91 41
7f Note on, note=65, vel=127
after 176/192 quarternote
06
81 3c 40 Note off, note=60, vel=64
after 6/192 quarternote
0c
37 40
note=55, vel=64, after 12/192 quarternote
4e
91 37 7f Note on, note=55,
vel=127, after 78/192 qnote
Jazz.mid
LOC 0: 4d 54 68 64 00 00 00 06 00 01 00 0f 00 c0
4d 54 "MThd..........MT??"
LOC 16: 72 6b 00 00 00 aa 00 ff 7f 03 00 00 41 00 ff
58 "rk..........A..X??"
LOC 32: 04 04 02 18 08 00 ff 51 03 08 2c a2 00 ff 59
02 ".......Q..,...Y.??"
LOC 48: 00 00 00 ff 01 2a 43 6f 70 79 72 69 67 68 74
20 ".....*Copyright ??"
LOC 64: 28 43 29 20 31 39 39 30 20 62 79 20 56 6f 79
65 "(C) 1990 by Voye??"
LOC 80: 74 72 61 20 54 65 63 68 6e 6f 6c 6f 67 69 65
73 "tra Technologies??"
LOC 96: 00 ff 01 2a 43 6f 70 79 72 69 67 68 74 20 28
43 "...*Copyright (C??"
LOC 112: 29 20 31 39 39 31 20 62 79 20 56 6f 79 65 74 72
") 1991 by Voyetr??"
LOC 128: 61 20 54 65 63 68 6e 6f 6c 6f 67 69 65 73 00 ff
"a Technologies..??"
LOC 144: 01 2a 43 6f 70 79 72 69 67 68 74 20 28 43 29 20
".*Copyright (C) ??"
LOC 160: 31 39 39 31 20 62 79 20 56 6f 79 65 74 72 61 20
"1991 by Voyetra ??"
LOC 176: 54 65 63 68 6e 6f 6c 6f 67 69 65 73 00 ff 2f 00
"Technologies../.??"
LOC 192: 4d 54 72 6b 00 00 06 be 00 ff 21 01 01 00 ff 03
"MTrk......!.....??"
LOC 208: 14 42 61 73 73 20 44 72 75 6d 20 20 20 20 20 20
".Bass Drum ??"
LOC 224: 20 20 20 20 20 00 c9 01 00 b9 07 75 00 b9 0a 7f
" ......u....??"
LOC 240: 87 00 99 24 59 30 89 24 40 82 10 99 24 5b 30 89
"...$Y0.$@...$[0.??"
LOC 256: 24 40 50 99 24 59 30 89 24 40 84 10 99 24 48 30
"$@P.$Y0.$@...$H0??"
LOC 272: 89 24 40 82 50 99 24 33 30 89 24 40 82 50 99 24
".$@.P.$30.$@.P.$??"
...
There are quite a lot of midi editors out there.
- We can specify the midi file properities by select the "operations | edit midi file properties..." menu. Enter the copyright info, change the meter and tempo.
- 1. Select the new note tool icon on the tool bar, or hit control-n (note) to start specifying the notes.
- 2. For each note click a locatio on the main drawing window,
- to specifying the playing starting time (see the time bar on the top, x position represent time)
- to specifying the key to play (see the key board on the left, y position represent a specify key)
- Then drag the note symbol to the right for a rough estimate duration of playing (the bar length corresponding the playing duration).
- 3. Right click the note bar symbol to bring up note properties dialog window and fine tuen the specification of the note.
For the above simple midi file, we run the command " ~cs520/bin/bigend simple.mid > simple.mid.txt"
and got the following data content:
argv=simple.mid
LOC 0: 4d 54 68 64 00 00 00 06 00 01 00 02 00 78 4d 54 "MThd.........xMT"
LOC 16: 72 6b 00 00 00 30 00 ff 02 13 43 68 6f 77 54 68 "rk...0....ChowTh"
LOC 32: 65 43 6f 6d 70 6f 73 65 72 32 30 30 36 00 ff 58 "eComposer2006..X"
LOC 48: 04 04 02 18 08 00 ff 59 02 00 00 00 ff 51 03 07 ".......Y.....Q.."
LOC 64: a1 20 00 ff 2f 00 4d 54 72 6b 00 00 00 36 00 c0 ". ../.MTrk...6.."
LOC 80: 30 00 90 40 64 81 6f c0 37 00 90 4a 64 01 80 40 "0..@d.o.7..Jd..@"
LOC 96: 00 21 c0 00 00 90 2e 64 2d 90 27 64 13 90 2a 64 ".!.....d-.'d..*d"
LOC 112: 1d 80 27 00 11 80 2e 00 02 80 2a 00 3a 80 4a 00 "..'.......*.:.J."
LOC 128: 00 ff 2f 00 "../."
ff 58 04 04 02 18 08 specifies the meter 4/4
ff 59 02 00 00 00: C major
ff 51 03 07 a1 20: tempo 120 Quarters per minute.
co 30 program change patch 0x30=48 (string ensemble #1)
Exercise: (Part 1 of hw2)
- Create a 10 second midi file using tool such as ablemidi. Save the midi file as <login>TheComposer.mid in your cs525 web page directory. Let us vote next wednesday on whose midi is the best. The winner gets some bonus point?
Here are some simple mid files created using Able MIDI editor.
Windows and Multimedia Extensions
Code for Playing a MIDI File
The following function opens a MIDI sequencer device, verifies that the
MIDI Mapper was selected as the output port, plays the MIDI file specified
by the lpszMIDIFileName parameter, and closes the device after playback
is complete:
/* Plays a given MIDI file using MCI-OPEN, MCI-PLAY. Returns as
soon as
* playback begins. The window procedure function for the
given window
* will be notified when playback is complete. Returns 0L
on success;
* otherwise, it returns an MCI error code.
*/
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
UINT wDeviceID;
DWORD dwReturn;
MCI-OPEN-PARMS mciOpenParms;
MCI-PLAY-PARMS mciPlayParms;
MCI-STATUS-PARMS mciStatusParms;
MCI_SEQ_SET-PARMS mciSeqSetParms;
/* Open the device by specifying the device name and device element.
* MCI will attempt to choose the MIDI Mapper as the output port.
*/
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.lpstrElementName = lpszMIDIFileName;
if (dwReturn = mciSendCommand(NULL, MCI OPEN,
MCI-OPEN-TYPE | MCI_OPEN_ELEMENT,
(DWORD)(LPVOID) &mciOpenParms))
{
/* Failed to open device; don't close it, just return
error. */
return (dwReturn);
}
/* Device opened successfully, get the device ID. */
wDeviceID = mciOpenParms.wDeviceID;
/* See if the output port is the MIDI Mapper. */
mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM,
(DWORD)(LPVOID) &mciStatusParms))
{
mciSendCommand(wDeviceID, MCI-CLOSE, 0, NULL);
return (dwReturn);
}
/* The output port is not the MIDI Mapper,
* ask if user wants to continue.
*/
if (LOWORD(mciStatusParms.dwReturn) != MIDI-MAPPER) {
if (MessageBox(hMainWnd,
"The MIDI Mapper is not available. Continue?",
"", MB_YESNO) == IDNO)
{
/* User does not want to continue. Not an error, * just
close the device and return.
*/
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
}
/* Begin playback. The window procedure function for the parent
window
* will be notified with an MM-MCINOTIFY message when playback
is
* complete. At this time, the window procedure closes the
device.
*/
mciPlayParms.dwCallback = (DWORD) hWndNotify;
if (dwreturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
(DWORD)(LPVOID) &mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
return (0L);
}