Skip to content

Instantly share code, notes, and snippets.

@bdeshi
Created January 29, 2023 13:56
Show Gist options
  • Save bdeshi/98e55ac93e1ee4aae7922f9ea1f717c8 to your computer and use it in GitHub Desktop.
Save bdeshi/98e55ac93e1ee4aae7922f9ea1f717c8 to your computer and use it in GitHub Desktop.
a dumb script to toggle audio input/output mute
#!/bin/bash
# a dumb script to toggle audio input/output mute
class="$1"
state="$2"
target="$3"
device="$4"
default_device=""
default_state=""
kind=""
function error () { echo "$@" >&2; }
function errorexit () { error "$@"; exit 1; }
function get_default_device () {
search=Sink
[[ $class == input ]] && search=Source
_device=$(pactl info | grep -oP "(?<=\Default $search: ).*")
echo "$_device"
}
function get_default_state () {
filter=sinks
[[ $class == input ]] && filter=sources
_state=$(pactl list $filter \
| grep -oE '^\s+(Name|Mute):.*' \
| sed 's/^\s*//' \
| grep "Name: $default_device" -B0 -A1 \
| tail -1 \
| cut -d" " -f2
)
if [[ $_state == yes ]]; then
_state=true
else
_state=false
fi
echo "$_state"
}
function show_help {
NAME=${0/**\//}
echo -ne "
\e[1m$NAME\e[0m - a dumb script to control audio in/out mute state
\e[4mUsage:\e[0m
$NAME CLASS [STATE] [FILTER [NAME]]
\e[4mOptions:\e[0m
\e[1mCLASS\e[0m device class to work on:
in, source, mic audio input
out, sink, speaker audio output
\e[1mSTATE\e[0m desired mute state:
toggle, -1 * toggle device muting
mute, true, 1 mute device(s)
unmute, false, 0 unmute devices
\e[1mFILTER\e[0m choose which device(s) to work on:
-d, --default * default device
-a, --all all devices (all follow default device)
-s, --sink, --source sink or source named \e[1mNAME\e[0m
\e[1mNAME\e[0m device name or index if \e[1mFILTER\e[0m is -s, --sink or --source
default value is indicated by an asterisk.
" \
| sed 's/^ \{4\}//'
exit 1;
}
if [[ $1 == "--help" ]] || [[ $1 == "-h" ]]; then show_help; fi
[[ -z $state ]] && state=toggle
[[ -z $target ]] && target=default
case $class in
source*|in*|mic*) class=input; kind=source;;
sink*|out*|spk|speak*) class=output; kind=sink;;
-*) target="$1"; target_lookahead=class;;
*) errorexit "unknown device class '$class'";;
esac
case $state in
mute|yes|true|1) state=true;;
unmute|no|false|0) state=false;;
toggle|2) state=toggle;;
-*)
if [[ $target_lookahead == class ]]; then
device="$2"
else
target_lookahead=state
target="$2"
device="$3"
fi
;;
*) errorexit "unknown state '$state'";;
esac
case $target in
--default|-d|default)
target=default
default_device=$(get_default_device)
devices="$default_device"
;;
--sink|--source|-s)
target=single
[ -z "$device" ] && errorexit "unknown $class $kind: ''"
devices="$device"
;;
--all|-a)
target=all
if [[ $class == input ]]; then
devices=$(pactl list sources short | grep -vP '\.monitor' | cut -f1)
else
devices=$(pactl list sinks short | cut -f1)
fi
;;
*) errorexit "unknown filter '$target'";;
esac
if [[ $state == toggle ]] && [[ ! $target == single ]]; then
default_device=$(get_default_device)
default_state=$(get_default_state)
state=true
[[ $default_state == true ]] && state=false
fi
[ -z "$devices" ] && errorexit "could not find $class $kind '$devices'"
for device in $devices; do
echo "$kind, $device, $state"
pactl set-$kind-mute "$device" "$state"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment