cs525 logo
animation ranibow gif

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?

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 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.

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.

Pressure (After-Touch)

Control Change (0xB0-0xBF)

provide control similar to knobs and switches.
There are 128 possible MIDI controller numbers (0-127)

Program Change (Patch Select) 0xC0-0xCF

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.

System Messages (0xF0, 0xFF)

There are three types of system messages:

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



 

Window MIDI authoring Guidelines

Follow these guidelines to author device-independent MIDI files 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

   File Format

   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

Delta Time

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
midi file format

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:

>> 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.

You can download Able MIDI editor from widisoft.com.

able midi tool image

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)

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)
{

}