From bab92816d0a4dd12bd53dc1b17040a96fef57228 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 17 Mar 2021 14:19:28 +0100 Subject: [PATCH] scripts: Switch to own volume script `pavolume` was nice, but my own volume script is just more flexible. No real changes yet, but includes notification through dunst and works the same way as `control-brightness` script does, just invoked through `control-volume`. --- scripts/.local/bin/control-volume | 91 +++++++++++++++++++++++++++++++ sxhkd/.config/sxhkd/sxhkdrc | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100755 scripts/.local/bin/control-volume diff --git a/scripts/.local/bin/control-volume b/scripts/.local/bin/control-volume new file mode 100755 index 0000000..571c45f --- /dev/null +++ b/scripts/.local/bin/control-volume @@ -0,0 +1,91 @@ +#!/usr/bin/env sh +# change Volume in an easy and unified way + +direction=$1 +step=${2:-5dB} + +# amixer channel to target +amixer_quiet() { + amixer -c 0 "$@" >/dev/null +} +amixer_noisy="amixer -c 0 " + +# Query amixer for the current volume +get_volume() { + $amixer_noisy get Master | tail -1 | awk '{print $4}' | sed 's/[^0-9]*//g' +} + +# query amixer for mute state (off=muted) +get_mute() { + $amixer_noisy get Master | tail -1 | awk '{print $6}' | sed 's/[^a-z]*//g' +} + +set_volume() { + # Change the volume using alsa(might differ if you use pulseaudio) + amixer_quiet set Master "$@" >/dev/null + + # Play the volume changed sound + # -- this is *extremely* laggy (>8s) on my system, no idea why and no time to find out + # canberra-gtk-play -i audio-volume-change -d "changeVolume" +} + +# set mute state (off=mute) +set_mute() { + if [ "$1" = 'off' ]; then + amixer_quiet set Master mute + elif [ "$1" = 'on' ]; then + amixer_quiet set Master unmute + else + [ "$(get_mute)" = "on" ] && newm=off || newm=on + amixer_quiet set Master "$newm" + fi +} + +usage() { + echo "control-volume up|down [step], with step any int value +or: control-brightness set [target], with target any int value 0-100" +} + +send_notification() { + if type dunstify 1>/dev/null 2>/dev/null; then + notcmd="dunstify -h string:x-dunst-stack-tag:brightness" + else + notcmd="notify-send -h string:x-dunst-stack-tag:brightness" + fi + + vol="$(get_volume)" + if [ "$vol" -eq 0 ] || [ "$(get_mute)" = "off" ]; then + # Show the sound muted notification + $notcmd -a "changeVolume" -u low -i audio-volume-muted "Volume muted" + else + # Show the volume notification + $notcmd -a "changeVolume" -u low -i audio-volume-high \ + -h int:value:"$vol" "Volume: $vol%" + fi +} + +case $direction in +up) + set_volume "${step}+" + ;; +down) + set_volume "${step}-" + ;; +mute) + set_mute "$step" + ;; +mutetoggle) + set_mute + ;; +set) + if [ -z "$step" ]; then + echo "set option requires target brightness to be specified." + return 1 + fi + set_volume "$step" + ;; +*) + usage + ;; +esac +send_notification diff --git a/sxhkd/.config/sxhkd/sxhkdrc b/sxhkd/.config/sxhkd/sxhkdrc index 6601117..9c46761 100644 --- a/sxhkd/.config/sxhkd/sxhkdrc +++ b/sxhkd/.config/sxhkd/sxhkdrc @@ -21,9 +21,9 @@ super + F7 XF86MonBrightness{Up,Down} control-brightness {up, down} 10 XF86AudioMute - pavolume mutetoggle + control-volume mute XF86Audio{LowerVolume,RaiseVolume} - pavolume {voldown, volup} + control-volume {down, up} # Open terminal emulator (the variable gets set in sh module basic env vars) super + Return