Reading and Transforming a MIDI File

Written by on April 27, 2016 in LISP, Music, Programming with 0 Comments

Sometimes it can be interesting to take a MIDI file and transform the score it represents. For example, you can change the tempo, alter the instruments, or even modify the pitches and rhythm. To do something like this, it is helpful to build a convenient framework. In today’s post, I am outlining one approach that you can start with and enhance as needed to suit your requirements.

This discussion applies to the Opusmodus software environment.

First we need to convert a MIDI file to a def-score object (actually, a list with name, title section and multiple instrument sections). The function convert-midifile-to-score does just that. To make it easy to traverse the different sections and apply an operation on each one, we define a function called transform-score. It takes a score and a user-written handler function and applies the handler on each section of the score.

The handler function provided by the user takes two arguments: the actual section it should operate on and an ID that denotes that section. In the current implementation, there are three types of sections, namely, the score name, title section (with fields such as :composer, :time-signature, etc.) and one or more instrument sections. If the handler chooses not to modify a particular section, it can return t. Returning nil will cause that section to be skipped in the output.

I have also defined a utility function called replace-key-values that is handy when you want to change any key-value pair in a section, for example in the title section.

Transform Score

Transform Score

What I have outlined is just a basic idea. You can extend this further by introducing sophisticated pattern matching techniques to manipulate the score object.

You can download the code here. The sample MIDI file that I have used is available here. Make sure that you change the file path appropriately if you are testing this code.

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *