Created
April 15, 2022 09:01
-
-
Save gandalf3/5184313fdadcf708bea399aacce1bb92 to your computer and use it in GitHub Desktop.
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/zsh | |
# Adjust the panning of applications based on window position.. Enjoy <3 | |
# Very hacky, no guarantees it wont hurt your ears so be careful! | |
# | |
# Requires: | |
# pulseaudio (or pipewire) | |
# xdotool | |
# awk | |
# use this to move where the "center" is, useful for multi-monitor setups | |
bias=0 | |
eval $(xdotool search --maxdepth 0 --class '.*' getwindowgeometry --shell) | |
desktop_center=$(( ($WIDTH + $bias)/2 )) | |
max_window_dist=$(( ($WIDTH - $desktop_center) / 100)) | |
declare -A window_positions | |
while :; do | |
stream_ids=($(pactl list sink-inputs | awk -v pidlist="$pidlist" \ | |
'/^Sink Input/{n=split($NF,a,"#"); id=a[n]} | |
$1 == "application.process.id" {pid=$3} | |
$1 == "window.x11.display" {printf "%s:%s ", id, substr(pid,2,length(pid)-2)} | |
')) | |
window_positions=() | |
for s_id in $stream_ids; do | |
echo ${s_id} | IFS=":" read stream_id pid | |
if [[ -z $window_positions[$pid] ]]; then | |
window_id=$(xdotool search --pid ${pid} | head -1) | |
eval $(xdotool getwindowgeometry --shell $window_id 2>/dev/null) | |
(($? != 0)) && continue | |
window_center=$(($X + ($WIDTH/2))) | |
audio_placement=$((($window_center - $desktop_center) / $max_window_dist)) | |
(( audio_placement > 100 )) && audio_placement=100 | |
(( audio_placement < -100 )) && audio_placement=-100 | |
window_positions[$pid]=$audio_placement | |
fi | |
right_vol=$((($window_positions[$pid]+100)/2)) | |
left_vol=$(( 100-$right_vol )) | |
pactl set-sink-input-volume $stream_id ${left_vol}% ${right_vol}% | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment