linux arecord: capture sound card output rather than microphone input

I am trying to capture sound card output and encode it in Vorbis ogg format using the following command:

arecord -f cd -t raw | oggenc - -r -o file.ogg

However, the computer's microphone input is recorded rather than the sound card output.

The output of the command arecord -l is

**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The output of the command arecord -L is contained in this pastebin entry.

The arecord man page indicates that the input device can be selected using the -D flag, but gives no syntactical examples.

I would like to know how to change the input for arecord from the microphone to the sound card. I am not interested in GUI-based alternatives such as Audacity. Thanks.

Edit: I am running Debian 7.0 (stable) with arecord version 1.0.25, vorbis-tools version 1.4.0, and ALSA driver version 1.0.24.


Solution 1:

years later the struggle continues... how to specify the recording device, lo and behold:

➜  ~ arecord -l                                                              
**** List of CAPTURE Hardware Devices ****                                   
card 0: PCH [HDA Intel PCH], device 0: CX20590 Analog [CX20590 Analog]       
  Subdevices: 1/1                                                            
  Subdevice #0: subdevice #0                                                 
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]       
  Subdevices: 1/1                                                            
  Subdevice #0: subdevice #0     

➜  ~ arecord -f S16_LE -r 44100 --device="hw:1,0" plik.wav                   
Recording WAVE 'plik.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono 
^C
Aborted by signal Interrupt...  

So the format for device string is hw:<card>,<device>

format of device id

Solution 2:

I'd suggest using the ALSA loopback driver. To load it:

sudo modprobe snd-aloop

This should add a new capture device to the output from arecord -L.

If you have only one physical soundcard in your computer, you can make the loopback soundcard the default for all apps by creating a file .asoundrc in your home folder with the following content:

pcm.!default {
    type hw
    card 1
    device 0
}

While this file is in place, any sound output from all applications should go to the loopback adapter rather than your actual speakers. Just specify the correct device in your arecord command and once you're done, remove the .asoundrc file to go back to normal.

Solution 3:

I had the same problem and found another solution here. The main idea is to use pacat. For me it works like this:

  1. Find monitor device:

    name: <alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor>
    name: <alsa_output.pci-0000_00_1b.0.analog-stereo.monitor>
            alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor/#0: Monitor of HDA NVidia Digital Stereo (HDMI)
            alsa_output.pci-0000_00_1b.0.analog-stereo.monitor/#2: Monitor of Built-in Audio Analog Stereo```
    
  2. Capture audio to file:

    $ pacat --record -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor > dump.raw

  3. Convert file to WAV:

    $ sox -t raw -r 44100 -e signed-integer -L -b 16 -c 2 dump.raw output.wav

That's it!

Solution 4:

Your hardware does not support capturing its output; once the data has gone into the actual playback device, it's gone.

You need to use some software that duplicates the audio data before it is written to the hardware.

I'd suggest using PulseAudio; there you can simply select one of its outputs as capture source: pavucontrol output monitoring