Skip to content

Instantly share code, notes, and snippets.

@whi-tw
Created December 2, 2020 13:04
Show Gist options
  • Save whi-tw/d477a9b9c543f894aaba52a95496f9b7 to your computer and use it in GitHub Desktop.
Save whi-tw/d477a9b9c543f894aaba52a95496f9b7 to your computer and use it in GitHub Desktop.
Mute either USB microphone or internal soundcard, depending on presence.
#!/usr/bin/env bash
USB=false
select_card() {
USB="false" # assume that usb mic is not attached
cards="$(arecord -l | grep card)"
echo "${cards}" | grep -q USB && USB="true" # if a device has USB in the name, usb mic is attached and we probs want to use it
while read -r card; do
# loop through cards. if USB, then pick the first card with USB in the name. else, pick the first card w/o
if [ "${USB}" == "true" ]; then
echo "${card}" | grep -q USB && echo "true" $(echo "${card}" | awk '{print $2}' | cut -d':' -f1) && return
else
echo "${card}" | grep -vq USB && echo "false" $(echo "${card}" | awk '{print $2}' | cut -d':' -f1) && return
fi
done <<<"${cards}"
}
SELECT="$(select_card)"
USB="$(echo ${SELECT} | cut -d' ' -f1)"
ALSA_CARD_NO="$(echo ${SELECT} | cut -d' ' -f2)"
general_mute_toggle() {
if [ "${USB}" == "false" ]; then
amixer -c "${ALSA_CARD_NO}" set Capture toggle # my internal soundcard's mic mute setting is called "Capture"
else
amixer -c "${ALSA_CARD_NO}" set Mic toggle # my usb mic's mute setting is called "Mic"
fi
exit 0
}
general_mute_toggle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment