34

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.

user001
  • 3,474
  • 7
  • 24
  • 32
  • Does your hardware have some mixer control to route the output back to the capture device? – CL. May 19 '13 at 08:37
  • @CL. The only pre-installed mixer that I am aware of is `alsamixer`. If I select the loopback card within `alsamixer` (F6 key to select sound card), I receive the message `This sound device does not have any controls`. If I exit the mixer and return, I find that the sound card is restored from loopback to the default card. Attempting to suspend (`ctrl-z`) and background (`bg`) the `alsamixer` process after selecting `loopback` does not enable successful recording of audio via the `arecord` command. – user001 May 20 '13 at 04:15
  • The card selection in `alsamixer` does not affect the rest of the system; it just changes which controls are shown. What are the recording selection controls of the Conexant device? – CL. May 20 '13 at 06:45
  • @CL. For my Conexant device, I have one playback control (`Master`) and one capture control (`Capture`) within `alsamixer`. The capture can be toggled on and off with the spacebar within `alsamixer` and it is on by default. Please let me know if this is not what you intended. Thanks. – user001 May 20 '13 at 09:51
  • related: https://askubuntu.com/questions/229352/how-to-record-output-to-speakers – Ciro Santilli OurBigBook.com Dec 08 '20 at 21:06

6 Answers6

13

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

  • The values accepted by `--device` switch are given by `arecord -L`, not `arecord -l`. You can use names returned by `arecord -L` directly. The switch name is confusing as it does not accept "devices" but "PCMs" - from manpage: `-L, --list-pcms List all PCMs defined -D, --device=NAME Select PCM by name` – Strachu Sep 06 '21 at 18:04
11

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:

    $ pacmd list | grep "\.monitor"
    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!

Matthias Braun
  • 1,162
  • 1
  • 17
  • 29
hochl
  • 771
  • 3
  • 14
  • 23
  • 6
    Note that you can use `parec` instead of `pacat --record`. You can also avoid the use of `sox` and use `parec -d --file-format=wav output.wav`. You can list available file formats with `parec --list-file-formats`. – Peque Apr 05 '17 at 17:20
9

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.

beveradb
  • 176
  • 2
  • Thanks for your suggestions. Despite creating `.asoundrc` in my home directory and restarting the terminal emulator session, audio is still directed to speakers. The `arecord -f cd -t raw | oggenc - -r -o file.ogg` command records nothing. Issuing `arecord -l` provides the same list as before, but with two additional lines for `card 1: Loopback` (`devices 0, 1`), each with 8 subdevices (`#0-#7`). Likewise, `arecord -L` added the following three lines: `sysdefault:CARD=Loopback [newline] Loopback, Loopback PCM [newline] Default Audio Device`. Continued in next comment due to character limit. – user001 May 20 '13 at 03:56
  • Attemping `arecord -f cd -t raw --device=sysdefault | oggenc - -r -o file.ogg` records a blank audio file for the duration the command is executed (it does not draw audio data from the microphone as before). Unfortunately, `sysdefault` is degenerate as it is defined to be both `CARD=PCH` (first line of `arecord -L`) and `CARD=Loopback` (last line of `arecord -L`). Thanks for any follow-up suggestions you are able to provide. – user001 May 20 '13 at 04:01
  • The loopback allows to record only what is played to *its* playback device, not to the sound card output. – CL. May 20 '13 at 06:51
  • Sorry my answer didn't work - it works on my setup. I'd recommend taking a look at this explanation of the asoundrc file: http://www.alsa-project.org/main/index.php/Asoundrc I'm fairly sure if you try a few things and play around with different devices a bit more you should get it working. A bit of a kludge, but do you happen to have a physical 3.5mm->3.5mm cable? If so, you could always just connect your headphones output jack to the mic/line in jack. – beveradb May 20 '13 at 20:28
  • If you can't get it working with asoundrc, these links may provide more info to help figure it out: http://www.sabi.co.uk/Notes/linuxSoundALSA.html#tasksRecordPlay http://forum.vectorlinux.com/index.php?topic=7500.0 If you still can't get it working, you could switch to using PulseAudio instead of ALSA. This answer explains how to do this using PulseAudio: http://askubuntu.com/questions/171287/how-to-pass-record-audio-output-as-an-input-device – beveradb May 20 '13 at 20:30
3

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

CL.
  • 1,595
  • 12
  • 12
  • I installed `pavucontrol` and tried recording using `gnome-sound-recorder`. When I open `gnome-sound-recorder` and begin recoding (`Ctrl-R`), the stream appears in the PulseAudio Volume Control window. The stream does not actually record any data, however (the final length is 0:00). Also, are you familiar with how to perform the recording through command line instead of a GUI? Thanks . – user001 May 21 '13 at 22:16
1
  1. Go to PulseAudio Volume Control -> Input Devices Tab. At the bottom, choose to show "ALL input devices". Check if "Monitor of Internal Audio Analog Stereo" is not muted and the volume going up and down as you play the audio!!

  2. Use Audio Recorder to record Audio Source = Internal Audio Analog Stereo (Audio Input). See screenshot below.

enter image description here

nc4pk
  • 9,037
  • 14
  • 59
  • 71
Zid
  • 11
  • 1
0

I have just installed audio-recorder: https://launchpad.net/audio-recorder which in its options show what you need...

enter image description here

SergioAraujo
  • 251
  • 3
  • 6