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



Comments

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

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

    Thanks
    Yves

    • 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

      Reply
    Reply
  • How to read mmf file?

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

    Originally posted by: leebaozhong

    thanks!

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

    • MISSION IMPOSSIBLE!!

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

      Reply
    • Me too..

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

      If any one know , help me ,please.

      Reply
    Reply
  • 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

    Reply
  • How to read midi file?

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

    Originally posted by: roger

    Please!

    Reply
  • 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?

    Reply
  • 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 ?

    Reply
  • 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)

    Reply
  • 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?

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds