Skip to content

Instantly share code, notes, and snippets.

@black-silence
Created February 6, 2023 15:59
Show Gist options
  • Save black-silence/9cbbf838c08cc30f5c9aed0694842579 to your computer and use it in GitHub Desktop.
Save black-silence/9cbbf838c08cc30f5c9aed0694842579 to your computer and use it in GitHub Desktop.
Simple script to toggle pulseaudio microphone mute, to be used with a global hotkey.
#!/usr/bin/env python3
# Script to toggle microphone mute
# Toggles the preferred (usb) mic if present, or the fallback mic
# device.description from `pacmd list-sources`
preferred_mic = "USB PnP Audio Device Mono"
fallback_mic = "HD Audio Controller Stereo Microphone"
try:
import pulsectl
except ImportError:
import sys
sys.exit("Module pulsectl not found, please install")
mic = None
pulse = pulsectl.Pulse('mic-toggler')
# First loop the sources to find the correct mic
for item in pulse.source_list():
if item.description == preferred_mic:
mic = item
break
elif item.description == fallback_mic:
mic = item
# no break so the preferred mic is selected if it comes later in the list
if mic is not None:
# Set this mic default so we get the nice volume display by the system. USB mics tend to lose the default flag...
pulse.source_default_set(mic)
# Then toggle mute state
pulse.source_mute(mic.index, False if mic.mute == 1 else True)
pulse.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment