Skip to content

Instantly share code, notes, and snippets.

@tunalad
Last active May 21, 2024 20:29
Show Gist options
  • Save tunalad/ab86801de0c8a6df23b971b6a07e528d to your computer and use it in GitHub Desktop.
Save tunalad/ab86801de0c8a6df23b971b6a07e528d to your computer and use it in GitHub Desktop.
Volume control script for pulseaudio. Here dunstify is being used, since it has some useful options that notify-send doesn't have.
#!/usr/bin/bash
DEFAULT_SINK=$(pactl list sinks | awk '/^Sink/{getline; getline; sub(/^[ \t]+Name: /, ""); print}' | sort | head -n 1)
drawVol(){
getVol=$(pactl get-sink-volume $DEFAULT_SINK | awk '{print $5}')
getVolTrim=${getVol::-1}
dunstify -u low -r 9993 -h int:value:"$getVolTrim" "Volume: ${getVol}" -t 2000
}
drawHelp(){
echo "Usage: volumectl.sh [OPTION]"
echo " -h, --help, Help message"
echo " -s, --set VOLUME Set volume"
echo " -dec, --decrease Decrease volume"
echo " -inc, --increase, Increase volume"
echo " -rs, --restart Restart volume (sets it on 100%)"
echo " -t, --toggle Toggle mute"
}
setVol(){
local vol=$1
if [ -n "$vol" ] && [ "$vol" -ge 0 ]; then
pactl set-sink-mute $DEFAULT_SINK no; pactl set-sink-volume $DEFAULT_SINK $vol%; drawVol
else
echo "Error: Invalid volume level. It must be a number greater than 0."
exit 1
fi
}
case $1 in
"-h" | "--help") drawHelp ;;
"-s" | "--set") setVol $2; shift 2 ;;
"-dec" | "--decrease") pactl set-sink-mute $DEFAULT_SINK no; pactl set-sink-volume $DEFAULT_SINK -2%; drawVol ;;
"-inc" | "--increase") pactl set-sink-mute $DEFAULT_SINK no; pactl set-sink-volume $DEFAULT_SINK +2%; drawVol ;;
"-rs" | "-res" | "--restart") pactl set-sink-mute $DEFAULT_SINK no; pactl set-sink-volume $DEFAULT_SINK 100%; drawVol ;;
"--toggle" | "-t") pactl set-sink-mute $DEFAULT_SINK toggle; drawVol ;;
#$1 =~ ^[0-9]+$) pactl set-sink-mute @DEFAULT_SINK@ $1 ;;
*) pactl get-sink-volume $DEFAULT_SINK; drawVol ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment