Skip to content

Instantly share code, notes, and snippets.

@kanna5
Last active September 18, 2022 16:54
Show Gist options
  • Save kanna5/c12c3fb28322618b0548c9004e0aa1ac to your computer and use it in GitHub Desktop.
Save kanna5/c12c3fb28322618b0548c9004e0aa1ac to your computer and use it in GitHub Desktop.
Easy volume control in the console. Works with PulseAudio and PipeWire with pipewire-pulse
#!/bin/bash
mute() {
pamixer --mute
}
unmute() {
pamixer --unmute
}
is_muted() {
pamixer --get-mute | grep -q true
}
get_default_sink() {
pamixer --get-default-sink | tail -n+2
}
get_volume() {
pamixer --get-volume
}
set_volume_safe() {
pamixer --set-volume "$1" --set-limit 30
}
set_volume() {
pamixer --set-volume "$1"
}
if ! command -v pamixer > /dev/null; then
echo '"pamixer" not installed' # https://github.com/cdemoulins/pamixer
exit 1
fi
RESET="\033[0m"
BOLD="\033[1m"
BLUE="\033[34m"
RED="\033[31m"
CYAN="\033[36m"
show_default_sink() {
echo -e "${BOLD}Default sink: ${RESET}${BLUE}$(get_default_sink)${RESET}"
}
show_volume() {
echo -ne "${BOLD}Volume: ${RESET}${CYAN}$(get_volume)%${RESET}"
is_muted && echo -ne "${RED} (Muted)${RESET}"
echo
}
case $1 in
m|mu|mut|mute)
mute
show_volume
;;
u|un|unm|unmu|unmut|unmute)
unmute
show_volume
;;
[0-9]|[0-9][0-9]|100)
set_volume_safe "$1"
show_volume
;;
[0-9]!|[0-9][0-9]!|100!)
set_volume "${1%!}"
show_volume
;;
.)
show_default_sink
show_volume
;;
*)
echo "Usage: $0 <volume>[!]|m[ute]|u[nmute]|."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment