Mike Slinn

midi_create - A Ruby Program to Create MIDI Files

Published 2024-10-28.
Time to read: 2 minutes.

This page is part of the av_studio collection.

I decided to write a small Ruby program to generate MIDI files. See How Pro Tools Imports MIDI Files for the motivation for doing this.

About Middle C

In Pro Tools and Ableton Live, middle C is known as C3. See a discussion about middle C, and this article.

Creating the Ruby Gem Scaffold

I used the F/OSS nugem command-line program that I published a few years ago to create the scaffold for my new Ruby gem.

I did not use nugem’s --executable option, because the current version of nugem generates code for Thor instead of OptionParse. Previously, I discussed why most people should not use Thor to generate command-line skeletons. One day I will update nugem to generate scaffolding for OptionParse instead of Thor.

You can inspect the OptionParse code that I wrote for midi_create.

Shell
$ nugem plain midi_create
Creating a scaffold for a new plain Ruby gem named midi_create in /mnt/f/work/ruby/my_gems/generated/midi_create.
Creating the local git repository
Initialized empty Git repository in /mnt/f/work/ruby/my_gems/generated/midi_create/.git/
Do you want to create a repository on GitHub named midi_create? (y/N) y
Creating a remote #<struct Nugem::Repository::Host domain="github.com" , camel_case="GitHub" , id=:github> repository
Pushing initial commit to remote #<struct Nugem::Repository::Host domain="github.com" , camel_case="GitHub" , id=:github> repository
Enumerating objects: 32, done.
Counting objects: 100% (32/32), done.
Delta compression using up to 24 threads
Compressing objects: 100% (29/29), done.
Writing objects: 100% (32/32), 8.09 KiB | 188.00 KiB/s, done.
Total 32 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:mslinn/midi_create.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
The midi_create gem was successfully created.
There are no TODOs. You can run 'bundle install' from within your new gem project now. 

midilib

After looking at available Ruby gems I decided to try midilib to create the empty MIDI files. It has 34 forks and 181 stars.

midilib can read and write MIDI files in two common flavors of MIDI:

  • Format 0 (a single track)
  • Format 1 (multiple tracks)

By default, midilib writes format 1, which is the most common format. MIDI file format 2 is not yet supported, and it is rarely seen in the wild at the time of writing.

The Completed Program

The midi_create repo on GitHub contains the source code. Assuming you have installed Ruby on your computer, you could install midi_create by typing the following at a shell prompt:

Shell
$ gem install midi_create

If you are running rbenv (and you should, according to the instructions mentioned above), type:

Shell
$ rbenv rehash

This is the help message:

Shell
$ midi_create -h
midi_create: Creates a MIDI file containing an 8-note scale in the key of C,
             starting at middle C (C3 in Pro Tools and Ableton Live).

Syntax: midi_create [Options] PATH_TO_MIDI_FILE

Options:
  -b 120   Specify beats per minute (BPM); default is 120 bpm.
           Only integers can be used; a decimal point will cause an error.
  -f       Overwrite the output file if present
  -t NAME  Use this title for the track
  -h       Show this help message 

Sample Usage

Create a MIDI type 1 file called filename.mid with one track in the current directory, without a title. Fail if the file already exists. Include an 8-note scale in the key of C major, starting at middle C (C3).

Shell
$ midi_create filename.mid

Like the previous example, but overwrite filename.mid if it already exists:

Shell
$ midi_create -f filename.mid

Like the previous example, plus include the title Test MIDI clip for the track. Ableton Live ignores the MIDI track title, but Guitar Pro uses it.

Shell
$ midi_create -ft 'Test MIDI clip' filename.mid

Like the previous example, plus set the beats per minute (BPM) to 150. BPM can only be specified as an integer; using a decimal point will cause an error.

Shell
$ midi_create -b 150 -ft 'Test MIDI clip' filename.mid
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.