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

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds