#!/bin/bash #This script increases/decreases/toggles the volume by a fixed increment specified in step. #This script relies on the script notify-send-improved and on pactl. # i3-volume-toggle - Increases and decreases volume. # Copyright (C) 2017 Fabrizio Romano Genovese # This copyrighted material is made available to anyone wishing to use, # modify, copy, or redistribute it subject to the terms and conditions of # the GNU General Public License v.2, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY expressed or implied, including the implied warranties of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # You should have received a copy of the # GNU General Public License along with this program; if not, # see # Put this file in /bin/ or in /usr/bin. Do not forget to give # sudo chmod +x i3-volume-toggle # to make it executable. step=5 # Check if pactl, amixer and notify-send-improved are installed check_installed() { if [ ! -f /bin/notify-send-improved -a -f /bin/pactl -a -f /bin/amixer ]; then echo "Notify-send-improved, amixer and/or pactl not present." echo "Please install them." exit 2 fi } #Here we actually change the volume using pactl if [[ $# -le 1 ]]; then case $1 in -v | --version) echo "$0 - Version $version" exit 0 ;; -h | --help) echo "- v, --version Display current version" echo "-h, --help Display this message" echo "up Increase volume by $step%" echo "down Decrease volume by $step%" echo "mute Mute/unmute" exit 0 ;; "up") check_installed pactl set-sink-volume 1 +$step% #This produces sound feedback. Comment if you don't like it. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid ;; "down") check_installed pactl set-sink-volume 1 -$step% #This produces sound feedback. Comment if you don't like it. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid ;; "toggle") check_installed pactl set-sink-mute 1 toggle #This produces sound feedback. Comment if you don't like it. (speaker-test -t sine -f 400)& pid=$!; sleep 0.1s; kill -9 $pid ;; *) echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}" exit 2 esac fi # Here we get the current status of audio from alsamixer muted=`amixer get Master|tail -n1|sed -E 's/.*\[([a-z]+)\]/\1/'` volume=`amixer get Master|tail -n1|sed -E 's/.*\[([0-9]+)\%\].*/\1/'` # Selects the right icon for the notification # If icons do not exist they won't be displayed muteicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-muted-symbolic.symbolic.png" if [ "$volume" -eq "0" ]; then volumeicon=$muteicon elif [ "$volume" -lt "33" ]; then volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-low-symbolic.symbolic.png" elif [ "$volume" -lt "66" ]; then volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-medium-symbolic.symbolic.png" elif [ "$volume" -lt "120" ]; then volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-high-symbolic.symbolic.png" else # The following image is obtained with this command: # sudo convert "/usr/share/icons/Adwaita/32x32/status/audio-volume-high-symbolic.symbolic.png" -fuzz 100% -fill red -opaque "/usr/share/icons/Adwaita/32x32/status/audio-volume-danger-symbolic.symbolic.png" volumeicon="/usr/share/icons/Adwaita/32x32/status/audio-volume-danger-symbolic.symbolic.png" fi # Here we read the notification id from a temporary file. If the file doesn't exist, we initialize it the notification to 0 # Then we send the notification out and we reassign the notification id to the current one using tee # # If the file exists, we just replace that notification id, that will then stay the same and not increment. if [ ! -f "/tmp/volume_notification_id.dat" ]; then notification_id="0" if [[ $muted == "off" ]]; then notification_id=$(notify-send-improved "Volume Control" -i $muteicon --print-id -t 1000 "Muted \($volume\%\)" | tee /dev/tty) else notification_id=$(notify-send-improved "Volume Control" -i $volumeicon --print-id -t 1000 "Volume: $volume\%" | tee /dev/tty) fi else notification_id=`cat /tmp/volume_notification_id.dat` if [[ $muted == "off" ]]; then notify-send-improved "Volume Control" -i $muteicon --print-id --replace=$notification_id -t 1000 "Muted \($volume\%\)" else notify-send-improved "Volume Control" -i $volumeicon --print-id --replace=$notification_id -t 1000 "Volume: $volume\%" fi fi # Here we write the notification id to a file. Next time we use this script it will know if the notification has to be overwritten or not echo "${notification_id}" > /tmp/volume_notification_id.dat