# Get MIDI file format

**URL:** <https://forum.juce.com/t/get-midi-file-format/17009>\
**Category:** General JUCE discussion\
**Created:** [March 25, 2016, 1:10pm UTC](https://forum.juce.com/t/get-midi-file-format/17009 "2016-03-25T13:10:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Untero](https://avatars.discourse-cdn.com/v4/letter/u/c6cbf5/32.png) [@Untero](https://forum.juce.com/u/Untero)\
**Post date:** [March 25, 2016, 1:10pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/1 "2016-03-25T13:10:14Z")

</div>

How can I extract MIDI file format with JUCE?&nbsp;  
I've searched in MIDI Message and MIDI File classes but couldn't find this exact thing.&nbsp;  
Should I extract a value from header chunk by myself, or is there a function for that I'm overlooking?&nbsp;

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [March 25, 2016, 1:54pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/2 "2016-03-25T13:54:00Z")

</div>

Do you mean: How to get a list of MIDI events from a MIDI-file?

If so, e.g.:

```
File F = ...; FileInputStream S(F);
MidiFile midifile;
midifile.readFrom(S);
midifile.convertTimestampTicksToSeconds(); **MidiMessageSequence** seq;
seq.clear();
for (int t = 0; t < midifile.getNumTracks(); t++)
    seq.addSequence(* midifile.getTrack(t), 0.0 /*timeAdjustmentDelta*/);
seq.updateMatchedPairs();
```

That example flattens all tracks into a single sequence.

Then you can do e.g.

```
seq.getEventPointer(42)->message.getNoteNumber()
```

To play as audio, you'll probably want to manually create a MidiBuffer from the sequence.

π

PS It would be nice to have an iterator, to allow:

```
for (auto& track : midifile.getTracks()) ...
```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [March 25, 2016, 2:14pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/3 "2016-03-25T14:14:00Z")

</div>

I think he means Type 0/1/2&nbsp;midi files. &nbsp;however, your code suggest that JUCE internally doesn't care what kind of midi file it receives when you load it into a MidiMessageSequence object. &nbsp;

https://www.csie.ntu.edu.tw/~r92092/ref/midi/

Type 0

```
Format 0 MIDI files consist of a header-chunk and a single track-chunk.
The single track chunk will contain all the note and tempo information.
```

Type 1

```
Format 1 MIDI files consist of a header-chunk and one or more track-chunks, with all tracks being played simultaneously.

The first track of a Format 1 file is special, and is also known as the 'Tempo Map'. It should contain all meta-events of the types Time Signature, and Set Tempo. The meta-events Sequence/Track Name, Sequence Number, Marker, and SMTPE Offset. should also be on the first track of a Format 1 file.
```

Type 2

```
Format 2 MIDI files consist of a header-chunk and one or more track-chunks, where each track represents an independant sequence.
```

&nbsp;

Juce internally cares about the file type as shown here:&nbsp;

JUCE/modules/juce\_audio\_basics/midi/juce\_MidiFile.cpp&nbsp;line 260

But externally the MidiFile class&nbsp;doesn't. &nbsp; And neither should you!

---

<div class="post-metadata">

**Author:** ![Untero](https://avatars.discourse-cdn.com/v4/letter/u/c6cbf5/32.png) [@Untero](https://forum.juce.com/u/Untero)\
**Post date:** [March 25, 2016, 3:01pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/4 "2016-03-25T15:01:46Z")

</div>

Thanks, that's an answer I needed.

---

<div class="post-metadata">

**Author:** ![chrisgr](https://avatars.discourse-cdn.com/v4/letter/c/cdc98d/32.png) [@chrisgr](https://forum.juce.com/u/chrisgr)\
**Post date:** [September 16, 2016, 12:43am UTC](https://forum.juce.com/t/get-midi-file-format/17009/5 "2016-09-16T00:43:31Z")

</div>

I have a followup in this question. Why is there not a getFileType() method on class MidiFile. There is for example a method for getTimeFormat(). In juce\_MidiFile.cpp line 266 the file type is read but assigned to a local variable.

I made fileType a class property defined:

short MidiFile::getFileType() const noexcept  
{  
return fileType;  
}

And it seems to return correct types for type 0 and1 midi files. (I didn’t have a type 2 to test).

So should it be supported, or is there a reason why it isn’t?

---

<div class="post-metadata">

**Author:** ![dhjdhj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dhjdhj/32/39_2.png) [@dhjdhj](https://forum.juce.com/u/dhjdhj)\
**Post date:** [December 6, 2016, 1:54pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/6 "2016-12-06T13:54:19Z")

</div>

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 based on the track number, not the MIDI channel number.

---

<div class="post-metadata">

**Author:** ![dhjdhj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dhjdhj/32/39_2.png) [@dhjdhj](https://forum.juce.com/u/dhjdhj)\
**Post date:** [December 6, 2016, 3:08pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/7 "2016-12-06T15:08:23Z")

</div>

And a quick test shows that MidiFile class does not in fact break up different channels into tracks so in fact you do need to know the format.

---

<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:** [December 6, 2016, 8:20pm UTC](https://forum.juce.com/t/get-midi-file-format/17009/8 "2016-12-06T20:20:49Z")

</div>

> [@chrisgr](#):
>
> I made fileType a class property defined:
> 
> short MidiFile::getFileType() const noexcept{ return fileType;}

Sounds like a good request.

Rail
