Files
env-ansible/files/dwm/volume.function
2025-11-10 14:02:31 -07:00

150 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# 2025-11-10 Hyperling
# Volume management for systems without working volume buttons.
# Written for ease of use in terminal and simple window management systems.
function volume-usage {
cat <<- EOF
Usage: volume [%int] [(+/-)%int] [-d] [-r] [-t] [-h]
Set to a static value:
volume 100, volume 65
Increment or decrement. Passing only + or - and no number defaults to 5.
volume -5, volume 5-, volume - 5, volume 5 -
volume +20, volume 20+, volume + 20, volume 20 +
Display the current volume:
volume,
volume-display, volume display, volume --display, volume -d,
volume +d
Set volume to a random number between 0 and 100.
volume-random, volume random, volume --random, volume -r
Be silly and oscillate volume in a loop:
volume-trip, volume trip, volume --trip, volume -t
Display this help text:
volume-usage,
volume-help volume help, volume --help, volume -h
EOF
return "$1"
}
alias volume-help="volume-usage"
alias vol-h="volume-help"
function volume {
if [[ "$1" == "--help" || "$1" == "help" || "$1" == "-h" ]]; then
volume-usage
fi
if [[ (-z "$1" && -z "$2")
|| "$1" == "--display" || "$1" == "display" || "$1" == "-d" || "$1" == "+d"
]]; then
volume-display
fi
if [[ "$1" == *"%"* || "$2" == *"%"* ]]; then
set -- "${1//%/}" "${2//%/}"
fi
dir=""
typeset -i amt
# Check if format is written text.
if [[ "$1" == "full" || "$1" == "max" ]]; then
amt=100
fi
if [[ "$1" == "mute" || "$1" == "none" ]]; then
amt=0
fi
# Check if the format is "volume +/- %int"
if [[ -z "$amt" && -z "$dir" ]]; then
if [[ "$1" == "+" || "$1" == "-" ]]; then
dir="$1"
amt="$2"
if [[ -z "$2" ]]; then
amt="5"
else
amt="$2"
fi
fi
fi
# Check if the format is "volume +/- %int".
if [[ -z "$amt" && -z "$dir" ]]; then
if [[ "$2" == "+" || "$2" == "-" ]]; then
amt="$1"
dir="$2"
fi
fi
# Check if amount and direction are adjacent, and if so, pull the values.
if [[ -z "$amt" && -z "$dir" ]]; then
if [[ "$1" == *"+"* ]]; then
dir="+"
elif [[ "$1" == *"-"* ]]; then
dir="-"
fi
if [[ -n "$dir" && -z "$amt" ]]; then
amt="$1"
fi
fi
amt=${amt//-/}
amt=${amt//+/}
if [[ -n "$dir" && -n "$amt" ]]; then
echo "> Changing volume by '$dir$amt%'."
amixer -q sset Master ${amt}%${dir}
echo "> Volume is now '`volume-display`'."
return 0
fi
if [[ -z "$dir" && -n "$1" ]]; then
echo "> Setting volume to '$amt'."
amixer -q sset Master ${amt}%
echo "> Volume has been set to '`volume-display`'."
return 0
fi
echo "Error: Should not have made it here. Please report this to the developer."
echo "$0 $1 $2"
return 7
}
alias vol="volume "
alias vol-="volume - "
alias vol+="volume + "
function volume-display {
if [[ "$1" == "-v" || "$1" == "--verbose" || "$1" == "verbose" ]]; then
amixer sget Master
else
amixer sget Master | egrep -o '[0-9]{1,3}%' | head -n 1
fi
}
alias "vol-d"="volume-display"
# Go up and down until cancelled.
#function volume-trip {
# curr eq amixer current readout
# if curr lt 50 then dir eq + else dir eq -
# while true; do
# if vol is 0 then dir eq +
# if vol is 100 then dir eq -
# curr eq \$curr $dir 1
# amixer set volume to curr
# sleep 0.1
# done
#}
#alias vol-t="volume-trip"
#alias volume-wub="volume-trip"
#alias vol-w="volume-wub"
#function volume-random {
# v eq $RANDOM % 101
# amixer set volume eq v
#}
#alias vol-r="volume-random"