C++ MIDI Library - FAQ
Licensing
Q:
I want to use the library in a closed-source application. May I?
A:
This library is released under the GPLv2, and may not be used in closed source applications unless you contact Jeff Koftinoff <jeffk@…> and specify what your requirements are and we can discuss the licensing.
Comments
Q:
I'm confused! There are not enough comments!
A:
Yes, I am adding more comments and examples in my spare time.
Contact Jeff Koftinoff <jeffk@…> with specific questions in the meantime.
Creating a MIDI file
Q:
I looked at several of the examples included in your library, but I was unable to find an example of how to generate notes and throw them into a *.mid file. I was wondering if you could send me a small example on how to generate a simple midi file
A:
First, take a look at jdkmidi_rewrite_midifile.cpp
It reads a midifile and then writes it out. You want to write out a file of your own events.
First you must create and fill in a MultiTrack object with your data:
// the object which will hold all the tracks jdkmidi::MIDIMultiTrack tracks; tracks.SetClksPerBeat( 24 );
Create individual midi events with the MIDITimedBigMessage and add them to a track:
{
MIDITimedBigMessage m;
m.SetTime( 24 );
m.SetNoteOn( channel, note, velocity );
if( !tracks.GetTrack(1)->PutEvent( m ) ) { abort(); }
m.SetTime( 48 );
m.SetNoteOff( channel, note, 0 );
if( !tracks.GetTrack(1)->PutEvent( m ) ) { abort(); } // add messages in time order!
}
Track 0 is used for tempo and time signature info:
{ MIDITimedBigMessage m; m.SetTime( 0 ); m.SetTimeSig( 4, 4 ); if( !tracks.GetTrack(0)->PutEvent( m ) ) { abort(); } m.SetTime( 0 ); m.SetTempo32( 120 * 32 ); // tempo stored as bpm * 32, giving 1/32 bpm resolution if( !tracks.GetTrack(0)->PutEvent( m ) ) { abort(); } }
To write the multi track object out, you need to create an output stream for the output filename:
// create the output stream jdkmidi::MIDIFileWriteStreamFileName out_stream( outfile_name );
And then output the stream like my example does, except setting num_tracks and division to match your data.
int num_tracks = 2; int division = 24; if( out_stream.IsValid() ) { // the object which takes the midi tracks and writes the midifile to the output stream jdkmidi::MIDIFileWriteMultiTrack writer( &tracks, &out_stream ); // write the output file if( writer.Write( num_tracks, division ) ) { return_code = 0; } else { fprintf( stderr, "Error writing file '%s'\n", outfile_name ); } }
MIDI File Structure
Q:
Where do I get more info on midi file structure?
A:
See the specification at: http://www.sonicspot.com/guide/midifiles.html
Linker errors
Q:
I get linker errors trying to build jdkmidi_test_drvwin32.cpp on windows.
A:
On windows you must link in the standard WINMM.LIB library - It contains the MIDI I/O functions.
