nix-files/hosts/common/programs/wob/wob-audio

50 lines
1.0 KiB
Plaintext
Executable File

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p coreutils -p gnugrep -p gnused -p wireplumber
set -e
debug() {
printf "$@" >&2
printf "\n" >&2
}
verbose() {
if [ "$WOB_VERBOSE" = "1" ]; then
debug "$@"
fi
}
volget() {
# returns audio volume, as an integer (0 - 100)
volLine="$(wpctl get-volume @DEFAULT_AUDIO_SINK@)"
if echo "$volLine" | grep -q '\[MUTED\]'; then
verbose "muted"
echo "0"
else
vol="$(echo "$vol" | cut -d' ' -f2)"
echo "$(( $vol * 100 ))"
fi
}
notify_volume_change() {
verbose "notify_volume_change"
vol="$(volget)"
verbose "got volume: %d -> %d" "$lastvol" "$vol"
if [ "$vol" != "$lastvol" ]; then
debug "notify wob: %d -> %d" "$lastvol" "$vol"
printf "%s\n" "$vol" > "$XDG_RUNTIME_DIR/$WOBSOCK_NAME"
fi
lastvol="$vol"
}
pactl subscribe | while read -r line; do
verbose "pactl says: %s" "$line"
case "$line" in
"Event 'change' on sink "*)
notify_volume_change
;;
"Event 'change' on source "*)
# microphone volume changed. ignore.
;;
esac
done