How to redirect the ALSA output to a file?

Submitted by Kamal Wickramanayake on November 6, 2008 - 12:52

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


  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.