How to get MidiFile format type

Hi,
I’m trying to read the MIDI format type from a MidiFile object - i.e. 0, 1, or 2, the same as the second argument to MidiFile.writeTo() - but there doesn’t seem to be a method for this ? Am I missing something or am i supposed to read it from the file separately myself ?
Thanks <3

Rail

So there’s a feature request in for it from 2016 ?

I should clarify that the purpose of this is that I am trying to write a plugin which will read, visualize, and output a midi file as a stream to the plugin output. I would like to know the MIDI format type of the file so that if it’s a type 2 file i can handle it differently (probably reject the file). Type 0 is easy enough to detect because there’s only 1 track.

An additional question:
How can i move the messages stored in the MidiMessageSequences in the MidiFile object into the MidiBuffer for the plugin output ? I’m spending a long time looking at MIDI documentation and some tips would be helpful.

Thanks <3

I’ve added an extra optional out-parameter which can be used to query the kind of file which was read by MidiFile::readFrom:

Please try it out and let me know if you run into any issues.

I don’t think there’s a one-liner for this, but I think you should be able to call MidiFile::getTrack to extract a MidiMessageSequence representing that track. Then, you can iterate through the Sequence and add each individual message to your MidiBuffer.

Here, I’m using the imaginary function timeStampToSample to convert between the time units used by the MidiMessageSequence and the MidiBuffer. After reading a MIDI file, the timing information will be in ticks rather than seconds, so you might need to call convertTimestampTicksToSeconds to convert the event times to seconds, and then multiply by your samplerate to convert to sample positions.

juce::MidiBuffer buffer;

if (const auto* track = midiFile.getTrack (0))
  for (const auto* holder : *track)
    buffer.addEvent (holder->message,
                     timeStampToSample (holder->message.getTimeStamp()));

Thank you reuk ! :pray: That’s wonderful :slight_smile:

As for the second question, I found the example in this answer to be particularly useful, and yes it’s pretty much exactly as you said <3