Skip to content

Instantly share code, notes, and snippets.

@quantenschaum
Last active November 25, 2016 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantenschaum/81e20314144dd805b052e0d9ac0abb59 to your computer and use it in GitHub Desktop.
Save quantenschaum/81e20314144dd805b052e0d9ac0abb59 to your computer and use it in GitHub Desktop.
#!/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