Linux: Recording soundcard output using arecord

Through this brief article I want to share with you, my experiences as I tried to
record audio, playing on my computer speakers. I have a Fedora Core 6 with ALSA
configured for my sound card.

What I was trying to do was this:
Play a song or movie through one of the players available like: xine, xmms etc.
Record this audio output.

There is an application called KRec which is bundled with KDE, but this app can only record an aRTs ouput, not ALSA’s. Besides, the output format is not WAV.

When I google’d for other alternatives, I came across a command line utility called
‘arecord’ which comes with ALSA.

I checked it out and it was installed in my system.
An example command to record soundcard ouput was specified in the man pages and I tried it:

arecord -d 10 -f cd -t wav -D copy foobar.wav

This command records a 10 second audio clip of CD quality from the device ‘copy’
and the output is in ‘wav’ format.
But, the command failed saying that it could not find ‘copy’.

According to the manual the ‘copy’ device has to be specified in the user’s .asoundrc file.
As I did not find the file in my home folder, I created it and named it  ‘.asoundrc’ in my home folder and copied the following lines into it:

pcm.copy {
type plug
slave {
pcm hw
route_policy copy

Now when I tried the previous command again, it worked!
But unfortunately, when I tried to play the wav file back all I heard was static.

Then I saw that the ‘arecord’ man pages referred to the command ‘amixer’.

After a bit of digging I issued the following command:

amixer contents

This command displays all controls applicable to the sound card.

The ouput of this command listed a control called ‘Capture Source’ which was currently set to 3 which meant it was recording from the external microphone.

I set the capture source to 4 which meant ‘MIX’:

amixer cset numid=27,iface=MIXER,name='Capture Source" 4
(The ‘numid’ might be different in your system, use ‘amixer controls’  to find the exact name)

Then I tried the old arecord command:

arecord -d 10 -f cd -t wav -D copy foobar.wav

After all that waiting, it felt amazing to hear something recorded when I played back foober.wav. But the recording was horrible with a lot of static, so I dug up the man pages again and issued the following command:

arecord -d 10 -c 2 -f S16_LE -r 44100 -t wav -D copy foobar.wav

This is exactly same as the earlier command (‘-f cd’internally is actually ‘-c2 -r44100 -f S16_LE’) but now the audio quality was perfect with no noise as far as I could hear.

The output was a bulky file which blew up to 1.7 MB for 10 seconds which is understandable given that the output is wav format.

If you have lame installed on your system you can use the following command to convert wav to MP3.
lame -h foober.wav foobar.mp3

Do not set you player volume to max while recording. This will introduce noise into your recording. Set it halfway through, say between 50%-60%.

If you do not know how long a recording will take (For example a live interview on LQRadio), set the -d option of ‘arecord’ command to an arbitrarily large value like 1400.
This way when you have reached the end of the recording you can just ctrl-c.

Good bye and happy recording.

About this entry