MacOS – How to play a midi file from terminal

macosmiditerminal

I'm looking for a most simple command line app (or an on-board tool on Mavericks I am not aware of) to play a midi file from the terminal. As plain as possible, something like play myfile.mid.

Context: I'm playing around with midi in Python and I need something I can launch from a script. No GUI, no windows, just playback. It must be callable from the Python script to be accepted, but anything that works in terminal should be.

Best Answer

This turned out to be a more complicated problem than I originally expected.

QuickTime X cannot play MIDI files, although QuickTime 7 could.

As far as I can tell that means that there is no "built-in" solution to playing MIDI files on Mac OS X (for example, afplay does not work). Therefore I believe that any solution will involve downloading and installing some other program.

Option #1: Download and install QuickTime 7 which still works fine on Mavericks, and then you can play midi files by:

open -a QuickTime\ Player\ 7 /path/to/your/file.mid 

however that will only autoplay if the user has enabled that preference, which I believe is off by default.

Option #2: Use FluidSynth

To install it, you have to be using either Fink, MacPorts, or (my recommendation) Homebrew. Once Homebrew is installed, type this in Terminal:

brew install fluidsynth

(MacPorts' command would be sudo port install fluidsynth and Fink's would be fink install fluidsynth.)

However, downloading fluidsynth only gets you part-way there. Then you need a "SoundFont" file, which I had never heard of before. There is information about them here

I downloaded one from S. Christian Collins called "GeneralUser" which is free. The current version (as of 2013-11-27) is FluidSynth version 1.44. {If that direct link breaks in the future, use the previous link which will take you to the regular web page for GeneralUser.}

Once you have downloaded and unzipped that, you will have a series of files including "GeneralUser GS FluidSynth v1.44.sf2" (obviously the name may change in the future). I renamed that file and moved it to /usr/local/share/fluidsynth/generaluser.v.1.44.sf2.

Once the SoundFont file is place and fluidsynth is installed, you can play a midi by using this command:

fluidsynth -i /usr/local/share/fluidsynth/generaluser.v.1.44.sf2 ~/Music/example.mid 

n.b. There are some (seemingly harmless) error messages which get displayed when you do that. If you want to suppress them use:

(fluidsynth -i /usr/local/share/fluidsynth/generaluser.v.1.44.sf2 ~/Music/example.mid 2>&1) >/dev/null

instead.

Obviously I'm never going to remember all of that, so I made a zsh function called playmidi

function playmidi {

    SOUNDFONT='/usr/local/share/fluidsynth/generaluser.v.1.44.sf2'

    if [ -e "$SOUNDFONT" ]
    then 

        for i in "$@"
        do 
            if [ -e "$i" ]
            then
                (fluidsynth -i "$SOUNDFONT" "$i"  2>&1) >/dev/null
            else
                echo "[playmidi]: cannot find file at $i"   
                return 1
            fi  
        done 
    else
            echo "[playmidi]: SOUNDFONT file not found at $SOUNDFONT"
            return 1
    fi  
}

(That should work for bash too I believe.)

Now all I have to do is type:

playmidi example.mid 

and example.mid will play.