48 lines
1.4 KiB
Python
Executable file
48 lines
1.4 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
from subprocess import check_output, CalledProcessError
|
|
from re import findall
|
|
from signal import signal, SIGTERM
|
|
from sys import exit
|
|
from time import sleep
|
|
|
|
|
|
def handle_signal(_signo, _stack_frame):
|
|
exit(0)
|
|
|
|
signal(SIGTERM, handle_signal)
|
|
|
|
|
|
try:
|
|
print('Startup', flush=True)
|
|
|
|
while True:
|
|
try:
|
|
for line in check_output(['pactl', 'list', 'sink-inputs']).decode().splitlines():
|
|
line = line.strip().lower()
|
|
|
|
if not line:
|
|
continue
|
|
|
|
if line.startswith('sink input #'):
|
|
sink_id = line[len('sink input #'):]
|
|
needs_adjusting = False
|
|
|
|
if line.startswith('volume:'):
|
|
for speaker, absolute, percent in findall('([a-z-]+):\W+([0-9]+)\W+\/\W+([0-9]+)%', line):
|
|
if int(percent) < 100:
|
|
print(f' sink {sink_id} speaker {speaker} at {percent} % volume', flush=True)
|
|
needs_adjusting = True
|
|
|
|
if needs_adjusting:
|
|
check_output(['pactl', 'set-sink-input-volume', sink_id, '100%'])
|
|
print(f' adjusted sink {sink_id} to 100%', flush=True)
|
|
|
|
needs_adjusting = False
|
|
|
|
except CalledProcessError as e:
|
|
print(repr(e))
|
|
|
|
sleep(0.5)
|
|
finally:
|
|
print('Shutdown', flush=True)
|