33

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.

0

7 Answers 7

39
+100

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.

8
  • StackExchange at its best! While the fluidsynth option is way more hacking than I was willing to accept (not everyone has brew installed...) it works really well and launches quicker in the script than VLC did. So I'll wait a day or two if some ridiculously simple alternative solution turns up, but if not the bounty is yours. BTW, for me fluid-synth turns up in /usr/local/Cellar/fluid-synth/1.1.6. (I don't like the old version of Quicktime solution, you never know how long it is going to work).
    – DCS
    Commented Nov 27, 2013 at 13:58
  • Yeah, I agree that "use Homebrew|MacPorts|Fink" isn't an ideal, but a) anyone who spends any amount of time using the command line is going to find themselves wanting more than what Apple can/does provide and b) lacking a built-in solution, it seemed a reasonable one here. As for QuickTime7, I agree, it’s survived longer than I figured it would, but OTOH I can't believe "QuickTime X" still can't do things QuickTime 7 could. (Makes me fearful of iWork's future, but that's another discussion for another thread on another day :-)
    – TJ Luoma
    Commented Dec 1, 2013 at 6:35
  • The recent release of iWorks actually has been crippled compared to the last version... I'm only waiting for Terminal to disappear. But enough, or we will be bashed for chatter.
    – DCS
    Commented Dec 1, 2013 at 13:42
  • @DCS We won't bash informed complaints. At worst, we'll ask they spawn to a chat room or open a new thread if warranted. Also, I edited some details out of your post. Please pop that info as an answer or edit one of the answers if that info fits with them.
    – bmike
    Commented Dec 1, 2013 at 19:06
  • I tried this on Yosemite and could not get it to work. I even uninstalled and reinstalled mac ports. Error: fluidsynth: warning: No preset found on channel 9 [bank=128 prog=56] could not handle external client request. jack main caught signal 31. Commented Mar 17, 2016 at 18:56
42

This worked for me:

$ brew install timidity
$ timidity file.midi
6
  • 1
    Worked for me too - and it's a LOT less trouble than the answer involving fluidsynth.
    – glenra
    Commented Apr 8, 2016 at 1:43
  • 3
    Best answer here. The fluidsynth answer is really clever, but it's liable to break at some point (if it hasn't already) because it's so much more complicated than this one. Commented Apr 20, 2016 at 8:31
  • This didn't work for me. After running the first command above, I ran the second, and received this message: file.midi: No such file or directory
    – user65526
    Commented Jan 11, 2019 at 11:20
  • 1
    I tried this but it didn't play anything. Instead it saved output to a .ogg file, as if I'd specified a "-o" option but I didn't.
    – sh37211
    Commented Feb 17 at 21:07
  • Same problem as @sh37211 here.
    – thorwhalen
    Commented Jun 4 at 11:57
5

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
4
  • 1
    Works! However, VLC, being the full-blown super player it is, has some very noticeable startup delay before playback starts, which my Windows solution to the problem, a super-small app named playsmf.exe did not have. That does not kill VLC for my purpose, but having something smaller would still be nice. I'll wait a few days to see if another answer pops up; if not, I'll accept yours. BTW: vlc://quit must be added to the arguments in order to quit VLC and allow my script to resume.
    – DCS
    Commented Oct 29, 2013 at 15:17
  • See my update above - damn, this used to work well!
    – DCS
    Commented Nov 26, 2013 at 21:35
  • is it possible to use an old version of vlc?
    – wrossmck
    Commented Nov 26, 2013 at 22:23
  • @RossMcKinley: While technically possible this is certainly not what I want to do. Software is updated for a reason, and I want a heavy-weight program like VLC to be up to date on my system. This would be different if it was just a 100kb mini tool, but VLC is not like that.
    – DCS
    Commented Nov 27, 2013 at 6:35
1

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.

1
  • Unfortunately, this does not work as of VLC 2.2.1 (Nov 2015) Commented Nov 23, 2015 at 17:42
1

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/

1
  • hey thanks that is something exactly like i was looking for, why i was skimming posts on midi on stackexchange Commented Jul 16, 2016 at 15:55
0

To make things more convenient, you can wrap up TJ Luoma's answer in an AppleScript app:

on open inputFile
    tell application "Terminal"
        do script "fluidsynth -i /usr/local/share/fluidsynth/generaluser.v.1.44.sf2 " & (quoted form of POSIX path of inputFile as string)
        activate
    end tell
end open

You can then select Get Info on a random .mid file, change "Open with:" to the AppleScript app, and press "Change All..." to make it the default way of opening .mid files. Now when you double-click on a MIDI file, it'll open up the Terminal window and play it automatically.

2
  • of course,that is helpful maybe for some people in general, but the op was asking about a command-line soution ;) Commented Jul 16, 2016 at 15:55
  • Oops sorry, misread the question! Commented Sep 6, 2016 at 0:20
0

The following python3 script will use macOS's CoreMIDI to play any MIDI files supplied as arguments on the command line.

#!/usr/bin/env python3

from AVFoundation import AVMIDIPlayer
from Foundation import NSURL
import time
import sys

def myCompletionHandler():
    return

def playMIDIFile(filepath):
    midiFile = NSURL.fileURLWithPath_(filepath)
    midiPlayer, error = AVMIDIPlayer.alloc().initWithContentsOfURL_soundBankURL_error_(midiFile, None, None)
    if error:
        print (error)
        sys.exit(1)
    MIDItime = midiPlayer.duration()
    midiPlayer.prepareToPlay()
    midiPlayer.play_(myCompletionHandler)
    if not midiPlayer.isPlaying:
        midiPlayer.stop()
    else:
        time.sleep(MIDItime)
    return  
    
if __name__ == "__main__":
    for filename in sys.argv[1:]:
        playMIDIFile(filename)

Here's a Swift script which will also play MIDI files given as arguments. You can run the uncompiled script (though it's slower than python!), or compile it to make a command line tool.

#!/usr/bin/swift

import AVFoundation
  
func playMIDIFile(filepath: String) {

let midiFile = NSURL.fileURL(withPath: filepath)
var midiPlayer: AVMIDIPlayer?


do {
    try midiPlayer = AVMIDIPlayer.init(contentsOf: midiFile as URL, soundBankURL: nil)

    if let time = midiPlayer?.duration {
        let hours = Int(floor(time / 3600))
        let minutes = Int(floor(time / 60))
        let seconds = Int((time)) % 60
        let timeString = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
        print("\(filepath) : \(timeString)") 
        }
    midiPlayer?.prepareToPlay()
    midiPlayer?.play({return})
        
    while midiPlayer!.isPlaying {
        usleep(10000)
    }
  } catch {
    print("could not create MIDI player")
}  

 }

// "main"
if CommandLine.argc > 1 {
    for (index, args) in CommandLine.arguments.enumerated() {
        if index > 0 {
        // Check that args is MIDI file here.
        let fileType = (args as NSString).pathExtension
        if (fileType == "mid" || fileType == "midi" || fileType == "smf") {
            playMIDIFile(filepath: args)
            } 
        }
    }
 
} else {
    // For CLI use:
    print("MIDI files required as arguments")
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .