dotfiles/.bin/pulseaudio-assert-volume

49 lines
1.4 KiB
Text
Raw Normal View History

2021-07-09 17:18:23 +00:00
#!/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)
2021-07-09 17:18:23 +00:00
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):
2022-02-14 18:38:06 +00:00
if int(percent) > 3 and int(percent) < 100:
print(f' sink {sink_id} speaker {speaker} at {percent} % volume', flush=True)
2021-07-09 17:18:23 +00:00
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)
2021-07-09 17:18:23 +00:00
needs_adjusting = False
except CalledProcessError as e:
print(repr(e))
sleep(0.5)
finally:
print('Shutdown', flush=True)