MIDI smash

I’ve been looking into why Melody Assistant and GarageBand seem to have trouble communicating on the subject of pitch bend.

Turns out, Melody Assistant puts a pitch bend range of +-24 semitones in the MIDI files it writes.  GarageBand, on the other hand, apparently ignores this and assumes +-2 semitones.  Thus all pitch bends are rendered a factor of 12 too small.

So, okay, I figured. There must be a Perl module for parsing MIDI files. Use that to multiply all pitch bends by 12 (and specify +-2 semitones range, so apps like QuickTime Player that do respect the specified range will do the right thing). Indeed there is a Perl MIDI module. It can take a MIDI file, break out the tracks, break out the events in each track, parse the events, and so on. And then write them out. Great!

But [you knew there was a but coming, right?]… the documentation says:

MIDI tracks have, currently, three attributes: a type, events, and data. Almost all tracks you’ll ever deal with are of type “MTrk”, and so this is the type by default. Events are what make up an MTrk track. If a track is not of type MTrk, or is an unparsed MTrk, then it has (or better have!) data.

In other words, 1) events are meaningful only in an MTrk track, 2) you probably don’t want both data and events defined, and 3) 99.999% of the time, just worry about events in MTrk tracks, because that’s all you ever want to deal with anyway.

So… of course… in the MIDI file written by Melody Assistant, each track has zero events and a bunch of data, which the Perl MIDI module does not parse.

I can see what needs to be done using a MIDI editor — I’m using MidiKit. I can even do it by hand, using MidiKit. It’d only take about a month for “All Roads Lead Away From Rome”. They call it a MIDI editor, but does it do global search and replace? I don’t think so.

WAIT WAIT WAIT WAIT I just remembered seeing something, where was it, yes there, download compile check install that works…


It works. midicsv converts MIDI files into comma separated value (csv) text files, and there’s a cvsmidi that converts them back. By converting allroads.mid to allroads.csv, pulling that into a text editor, making changes like

< 12, 38976, Control_c, 10, 6, 24
> 12, 38976, Control_c, 10, 6, 2
< 12, 38976, Pitch_bend_c, 10, 8260
> 12, 38976, Pitch_bend_c, 10, 9008
< 12, 39072, Pitch_bend_c, 10, 8124
> 12, 39072, Pitch_bend_c, 10, 7376
< 12, 39168, Pitch_bend_c, 10, 8056

and converting back… I get a MIDI file with pitch bends that play correctly in both GarageBand and Quicktime Player.  (Pitch bends are integers offset from 8192, so x becomes 12(x-8192)+8192.)

See, I told you it was easy!  This means I can write microtonal stuff in Melody Assistant and then mess with it in GarageBand.* W00t.

*As long as I don’t want to do something crazy like, oh, I don’t know, change tempo during a piece.

3 thoughts on “MIDI smash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s