[python-win32] Control Volume on Windows

Tim Roberts timr at probo.com
Tue Mar 11 18:03:44 CET 2014


Trevor Haba wrote:
> I'm looking for a way to use python to change the volume on windows.
> After some googling the best I could come up with was this script I
> got from a relatively ancient entry on this mailing list (I think it
> was from 2005 or something). I'm using windows 7, and I would like it
> to work on windows 8 if possible, though its not necessary. I don't
> need to support any previous versions of windows.

Alas, the news is not good.

The simple and easy-to-use winmm APIs are no longer supported.  They
still work in many cases, but the mapping from the winmm device
numbering to the new audio API device numbering is mysterious, as you
have learned, and there is no good recipe to find them that works on
every system.

The "blessed" method of adjusting the volume in the Vista-and-beyond
audio world requires the use of COM.  You fetch an IMMDeviceEnumerator
interface, use that to fetch an IMMDevice for your device, then fetch an
IAudioEndpoint interface and call SetMasterVolumeLevel to adjust the
volume.  That's not really very much code in C++ terms, BUT none of
those interfaces have late-binding support, which means Python cannot
use them.

So, your only real choice is to write a Python extension in C++. 
Fortunately, using <boost/python.hpp>, that's not as hard as it used to be.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list