Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running Debian (technically Raspbian), trying to get a Star-Trek style voice-command system. I've got it mostly up and running, but in the interests of privacy and all that jazz, I don't want my microphone always recording.

I'm wondering, is there a way to poll which processes, if any, are currently accessing the microphone? The idea is that I'd make myself a little indicator to tell if the microphone was currently recording audio or not.

My ultimate goal is to turn the microphone on and off using a TV-remote, and to have an LED indicator for if it's on or off. I've got all the hardware stuff working, I just need the software end now.

I believe that right now I'm configured in pure ALSA i.e. not PulseAudio, though I could be wrong.

share|improve this question

2 Answers 2

First identify your microphone device file; should be something similar to /dev/snd/pcmC0D0c. To help you find the device file, you can start a test recording with arecord or such, then do lsof | grep '/dev/snd'; it will list all programs and their associated device file.

Then you can peek usage of the microphone using fuser /dev/snd/pcmC0D0c. It will return the PID of the program accessing the device, if said device is opened.

You may prefer to loop on inotifywait /dev/snd/pcmC0D0c alternatively, to detect changes in state instead of constantly polling the device for status.

share|improve this answer

The field owner_pid in the procfs file status of a PCM device shows which program has opened it:

$ grep owner_pid /proc/asound/card*/pcm*/sub*/status
/proc/asound/card2/pcm0p/sub0/status:owner_pid   : 1803
$ ps -p 1803
  PID TTY          TIME CMD
 1803 pts/0    00:00:00 aplay
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.