Last active
August 22, 2018 08:32
-
-
Save gandalf3/970179ccf929e9e1224ac5faa3d58c3c to your computer and use it in GitHub Desktop.
A little script to control volume for the active window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# simple script to set the volume of pulseaudio sinks for the currently focused window | |
# takes a single argument which is passed to pactl to set the volume; [+-]n% to adjust by n% up or down, respectively | |
# from https://superuser.com/a/784102/319302 | |
pidtree() ( | |
[ -n "$ZSH_VERSION" ] && setopt shwordsplit | |
declare -A CHILDS | |
while read P PP;do | |
CHILDS[$PP]+=" $P" | |
done < <(ps -e -o pid= -o ppid=) | |
walk() { | |
echo $1 | |
for i in ${CHILDS[$1]};do | |
walk $i | |
done | |
} | |
for i in "$@";do | |
walk $i | |
done | |
) | |
# get pid of currently focused window | |
focused_pid="$(xdo pid)" | |
# get a list of pids of all related (parent, child) process | |
pidlist="$(pidtree "$focused_pid" | head -c -1 | tr '\n' ':')" | |
# find pulseaudio sink ids corresponding to the pids we found | |
sink_ids=($(pactl list sink-inputs | awk -v pidlist="$pidlist" \ | |
'BEGIN { | |
# transform pids into an array (pids) and put the pids in the key so we can use "in" | |
split(pidlist,pidarr,":") | |
for(i in pidarr) pids["\""pidarr[i]"\""]="" | |
} | |
/^Sink Input/{n=split($NF,a,"#"); id=a[n]} | |
$1 == "application.process.id" && ($3 in pids) {print id} | |
')) | |
# adjust the volume of every sink we found | |
[[ -n "$sink_ids" ]] && for sink_id in ${sink_ids[@]}; do | |
pactl set-sink-input-volume "$sink_id" "$1" | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment