2

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?

Community
  • 1
  • 1
Kiv
  • 31,940
  • 6
  • 44
  • 59

4 Answers4

6

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.

tgray
  • 8,826
  • 5
  • 36
  • 41
3

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.

Community
  • 1
  • 1
Dustin Wyatt
  • 4,046
  • 5
  • 31
  • 60
  • I tried to get into the Windows API, but unfortunately found it impenetrable. I'm satisfied with my AutoIt solution though; if anyone's interested the script is at http://bitbucket.org/kiv/scripts/src/tip/muteit.au3 – Kiv Jul 09 '09 at 00:44
1

You are probably better off using ctypes - pywin32 is good if you are using one of the already included APIs, but I think you'll be out of luck with the sound APIs. Together with the example code from the C# link provided by tgray, use ctypes and winmm.dll, or alternatively, use SWIG to wrap winmm.dll. This may well be quicker as you won't have to build C structure mapping types in ctypes for the types such as MIXERCONTROLDETAILS which are used in the API calls.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
0

tgray seems to have pointed you in the right direction, but once you find out the right Win32 APIs to deal with, you have a couple of options:

1) Try using pywin32...but it may or may not wrap the functionality you need (it probably doesn't). So you probably only want to do this if you need to use COM to get at the functionality you need.

2) Use ctypes. It's generally pretty easy to wrap just about any C functionality with ctypes.

3) If the C# example looks like what you need, you should be able to translate it to IronPython with fairly little effort. Might be easier than using the C API. YMMV, of course.

Kevin Horn
  • 4,167
  • 28
  • 30