diff --git a/scripts/audio-baseline-watch.sh b/scripts/audio-baseline-watch.sh new file mode 100755 index 0000000..64f93b5 --- /dev/null +++ b/scripts/audio-baseline-watch.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# Re-apply the audio baseline whenever a sound device is added, changed, or +# removed, so the internal speaker stays at full volume and unmuted (the +# baseline) after a USB mic, HDMI, or headset is plugged or unplugged. +# +# `udevadm monitor` is readable without root, so this runs as a long-lived +# systemd user service (see `audio-baseline.service`); each sound event +# triggers one idempotent apply of `audio-baseline.sh`. +set -u + +BASELINE="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)/audio-baseline.sh" + +stdbuf -oL udevadm monitor --udev --subsystem-match=sound 2>/dev/null \ + | while IFS= read -r line; do + # Event lines look like `UDEV [1696.123] add /devices/... (sound)`; + # the `[` distinguishes them from the 3-line startup banner. + case "$line" in + UDEV*\[*) "$BASELINE" ;; + esac + done diff --git a/scripts/audio-baseline.sh b/scripts/audio-baseline.sh new file mode 100755 index 0000000..726c3d1 --- /dev/null +++ b/scripts/audio-baseline.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Restore the internal analog speaker to full volume, unmuted. +# +# PortAudio/ALSA has no volume control, so the voice gateway's cue beep and +# spoken replies are silent when the card's Master is muted or at 0%. The mixer +# also resets when a sound device is added or removed (e.g. a USB mic or HDMI +# unplug), so this runs both at startup and on every hotplug event (see +# `audio-baseline-watch.sh`). +# +# The card is identified by its 'Speaker' control (HDMI cards only expose +# IEC958), so a shifting card index after a device change does not matter. +# Idempotent and safe to run repeatedly; exits 0 when no card is present yet +# (the watcher retries on the next hotplug event). +set -u + +for dir in /proc/asound/card*/; do + [ -e "$dir" ] || continue + idx=${dir#/proc/asound/card} + idx=${idx%/} + amixer -c "$idx" scontrols 2>/dev/null | grep -q "'Speaker'" || continue + amixer -c "$idx" sset Master 100% unmute >/dev/null 2>&1 || true + amixer -c "$idx" sset Speaker 100% unmute >/dev/null 2>&1 || true + amixer -c "$idx" sset Headphone 100% unmute >/dev/null 2>&1 || true +done + +exit 0