4

I am wondering if there is a way to use python to mute and microphone? I am working on a project that requires the Microphone setting to be set to "Listen to this device". However, to prevent the Microphone from picking up unwanted noise from a TV or radio, I need a way to toggle between Mute and Unmute through a python script.

JRodDynamite
  • 11,607
  • 2
  • 40
  • 57
lawre6b3
  • 79
  • 3
  • 6
  • Operating system? For Linux, audio system (eg. PulseAudio, ALSA, OSS)? For things like audio it typically goes "What library/package does the system you need to run on use for this?" and then "Is there a Python API binding or wrapper for that library/package?". – Todd Knarr Jul 24 '16 at 06:40
  • I am using windows. I am having a hard time finding a module that will allow me to change the settings for the microphone: Listen to this device or mute unmute. Do you have any suggestions? – lawre6b3 Jul 24 '16 at 06:44
  • @lawre6b3 could you please accept one of the answers if any of them worked for you? Thanks! – Simon Flückiger Dec 22 '20 at 11:09

2 Answers2

4

This can easily be achieved by using PyWin32:

import win32api
import win32gui

WM_APPCOMMAND = 0x319
APPCOMMAND_MICROPHONE_VOLUME_MUTE = 0x180000

hwnd_active = win32gui.GetForegroundWindow()
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_MUTE)

Unlike the name APPCOMMAND_MICROPHONE_VOLUME_MUTE would suggest, this actually toggles the mic

muteunmute | unmutemute

Here is a list of other useful parameters that can be used with WM_APPCOMMAND: WM_APPCOMMAND message (Winuser.h) - Win32 apps | Microsoft Docs

Simon Flückiger
  • 515
  • 1
  • 5
  • 11
1

PyAudio is one cross-platform option for this. It's more than just direct access to the audio device controls, so it's moderately complicated to use. pymedia is another option, the pymedia.audio.sound package provides access to the mixer devices which is where the controls for the microphone (input level, mute etc.) would be.

Todd Knarr
  • 1,235
  • 1
  • 8
  • 14