Beyond #midiflip: Variations on MIDI Pitch Inversion

It’s easy to invert the pitches of a MIDI file, but inverting them in such a way that they actually sound good afterwards can be more of a challenge.  In this post, I’d like to share a few experiments I’ve carried out lately in that direction.  I think my results sound better on average than other MIDI pitch inversions I’ve heard, but it can take a little more time and effort to get them because of the number of adjustable parameters my techniques bring into play.

Basic tools for pitch-inverting MIDI notes have been available for some time now.  In November 2012, for example, Yehezkel Raz unveiled a free Ableton plugin called MIRROR and illustrated what it could do by flipping some Bach arpeggios with it.  Ableton Live 9, released a few months later, incorporated an official “invert” button for MIDI sequences, and there are comparable features in other software besides (e.g. FL Studio’s piano roll vertical flip).  However, MIDI inversion has been experiencing a dramatic surge in popularity since January 2017.  That’s when Andrew Huang posted a YouTube video of Beethoven’s “Für Elise” flipped in Ableton and invited others to share their own MIDI inversions with the Twitter hashtag #midiflip.  Among other things, Huang’s video has inspired the creation of several dedicated MIDI-flipping applications, including AutoMIDIFlip by Sophie Hamilton, Automatic MIDI Inverter by Dan Cusher, midiflip by Ret Samys, MidiFlip by Isaiah Odner, and MIDI Invert by Patrick Vares.

The principle involved here is fairly simple.  MIDI represents the notes of the scale internally by numbers ranging from 0 to 127.  Hamilton’s AutoMIDIFlip inverts that whole range by mapping the values 0-127 onto the values 127-0, while Cusher’s Automatic MIDI Inverter flips notes around the center pitch of a given file, and Huang inverted “Für Elise” around its first pitch.  Despite the variation in the choice of inversion axis, however, the idea always seems to be to invert all the pitches at once around some axis.  The highest notes become the lowest notes, and vice versa, which means that not only are pitch classes inverted (C, C#, D, etc.), but so are whole octaves.  C# and A might swap places, but so will bass and treble.

The audio processing technique that corresponds to this kind of MIDI inversion is simple frequency inversion, where an entire frequency scale gets inverted around an axis frequency (such as half the Nyquist frequency).  The results of simple frequency inversion aren’t very appealing to listen to, but there’s also another option which I introduced here: octave inversion, or inverting each octave separately.  Unlike simple frequency inversion, octave inversion inverts pitches while simultaneously preserving the distinction between bass and treble parts and a surprising amount of timbral detail besides.  Check it out if you haven’t.  When octave inversion operates on audio source material, the inversion follows a linear scale rather than a logarithmic one (or at least it does if it entails modulating by carrier frequencies and taking the lower sideband, which is what I do).  That means that notes of the chromatic scale don’t end up mapped to other notes of the chromatic scale.  Instead, we get a panoply of unfamiliar intervals which range from aesthetically interesting to downright ghastly.  That’s part of the fun.

If we octave-invert MIDI files instead of audio files, on the other hand, the notes of the chromatic scale will be mapped to other notes of the chromatic scale.  Timbres, of course, won’t be affected in the least; this is MIDI, after all.  But here’s the most interesting part: where ordinary MIDI pitch inversions flip bass and treble, octave-inverted MIDI compositions ought to keep them more or less where they are, and only the melodies and harmonies should change.

How much of a difference can this make?  Well, let’s compare the results with Beethoven’s “Für Elise,” which has become the iconic demonstration piece for this kind of work lately.  Here’s a simple inversion of a MIDI hosted at the Piano Midi Connection, carried out in typical #midiflip fashion, and more specifically by inverting the whole 0-127 range à la Hamilton’s AutoMIDIFlip:

And now here’s an octave-level inversion, with each octave’s worth of pitches flipped separately:

Both inversions are certainly interesting to hear, but the octave-level one preserves the overall balance of the original arrangement pretty effectively, whereas the simple one obscures it beyond recognition.

That said, you may have noticed a handful of anomalies in the octave-level inversion.  MIDI-based octave inversion, like the audio-based kind, introduces a discontinuity—a point at which the pitch sequence suddenly jumps an octave—midway between each pair of inversion axes.  This may be why an Ableton scale that inverts the notes of the octave is officially labeled “Inverted and Useless”; as Martin Russ observes here, the jumps are problematic for real-time keyboard playing, which is how Ableton scales typically get used.  However, just as with audio-based octave inversion, MIDI-based octave inversion can often deliver compelling results as long as we’re able to shift the discontinuities into relatively inconspicuous locations.  How do we do this?  Well, let’s define the generic pitches of a chromatic scale as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 (MIDI note numbers actually start with 0, but somehow I find it easier to think of the first pitch as 1).  If we simply flip that sequence, we’ll get 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, with the inversion axis falling midway between pitches 6 and 7 and a discontinuity occurring between pitches 1 and 12 as we repeat the following pattern all the way up the scale:

If we assign C=1 for some particular MIDI file, then there will be a jump between B and C.  But if we instead assign C=2, then there will be a jump between A and B.  Following this same logic, we can shift the discontinuity to any position we wish without affecting the underlying pitch classes.  The optimal location will vary from key to key, and even from composition to composition, depending on the idiosyncrasies of individual melodies.  General guidelines for this can be worked out, I’m sure, but trial and error may still be the best strategy.  For the octave-level inversion of “Für Elise” presented above, I set C=6 (according to the above chart), which situates the discontinuity between F=12 and G=1.

We can also select a span of pitches for inversion that’s larger or smaller than twelve.  Our results will still sound similar as long as we choose a span that remains symmetrical across multiple iterations, mapping any given pitch class across multiple octaves to the same other pitch class across multiple octaves.  Rising above twelve, options of this kind go up by increments of six: 18, 24, and so forth.  This approach reduces the number of discontinuities, so it can be helpful in handling melodies that likewise span more than an octave, but it makes the remaining discontinuities sharper and also skews bass and treble distinctions a bit more significantly, so there’s a trade-off.  Below is a chart showing the pattern we get from an eighteen-pitch inversion unit; the yellow and orange cells show pitches and the gray cells show pitch classes.  In this case, we not only reduce the number of discontinuities, but we also vary their positions so that they don’t reinforce each other quite so much across multiple octaves.

Here’s the opening of Tchaikovsky’s Piano Concerto No. 1 in B-flat Minor, from a sequence at MIDIWORLD, inverted with an eighteen-pitch unit offset upwards by three semitones from the bottom of the MIDI scale (C=4 in the above chart).  As it kicks off, you may find yourself expecting a theme and variations on “When the Saints Go Marching In.”

Some other inversion units that are symmetrical at the octave level are smaller than an octave: two, four, and six.  Of these, two and four yield rather outré-sounding results by mapping pitch classes respectively to 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11 and 4, 3, 2, 1, 7, 8, 6, 5, 12, 11, 10, 9, but a six-pitch inversion unit produces the same inverted sequence of pitch classes as a twelve-pitch one and accordingly sounds much like it—witness Liszt’s Hungarian Rhapsody No. 15, as sequenced by Bernd Krueger, inverted with a six-pitch unit:

And here are a few more examples to illustrate the effects of a straightforward twelve-pitch inversion unit on musical genres with percussion, pitch bends, and other such complicating factors thrown into the mix.

  • Bon Jovi’s “You Give Love a Bad Name,” inverted from a MIDI sequence at, sounds a lot more upbeat than the original, and the discontinuities aren’t too conspicuous except during some guitar noodling around the 2:25 mark.

  • “Satellite,” performed by Lena Meyer-Landrut as winner of the 2010 Eurovision Song Contest, inverted from a MIDI sequence at, is likewise not quite as edgy as the original, but it’s comparably catchy, which is saying something.

  • “Gangnam Style,” inverted from a MIDI sequence at, does a nice job of showing off the effects of inverting pitch bends; it’s a bit repetitive, but eminently danceable, so I’ve uploaded the whole thing just in case someone wants to play it at a club to see how people react.

How original is this alternative inversion technique?  I’m honestly not sure.  However, I’ve tried various keyword searches aiming to find any previously published example of a MIDI composition inverted at the octave level (or in groups of six or eighteen pitches), and so far I’ve come up dry.  The closest thing I’ve run into is an inversion of “Moscow, 1941” by YouTube user Woochia, who mentions that he tried to keep the various instruments of the orchestra in their original tessitures—not quite the same thing, but it could have a similar effect.

It should be easy to adapt the same principle to invert octaves without inverting pitch classes, but that doesn’t interest me as much, so I’ll leave it for someone else to try.

There is one other little modification I’ve experimented with, though.  In his video about inverting Beethoven, Andrew Huang remarks: “One thing I didn’t really even think about until now is that when you invert these pitches but you keep the intervals the same, you’re also preserving the chords—the types of chords anyway; they’re in different voicings.”  However, he qualifies that statement with some overlaid text:If it’s true that “major becomes minor and vice versa,” then we should be able to create an inversion that preserves major/minor distinctions by changing the mode of a piece of music before inversion from major to minor (or minor to major).  Let’s try this idea out on the opening of “Spring No. 1 (Allegro)” from Vivaldi’s Four Seasons, as sequenced by George Pollen.  First, here’s an octave-level inversion with an offset of -1.

But now let’s change the mode of the original MIDI from E major to E minor by globally switching G, C, and D with their sharped counterparts.

When we now invert the altered E minor version instead of the original E major version, the result does indeed come out major rather than minor.  I’m not sure whether I prefer the major or minor inversion, but it’s nice to have a choice!

Finally, just for comparison, here’s a simple inversion of the same piece—that is, the sort of inversion people have ordinarily been making and sharing under the #midiflip hashtag.  Spoiler alert: it didn’t come out very well, so if simple inversion were the only tool in our kit, we might well conclude that Vivaldi’s Four Seasons just isn’t a promising candidate for this sort of thing.

Want to try it yourself?

I implemented the principles described above in MATLAB (interchangeably with GNU Octave), piggybacking on Ken Schutte’s scripts for reading and writing MIDI.  As long as you know—or can figure out—how to run a script in MATLAB or GNU Octave, you should be able to run my code.  However, kindly bear in mind that it’s still experimental.  If you run it with unexpected settings, it might give you an error message, and while I think that’s the worst thing that could happen, be advised that my code comes with no warranty whatsoever.  It seems to work OK with most MIDI files, but I’ve occasionally run into mysterious problems using it for which I can’t yet account.

The easiest method I could figure out for changing the numerical values of notes without altering any other parameters entails using Schutte’s readmidi function to convert MIDI files into structure arrays; finding all values of—the field where note numbers are stored—corresponding to midi.track.messages.type entries between 128 and 159—”note on” or “note off”—and to midi.track.messages.chan values other than 9 (percussion track); adjusting these as desired; and then exporting the result using Schutte’s writemidi function.  I can also adjust “pitch bend” instructions by modifying values 1 and 2 corresponding to midi.track.messages.type entries between 224 and 239 (taking the difference from 127 of both values inverts the direction of the bend, if I’m understanding things correctly).

The function file I wrote is midivert.m, which you can download here; it references Ken Schutte’s writemidi.m and readmidi.m, which you’ll also need.  The input arguments, all optional, are:

  • type (default=0).  0 inverts by octaves, 1 inverts all at once, and 2 maps notes to the values listed in noteSequence.
  • offset (default=0).  With type=0, the designated value is added to all MIDI note numbers before inversion, and then added to the result. This procedure shifts the positions of discontinuities without affecting the underlying pitch classes.  If repunit=12, then the discontinuities will fall between note classes as follows: 0=(B/C), 1 or -11=(A/B), 2 or -10=(A/A), 3 or -9=(G/A), 4 or -8=(G/G), 5 or -7=(F/G), 6 or -6=(F/F), 7 or -5 =(E/F), 8 or -4=(D/E), 9 or -3=(D/D), 10 or -2=(C/D), 11 or -1=(C/C).  With type=2, the pattern in noteSequence will be applied beginning at the corresponding MIDI note number; thus, an offset of +2 will map the values in noteSequence to the source MIDI note numbers [2,3,4,5,6,7,8,9,10,11,12,13] rather than to the source MIDI note numbers [0,1,2,3,4,5,6,7,8,9,10,11].  If 0 is treated as the tonic in the source MIDI, then the offset should be applied to keys as follows: 0=C, 1=C/D, 2=D, 3=D/E, 4=E, 5=F, 6=F/G, 7=G, 8=G/A, 9=A, 10=A/B, 11=B.  After the mapping occurs, the offset value is added to each result to bring it back into conformity with MIDI note numbers.  With type=1, the offset setting has no effect.
  • transpose (default=0).  The output is transposed up or down by this many semitones.
  • repunit (default=12).  Pertains only to type=0, and specifies the size of the inversion unit.  With a value of 2, for example, the note numbers [0,1,2,3,4,5] would be inverted to [1,0,3,2,5,4]; with 3, they would instead be inverted to [2,1,0,5,4,3].
  • noteSequence (default=[11,10,9,8,7,6,5,4,3,2,1,0]).  Pertains only to type=2.  This specifies the values to which the pitch classes corresponding to MIDI note numbers [0,1,2,3,4,5,6,7,8,9,10,11] will be mapped.  The default just inverts the octave, but another option is [0,1,2,4,3,5,6,7,9,8,11,10], which will convert between major and minor scales if 0 is set to the corresponding tonic using offset.
  • pb (default=1).  If this is set to 1, pitch bend instructions are inverted.  Otherwise, they’re left unaltered.

Any values that ultimately end up outside the 0-127 range get transposed to the nearest available octave.  And for what it’s worth, I used Direct MIDI to MP3 Converter to convert MIDIs into mp3s for this blog post.  If you try out this code, or octave-level MIDI inversion more generally, I’d like to hear how things go for you—kindly drop me a note in the comments section below!


2 thoughts on “Beyond #midiflip: Variations on MIDI Pitch Inversion

  1. There is basically 3 ways of doing midiflip with mid files.

    The first one is andrew method:

    If lowest note (not frequency) is X and a note at the midi is X+Y, this note will become X-Y.

    The second method is like this:
    Its like the first, but instead of using the lowest note, you use the middle note between the lowest and higest one of the original song.

    The third method:

    The third method is the least arbritrary, Imaigne the closest note to 20hz (lowest frequency human can hear) is X and the closest note to 20000hz (highest frequency the human can hear) is Y.
    If a note is X, this note will become Y, and if a note is Y it will become X and so on.

    • These are all solid options for choosing a single inversion point for a “simple” flip, as opposed to inverting successive sub-units (e.g., octaves) as I’ve described here. I could also see going with mean or median note numbers for a “simple” flip.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s