Automated Raspberry Pi audio recorder

Today we’re gonna roll our very own sound recorder using a Raspberry Pi

Raspberry Pi Audio Recorder

This tutorial will show you how to build a fully automated recorder using an RPi, a USB sound card and a USB drive.

First up; install Raspbian, make sure it defaults to the command line and/or has SSH access so you can login to it from another computer. There’s plenty of tutorials out there explaining how to do this.

Second; make sure you have a compatible sound card, (I used a spare Griffin iMic, any class compatible one should work) and a USB flash drive. It should be possible to write to the SD card you’re using on the RPi, but people have had problems going that route. Besides, using a fat32 formatted usb drive will ensure you can plug it into you computer and play or copy the sound files without having to login (ssh) to the RPi.

First we’re gonna get rid of a lot of unwanted stuff! This isn’t a complete list, but I’m sure we can live without these applications installed taking up precious CPU cycles, RAM and disk space. Note: This will completely wipe the graphical user interface. You won’t be able to use the desktop anymore!

$ sudo apt-get purge galculator idle3 idle idle-python3.2 python3 midori scratch xpdf midori dillo netsurf-common netsurf-gtk leafpad penguinspuzzle pistore gpicview heirloom-mailx wpasupplicant aptitude xarchiver omxplayer aspell usbmuxd debian-reference-common debian-reference-en python-picamera cups-bsd cups-common vim-common vim-tiny desktop-base lightdm lxappearance lxde-common lxde-icon-theme lxinput lxpanel lxpolkit lxrandr lxsession-edit lxshortcut lxtask lxterminal obconf openbox raspberrypi-artwork xinit xserver-xorg xserver-xorg-video-fbdev xserver-common xserver-xorg-core xserver-xorg-video-fbturbo x11-common x11-utils x11-xkb-utils xauth xfonts-encodings xfonts-utils oracle-java7-jdk aptitude-common python python-gi python-minimal python-pifacecommon python-pifacedigitalio python-rpi.gpio python-serial python-support python2.7 python2.7-minimal python3.2 python3.2-minimal

Followed by:

$ sudo apt-get autoremove

And:

$ sudo apt-get autoclean

That’s a lot of stuff we won’t be using! Now, we’re gonna install what we do want:

$ sudo apt-get install sox libsox-fmt-mp3

Sox is a specialized recording, processing and playback application. I won’t go into it too much, but it fits our needs perfectly. The second, libsox-fmt-mp3, is a library so we can encode to MP3 (if that’s what you want).

Now that we deleted and installed what we need, let’s set-up the soundcard. First up, let’s kill the built-in sound card:

$ sudo nano /etc/modules

And comment out:

snd-bcm2835

By putting a “#” in front of it. Like so:

# snd-bcm2835

To be sure USB is first in line, we’re gonna force ALSA to pick it over the built-in one, even if we’ve just killed it.

$ sudo nano /etc/modprobe.d/alsa-base.conf

And comment out:

options snd-usb-audio index=-2

Again, with a “#” like this:

# options snd-usb-audio index=-2


Last but not least enter:

$ alsamixer

And set the recording levels of your soundcard to how you like them by pressing F4. To make sure the Raspberry Pi always boots up with these sound settings, enter this in the command line:

$ sudo alsactl store

Make a folder to mount the USB stick in:

$ sudo mkdir /mnt/USB/

Now we’re gonna make a few scripts. Don’t be afraid if you’ve never done this before, I’ve pre-fabricated them for you! In your home folder (Type “cd” and hit enter if you’re not certain), type:

$ mkdir Scripts

This’ll make a neat little folder for you to put scripts in. Change into it by typing:

$ cd Scripts/

Next:

$ nano MyBoot.sh

Copy and paste the following into it.

#!/bin/bash
## Created by Son of 8-Bits
## http://sonof8bits.com/
## Borrowing parts from Autostatic
## http://autostatic.com/

## Mount the USB stick and kill unwanted services

## mount the USB stick
sudo mount -t auto /dev/sda1 /mnt/USB

## Stop the ntp service
sudo service ntp stop

## Stop the triggerhappy service
sudo service triggerhappy stop

## Stop the dbus service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo service dbus stop

## Stop the console-kit-daemon service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo killall console-kit-daemon

## Stop the polkitd service. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
sudo killall polkitd

## Kill the usespace gnome virtual filesystem daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall gvfsd

## Kill the userspace D-Bus daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall dbus-daemon

## Kill the userspace dbus-launch daemon. Warning: this can cause unpredictable behaviour when running a desktop environment on the RPi
killall dbus-launch

## Set the CPU scaling governor to performance
echo -n performance | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

exit

Press “ctrl+x” to exit, “Y” to save, followed by an enter. You should now be back in the command line. This script will automatically mount your USB drive in /mnt/USB/ and kill some unwanted services clogging up RAM and CPU cycles. Most of this script was made by Autostatic, check out his original work here.

Now for the recording script:

$ nano Record.sh

Containing:

#!/bin/bash
## Created by Son of 8-Bits
## http://sonof8bits.com/
## 48000Hz stereo MP3 at 320kbps that automatically stops recording after 1 minute of silence.

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

sudo rec -c 2 -r 48000 -C 320.99 –buffer 262144 /mnt/USB/${FILENAME}.mp3 silence 1 0.1 1% 1 1:00 1%

[ $? -eq 0 ] || exit $?

sleep 10

sudo shutdown now

exit

ctrl+X, Y, enter

To make these scripts work as planned enter:

$ sudo chmod 755 MyBoot.sh Record.sh

But these scripts won’t start by themselves just by being there! To make them start at boot type:

$ sudo nano /etc/rc.local

On the empty line after “# By default this script does nothing.” add:

sudo /home/pi/Scripts/MyBoot.sh
sudo /home/pi/Scripts/Record.sh

ctrl+X, Y, enter

BAM! You’re done!

Try it out by hooking up a sound source to the line-in of your soundcard, booting the Pi, and recording a part of whatever it is you want to record. Note that after one minute of silence the RasPi will shut itself down. Listen back what you’ve just recorded by unplugging the USB flash drive and plugging it in you computer. Here are some tips if things are not to your liking.

Volume too low or high? Check the part about Alsamixer. (And don’t forget to check the level coming out of your audio source as well!)
Like to stop sooner (or later) after silence sets in, edit the recording script. See the part that says “1:00”? This is 1 minute, edit it to your liking. For instance ‘0:30’ for 30 seconds or ‘2:00’ for 2 minutes.
Pi not getting enough power? Use a 2 ampere powerplug or put a powered USB-hub between the RasPi and the soundcard/USB stick.

 

 

Comments

9 Comments

  • By narutonz, 27/02/2015 @ 13:28

    Can I not remove the desktop GUI/python and still run this script? Or another way to phrase it, can I still use the desktop environment after stopping the script?

  • By JordyVision, 27/02/2015 @ 13:35

    Fine with me! I did so cause a) I didn’t need it for this project and b) to save resources. I ran into quite a few buffer over/underruns causing audio to pop, break up, or being discarded entirely from the recording.

    If possible, please post your findings here after you’ve tried it!

  • By Dawood, 31/03/2015 @ 12:26

    hi
    i have followed all the steps to program the raspberry pi to record however when i rebooted the pi it says ‘ [FAIL] startpar: service(s) returned failure: rc.local …failed

  • By zebadoba, 23/04/2015 @ 16:39

    I am having the same error too. Have you fixed yours? I am investigating now.

  • By zebadoba, 23/04/2015 @ 17:03

    Ok, it is failing on the Record.sh script. I tested it by first manually running the commands one at a time in tehs scripts, until it failed on the “sudo rec . . .” comment in Record.sh

    Then I commented out Record.sh in rc.local and rebooted.
    getting closer!

  • By Dawood, 23/04/2015 @ 17:32

    hi i have still not been able to work it out. if you find a solution please help me out. iv also asked jordy the person who made this blog and he does not know why it is doing this. he has a raspberry pi model b.

  • By Sabreur, 19/04/2016 @ 18:44

    Just what I was looking for but…

    I started by using Jessie Lite (were you using Weezy?)

    The purge failed as there is no pistore. I deleted this and then it ran but most items are not in LTE anyway.

    /etc/modules was empty
    /etc/modprobe.d/alsa-base.conf did not exist

    I am using a Yoga AD-200 USB Adaptor but perhaps this is not compatible, never used it before. It’s intended for headsets.

    Am I wasting my time with Jessie? Raspbian Wheezy seems to have been removed. I have an early Weezy (2012) I could try that but upgrade won’t have finished untill tomorrow! Might as well kick it off and go to bed!

  • By Sabreur, 21/04/2016 @ 09:57

    It’s sort of working but a few teething problems.

    Gave up trying to get it going on Jessie and used an old Weezy.

    I had the same problem as Dawood and Zebadoba. I added an extra hyphen before the buffer statement. I now reads:

    sudo rec -c 2 -r 48000 -C 320.99 -–buffer 262144 /mnt/USB/${FILENAME}.mp3 silence 1 0.1 1% 1 1:00 1%

    Not sure if this is a sound card problem, I’m using an AD-200 C-media device but the background noise level is very high even with the REC level at 0, so the silence detection is negated. The card works fine when used with skype on a PC.

    Any suggestions for a fix or another sound card?

    Once it is running there is no way to stop it. SoX man pages say CTL+C twice but this only works if rec is started from the command line not when it is started by rc.local.

    Is it supposed to terminate after one recording or does it make many until stopped? If the former, please can you suggest a modification to your script so it will loop until stopped? I need individual files of each incident not one file with the silences suppressed.

    Not sure why you purged NTP, an accurate timestamp of the recording stamp and if possible, a duration would be nice. Otherwise I could get a duration from the timestamp and file creation time.

  • By Son of 8-Bits, 21/04/2016 @ 11:20

    Sorry for taking a while to get back to you, I had problems accessing my own website..

    I used Wheezy yes, and haven’t tried it with Jessie. I did make a new recorder, but it’s not suited for the wild yet. I’ve moved away form rec and use arecord now. Maybe that helps with the project?

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment