2023-05-21 14:24:34 +02:00

73 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
function get_time_ms {
date -u +%s%3N
}
volicons=("󰕿" "󰖀" "󰕾")
vol() {
wpctl get-volume @DEFAULT_AUDIO_"$1"@ | awk '{print int($2*100)}'
}
ismuted() {
wpctl get-volume @DEFAULT_AUDIO_"$1"@ | rg -qi muted
echo -n $?
}
setvol() {
wpctl set-volume @DEFAULT_AUDIO_"$1"@ "$(awk -v n="$2" 'BEGIN{print (n / 100)}')"
}
setmute() {
wpctl set-mute @DEFAULT_AUDIO_"$1"@ toggle
}
gen_output() {
percent="$(vol "SINK")"
lvl=$(awk -v n="$percent" 'BEGIN{print int(n/34)}')
ismuted=$(ismuted "SINK")
if [ "$ismuted" = 1 ]; then
icon="${volicons[$lvl]}"
else
icon="󰝟"
fi
echo '{"icon": "'$icon'", "percent": "'$(vol "SINK")'", "microphone": "'$(vol "SOURCE")'"}'
}
case "$1" in
"mute")
if [ "$2" != "SOURCE" ] && [ "$2" != "SINK" ]; then
echo "Can only mute SINK or SOURCE"
exit 1
fi
setmute "$2"
;;
"setvol")
if [ "$2" != "SOURCE" ] && [ "$2" != "SINK" ]; then
echo "Can only set volume for SINK or SOURCE"
exit 1
elif [ "$3" -lt 0 ] || [ "$3" -gt 100 ]; then
echo "Volume must be between 0 and 100"
exit 1
fi
setvol "$2" "$3"
;;
"")
last_time=$(get_time_ms)
gen_output
pw-cli -m | rg --line-buffered "PipeWire:Interface:Client" | while read -r event; do
current_time=$(get_time_ms)
delta=$((current_time - last_time))
if [[ $delta -gt 50 ]]; then
gen_output
last_time=$(get_time_ms)
fi
done
;;
*) echo "wrong usage" ;;
esac