Sound Activated Recording – Setup

(Last updated 5th June 2016)

SoX, being the “Swiss Army knife of audio manipulation”, is ideal for sound activated recording, triggered by the sound of bat calls.

Install SoX

SoX can be installed on the Raspberry Pi with this command:

sudo apt-get install sox

Sound activated recording with SoX

By specifying the right parameters to SoX’s silence command, sound can be monitored effectively trimming all noise until noise is detected, and then trim everything after so many seconds of silence.

This can be achieved with the following command:

rec - c1 -r 192000 record.wav silence 1 0.1 1% 1 5.0 1% : newfile : restart

This specifies that all silence will be trimmed until a noise that is 1% of the sample value is heard for at least 0.1 second, and then trim all silence after 5 seconds of silence is heard below the same 1% threshold.  This will result in the noise being recorded to a file called record001.wav. SoX will then start listening for noise again.  “newfile” tells SoX to create multiple output files, and “restart” tells SoX to repeat the effect chain once more.  The next file recorded will be called record002.wav, followed by record003.wav and so on.

High-pass filtering

In practice, this yields good results, but you may find you end up with many very small files, where a brief noise has triggered a recording.  SoX has a solution in the form of the sinc option, which is used as a high-pass filter.

rec - c1 -r 192000 record.wav sinc 10k silence 1 0.1 1% 1 5.0 1% : newfile : restart

The code above will only pass sounds higher than 10kHz to the silence option.  A higher frequency could be used, but some bat social calls may be impacted (as I found out!).

Record for a specific duration

Another option is to let SoX detect a bat call, and then record for a specified period, say 5 seconds.  This can be achieved as follows:

rec - c1 -r 192000 record.wav sinc 10k silence 1 0.1 1% trim 0 5

This uses SoX’s trim function to specify a start point (0 seconds) and a duration (5 seconds).  The result will be a 5 second long wav file triggered by a sound above 10kHz.

Create a spectrogram using SoX

SoX can  be used to create a spectrogram of the recorded sound.

sox record.wav -n spectrogram -t record.wav -o record.png

This will create a spectrogram of record.wav. The “-t” option specifies the title that will appear above the spectrogram, and the “-o” option specifies the filename of the spectrogram image.


Not just bats…birds too

With sound activated recording, other animals will be recorded, such as this interesting example below:


It is a recording of a Wren (Cistothorus palustris).  Having recorded a couple of these calls, I temporarily modified my code to record full spectrum from 0Hz so that the bird making the call was audible :

Peak Frequency

Using matplotlib and NumPy it is possible to perform a quick analysis of the peak frequencies from the sound activated recording.  This can be used to filter out nuisance recordings.  I now ignore everything below 17kHz, and only process recordings above this frequency, as these are most likely to include bat calls.

The peak frequency analysis takes just a few seconds (Pi2 and Pi3) to a minute or longer if using the older Pi1.

Peak frequencies also aid in the identification of the bat species.

For example, here is a graph showing the peak frequencies of a Soprano Pipistrelle recording which contains both echolocation calls (56.6kHz) and (louder) social calls (21.0kHz):


…and here is the spectrogram of the recording…


Here is the peak frequency of a Noctule (18.4kHz):


…and a spectrogram of the recording…


Logging weather data along with bat calls

I have a Maplin N96GY (WH1080) Personal Weather Station which has been attached to a Raspberry Pi Rev 1B (256 Mb RAM) since December 2012, powered 24 hours a day 7 days a week.  The Pi runs the excellent pywws Python software for weather stations which uploads data to Weather Underground every 48 seconds. I’m using pywws version 13.03 – the example shown below may differ slightly in later versions.

pywws allows a template file containing weather data (temperature, humidity, wind speed etc.) to be uploaded using ftp to a website.  A simple template file called weather.txt is shown below:

#timezone local#
#idx "%d/%m/%Y"#
#idx "%H:%M:%S"#
#temp_out "%s" "UNKNOWN"#
#hum_out "%d" "UNKNOWN"#
#wind_dir "%s" "-" "wind_dir_text[x]"#
#rain "%0.1f"#

Adding the name of the template file ( to the weather.ini file in pywws results in the weather data being uploaded to the website at regular intervals (in this case it’s “live” data).

yowindow = /var/log/Yo.xml
services = ['underground_rf']
twitter = []
plot = []
text = ['weather.txt']

A live file will be uploaded every 48 seconds to the website.  The resulting text file, in this example, would look like this:

17/05/2016 18:11:47 14.6 70 1.6 SW 0.6 1013.7

Using Python, it’s easy to download the weather data-file whenever required.

realtime = urllib.request.urlopen(r"").read()
realtime = realtime.decode('utf-8')
realtime_list = realtime.split()
Weather_date = (realtime_list[0])
Weather_time = (realtime_list[1])
Temp_Out = (realtime_list[2])
Humidity = (realtime_list[3])
Wind_speed = (realtime_list[4])
Wind_dir = (realtime_list[5])
Pressure = (realtime_list[6])

The weather data can be logged to a text file as each sound activated call has finished. It can also be used as part of the spectrogram title (see Sound Activated Recording – Results for examples).

title = ("Temp=" + Temp_Out + "degC_Wind=" + str(Wind_speed) + "mph")
sox my.wav −n spectrogram -t  title −o print.png

Ensure that there are no spaces in the spectrogram title text, otherwise SoX will fail with an error message.

Tip #1 – Use SoX to extract calls from long recordings

If you have a long recording containing bat calls at various intervals, SoX can be used to quickly extract these calls into separate files, using the silence command.

A 20 minute 2 channel recording at 192kHz is about 1.8GB in size. This can take some time using Audacity, for example, to find the bat calls and manually extract/save them. This SoX command will strip out the calls into separate .wav files very quickly (where record.wav is the input file containing the bats calls):

sox record.wav -c1 output.wav sinc 12k silence 1 0.01 1% trim 0 5 : newfile : restart

All bat calls with sound above 12kHz and above the 1% threshold will be extracted into 5 second long files named output001.wav, output002.wav and so on.

Original 20 minute, 2 channel, recording:

Screenshot from 2016-04-22 20-02-45

Example of first 5 second duration, single channel, extracted .wav file: