Sound Activated Recording – Setup

来源:互联网 发布:淘宝开店教程自学网 编辑:程序博客网 时间:2024/06/06 02:27

(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 calledrecord001.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 ofrecord.wav. The “-t” option specifies the title that will appear above the spectrogram, and the “-o” option specifies the filename of the spectrogram image.

20160418212008


Not just bats…birds too

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

bird002_9-22s_full

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 :

Audio Player
00:00
00:00
Use Up/Down Arrow keys to increase or decrease volume.


Peak Frequency

Using matplotlib andNumPy 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):

r20160604233928_peaks

…and here is the spectrogram of the recording…

r20160604233928

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

r20160604220303_peaks

…and a spectrogram of the recording…

r20160604220303



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##live##idx "%d/%m/%Y"##idx "%H:%M:%S"##temp_out "%s" "UNKNOWN"##hum_out "%d" "UNKNOWN"##wind_ave##wind_dir "%s" "-" "wind_dir_text[x]"##daily##rain "%0.1f"##live##rel_pressure#
Adding the name of the template file (e.g.weather.txt) 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).
[live]yowindow = /var/log/Yo.xmlservices = ['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"http://www.website.co.uk/weather/weather.txt").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 (seeSound 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 thesilence 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:

原创粉丝点击