
J.D. Koftinoff Software, ltd.
First, take a look at tests/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 );
}
}
Also see the specification at: http://www.sonicspot.com/guide/midifiles.html