jimm/midilib

Saving midi files as type 0?

rhelsing opened this issue · 6 comments

Is this possible? How would I best go about implementing functionality like this and contribute?

jimm commented

It's certainly possible. A few different approaches are possible. You'd probably want to reuse the low-level methods in MIDI::IO::SeqWriter such as write_var_len and write16 but override/replace functions like write_to, write_header, and write_track.

To keep the public interface the same, you might want to add an optional argument to SeqWriter#initialize for the MIDI file format that defaults to 1 but can accept 0 as well. In that case you would probably end up rewriting write_to so that it calls something like write_format_0 or write_format_1 depending on the requested format.

The trick bit would be merging all of the tracks' events into one list of properly time-ordered events first so that you can write that out. Actually, it's not that tricky --- Track already has the merge function so you could create a new temporary track and merge all of the events from those tracks into that one so you can write it out.

I'm not sure what other differences there are in a MIDI file format 0, so I have probably missed something.

Does tha tall make sense?

@jimm Yes this does make sense. Thanks for taking the time to write that up.

jimm commented

As a side note, I just realized that reading a type 0 file is already supported! A type 0 file is just a type 1 file with a format byte of 0 and a track count of 1.

@jimm thats great! so if the tracks are merged, and writing with a format byte of 0, that should solve it correct?

jimm commented

Yes, I think that's it.

jimm commented

@rhelsing implemented this feature in #17 which has been merged.