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
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);
   (new CMidiEventCommandProgramChange (MIDI_CHANNEL_2, 0, 70));
miditrack->PlayString(MIDI_CHANNEL_2, "MBT120O3T120l8dl4gl8bd"

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

Download demo project - 606KB


  • How is wave format file convert to midi format file?

    Posted by evy_ooi on 03/23/2004 03:22am

    May I know who know how wave format is convert into midiformat. What is the sharing format they using? May I have the source code please? Thank you.

  • How to read from midi device?

    Posted by Legacy on 09/29/2003 12:00am

    Originally posted by: Yves

    Does anybody know how you can read the midi instruction from a keyboard connected to the soundcard midi-interface?
    I'm looking for that info.


    • Reading instructions

      Posted by Ritchy Guerrero on 06/20/2014 10:13pm

      I used many years ago something called ' Midi View ' !! with Win98SE. I'm back in music writing. I must still have this in my goodies box!! If not I'll check the net Ritchy

  • How to read mmf file?

    Posted by Legacy on 09/12/2003 12:00am

    Originally posted by: leebaozhong


  • how to convert wave to midi

    Posted by Legacy on 08/11/2003 12:00am

    Originally posted by: brio

    Who know how to convert a wave file to midi.
    I wanna the source code. thanks.


      Posted by Leonardtlhoe on 03/28/2005 02:38am

      Wave and MIDI are totally different format, Wave has only one track but MIDI has many tracks. The track in WAVE contain combination of many tracks. After mastering each single track like percussion/voice/violin will be combined into one track. Therefore it can't convert to MIDI. You can only separate it back to each track with it mother record disc (multi-track disc). Where in that Mother disc you can find each track accordingly in each track. Finally, you need to use special machine to read each track and transfer it out to console for further mixing. Of course if the mother disc spoilt that mean it won't get back each track instead of record again each single track. That is Mission impossible to separate out single track (WAV) to multiple tracks (MIDI).

    • Me too..

      Posted by winbets on 10/28/2004 09:23am

      If any one know , help me ,please.

  • how to write our own wav files

    Posted by Legacy on 01/07/2003 12:00am

    Originally posted by: javax

    any one, how to write our own wav files?
    don't know how

  • How to read midi file?

    Posted by Legacy on 06/05/2002 12:00am

    Originally posted by: roger


  • how to rase pitch (c->c#,g->f#) while playing midi?

    Posted by Legacy on 11/23/2000 12:00am

    Originally posted by: vietfuong

    how to rase pitch (c->c#,g->f#) while playing midi?

  • how to raise pitch (c->C#,,..g->f#)while playing midi ?

    Posted by Legacy on 11/20/2000 12:00am

    Originally posted by: vietfuong

    how to raise pitch (c->C#,,..g->f#)while playing midi ?

  • how to raise pitch (c->c#,g->f#,...)while playing midi (*.mid)

    Posted by Legacy on 11/13/2000 12:00am

    Originally posted by: vietfuong

    how to raise pitch (c->c#,g->f#,...)while playing midi (*.mid)

  • Problems in opening generated midi with Windows Media Player and Winamp

    Posted by Legacy on 12/12/1999 12:00am

    Originally posted by: Tan Hung Teck

    Nice codes u have there in ur MidiGenerator for writing a midi file. However, I do have some problems with opening the resultant midi file. Commercial player like Windows Media Player and Winamp both failed to open the generated files.
    However they do work for midi files i got from windows\media. Although I am still able to open the midi using some C++ developer programs, I am still baffled by why the generated files cannot be opened by the mentioned players.
    Is there something in the header not set by MidiGenerator that is checked by the players?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date