Created
January 17, 2021 01:23
-
-
Save diamondburned/8414a95966d5d1caf2253a016b18abd3 to your computer and use it in GitHub Desktop.
PulseAudio volume control with notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# const | |
NOTIFICATION_APPNAME="vol.sh" | |
NOTIFICATION_ID=48741 # dice-roll randomness guaranteed | |
# notify title subtitle hints? icon? -> void | |
notify() { | |
title="$1" | |
subtitle="$2" | |
hints="$3" | |
icon="$4" | |
[[ ! "$hints" ]] && hints="{}" | |
gdbus call -e \ | |
-d org.freedesktop.Notifications \ | |
-o /org/freedesktop/Notifications \ | |
-m org.freedesktop.Notifications.Notify \ | |
"$NOTIFICATION_APPNAME" "$NOTIFICATION_ID" \ | |
"$icon" "$title" "$subtitle" "[]" "$hints" 2500 > /dev/null | |
} | |
# vol (+|-|m) -> void | |
vol() { | |
case "$1" in | |
+) pactl set-sink-volume @DEFAULT_SINK@ +2% ;; | |
-) pactl set-sink-volume @DEFAULT_SINK@ -2% ;; | |
m) pactl set-sink-mute @DEFAULT_SINK@ toggle ;; | |
*) return 1 ;; | |
esac | |
} | |
volume_regex="Volume: front-left: .* ([0-9]+)%.* front-right: .* ([0-9]+)%" | |
mute_regex="Mute: (yes|no)" | |
# get_vol -> muted(yes|no), left, right | |
get_vol() { | |
pactl list sinks | while read -r line; do | |
[[ "$line" =~ $mute_regex ]] && echo -n "${BASH_REMATCH[1]} " | |
[[ "$line" =~ $volume_regex ]] && { | |
echo -n "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" | |
return | |
} | |
done | |
} | |
# vol_icon int -> string | |
vol_icon() { | |
(( $1 > 70 )) && echo -n audio-volume-high-symbolic && return | |
(( $1 > 35 )) && echo -n audio-volume-medium-symbolic && return | |
(( $1 > 10 )) && echo -n audio-volume-low-symbolic && return | |
echo -n audio-volume-muted-symbolic | |
} | |
# fatal ...args -> void | |
fatal() { | |
echo "$@" >&2 | |
exit 1 | |
} | |
# main -> void | |
main() { | |
vol "$1" || fatal "Unknown arguments; use +, - or m." | |
read muted left right < <(get_vol) | |
title="Volume" | |
description="$left%" | |
(( left != right )) && description+=" / $right%" | |
[[ "$muted" == yes ]] && { | |
title="Muted" | |
description="" | |
left=0 | |
right=0 | |
} | |
avg_vol=$[ (left+right) / 2 ] | |
read icon < <(vol_icon "$avg_vol") | |
echo "Icon: $icon" | |
notify "$title" "$description" "{'value':<$avg_vol>}" "$icon" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment