Would you like to write for us? Or would you like to start blogging? Then become a tester of our our new AI-powered blogging platform Meta-Blogger and join our community of over 200 active bloggers worldwide!
Inversed Piano.

Create Inverted Music using Python

Ever wondered how inverted music would sound like? Do you know what the original song is of this inverted song (and don’t look at the URL!)? In this post, some MIDI files are inverted and it is shown how you can invert a MIDI file yourself.

Inverted Music

As inspired by this video, inverted music is defined as follows here. Suppose there is a basenote of value $latex V$. Any note of value $latex X$ will be changed in opposite direction, such that the inverted note has value $latex V + (X – V)$. So, the function $latex f$ which inverts a note is defined as $latex f(X) := V + (X – V)$.

As an example, assume that D is the basenote. A C (which has a distance 2 to D [C C# D]) is converted to an E (a distance 2 in the opposite direction: [D D# E]). Something funny happens with chords. A major chords will become a minor chord once it is inverted. Check it out yourself by looking at $latex f(X)$, $latex f(X+4)$ and $latex f(X+7)$.


The code can be found on GitHub. It just applies the rule above on all the MIDI tracks (except for the drum track).

    for track in mid.tracks:
        new_track = MidiTrack()
        if 'drum' in track.name.lower():
            new_track = track
            for message in track:
                if isinstance(message, MetaMessage):
                    if 'note' in dir(message):
                        inverted_note = basenote - (message.note - basenote)
                        new_track.append(message.copy(note=inverted_note, time=int(message.time)))


Python is besides music also suitable for machine learning tasks. Maybe this article on fraud detection using machine learning algorithms is interesting for you. If not, you might be interested in a simple tutorial on building a spam detection system.

Inverted MIDI files

Check out the original Fools Garden – Lemon Tree MIDI file and the inverted MIDI file.


It is actually quite fun to experiment with MIDI files. Except for the poor quality of the music, it gives lots of insights into the dynamics of a piece of music. Have fun with inverting your MIDI files :-)!

Kevin Jacobs

Kevin Jacobs is a certified Data Scientist and blog writer for Data Blogger. He is passionate about any project that involves large amounts of data and statistical data analysis. Kevin can be reached using Twitter (@kmjjacobs), LinkedIn or via e-mail: kevin8080nl@gmail.com. Want to write for us? Then please check out Meta Blogger!