Recording Sound on the Raspberry Pi

The Raspberry Pi does not have a microphone socket, which is inconvenient when you wish to record sound. To fix this you will need a USB Sound Card, for which I bought a Creative Sound Blaster Play! for about £20 and a short USB extension lead as the sound card is slightly too large and blocks the other USB port.

With the latest Raspbian “wheezy” installed on a Pi Model B with 512Mb of RAM and the overclocking set to High in raspi-config, here is a recipe for getting your Raspberry Pi to record sound from the command line. For the test setup I connected my iPod to the microphone port of the sound card, plugged everything in and powered up.

Raspberry Pi Records

After logging into the Pi, check that the computer can see the card, use lsusb to find it, here the card is highlighted in blue:
$ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 046d:c52e Logitech, Inc.
Bus 001 Device 005: ID 041e:30d3 Creative Technology, Ltd Sound Blaster Play!
remember that different makes of card will have different names and ID’s

Your user will need to be in the audio group, check this with groups <username>:

$ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users netdev input
if not, then add them with:$ sudo usermod -a -G audio <username>

There is/was an issue with the Pi’s USB port that meant it can/could become overwhelmed1 with data which causes popping and bubbling noises to be included in your recordings, this can be fixed with an update of the Pi’s firmware:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install rpi-update
$ sudo rpi-update

The Raspbian image already has the alsa-utils for sound already installed, the programs I am using for recording and playback are:

  • alsamixer – GUI for setting the recording and playback levels
  • amixer – Command Line for setting the recording and playback levels
  • alsactl – for saving the settings set in alsamixer or amixer to use again after a reboot
  • arecord – For recording the sound
  • aplay – For playing back your recording

All of these programs have a –help option.

There are two methods for setting up the the microphone port on the card, the first is alsamixer:

alsa mixer

$ alsamixerPress F6: Select Sound Card, and choose yours from the list, the bcm2835 ALSA is the on-board sound, for me the one to pick was: USB Device 0x41e:0x30d3 and take a note of the card number, in my case: 1. Now select the Mic and increase the volume to 52, or the first white blob, you’ll need to change it later, but its a good place to start. The Auto Gain Control wants to be off, select the gain control and press M to toggle so it displays [MM] for mute. Press Esc to exit and save the settings with:$ sudo alsactl store 1 where 1 is the card number.

Alternatively, you can use amixer. First find your sound card, in amixer there does not appear to be a method of listing the available cards, but on a Raspberry Pi I guess it will always be card 1, you can list the cards current status with:

$ amixer --card 1 contents
numid=1,iface=MIXER,name='Mic Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=2,iface=MIXER,name='Mic Playback Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=32,step=0
: values=21
| dBminmax-min=0.00dB,max=47.81dB
numid=5,iface=MIXER,name='Mic Capture Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=7
| dBminmax-min=0.00dB,max=23.81dB
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=3,iface=MIXER,name='Speaker Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=4,iface=MIXER,name='Speaker Playback Volume'
; type=INTEGER,access=rw---R--,values=2,min=0,max=151,step=0
: values=52,52
| dBminmax-min=-28.37dB,max=0.06dB

So I want to turn the Auto Gain Control off, and the recording volume to 14:
$ amixer -c 1 cset numid=7,iface=MIXER,name='Auto Gain Control' 0
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=off
$ amixer -c 1 cset numid=6,iface=MIXER,name='Mic Capture Volume' 14
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=14
| dBminmax-min=0.00dB,max=23.81dB

again, store the settings so that they will be used again on a reboot:$ sudo alsactl store 1

Now we are ready to do a test recording, first check that arecord will see your card:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: U0x41e0x30d3 [USB Device 0x41e:0x30d3], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

and now for a ten second test recording, this will create a file called rectest.vav in your home directory. Remember to set the Device (-D plughw:1) number to the right card (card 1):

arecord -D plughw:1 --duration=10 -f cd -vv ~/rectest.wav the vv option displays extra information on the screen as well as a volume meter, this should be peaking at around 95% on the loudest sounds, if it is at 100% all a lot of the time then you are probably recording distortion. Playback the recording with aplay:

aplay ~/rectest.wav

the default settings will play the wav fie through the TV if it is connected by HDMI, To playback through the USB sound card set the device to the card number, like in arecord:

aplay -D plughw:1 ~/rectest.wav

Congratulations, you now have a fully working Pi Recording Device. Remember to experiment with the volume levels, too high and your recording will sound distorted.

References:

  1.  Raspberry Pi Usb Audio fix (10 May 2013)

15 thoughts on “Recording Sound on the Raspberry Pi”

  1. Congratulations. Great article.
    I have one question. In your opinion, could someone record sound and something later (for example 60 seconds) playback the file. That is, could you introduce this type of delay to your IPOD. I am interested in designing one device that record what I listen from my radio and sincronize with a video streaming.

    Thanks so much.
    Pablo.

  2. Hey there,
    I am new to the whole raspberry pi thing, but I am trying to record sound with one. Would I be able to connect a microphone into the sound card instead of the iPod and still record sound? (I want to record sound on a street). And then would I need to write additional code for that or could i just use the code you have created?

    Looking forward to your advice.

    Thanks,
    Jessica

    1. Yes a microphone will work. You’ll want to set for different recording levels, the microphone output will be much quieter than the iPod. Just experiment to get the settings you want.

  3. Great article…I connected a Logictech C615 webcam and tested it according to your tutorial for microphone audio on webcam and it worked like a charm!

  4. Hi, great advises!

    I want to build/program a “Tape-Recorder”, a PI with USB Soundcard in a old TapeDeck Case.
    Your record sample with arecord works fine, but one thing i really need: a manuel stop or pause function of the record!
    It should record all the time until a gpio goes lo.
    Do you know how to make this possible?

    regards,
    Wolfram.

    1. Hello Wolfram
      An interesting question, but at the moment I cannot give you an answer. I suppose some kind of keyboard emulator that works of the GPIO would work?

      I believe the Arduino Leonardo can be used as a USB keyboard, but that may be a bit bulky for what you want to do.

    1. Looking at the man pages for arecord http://linux.die.net/man/1/arecord the –interactive option uses the space bar as a pause/resume, but more of interest to you: “When recording, SIGINT, SIGTERM and SIGABRT will close the output file and exit.” so a python script running as a deamon (background process) should be able to send one of those in response to a GPIO action (this is theory, and not tested).

  5. that, karl, sounds nearly exactly for what i am looking for!
    Its late, but i will try something with that!

    Thank you very much!

  6. Thanks for the information on command line recording.

    While not entirely relevant, you can check in alsamixer what your sound card will be by pressing F2(System information) and selecting /proc/asound/cards. The creative media sound card will be card 1 and it should tell you all the details.

    I had a bit of a stumble at first because I’m using a Behringer C-1U for my recording, and it registers as a different card.

    Generally you can use Audacity if you are using the X command window (via startx), but headless it’d be a good idea to use the command line interface.

    Another note is that it depends on how you record (or rather what recording device you use) that affects distortion. I haven’t really had problems cranking up my microphone to 100% and recording using that. Though again, your mileage may vary.

    Hope this helps.

    1. I’m not aware of a limit on the length of recordings, as far as I can tell it’ll record until run out of hard disk space, although I’ve not tried this.

  7. Hi, I am using a Logitech USB Headset to record user commands as a part of another project..
    I tested the above arecord and aplay commands and after adjusting levels, the record and playback are working great for the headset.
    But when I run it as a part of the main code with the same alsaaudio settings, it throws the following error:
    File “main.py”, line 156, in
    start()
    File “main.py”, line 130, in start
    l, data = inp.read()
    alsaaudio.ALSAAudioError: Capture data too large. Try decreasing period size

    Any clues how to resolve this? I have tried decreasing periodSize but still the same error.

Leave a Reply

Your email address will not be published. Required fields are marked *