Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Last active March 30, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/6b3a688934ff76adc2a9 to your computer and use it in GitHub Desktop.
Save PhrozenByte/6b3a688934ff76adc2a9 to your computer and use it in GitHub Desktop.
Zenity dialog to set audio volume using PulseAudio
#!/bin/bash
SINK="$(pacmd list-sinks | awk '/* index: [0-9]+/{ print $3 }')"
# current volume snippet taken
# from pulseaudio-ctl <https://github.com/graysky2/pulseaudio-ctl>
# by graysky <graysky@archlinux.us>, licensed under MIT
CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: /{ print $3 }' | grep -m 1 % | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: front/{ print $5 }' | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL=100
printf "Current volume: %3d%%\n" "$CURVOL"
printf "\rVolume: %3d%%" "$CURVOL"
pactl set-sink-volume "$SINK" "$CURVOL%"
ICON_PATH=""
[ ! -x "$(which gtk-icon-path)" ] || ICON_PATH="$(gtk-icon-path PulseAudio 256 2> /dev/null)"
[ -n "$ICON_PATH" ] || ICON_PATH="question"
zenity --scale --print-partial \
--title="PulseAudio Volume" \
--text="Set volume to..." \
--window-icon="$ICON_PATH" \
--min-value=0 --max-value=300 \
--value="$CURVOL" 2> /dev/null \
| while read VOL; do
printf "\rVolume: %3d%%" "$VOL"
pactl set-sink-volume "$SINK" "$VOL%"
done
if [ "${PIPESTATUS[0]}" -ne 0 ]; then
printf "\nReset volume: %3d%%" "$CURVOL"
pactl set-sink-volume "$SINK" "$CURVOL%"
fi
echo
#!/bin/bash
APPS="$(pacmd list-sink-inputs | grep -A 15 'index: ' | awk '/index: /{ print $2} /volume: front/{ print $5 } /volume: mono:/{ print $5 } /client: /{ print substr($0,index($0,"<")+1,length($0)-index($0,"<")-1); }')"
[ -z "$APPS" ] && echo "No applications found" && exit 1
APP_DATA="$(
echo "$APPS" | \
zenity --list \
--title="PulseAudio Application Volume" \
--text="Select application to set volume for..." \
--window-icon="$HOME/.local/share/icons/hicolor/256x256/apps/PulseAudio.png" \
--column="Index" --column="Volume" --column="Application" \
--print-column=ALL --hide-column=1,2 --hide-header 2> /dev/null
)"
[ -z "$APP_DATA" ] && exit
APP="$(echo "$APP_DATA" | cut -d '|' -f 1)"
CURVOL="$(echo "$APP_DATA" | cut -d '|' -f 2 | sed 's/[%|,]//g')"
NAME="$(echo "$APP_DATA" | cut -d '|' -f 3-)"
printf "Application: %s\n" "$NAME"
printf "Current volume: %3d%%\n" "$CURVOL"
printf "\rVolume: %3d%%" "$CURVOL"
pactl set-sink-input-volume "$APP" "$CURVOL%"
ICON_PATH=""
[ ! -x "$(which gtk-icon-path)" ] || ICON_PATH="$(gtk-icon-path PulseAudio 256 2> /dev/null)"
[ -n "$ICON_PATH" ] || ICON_PATH="question"
zenity --scale --print-partial \
--title="$NAME - PulseAudio Application Volume" \
--text="Set volume of $NAME to..." \
--window-icon="$ICON_PATH" \
--min-value=0 --max-value=300 \
--value="$CURVOL" 2> /dev/null \
| while read VOL; do
printf "\rVolume: %3d%%" "$VOL"
pactl set-sink-input-volume "$APP" "$VOL%"
done
if [ "${PIPESTATUS[0]}" -ne 0 ]; then
printf "\nReset volume: %3d%%" "$CURVOL"
pactl set-sink-input-volume "$APP" "$CURVOL%"
fi
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment