# How to get MidiFile format type

**URL:** <https://forum.juce.com/t/how-to-get-midifile-format-type/46796>\
**Category:** General JUCE discussion\
**Created:** [July 2, 2021, 4:24pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796 "2021-07-02T16:24:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnymayhew28](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johnnymayhew28](https://forum.juce.com/u/johnnymayhew28)\
**Post date:** [July 2, 2021, 4:24pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796/1 "2021-07-02T16:24:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [July 2, 2021, 5:00pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796/2 "2021-07-02T17:00:53Z")

</div>

> [@Get MIDI file format](https://forum.juce.com/t/get-midi-file-format/17009/6):
>
> If the MIDI file is format 0, does the JUCE MidiFile class break the events into different tracks based on the MIDI channel number of the event? If so, I agree that you don’t need to know whether you have a format 0 or format 1. However, if it’s a format 0 file and the data is not split, then you definitely care whether it’s a format 0 or format 1 file because on a multi-track file, you often won’t care about the actual MIDI channel of an internal event and you’ll want to assign instruments bas…

Rail

---

<div class="post-metadata">

**Author:** ![johnnymayhew28](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johnnymayhew28](https://forum.juce.com/u/johnnymayhew28)\
**Post date:** [July 2, 2021, 5:15pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796/3 "2021-07-02T17:15:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [July 6, 2021, 1:11pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796/4 "2021-07-06T13:11:34Z")

</div>

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

> <https://github.com/juce-framework/JUCE/commit/1fb22035172a0bf077e8cdb85a5d5a597cc4f401>

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

> [@johnnymayhew28](#):
>
> 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.

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.

```auto
juce::MidiBuffer buffer;

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

```

---

<div class="post-metadata">

**Author:** ![johnnymayhew28](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johnnymayhew28](https://forum.juce.com/u/johnnymayhew28)\
**Post date:** [July 6, 2021, 1:36pm UTC](https://forum.juce.com/t/how-to-get-midifile-format-type/46796/5 "2021-07-06T13:36:31Z")

</div>

Thank you reuk ! 🙏 That’s wonderful 🙂

As for the second question, I found the example in [this answer](https://forum.juce.com/t/playing-midi-file-thru-synth/16962/2) to be particularly useful, and yes it’s pretty much exactly as you said \<3
