Skip to content

Instantly share code, notes, and snippets.

@mikkelrask
Last active May 27, 2024 09:12
Show Gist options
  • Save mikkelrask/2fe7904db210f78ff9273588df0ed10b to your computer and use it in GitHub Desktop.
Save mikkelrask/2fe7904db210f78ff9273588df0ed10b to your computer and use it in GitHub Desktop.
Volume control with pactl and notify-send
#!/usr/bin/env bash
ICON_DIR="/usr/share/icons/breeze-dark/status/24"
APP_NAME="$(hostname)"
ID=1847
function get_volume {
pactl list sinks | grep 'Volume: front-left' | awk '{print $5}' | sed 's/%//g'
}
function mute {
pactl set-sink-mute @DEFAULT_SINK@ toggle
}
function is_muted {
pactl list sinks | grep 'Mute: yes' &> /dev/null
}
function send_notification {
volume=`get_volume`
if is_muted ; then
icon_name="$ICON_DIR/audio-volume-muted.svg"
notify-send -a "$APP_NAME" -i $icon_name -r "$ID" "Volume muted" -t 1500 -h int:value:$volume
else
if [ "$volume" -gt 70 ]; then
icon_name="$ICON_DIR/audio-volume-high.svg"
elif [ "$volume" -gt 30 ]; then
icon_name="$ICON_DIR/audio-volume-medium.svg"
else
icon_name="$ICON_DIR/audio-volume-low.svg"
fi
notify-send -a "$APP_NAME" -i $icon_name -r "$ID" "Volume: $volume%" -t 1500 -h int:value:$volume
fi
}
case $1 in
up)
if is_muted ; then
pactl set-sink-mute @DEFAULT_SINK@ 0
fi
# don't exceed 100%
if [ $(get_volume) -ge 100 ]; then
send_notification
exit
fi
pactl set-sink-volume @DEFAULT_SINK@ +2%
send_notification
;;
down)
if is_muted ; then
send_notification
exit
fi
# don't go below 0%
if [ $(get_volume) -le 0 ]; then
mute
send_notification
exit
fi
pactl set-sink-volume @DEFAULT_SINK@ -2%
send_notification
;;
mute)
pactl set-sink-mute @DEFAULT_SINK@ toggle
send_notification
;;
*)
echo "Usage: volume {up|down|mute}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment