Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a tool that will tell me, in less than half a second, if the microphone is picking up any sound above a certain threshold. (I plan to then mute the Master channel with another command line tool, like amixer.)

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This solution will avoid writing repeatedly to disk, and even though it in worst case takes a second instead of the desired less than half a second, I found it to be fast enough after trying it. So, here are the two scripts I use:

./detect:

while true; do
    arecord -d 1 /dev/shm/tmp_rec.wav ; sox -t .wav /dev/shm/tmp_rec.wav -n stat 2>\
    &1 | grep "Maximum amplitude" | cut -d ':' -f 2 | ./check.py
    if [ $? -eq 0 ] ; then
         amixer set Master 0
    else
         amixer set Master 80
    fi
done

./check.py:

#!/usr/bin/env python
import sys

number = 0.0
thing="NO"

line = sys.stdin.readline()
thing = line.strip()
number = float(thing)

if number < 0.15:
    raise Exception,"Below threshold"

Hardly elegant, but it works.

Note: If you want a more gradual thing, add something like this:

   for i in `seq 0 80 | tac`; do
      amixer set Master $i
   done

for muting and

   for i in `seq 0 80`; do
      amixer set Master $i
   done

for unmuting.

share|improve this answer
1  
A slightly more elegant solution, which supports half-second resolution and doesn't require a temporary file: while true; do amixer set Master $(rec -n stat trim 0 .5 2>&1 | awk '/^Maximum amplitude/ { print $3 < .15 ? 80 : 0 }'); done –  nandhp Jun 29 '12 at 13:17

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.