LinuxPlanet Blogs

By Linux Geeks, For Linux Geeks.

Archive for the ‘silence detection’ Category

Linux Scanner Recorder (rev 3)

without comments

In the past I posted a few tips and scripts for doing voice operated audio recording (VOX) under Linux.  I’ve done more research on this over time and now have what I believe to be a more refined version as well as finally figured out how to make a timed script with the help of folks from LinuxQuestions.org.

#!/bin/bash
read -p "How many seconds to run? ==> " count_secs
if [[ $count_secs -gt 0 ]]
then
rec -r 22050 -c 1 -p | sox -p OUTPUT.mp3 silence 1 0.1 1% -1 0.5 1% &
my_PID=$!
sleep $count_secs
kill -15 $my_PID
fi

When you run the above script it will ask you to input the total recording time in seconds, then end itself when that time expires. I’ve hard coded the sox command to output a 22 kHz, mono, 32 kbps MP3 file. There are still many many ways to modify and improve this script, but this is working for my needs now.

I’ve also learned a lot more about how to use the “silence” option in sox thanks to this blog post which I suggest you read to understand how to tweak the parameters.  The way I hardcoded the volume thresholds and silence detection time works well to my ear as far as cutting silence from scanner radio traffic. YMMV.

I have one other revised script which first uses arecord and then sends the output to sox for trimming.  The method below has an advantage in that you can more finely tweak the initial capture parameters.  The disadvantage to this method is that there is a second separate processing step with sox, instead of doing it all in one shot as in the first script.

#!/bin/bash
#USAGE: ./sox_vox_recorder.sh xxx output.mp3
#USAGE: Records from line-in for xxx seconds to output.mp3, then detects and trims silence
TIME=$1
OUTPUT=$2
arecord -f S16_LE -c1 -r22050 -t raw -d ${TIME} | lame -r -s 22.05 -m m -b 64 - ${OUTPUT} ;
sleep 1 ;
sox ${OUTPUT} ${OUTPUT}_processed.mp3 silence 1 0.1 1% -1 0.5 1% ;

I can see lots of tweaks to both of these scripts, such as capturing with OGG, or making a case-selection menu to choose capture parameters, and so forth. Maybe I’ll play with this in the future.


VOX Recording in Linux (part 2)

without comments

After I made my last post about how to do line-in recordings from your scanner radio with silence detection and cutting, also known as VOX recording in the old days, I started looking at SoX the Linux Sound eXchange CLI audio processing application. Using SoX is a lot slicker than my arecord-mp3splt-mpgjoin method. It lets you do the recording, silence detection, and mp3 encoding in one line, and it puts everything into one audio file, no muss no fuss.

Here are some examples:

rec -r 44100 -p | sox -p OUTPUT.mp3 silence -l 1 00:00:00.5 -45d -1 00:00:00.5 -45d

rec -r 22050 -c 1 -p | sox -p OUTPUT.mp3 silence -l 1 00:00:00.5 -45d -1 00:00:00.5 -45d

The first one will give you a 44.1 kHz, stereo, 128 kbps MP3. The second one will give you a 22.05 kHz, mono, 32 kbps MP3.

The rec command does the recording, -r specifies the sampling rate and -c sets the amount of audio channels, “-p” means the input is piped to another command. The sox pipe also needs a “-p” to tell it that its input is coming from a pipe, in this case the rec command.

The crypt CLI switches in the piped sox command “silence -l 1 00:00:00.5 -45d -1 00:00:00.5 -45d” is what does the silence detection and trimming. It means to cut silence from the beginning of the file and parts within the middle and end. “xx:xx:xx.x” is in hh:mm:ss.s format and tells sox the minimum duration to consider valid audio. In my examples I have this set to 0.5 seconds for valid audio. The “-45d” means -45dB and is the minimum valid audio level to do trimming. In other words, if you were to say -100d, basically everything would be considered valid and there would be no trimming, you will need to play with this depending on your input levels.

I made a test for almost 2 hours with my scanner connected and scanning the aircraft bands. I ended up with a 22 minute file. Very nice!! Now I’d say we have a very nice Linux-style “scanner recorder” program, and it’s a hell of a lot slicker than anything you’d find in Windows.

If you are a Debian or Ubuntu user, you will need to compile your own version of SoX if you want to encode to MP3. This bug explains that the version of SoX in the Debian repos was compiled without MP3 support. Anyway, all you need to do is download the latest source, make sure you have all the build deps (apt-get build-dep sox), make sure you have the libmp3lame and libmp3lame-dev packages installed, then just do ./configure, make, make install.

When you do the ./configure you’ll see a list at the end of the output of what capabilities will be compiled into sox, just make sure that it says you will have MP3 support. Your compiled version will go into /usr/local by default, so you don’t need to uninstall the repo version of sox, since that one is in /usr and programs in /usr/local always take precedance. You can verify that by doing “which sox” after installing it and it should show you “/usr/local/bin/sox”.