How can I play a midi file from terminal?

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.


Solution 1:

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.

Solution 2:

This worked for me:

$ brew install timidity
$ timidity file.midi

Solution 3:

You can use VLC with a non-interactive CLI interface with -I dummy:

/Applications/VLC.app/Contents/MacOS/VLC -I dummy file.mid vlc://quit

To enable midi support, go to Preferences > Show All > Input / Codecs > Audio codecs > Fluid Synth and set the soundfont to a file like the FluidSynth soundfont from http://www.schristiancollins.com/generaluser.php.

Edit: midi support was removed from recent version of VLC. You can still use fluidsynth from the shell as described by TJ Luoma:

brew install fluidsynth
wget http://www.schristiancollins.com/soundfonts/GeneralUser_GS_1.44-FluidSynth.zip
unzip GeneralUser_GS_1.44-FluidSynth.zip
mkdir -p /usr/local/share/fluidsynth
mv GeneralUser\ GS\ 1.44\ FluidSynth/GeneralUser\ GS\ FluidSynth\ v1.44.sf2 /usr/local/share/fluidsynth
fluidsynth -i /usr/local/share/fluidsynth/GeneralUser\ GS\ FluidSynth\ v1.44.sf2 file.mid

Solution 4:

According to this post on VLC's own forums Felix Paul Kuehne, the site admin, said

Hello, apparently, FluidSynth got lost in the compilation process for the last update. Be assured that it will be back in 2.1.2 very soon!

So for now either downgrade to vlc 2.0.9, or wait for vlc 2.1.2. This way, your existing solution will work as it used to.

Solution 5:

Not exactly the answer but there is a small app that can send midi data on the Mac OS X command line. It can also control basic GarageBand functions as well: http://www.bibiko.de/music/MIDImyAPP/