Error in Copying Time Format of MIDI File?


I’m copying some tracks from one MIDI file, track by track message by message. All transfer correctly except the header message for which the time format is getting mangled if I don’t set it manually. Time format in the header gets mangled from ticksPerQuarter note into a SMPTE format. I have to manually get the time format from the original and write it to the output file to get the time format to transfer correctly.

For example:

int tracksToCopy = midiFile.getNumTracks();
MidiFile outputFile;
short  timeFormat = midiFile.getTimeFormat();
for (int trkNumber=0;trkNumber<tracksToCopy;trkNumber++)
    MidiMessageSequence seq;
    const MidiMessageSequence *theTrack = midiFile.getTrack(trkNumber);
    const int numEvents = theTrack->getNumEvents();
    for (int i=0;i<numEvents;i++)
        MidiMessage msg = theTrack->getEventPointer(i)->message;
std::cout << "Original Time Format " << midiFile.getTimeFormat() << "\n";
std::cout << "Output Time Format " << outputFile.getTimeFormat() << "\n";

If I don’t do the:


Output Time Format becomes negative (SMPTE) even if the original time format is positive (Ticks Per Quarter)

Every other record in the file, out of thousands of messages of different types is copied correctly. So it seems only the header record is getting mangled.


why not just do
outputFile.addTrack( *midiFile.getTrack(trkNumber) ); ?