From d4400473a0d5a099ceb36c6ab0d5f6fe7d0321cf Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Fri, 9 Jul 2021 19:18:23 +0200 Subject: [PATCH] add .bin/pulseaudio-assert-volume --- .bin/pulseaudio-assert-volume | 48 +++++++++++++++++++ .../user/pulseaudio-assert-volume.service | 7 ++- .../user/pulseaudio-assert-volume.timer | 11 ----- 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100755 .bin/pulseaudio-assert-volume delete mode 100644 .config/systemd/user/pulseaudio-assert-volume.timer diff --git a/.bin/pulseaudio-assert-volume b/.bin/pulseaudio-assert-volume new file mode 100755 index 0000000..ac6458e --- /dev/null +++ b/.bin/pulseaudio-assert-volume @@ -0,0 +1,48 @@ +#!/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') + + 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') + needs_adjusting = True + + if needs_adjusting: + check_output(['pactl', 'set-sink-input-volume', sink_id, '100%']) + print(f' adjusted sink {sink_id} to 100%') + + needs_adjusting = False + + except CalledProcessError as e: + print(repr(e)) + + sleep(0.5) +finally: + print('Shutdown') diff --git a/.config/systemd/user/pulseaudio-assert-volume.service b/.config/systemd/user/pulseaudio-assert-volume.service index fcde58b..26a007a 100644 --- a/.config/systemd/user/pulseaudio-assert-volume.service +++ b/.config/systemd/user/pulseaudio-assert-volume.service @@ -2,4 +2,9 @@ Description=pulseaudio sickofit [Service] -ExecStart=/usr/bin/bash -c "pactl list sink-inputs | grep -F 'Sink Input #' | cut -d# -f2 | while read id; do pactl set-sink-input-volume $id 100%; done" +ExecStart=/home/kunsi/.bin/pulseaudio-assert-volume +Restart=always +RestartSec=10 + +[Install] +WantedBy=default.target diff --git a/.config/systemd/user/pulseaudio-assert-volume.timer b/.config/systemd/user/pulseaudio-assert-volume.timer deleted file mode 100644 index 7bc5ffc..0000000 --- a/.config/systemd/user/pulseaudio-assert-volume.timer +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=pulseaudio sickofit - -[Timer] -Persistent=False -OnUnitActiveSec=1s -OnUnitInctiveSec=1s -AccuracySec=1ms - -[Install] -WantedBy=timers.target