How to redirect the ALSA output to a file?

Different people have tried this in different ways. For me, configuring ALSA to capture from the "Mix" device didn't work since I don't have a "Mix" listed when I try the amixer command. I could however write the ALSA output to a file by configuring the PCM chain.

Have the following code in your ~/.asoundrc file:

pcm.!default {
    type plug
    slave {
        pcm rate48000Hz # Direct default output to the below converter
    }
}

pcm.rate48000Hz {
    type rate
    slave {
        pcm writeFile # Direct to the plugin which will write to a file
        format S16_LE
    #    channels 2
        rate 48000
    }
    #route_policy copy
}

pcm.writeFile {
    type file
    slave {
        pcm card0 # Now write to the actual sound card
    }
    file "aplay-D_card0-t_raw-f_S16_LE-r48000-c_2.raw"
    format "raw"
}

pcm.card0 {
    type hw
    card 0
}

ctl.card0 {
    type hw
    card 0
}

Now get a terminal, go to a directory that contains a media file, issue the player command to play the media.  For example,

$ xmms my-song.mp3

Well, you will later on do something useful than the above since in this case you already have the audio in a file. Here we are experimenting.

Once the play is finished, you will have the “aplay-D_card0-t_raw-f_S16_LE-r48000-c_2.raw” file in the directory. That's the recording.

Now you are going to play the file. Here's one approach:

$ aplay -D card0 -t raw -f S16_LE -r 48000  -c 2 aplay-D_card0-t_raw-f_S16_LE-r48000-c_2.raw

Or else, first rename the ~/.asoundrc to something else to prevent overwriting the recording.

$ mv ~/.asoundrc ~/.asoundrc-alsa-record

Let's convert the raw file to a wav file (which will include the encoding details in a header):

$ sox -w -s -L -r 48000  -c 2 aplay-D_card0-t_raw-f_S16_LE-r48000-c_2.raw out.wav

Now play

$ play out.wav

My environment: Fedora 7, ALSA 1.0.14, Kernel 2.6.23.15

Tips:

  1. Try changing the format mentioned in the .asoundrc file from “raw” to “wav”. If you are lucky (probably because you have a newer version of ALSA than mine), you may directly get a wav file written. Hence, you may not have to convert using sox or provide lengthy arguments to the aplay command. If that works for you, try removing the rate conversion line as well.
  2. Use a tool like ffmpeg if you want to convert the generated raw audio file (or the wav) to something like mp3 since raw or wav audio files are large in their size.
  3. If you want to record the audio of a web site which plays something via Flash player, close the browser first, get a terminal, run the browser from the command line ($ firefox). Otherwise, you may not be able to figure out where the recorded file will end up. I couldn't!
  4. Instead of double clicking the media files (to play), try issuing the player command from a terminal.
  5. If something doesn't work in the way it should be, it can be due to an incompatible setting in the .asoundrc file. Try changing channels, rate, format and so on.
  6. In some cases, I couldn't get this working properly. For example, my version of VLC didn't work correct. If you find some wisdom, let us know. Use the comment section found below.

 

AttachmentSize
asoundrc (save as .asoundrc)449 bytes

Thanks!

Wow that was really useful.
I have Mix and Mix Mono but none of them works, I even made a script to test all the possible combinations and none of them worked xP.
This one worked right away, and the sound quality is better than the one people whose Mix devices work claim to have.
Just a question, where did you learn that stuff? I looked everywhere and ALSA seems to be poorly documented.

asoundrc ALSA

Great Help page! Thanks

Worked perfectly!!

I'm so glad to have found this article! I have an HDA Intel ALC888 onboard chip, and have been looking for a "loopback" method for quite some time (I really should read the ALSA docs...). The "wav" format works well with alsa-lib 1.0.21a (and alsa-utils 1.0.18, fwiw). Regarding FF or anything not started from the terminal, just use a full path for the "file" argument. I used "/tmp/sound-capture.wav". Works like a charm!

Thank you so much!!

piping the output

Following up from my previous comment, another option that I found extremely useful is the ability to use a pipe instead of a file. If you have lame installed, and a recent version of ALSA, you can pipe raw pcm output and convert it to mp3 on-the-fly as follows (read the lame manpage for details):


format "raw"
file "|lame -S -r -s %r --bitwidth %b -m j - /tmp/sound-cap.mp3"

CPU impact was minimal on my system, but ymmv. A couple notes: the output file doesn't necessarily have to be in /tmp, and to avoid a PATH exploit, you may want to specify the full path to lame.

Here are the docs I used to find this info:
http://alsa.opensrc.org/index.php/.asoundrc#Plugins
http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html

Hope this helps someone out there! :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.