Last active
November 25, 2016 12:51
-
-
Save quantenschaum/81e20314144dd805b052e0d9ac0abb59 to your computer and use it in GitHub Desktop.
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 | |
# turns power supply for active speakers on/off when sound is played/not played | |
# very handy on a RaspberryPi with active speakers | |
# you need to install "gpio" from http://wiringpi.com/ first | |
# see http://wiringpi.com/pins/ for pin numbers | |
# run this as service with | |
#[Unit] | |
#After=mpd.target | |
#[Service] | |
#ExecStart=/speakerd | |
#Restart=always | |
#[Install] | |
#WantedBy=multi-user.target | |
PIN=5 | |
function playing { | |
grep RUNNING /proc/asound/card*/*p/sub*/status >/dev/null | |
} | |
function speaker { | |
echo "speaker $1" | |
gpio mode $PIN output | |
[ "$1" = on ] && gpio write $PIN 0 || gpio write $PIN 1 | |
} | |
STATE=unknown | |
PLAYING=0 | |
speaker off | |
while true; do | |
playing && PLAYING=3 | |
if [ $PLAYING -gt 0 ]; then | |
[ $STATE != on ] && speaker on | |
STATE=on | |
((PLAYING--)) | |
sleep 10 | |
else | |
[ $STATE != off ] && speaker off | |
STATE=off | |
sleep 1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment