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.Recording Sound on the Raspberry Pi

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:

$ 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

Recording Sound on the Raspberry Pi schematicNow 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.

For more detail: Recording Sound on the Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top