Chinasaur
Posts: 116
Joined: Tue Sep 11, 2012 10:15 pm

Automatic speakers on/off; ALSA callbacks or...?

Sun Mar 03, 2013 1:19 am

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.

Chinasaur
Posts: 116
Joined: Tue Sep 11, 2012 10:15 pm

Re: Automatic speakers on/off; ALSA callbacks or...?

Fri Mar 22, 2013 6:38 am

Well, digging around reveals:

Code: Select all

cat /proc/asound/card0/pcm0p/sub0/status
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?

Chinasaur
Posts: 116
Joined: Tue Sep 11, 2012 10:15 pm

Re: Automatic speakers on/off; ALSA callbacks or...?

Fri Mar 22, 2013 3:48 pm

No, sadly /proc/asound doesn't appear to deploy any events to inotify.

esperanto
Posts: 2
Joined: Tue Mar 11, 2014 9:43 am
Location: Valencia, España

Re: Automatic speakers on/off; ALSA callbacks or...?

Mon Mar 24, 2014 11:45 am

You can watch Alsa's device file. In my case,
/dev/snd/pcmC0D0p
. Perhaps you can detect it automatically.

First, install iwaitnotify:

Code: Select all

sudo apt-get install -y inotify-tools
Then you can leave an inotifywait process in monitor mode, which keeps running until killed, showing occurring events.

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
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.

Return to “Media centres”