Hi guys, I'm pretty happy with my RasPi internet radio setup in my kitchen. The only thing that bugs me about it is that I have to manually turn the speakers on and off.
I'm going to go ahead and setup a simple transistor switch for the speakers off of GPIO so I can turn the speakers on and off from the pi. But it would be even nicer if I could just have the pi automatically turn the speakers on whenever there is audio output. I imagine there are multiple ways to achieve this through ALSA, but could use suggestions or pointers; the ALSA docs are kind of opaque.
Re: Automatic speakers on/off; ALSA callbacks or...?
Well, digging around reveals: which says "closed" if there's no audio and "state: RUNNING... etc." if there is audio. I'm imagining I could accomplish my goal by combining this with inotify?
Code: Select all
cat /proc/asound/card0/pcm0p/sub0/status
Re: Automatic speakers on/off; ALSA callbacks or...?
No, sadly /proc/asound doesn't appear to deploy any events to inotify.
Re: Automatic speakers on/off; ALSA callbacks or...?
You can watch Alsa's device file. In my case,
First, install iwaitnotify:
Then you can leave an inotifywait process in monitor mode, which keeps running until killed, showing occurring events.
Since GPIO activation is idempotent (setting it again to the current value has no effect), you can simply output a 1 or 0 to the GPIO line inside the if-statement.
. Perhaps you can detect it automatically./dev/snd/pcmC0D0p
First, install iwaitnotify:
Code: Select all
sudo apt-get install -y inotify-tools
Code: Select all
ALSA_DEV=/dev/snd/pcmC0D0p
inotifywait -e open -e close $ALSA_DEV -m |while read file event
do
echo $file -- $event
if fuser -s $ALSA_DEV
then
echo Programs playing.
else
echo No program playing.
fi
done