Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Change Windows 7 master volume

I want to be able to control the master volume (not for an application, but for the current active speaker) in Python. This seems to be a tricky topic; I tried doing it in C# but I couldn't even get it to work there. Is there a way to accomplish this? (Windows 7 x64)

Please note that this question has not been answered before since my question is specifically about Python. Also, the project VonC is referring to there does't work on Windows 7.

like image 739
Salomon V Avatar asked Oct 03 '22 07:10

Salomon V


1 Answers

The easy way to do this is through ISimpleAudioVolume.

If you're using the Win32 COM wrappers from the pywin32 project, this should be pretty easy to access in Python.

As the documentation explains, there are multiple ways to get a reference to an ISimpleAudioVolume. You need to get a cross-process session, the way sndvol.exe does. See the top-level documentation on WASAPI for details.

The pseudocode will look something like this:

mmde = CoCreateInstance(CLSID_MMDeviceEnumerator, None, 
                        CLSCTX_ALL, IID_IMMDeviceEnumerator)
mmd = mmde.GetDefaultAudioEndpoint(eRender, eMultimedia)
mgr = mmd.Activate(IID_IAudioSessionManager)
sav = mgr.GetSimpleAudioVolume(None, True)
sav.SetMasterVolume(0.5)
like image 120
abarnert Avatar answered Oct 12 '22 09:10

abarnert