Listen to this article
Download the C# Project

Contact Rick
Twitter
RSS

Creating and Playing MIDI

Richard C. Leinecker, January 2014

The purpose of this article is to teach readers how to create MIDI files programatically. The downloadable program is named CreateAndPlayMidi, and is written in C# using Visual Studio 2008. At the heart of the program are several classes that make the MIDI file creation easy.

The Silverlight program that is embedded in this web page is not the C# program that is available for download. The downloadable program is a C# Windows form application. The Silverlight app was created simply to provide an interactive experience showing the results of the code. Since Silverlight cannot play MIDI files, several JavaScript components are used to play the generated MIDI files. The C# Windows form application has an embedded Windows Media Player component that plays the MIDI files.

The special MIDI classes are as follows:

Class NameDescription
VariableLengthStatic class providing functionality to write out standard MIDI variable length quantities
MidiTrackRepresents one track of a standard MIDISong file
MIDISongRepresents a standard MIDI song, consisting of multiple tracks of notes

MIDI files do not produce the high quality experience that MP3, WMA, and other modern audio formats produce. The reason for this is that files such as MP3 and WMA, once they are decoded, contain the literal raw audio data that can be directly played. MIDI files, on the other hand, contain commands that do things such as play a C#, set the volume, and change the tempo.

In spite of the limited quality of MIDI files, they are useful for computer-assisted musical composition. It is easy to generate a list of note values which directly correspond to MIDI note values (Wrenn, 1995). This is because they are all simple integers. Using MIDI notes, programmers can create computer-generated music with simple integers. The following chart in table 1 ("MIDI Notes," 2014) shows the standard MIDI note values.

Table 1: MIDI Note Values
Octave  Note Values
C C# D D# E F F# G G# A A# B
-1 0 1 2 3 4 5 6 7 8 9 10 11
0 12 13 14 15 16 17 18 19 20 21 22 23
1 24 25 26 27 28 29 30 31 32 33 34 35
2 36 37 38 39 40 41 42 43 44 45 46 47
3 48 49 50 51 52 53 54 55 56 57 58 59
4 60 61 62 63 64 65 66 67 68 69 70 71
5 72 73 74 75 76 77 78 79 80 81 82 83
6 84 85 86 87 88 89 90 91 92 93 94 95
7 96 97 98 99 100 101 102 103 104 105 106 107
8 108 109 110 111 112 113 114 115 116 117 118 119
9 120 121 122 123 124 125 126 127

It is easy to create a MIDI file with the included classes. You can save it to disk, or simply use the MemoryStream that is created. The following steps are the basics to creating MIDI files.

1. Instantiate a MIDISong object.

MIDISong song = new MIDISong();

2. Add a track which is specified by a text string naming the track.
song.AddTrack( "Random" );

3. Set the time signature.
song.SetTimeSignature( 0, 4, 4 );

4. Set the tempo.
song.SetTempo( 0, 112 );

5. Set the instrument.
song.SetChannelInstrument( 0, 0, 45 );

6. Add notes.
song.AddNote( 0, 0, 50, 12 );

References

MIDI Note Numbers. (2014). Retrieved from http://www.midikits.net23.net/midi_analyser/midi_note_numbers_for_octaves.htm

Wren, R. (1995). General MIDI: The ins and outs of MIDI. The American Music Teacher, 44(4), 54. Retrieved from http://search.proquest.com.library.capella.edu/docview/217467032?accountid=27965

Citations

APA

Leinecker, R. C. (2014). Creating and playing MIDI. Retrieved from http://rickleinecker.com/Rick-Leinecker-Creating-And-Playing-Random-MIDI-Files.html

MLA
Leinecker, Richard C. "Creating and Playing MIDI." Creating and Playing MIDI. Rick Leinecker, Jan. 2014. Web. 11 Jan. 2014.