Turn your amplifier on whenever something starts to play on your ALSA soundcard
One of my first posts showed how to control RC wall sockets with your computer. I use this system at home for switching my lights and my music amplifier (which is connected to the soundcard of the computer which controls the RC wall sockets). I’ve written an Android App for switching everything on and off and there is also the whistle control for those devices. These things are great but it always happened to me that I forgot to switch off the amplifier. So I wrote a script to automate the power switching of the amplifier. It is called turn_on_amplifier_if_audio_starts_playing. Now whenever I am starting a song via MPD or I watch a film with VLC or I simply use the computer as a remote sound card via pulse audio, the amplifier turns on immediately. After the music/whatever is finished, the script waits for 60 seconds and then turns the amplifier off. A nice feature of this is that all the actions are ‘edge’ based. If the sound card goes from idle->active, the amplifier is turned on. If it goes from active->idle, it is turned off. The advantage of this is that when I turn on the amplifier manually for listening to LPs, it stays on. In contrast, ‘state’ based actions would let the computer think ‘hey, I’m not playing anything, I’ll turn off the amplifier’.
There is also a conf file for upstart which can be placed under /etc/init for turning this script into a service.
The script works by checking every second the content of the file /proc/asound/card0/pcm0p/sub0/status . If it contains ‘RUNNING’, something is playing on the sound card. I admid, it is reeally hacky. But maybe it is useful to someone. I would much more prefer to have a hook which is called by ALSA whenever the state of the sound card changes. If someone happens to know about something like that, please leave a comment!