Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows XP - mute/unmute audio in programmatically in Python

My machine has two audio inputs: a mic in that I use for gaming, and a line in that I use for guitar. When using one it's important that the other be muted to remove hiss/static, so I was hoping to write a small script that would toggle which one was muted (it's fairly inconvenient to click through the tray icon, switch to my input device, mute and unmute).

I thought perhaps I could do this with pywin32, but everything I could find seemed specific to setting the output volume rather than input, and I'm not familiar enough with win32 to even know where to look for better info.

Could anybody point me in the right direction?

like image 840
Kiv Avatar asked Nov 22 '25 21:11

Kiv


2 Answers

Disclaimer: I'm not a windows programming guru by any means...but here's my best guess

Per the pywin32 FAQ:

How do I use the exposed Win32 functions to do xyz?

In general, the trick is to not consider it a Python/PyWin32 question at all, but to search for documentation or examples of your problem, regardless of the language. This will generally give you the information you need to perform the same operations using these extensions. The included documentation will tell you the arguments and return types of the functions so you can easily determine the correct way to "spell" things in Python.

Sounds like you're looking to control the "endpoint device" volumes (i.e. your sound card / line-in). Here's the API reference in that direction.

Here's a slightly broader look at controlling audio devices in windows if the previous wasn't what you're looking for.

Here's a blog entry from someone who did what you're trying to do in C# (I know you specified python, but you might be able to extract the correct API calls from the code).

Good luck! And if you do get working code, I'm interested to see it.

like image 131
tgray Avatar answered Nov 25 '25 10:11

tgray


I had a similar problem and couldn't figure out how to use Windows API's to do what I wanted. I ended up just automating the GUI with AutoIt. I think that will be the fastest and easiest solution (albeit a "hacky" one). As I answered earlier today, you can use AutoIT from within Python.

like image 37
Dustin Wyatt Avatar answered Nov 25 '25 11:11

Dustin Wyatt