Hardware Reference
In-Depth Information
Start recording with noise detection
Wouldn't it be cool if the Pi could listen for activity in the room and only start
recording when something or someone makes a sound? Once again SoX comes
to the rescue.
Our noise detection method works in two simple steps:
1.
Start listening for one second and measure the noise level during that second.
2.
If the measured noise was above a certain threshold, start recording for 5
minutes, or if not, start over and listen for another second.
First, let's calibrate the microphone and figure out a good amplitude threshold value:
pi@raspberrypi ~ $ sox -t alsa plughw:1 -n stat trim 0 00:00:01 : restart
This command starts monitoring your microphone but the -n argument tells sox
to discard the output since we are only interested in the statistics produced by
the stat effect. The trim effect then cuts of the monitoring after one second, the
important statistics are printed, and a new monitoring second starts thanks to the
restart argument.
Now, keep your eyes on the Maximum amplitude value in the statistics output.
As long as you stay quiet, the value shouldn't fluctuate too much from one readout
to the other. Next, make a loud noise and watch the Maximum amplitude value
jump. Now try moving further away from the microphone and say something in
your normal tone of voice. If there was a significant change in amplitude value, write
that value down as a rough starting point for your threshold value. If not, try raising
the capture volume of your microphone in alsamixer until you see a significant
increase in the amplitude value.
Alright, now all we need to do is translate the theory into program logic with the
following script:
#!/bin/bash
#
# Noise activated recorder script for Raspberry Pi.
# Use chmod +x ~/noisedetect.sh to enable.
THRESHOLD=0.010000
noise_compare() {
awk -v NOISE=$1 -v THRESHOLD=$2 'BEGIN {if (NOISE > THRESHOLD) exit
0; exit 1}'
}
 
Search WWH ::




Custom Search