Last active
April 6, 2023 09:48
-
-
Save AntonSol919/4d75cd79ef7c2314482ecc0cb6c25db0 to your computer and use it in GitHub Desktop.
Balance audio left/right based on window location
This file contains hidden or 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
import pulsectl | |
import gi | |
gi.require_version('Wnck', '3.0') | |
from gi.repository import Wnck, GLib | |
screen = Wnck.Screen.get_default() | |
screen.force_update() | |
width = screen.get_width() | |
pulse = pulsectl.Pulse('audio-window-shift') | |
sink = pulse.sink_list()[0] | |
width = screen.get_width() | |
def balance(uniform): | |
range = [0.3,1.3] | |
diff = range[1]-range[0] | |
left = diff * uniform | |
right = diff * (1-uniform) | |
return [ | |
range[0] + right, | |
range[0] + left, | |
] | |
def set_balance(window): | |
for proc in pulse.sink_input_list(): | |
try: | |
pid = int(proc.proplist['application.process.id']) | |
if window.get_pid() != pid : | |
continue | |
volume = proc.volume; | |
geo = window.get_geometry() | |
center_norm = (geo.xp+(geo.widthp/2))/width | |
[left,right] = balance(center_norm) | |
volume.values[0] = left | |
volume.values[1] = right | |
print(geo,center_norm,pid,str(volume),str(proc)) | |
pulse.sink_input_volume_set(proc.index,volume) | |
except Exception as e: | |
print(e) | |
def setup_window(window): | |
set_balance(window) | |
window.connect("geometry-changed", lambda window,_:set_balance(window), None) | |
screen.connect("window-opened",lambda _,window: setup_window(window)) | |
for window in screen.get_windows(): | |
setup_window(window) | |
# Connect the callback function to the "window-moved" signal | |
# Run the main loop to listen for signals | |
GLib.MainLoop().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment