Automatically Record Audio With Linux

This is a procedure that uses a shell script to automate audio recording using free and open source programs on a Linux computer.  This can be very useful to automatically record radio station ID periods for late night DX'ing or to capture a favorite radio program for future playback.  The Linux operating system cron scheduler is used to start unattended recording sessions.  It will start at the times scheduled by cron and will run for the length of time programmed into the shell script. Note that some knowledge of Linux file editing, shell scripting and cron jobs is helpful when using this procedure.

This procedure uses the Linux program arecord, which is the command-line sound file recorder component of the ALSA sound card utilities to create .wav audio files.  My system runs Ubuntu 10.04 and arecord was found pre-installed.  To see if arecord is installed on your system, issue the command "alsamixer" at a command prompt.  You should see the following window open.  If it is not installed, use your system's package installer to add the ALSA sound card utility files.

ALSAmixer playback control panel

The image above is the AlsaMixer playback control panel.  To navigate, use the left - right arrow keys to select the desired level control, then use the up-down arrow keys to adjust the audio levels.

For recording, you will need to check the capture control panel.  Use the F4 key to switch to the capture  control panel view, shown below:

ALSAmixer record control panel 

In the capture control panel, navigate between the channels with the left - right arrow keys.  Use the space bar to activate the control which causes the red "CAPTURE" to appear immediately below the level indicator which confirms that this input channel is turned on.  Use the up-down arrow keys to adjust the audio level.  The escape key will close the control panel.

Probably you will have to feed audio into the sound card from your radio receiver to determine the correct level settings experimentally.  If you are using the speaker output of your radio, low audio gain will be needed in the sound card.  If the radio has a "record" output, greater gain will be needed in the sound card.  After you have the shell script installed, make several test runs and set the radio volume and sound card level adjustments for the best quality recorded sound.

For the shell script, the first step is to create a new directory in your user home directory to hold the script and captured audio files.  For example I'll use /home/username/radio where "username" is the your user name on the system.  Also, create a new sub-directory inside the radio directory to hold the recorded files.  For this, I'll use /home/username/radio/recordings

The next step is to create the shell script and place it into the /home/username/radio directory.  Copy the following text, paste into your text editor (gedit) and save the file with the file name "record.sh".

#!/bin/sh  
#
# record.sh # # This is a script that will automatically record a radio audio stream to a .wav file using the arecord program. # # arecord program significant options: # # -r, --rate=#<Hz> Sampling rate in Hertz. The default rate is 8000 Hertz. # # -d, --duration=# Interrupt recording after # seconds. # # -c, --channels=# The number of channels. The default is one channel. # #####################################################################################################################################
# First we determine today's date and set variables $DAY, $MO and $YR. Also set variable $NOW for current date and time.
DAY=$(date +%d) # set variable $DAY for day MO=$(date +%m) # set variable $MO for month YR=$(date +%y) # set variable $YR for year (2 digits) NOW=$(date) # sets date / time variable $NOW
# Next we determine the current time and set variables $H and $M. Also we set the frequency variable $F.
H=$(date +%H) # set variable $H for hour (24 hour) M=$(date +%M) # set variable $M for minute F=$(cat /${HOME}/radio/frequency.txt) # set variable $F for radio frequency echo echo echo $NOW "Recording radio frequency "$F KHz to audio file $YR$MO$DAY-$H$M-$F"KHz".wav # puts start time entry in record.log

# Edit the following line for the recording duration, examples: -d420 = 420 seconds -d600 = 600 seconds

arecord -d420 -r16000 -c1 /${HOME}/radio/recordings/new.wav # Records audio stream for 7 minutes (420 seconds)

mv /${HOME}/radio/recordings/new.wav /${HOME}/radio/recordings/$YR$MO$DAY-$H$M-$F"KHz".wav echo NOW=$(date) # resets date / time variable $NOW to current time echo $NOW "Finished recording "$YR$MO$DAY-$H$M-$F"KHz".wav # puts stop recording time entry in record.log echo ls -l /${HOME}/radio/recordings/$YR$MO$DAY-$H$M-$F"KHz".wav # lists output file size and details echo echo echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" # puts separation line in record.log

Edit to set your desired recording duration and save the file in the /home/username/radio directory.  Open a terminal (command prompt) and change directory to the /home/username/radio directory and mark it executable with the command:

chmod a+x record.sh


To link the radio frequency being recorded to the .wav file name, create a text file in the radio directory with the file name of "frequency.txt".  Edit the file and enter the frequency in KHz that the radio is tuned to for this recording session.  Make the frequency value the only contents of the file and no spaces or carriage returns are needed.  You will need to adjust the frequency value for each recording session if the radio is re-tuned from the previous recording session.  Having a static frequency.txt file instead using a read command for input data allows the recordings to be labeled correctly even after computer re-starts.

1240

The script can be tested in a terminal (command prompt) by changing directory to the  /home/username/radio directory and issuing the command ./record.sh  (you can also use the command "bash record.sh").  The script should run and create a .wav file in the /home/username/radio/recordings directory.  It will run for the duration programmed in the script.  You can prematurely stop the script by pressing the control and c keys (ctrl-c) at the same time.

The computer's cron utility is used to start the shell script at the desired times.  A sample cron file entry is shown below that will start recording at 59 minutes after the hour during the hours of darkness every night (6 PM to 6 AM).  Note that the term "bash" has been used in this example before the script name to allow the script will run even if it is not correctly marked executable.  This is handy for testing purposes.  Also, the script output is sent to the record.log file to create a log of every operation.  Again, this is handy for testing and debugging.

59 18-23 * * * bash /${HOME}/radio/record.sh >> /${HOME}/radio/record.log
59 0-6 * * * bash /${HOME}/radio/record.sh >> /${HOME}/radio/record.log

Edit this file for your desired schedule.  You may create as many recording runs as you require.  Save this file with the file name of "radiocron" in the  /home/username/radio directory.  To create the cron job from a command prompt, change to the radio directory, and enter the command "crontab radiocron".  To see if the cron job was saved, enter the command "crontab -l"  (that's the lower case letter "L" afrer the minus sign).  Info on Cron is available at:  http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

After the script runs, look in the /home/username/radio/recordings directory for the .wav audio recordings.  They will have filenames similar to:   101103-1859-1240KHz.wav   101103 is the date (10th year as in 2010, 11th month, 3rd day.  1859 is the recording start time in 24 hour time based on the computer's internal clock.  1240KHz is the radio frequency (based on what you saved in the frequency.txt file).

The log file is written when the script executes as a cron job.  To view, open the /home/username/radio/record.log file to see the log information.  Note that the log data accumulates on every cron job run and can be manually deleted after it is no longer relevant.  If logging is not required, simply remove ">> /${HOME}/radio/record.log" from the radiocron file and resave the cron job.

Hope this is beneficial to your radio listening.  73 and good DX'ing!

Page created November 3, 2010 - Updated July 25, 2011