[Pythonmac-SIG] getting/setting system volume in OS X with python?

Bob Ippolito bob at redivi.com
Thu May 5 04:35:53 CEST 2005


On May 4, 2005, at 9:51 PM, Erik Osheim wrote:

> I've been developing a curses-based music player in python for the
> last couple of years (http://www.bearhome.net/mpy3) and it is getting
> pretty good these days. I have added keybindings to control volume in
> linux using the ossaudiodev module found in python.
>
> I wanted to do the same thing under OS X which I am now trying to
> fully support. After some digging, it looked to me like Carbon.Snd was
> the ticket. However, I can't find any documentation on this module; I
> did some digging in Apple provided docs, and was able to get a
> semi-working, semi-broken version of volume control going (mutes one
> ear, controls the volume of the other), but I am not sure this will do
> it.
>
> My questions are:
>
> 1. Does anyone have a good idea how to go about doing this best on OS
> X? I am not going to be able to support OS 9 (too many unix
> dependencies) so if there is a cleaner way to do it than Carbon I
> would be interested.

The best way to do it on OS X is to use CoreAudio, but none of that  
is wrapped in Python.  You can, however, find an Objective-C  
framework that wraps what you need (MTCoreAudio should be able to do  
it, but there might be something easier) and just call into that with  
PyObjC.

There are a couple other ways, but they're all really, really old and  
deprecated and they often behave pretty strangely.

> 2. Is there anywhere to get better docs on things like ae*, Carbon.*,
> etc? It seems like python for mac is incredibly powerful but arcane,
> and between no documentation and no doc strings I have a hard time
> figuring out what I can do with it.

The first thing you should do is look for another way to do it, with  
PyObjC or some POSIX API (but probably PyObjC).  There's a very  
straightforward translation between Objective-C and Python, so you  
use Apple's Cocoa docs when developing with PyObjC.

If there is no way to do what you need with just PyObjC, you should  
consider just writing a little Objective-C wrapper that does what you  
need to do (calling into Carbon, CoreFoundation etc.), and call into  
that from PyObjC.  Use the Apple documentation.  Unfortunately this  
does require knowing C, but the ONLY documentation and the ONLY  
supported APIs are for C and Objective-C.  Most of the time, in my  
experience, it's just quicker to write and debug the code (i.e.  
QuickTime related stuff) in Objective-C and call into it from PyObjC.

If you still feel the need to try and do it with "pure Python", then  
read the Apple documentation for the relevant function(s) in C, and  
then guess at how it would be done from Python.  Everything in  
Carbon.* is automatically generated, but there are a bunch of special  
cases and the rules are a bit strange.  These modules would sooner go  
away than become documented.  Don't be surprised if there's a bug in  
the wrapper or some function call sequence is impossible because the  
wrapper won't let you pass NULL somewhere, etc.  Writing code using  
undocumented modules that may be broken is no fun.

-bob



More information about the Pythonmac-SIG mailing list