Detect when soundcard is playing

This forum is for topics specific to the Raspberry Pi and Arch Linux ARM

Detect when soundcard is playing

Postby Chinasaur » Fri Mar 22, 2013 5:08 pm

Hi all, I'm trying to come up with a way to detect when the soundcard is outputting anything so that I can use that to trigger turning on my attached speakers (via GPIO).

I can monitor /proc/asound/card0/pcm0p/sub0/status but there is no asynchronous solution as procfs doesn't appear to generate any inotify events (not surprising but I thought it was worth trying...).

I tested busylooping using watch 'head -n1 /proc/asound/card0/pcm0p/sub0/status'. With this set to refresh every 0.25-0.5s I'm using around 5% CPU. Presumably if I write it in C this will come down. But this solution sucks!

Hoping someone here can point me to a better way. I see reference to "callbacks" all over the ALSA docs, but I don't think it's what I want.
Chinasaur
 
Posts: 9
Joined: Fri Mar 22, 2013 5:02 pm

Re: Detect when soundcard is playing

Postby crashmaster » Sat Mar 23, 2013 4:04 am

I don't think there is a reasonable software solution if you need a reaction time around 250 msecs without eating too much cpu time.

Just an idea:
Output voltage on audio out increases when playing sound - although these are only a few millivolts or less. I would look for something like a "vox" (often used by hamradio operators to switch between transmit and receive only by voice). Such a kit should be not too expensive and it can switch your speakers with an adjustable delay without using any RPi resources.
crashmaster
 
Posts: 119
Joined: Wed Mar 13, 2013 1:00 am

Re: Detect when soundcard is playing

Postby Chinasaur » Sat Mar 23, 2013 7:46 am

Sad; I would have hoped that since all the sound is going through the ALSA subsystem there would be a way to know when sound is coming out. I saw something about plugins in the ALSA docs, which sound like they are custom code that can wrap an existing output? But I really don't know my way around and can't take on anything too complex at the moment.

Thanks for the hardware solution suggestion. I did think about just using an instrumentation amplifier to detect sound output on the line, but I would like to have this controlled by RasPi GPIO if possible, so that I have the option to control it manually (but remotely) as well. And it just seems like it should be doable in software!
Chinasaur
 
Posts: 9
Joined: Fri Mar 22, 2013 5:02 pm

Re: Detect when soundcard is playing

Postby crashmaster » Sun Mar 24, 2013 1:24 am

I also think there must be a way to do it by software, but you need a very quick polling which costs too much cpu time (you tried and wrote about it). Best would be an interrupt driven routine or simply switch the power of the instrumentation amplifier by a GPIO - that's what i would do to keep it simple - hopefully. ;)
crashmaster
 
Posts: 119
Joined: Wed Mar 13, 2013 1:00 am

Re: Detect when soundcard is playing

Postby Chinasaur » Sun Mar 24, 2013 4:17 am

Well, I think I can live with 5% CPU usage (will probably come down with a more efficient implementation) if there's no better way. It just seems like a kludge. But I would hardly consider 2-4 Hz "very quick polling" for a machine running at a billion Hertz clock! ;)

On the other hand, I need to make a little board to do the power switching for the speakers anyway, so I guess I may as well try to put the sound detection circuitry in as a backup. I think you're right that running the inst amp back into a GPIO and setting up an interrupt is a good way to go.

No ALSA gurus swooping in with a cleaner option?
Chinasaur
 
Posts: 9
Joined: Fri Mar 22, 2013 5:02 pm

Re: Detect when soundcard is playing

Postby crashmaster » Mon Mar 25, 2013 3:42 am

You're right, i haven't enough thought about the polling time. Around 100 msecs are pretty slow, also for a 700 mhz ARM. I played around using regular C commands to switch 8 gpio LED's after 100 microsecs and cpu usage stayed at a few percent.
Sorry, i'm going to be a little o.t. now, let's hope you get the fabulous Alsa tip soon ;)
crashmaster
 
Posts: 119
Joined: Wed Mar 13, 2013 1:00 am


Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 13 guests