Skip to content
Jan 5 / kkrizka

Announcing libmidi, A Very Simple MIDI File Library

I spent the last few days of my winter vacation experimenting with generating random music. This was inspired by my simple NXT Dancer robot, which currently just plays random tones. The idea behind this latest experiment is that (1) I build a probability density function of played notes from well known music and (2) I use that PDF to pick the notes to be played next, based on the previous note. I will post more details about this in a few days. But the important part is that I used MIDI files, which contain commands like “play this note” and “use this volume” to recreate the actual music.

Being lazy, I decided not to search a lot (I trie a bit) for a pre-made library for reading MIDI files and instead I decided to create my own. The result is libmidi, a very simple MIDI reading and writing library. To parse a MIDI file, just do this:

MIDI::File file("/path/to/file.mid");
file.open();

Then the contents of that file are available as they were found in the MIDI file. For example, to get a track and print out all of the events (the commands that tell the MIDI synchronizer what to do), do this:

MIDI::Track* track=file.track(0);
for(int j=0;j<track->numEvents();j++)
{
   MIDI::Event* event=track->event(j);
   event->debug();
}

Also libmidi contains a basic write support. You can add new events to the end of a track and call the MIDI::File::write() function to save the file that the track represents. See the randommusic example, included in the libmidi-0.1 package, on how to do this.

The downside to using the libmidi library right now is that it is not well developed yet. For example, there are only a few functions to modify the MIDI file (ei: tracks can only be appended to the end of the track) and not all meta event types have a nice define yet. Of course, I plan to fix this in the future.

But for now, you can download version 0.1 or checkout the latest development code at GitHUB. The API documentation is available here.

For more information about MIDI files, check out these websites:

2 Comments

Leave a comment
  1. WMF / Jan 28 2010

    You can do it with win amp, any file that can be played can be written to the disk using the disk writer output. I have done it before, many time. However for whatever reason its just not working for me.

  2. WMF / Feb 1 2010

    MIDI does not transmit an audio signal or media-it transmits “event messages” such as the pitch and intensity of musical notes to play, control signals for parameters such as volume, vibrato and panning, cues, and clock signals to set the tempo.

Leave a comment
Cancel reply