Unattended Audio Recordings with a Raspberry Pi

Have you heard about the new Raspberry Pi computer?  From the website: “The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It’s a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video.”

Basically, it’s an insanely cheap computer ($35) that you can do all sorts of geeky things with.  Go get one NOW (and then come back).

I waited 3 long months for my Raspberry Pi to arrive after ordering and boy was it was worth the wait!  I love this little thing.  I loaded the recommended image “Wheezy” on it and played around for awhile before quickly moving to Arch Linux ARM which I know well since I’ve had it running on my PogoPlug for almost 2 years.

After tinkering for awhile I kept trying to come up with a project for it.  There are so many cool Raspberry Pi projects out there right now and it’s easy to get excited about tackling one of them.  I’ve had my eye on the High Altitude Ballooning Pi project where you send your Pi to space to take pictures and track its movement with GPS.  Come on now, tell me that isn’t cool.

Anyway, I finally came up with an idea for a project.  I am responsible for recording the Sunday morning lesson in our church Sunday school class.  For the last 3 years the Sunday morning process has been to buy and bring CDRs, queue up the mic and recording system setup in the room, take the CDR home, rip it, encode the WAV to MP3 and upload it so it is available for Podcasting.  Whew, that’s a lot of steps.  Because it’s such a cumbersome process, it’s been hard to keep up with and easy to “forget” from time to time.  Also, it’s hard to train other volunteers on how to do the process end-to-end.  Since we already have a nice high quality microphone and mic amp setup in the room, I thought I would use the Pi to automatically record the audio and make it available as a Podcast.

IMG_0037

So, that’s exactly what I did.  It turned out to be a fun project and has been working for the last couple of weeks.  I found an old USB Belkin Wireless G Adapter (F5D7050/v4000) lying around which I used for wifi connectivity to our church’s wifi hotspot.  Since the Pi doesn’t have mic input capability I bought a $10 USB Sabrent sound card w/ mic input (SBCV/UP-100) from Micro Center which was easy to get up and working with the help of the ALSA guide on the Arch Linux wiki.   I bought an RCA to 3.5mm audio cable to hook up the mic amp output to the 3.5mm mic input on the USB sound card.

I have cron running the following script every Sunday morning at 11:00AM (when class starts) to record for one hour (podcast-record.sh 3600).  Obviously, I took out my credentials and such but this should be enough to give you an idea of what I’m doing.  This script does the following:

  • Record audio from mic using arecord
  • Use sox to convert stereo to mono for smaller file size
  • Use lame to encode to mp3 at 64kbps bit rate
  • Upload to Amazon S3 storage
  • Write a record to my mysql database.  I have a web app running on top of this database that generates the podcast xml feed.
  • Send a ‘success’ email notifying me it all worked
# Unattended podcast record process
# Author: brady@geekytidbits.com
# Notes: Invoke with seconds parameter specifying how many seconds to record, ex. 'podcast-record.sh 10'

FILENAME=$(date +"%Y%m%d_%H%M")

#record with sudo since arecord seems to want root privledges
sudo arecord -f dat -d $1 ./record/${FILENAME}.wav
[ $? -eq 0 ] || exit $?

#change ownership to jdoe
sudo chown jdoe ./record/${FILENAME}.wav
[ $? -eq 0 ] || exit $?

#copy to process directory
cp ./record/${FILENAME}.wav ./process/${FILENAME}.wav

#stereo > mono
sox ./process/${FILENAME}.wav -c 1 ./process/${FILENAME}-mono.wav

#convert to mp3 (64Kpbs) 
lame -b 64 ./process/${FILENAME}-mono.wav ./upload/${FILENAME}.mp3
[ $? -eq 0 ] || exit $?

#remove process files
rm ./record/${FILENAME}.wav
rm ./process/${FILENAME}.wav
rm ./process/${FILENAME}-mono.wav

NOTIFY_EMAIL=MY_EMAIL_ADDRESS@gmail.com
MODTIME=$(stat -c %y ./upload/${FILENAME}.mp3)
DATE=$(date --date="${MODTIME}" +%Y-%m-%d)

#upload to s3
s3cmd put --reduced-redundancy --acl-public ./upload/${FILENAME}.mp3 s3://MY_S3_BUCKET/${FILENAME}.mp3
[ $? -eq 0 ] || continue

#write db record
mysql -h www.mydomain.com -D MY_DB_NAME -u MY_DB_USERNAME -pMY_DB_PASSWORD --execute "INSERT INTO Lesson (Date, Title, Speaker, Audio_File_Name) VALUES ('${DATE}', 'TBD', 'TBD', '${FILENAME}.mp3')"
[ $? -eq 0 ] || continue

mv ./upload/${FILENAME}.mp3 ./complete/

#send email
echo "Done!" | mail -s "Podcast ${DATE} - Upload Complete" $NOTIFY_EMAIL

To install all the dependencies used in this script simply run the following:

pacman -S sudo alsa-utils sox lame s3cmd mysql

There you have it.  That was fun.  Now go get a Pi of your own and do something fun with it.

  • Thom

    Very cool. Thanks Brady. I’m assessing some gift ideas for projects and audio recording is one aspect. I will definitely be lifting a few lines from your code and following the instructions to configure the audio hardware. Cheers,

    Thom

  • Dave

    What size is the file?, what size sd card are you using.?

  • Brady

    The raw .wav file for an hour recording gets to be around 700MB. Once I run it through my script to to strip stereo and convert to 64b kbps mp3, the file size is around 30MB. My SC card is 8GB. I have 2 partitions: 1 for rootfs (2GB) and 1 for /home (6GB).

  • Dave

    One last question if I may, what is the quality like?

  • Brady

    Good. No concerns.

  • andrew

    Hi, I like this alot, exactly what I’m looking for!
    Any thoughts on slience detect to remove any silence at the start or end of the lesson? Or maybe a manual start/stop button so you can manually start before 11am or manually stop before 12pm?? I guess the later would be alot easier then the prior.
    I understand the concept, but what software/tools do you use to generate the podcast xml feed from the raw MySQL entries?
    Cheers.

  • Brady

    Andrew — Have a look at http://digitalcardboard.com/blog/2009/08/25/the-sox-of-silence/ which describes how to use sox to remove silence from an audio file. I did think about implementing this in my setup but just haven’t gone down that road yet. I also did think about a manual stop start but realized that, although possible, there’s a bit of work to get this to work correctly. My thoughts were leaning in the direction of a running a PHP backed website that had a ‘record’ button. The script would check to see if ‘arecord’ was already running to show the user and allow a button press to fire up arecord on demand. My podcast generation happens in an ASP.NET (mono) website hosted on a different server and is very similar to the answer on this SO post: http://stackoverflow.com/questions/2839102/how-to-create-rss-feed-for-a-website .

  • Andrew

    Im thinking the need might be high enough for me to make a number of these and sell them cheaply to churches – I’ve come across this problem quiet a bit.
    SOX looks good – exactly what would be required.
    A PHP website is a great idea, even with the ability to change parameters like start and end times, mySQL details and alert email address. Just as an easy way to reprogram the thing over the LAN.\
    Would you be interested in re-writing some of the code for our situation at some stage in the future for an agreed commission price? my email address is: and_rew_story@live.com_.au (without underscores)

  • Gershwin

    This is an interesting solution. We now use a real laptop for this kind of thing, but that also streams the sermon directly (live) to a streaming service using Shoutcast. I guess that it is compressing the audio under fly to MP3. Would such a feature also be possible to build with a Pi?

  • lotus

    does arecord record for the correct length of time for you? here’s my problem…

    SHELL SCRIPT
    date
    arecord -D hw:1,0 -c 1 -r 48000 -d 600 -f S16_LE ar0.wav
    date

    RESULT
    Mon Jul 1 19:56:52 PDT 2013
    Recording WAVE ‘ar0.wav’ : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
    Mon Jul 1 20:08:07 PDT 2013

    it records for 11 minutes instead of 10. the WAV file is 10 minutes long, so it compresses 11 minutes of audio into 10 minutes. any ideas?

  • Brady

    No, I don’t have that problem, sorry. No idea why it would be doing that.