Writing MIDI Files


The class library I wrote illustrates the structure of MIDI-Files.
There is a class CMidiFile, which consists of some header information
and a collection of Tracks, objects of class CMidiTrack. Each track itself
is a collection of Events, with some header information, too. Events are
such as NoteOn, NoteOff or Meta-Events like SetTempo, TrackName or Copyright.
A collection is polymorph, i.e. I can declare CMidiTrack as a collection
of CMidiEventCommand's. CMidiEvent is a base class, and for each type of
Event there is a special class. The overridden methods are the constructor,
the GetLength()-method and the WriteToFile(ostream& )-method, more is not
neccasary.
CMidiTrack has some methods to simplify some operations. The most impor-
tant member-function is the PlayString function, which takes a string you
know from the BASIC-PLAY-function. Then these few lines are enough to
create your own MIDI-file:

CMidiFile midifile(96);
CMidiTrack* miditrack = new CMidiTrack;
miditrack->Copyright(_T("Copyright (C) 1997 Andreas  Jaeger"));
miditrack->TimeSignature(0x04, 0x02, 0x18, 0x08);
miditrack->Tempo(120);
miditrack->Insert
   (new CMidiEventCommandProgramChange (MIDI_CHANNEL_2, 0, 70));
miditrack->PlayString(MIDI_CHANNEL_2, "MBT120O3T120l8dl4gl8bd"
"l4gl8bgl4al8a.l16al4aP8l8bO4l4cl8ccdcO3bal4bl8g.l16gl4gP8l8g"
"O4c.l16cl8ccl4ecO3l8bbgbO4l4dO3l8bgl4dl8f#aO4l4cO3l8af#l4ggg"
"P8l8gO4c.l16cl8ccl4ecO3l8bbgbO4l4dO3l8bgl4dl8f#aO4l4cO3l8af#l4gggP8");
miditrack->EndOfTrack();
midifile.Insert(miditrack);
midifile.WriteToFile(_T("Fr|htau.mid"));

You see you do not have to deal with Events, only with tracks.

Download demo project - 606KB