You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.3 KiB
67 lines
2.3 KiB
#!/bin/bash |
|
|
|
source stdlib.sh |
|
|
|
while getopts 'vsasp' opt ; do |
|
case "$opt" in |
|
v) verbosity=VERBOSE ;; |
|
q) verbosity=QUIET;; |
|
a) relative=ADD;; |
|
s) relative=SUBTRACT;; |
|
p) percentage=true ;; |
|
esac |
|
done |
|
|
|
value=${@:$OPTIND:1} |
|
log "value: ${value}" |
|
|
|
if [ -z "$value" ]; then |
|
error "Usage: $0 [OPTION] <volume>" |
|
error " -v print verbose output" |
|
error " -q quiet; don't print any output" |
|
error " -a relative; add value to current volume" |
|
error " -s relative; subtract value from current volume" |
|
error " -p interpret value as a percentage instead of an abosolute value" |
|
exit 1 |
|
fi |
|
|
|
default_sink=`pactl info 2> >(error) | grep "Default Sink" | cut -d ' ' -f 3` |
|
default_sink_id=`pactl list sinks short | grep "$default_sink" | cut -f 1` |
|
log "default sink: #${default_sink_id}: ${default_sink}" |
|
|
|
if [ -n "$relative" ]; then |
|
if [ "$percentage" = 'true' ]; then |
|
current_percentage=`pactl list sinks 2> >(error) | grep '^[[:space:]]Volume:' | head -n $(( ${default_sink_id} + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,'` |
|
log "current percentage: ${current_percentage}" |
|
value_percentage=$(echo "scale=2; "$value" / 65536 * 100" | bc 2> >(error) ) |
|
log "value percentage: ${value_percentage}" |
|
|
|
if [ "$relative" = 'ADD' ]; then |
|
value=$((current_percentage + value )) |
|
elif [ "$relative" = 'SUBTRACT' ]; then |
|
value=$((current_percentage - value )) |
|
fi |
|
log "new percentage: ${value}" |
|
else |
|
current_volume=`pactl list sinks 2> >(error) | grep '^[[:space:]]Volume:' | head -n $(( ${default_sink_id} + 1 )) | tail -n 1 | cut -d ' ' -f 3` |
|
log "current volume: ${current_volume}" |
|
|
|
if [ "$relative" = 'ADD' ]; then |
|
value=$((current_volume + value)) |
|
elif [ "$relative" = 'SUBTRACT' ]; then |
|
value=$((current_volume - value)) |
|
fi |
|
log "new volume: ${value}" |
|
fi |
|
fi |
|
|
|
if [ "$percentage" = 'true' ]; then |
|
value=$(echo "scale=2; 65536 * ("$value" / 100)" | bc 2> >(error) ) |
|
log "new volume: ${value}" |
|
fi |
|
value=$( printf "%.0f" "$value" 2> >(error) ) |
|
if [ "$value" -lt 0 ]; then value=0; fi |
|
if [ "$value" -gt 98304 ]; then value=98304; fi |
|
log "rounded value: ${value}" |
|
|
|
pactl set-sink-volume "$default_sink" "$value" > >(log) 2> >(error)
|
|
|