Created
February 2, 2026 08:46
-
-
Save git58/5a4d922401e50b09fe3fc1fa42d7ec1b to your computer and use it in GitHub Desktop.
Switch to analog audio if no sound in system
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
| #!/bin/bash | |
| ############################################################################### | |
| # MiniJack Hardware-Level Audio Watchdog with Recovery Attempts | |
| # ------------------------------------------------------------- | |
| # This script monitors the ALSA hardware output (minijack) and attempts to | |
| # restore audio up to 3 times if silence is detected while applications are | |
| # playing sound. If all 3 recovery attempts fail, a KDE Plasma notification | |
| # is shown to the user. | |
| ############################################################################### | |
| HW_SINK="alsa_output.pci-0000_00_1f.3.analog-stereo" | |
| MAX_ATTEMPTS=3 | |
| ATTEMPT_COUNT=0 | |
| check_signal_level() { | |
| LEVEL=$(pw-dump | jq -r \ | |
| ".[] | select(.info.props.\"node.name\"==\"$HW_SINK\") | .info.props.\"audio.level\"" \ | |
| 2>/dev/null) | |
| if [ -z "$LEVEL" ] || [ "$LEVEL" == "null" ]; then | |
| LEVEL=0.0 | |
| fi | |
| echo "$LEVEL" | |
| } | |
| attempt_recovery() { | |
| # Force hardware sink as default | |
| pactl set-default-sink "$HW_SINK" | |
| # Unmute and set safe volume | |
| pactl set-sink-mute "$HW_SINK" 0 | |
| pactl set-sink-volume "$HW_SINK" 80% | |
| # Move all active audio streams to hardware sink | |
| for INPUT in $(pactl list short sink-inputs | awk '{print $1}'); do | |
| pactl move-sink-input "$INPUT" "$HW_SINK" | |
| done | |
| sleep 1 | |
| } | |
| while true; do | |
| # 1. Check if any audio is playing | |
| if ! pactl list sink-inputs | grep -q "Sink Input"; then | |
| ATTEMPT_COUNT=0 | |
| sleep 2 | |
| continue | |
| fi | |
| # 2. Read signal level | |
| LEVEL=$(check_signal_level) | |
| # 3. If silence detected, try recovery | |
| if (( $(echo "$LEVEL < 0.0001" | bc -l) )); then | |
| ATTEMPT_COUNT=$((ATTEMPT_COUNT+1)) | |
| attempt_recovery | |
| # Re-check signal after recovery attempt | |
| LEVEL=$(check_signal_level) | |
| if (( $(echo "$LEVEL >= 0.0001" | bc -l) )); then | |
| ATTEMPT_COUNT=0 | |
| fi | |
| else | |
| ATTEMPT_COUNT=0 | |
| fi | |
| # 4. If 3 failed attempts → notify user | |
| if [ "$ATTEMPT_COUNT" -ge "$MAX_ATTEMPTS" ]; then | |
| kdialog --passivepopup "Звук упал, выключите и включите компьютер заново" 5000 | |
| ATTEMPT_COUNT=0 | |
| fi | |
| sleep 2 | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Данный скрипт пока не тестировал