Audio playback with Python
Mon Apr 02, 2007 · 149 words

I was really surprised there's seemingly no way to do cross-platform sound output with Python. Looks like you can definitely do it under Windows, and maybe even on Linux, but I couldn't find any info about OS X. There's the rather cryptic mention of Carbon.Snd Sound Manager in the Python docs but I honestly have no idea what that is or how to use it.

Anyways, since I (and probably any other Python user on OS X) had PyObjc installed, I decided to use Cocoa to do my sound playback. It turned out to be much easier than I anticipated:

import objc
from AppKit import NSSound

click = NSSound.alloc().initWithContentsOfFile_byReference_("click.wav", True)
click.play()

This is for a metronome app and is less than optimal because the wave file can't be played back fast enough (100 and 200 BPM sound the same), but it's a start! More info on using NSSound here.


back · essays · credits ·