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.


  • 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

  • Given today's threat environment and the increasing connectivity of digital infrastructures, security teams now realize that they must assume their IT environments are subject to periodic compromise. Gone are the days when preventive measures to secure the perimeter or trying to detect malware problems using signature-match technologies were enough. New practices based on an understanding of the phases of an attack, continuous threat monitoring, and rapid attack detection and remediation are necessary. This …

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds