Skip to content

Audio system setup

Eduardo edited this page Aug 14, 2021 · 11 revisions

Besides playing audio from video streams, audio files, etc, we want to be able to record the audio played by the computer and add it to Anki flashcards, as well as record our voice while shadowing. This wiki introduces the basics of the Linux sound system and how to setup everything you need in order to start recording audio.

Setting up audio in a Linux system depends on what sound system you have installed. There are 2 options to choose from: ALSA or OSS.

  1. Advanced Linux Sound Architecture (ALSA)
    1. Pulseaudio
    2. Jack
  2. Open Sound System (OSS)

All Linux distributions should come with ALSA as it's a kernel component, so there's no need to install anything and you should be able to play any sound without prior configuration. However, the default ALSA configuration doesn't allow recording playback sound, which is necessary for getting audio that we're going to use on Anki cards.

Pulseaudio and Jack and are sound servers that work on top of ALSA and allow for more control regarding multiple streams and network. Most desktop environments come with Pulseaudio and its control panel pavucontrol, so it's usually very easy to set up recording.

OSS was the default sound system before ALSA, and you'd have to manually install it in order to use it.

Setting up ALSA

If you have only ALSA without Pulseaudio or JACK, follow these instructions.

The idea is simple: first we duplicate the outcoming signal that would be played by our sound card through speakers/headphones, send a copy to a loopback device as input and the the other signal to our sound card as output. A loopback device output end is connected to its input end, so we can listen to that loopback device's output end and capture the signal at the same time our speakers play it.

Virtual loopback devices can be created with the snd_aloop kernel module. Load it with the following command and root privileges (non-persistent across rebooting).

# modprobe snd_aloop

You can add a line with snd_aloop to the file at /etc/modules-load.d/modules.conf or /etc/modules to make it persistent across rebooting.

# echo 'snd_aloop' >> /etc/modules

Now run the commmand aplay -l to get a list of playback devices. Here's a sample output from my computer:

**** List of PLAYBACK Hardware Devices ****
card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: SB [HDA ATI SB], device 0: ALC887-VD Analog [ALC887-VD Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: SB [HDA ATI SB], device 1: ALC887-VD Digital [ALC887-VD Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: HDMI [HDA ATI HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The format is card X: NAME [FULLNAME], device Y: description As you can see, I have 3 cards. The card I use for playback with my headphone is card 1 (aka "SB"). It's a good idea to take note of your card's name, as it'll be needed for configuring ALSA. If I were using a TV as my monitor it'd probably be plugged to the HDMI port, so the card used would be different. I also have 1 card with 2 loopback devices and 8 subdevices, because I loaded the snd_aloop kernel module.

In order to configure ALSA we need to edit the file ~/.asoundrc (you may need to create it). There's a sample file in the AUDIO directory of this repository so you can copy it to your home directory. The file might look a bit intimidating, but is consists basically of Pulse-code modulation (PCM) devices that abstract the physical sound card and the virtual loopback devices.

Essentially, that file creates a binding between the playback at the sound card (hw:0,0) and the input end of the loopback device (hw:Loopback,1,0). You need to change hw:0,0 with your actual card name (hw:SB,0 in my case). You may also need to edit the sample rate from 44800Hz to 44100Hz depending on how your system responds.

If everything went well, you should be able to type

$ arecord -f cd -D looprec test.wav

or

$ ffmpeg -f alsa -i looprec test.wav

and start recording audio from your computer.

It's possible to make a script with those commands, and you can find one called record in the repository. That script works just like ShareX on Windows for recording playback audio, so you can paste the resulting file to Anki with Ctrl + v at the Card Editor window or Ctrl + Shift + v at the Card Exporter window with the MIA Dictionary addon. The script is made so that it finishes recording upon second call, so I like to make a custom keybinding with sxhkd. That way I can press the same key combination to start and stop.

In case you want to record audio from you microphone for shadowing exercises you'd have to edit ~/.asoundrc with the correct card for your microphone. Input devices may be listed with the command:

$ arecord -l

which shows an output similar to aplay -l.

**** List of CAPTURE Hardware Devices ****
card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: SB [HDA ATI SB], device 0: ALC887-VD Analog [ALC887-VD Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: SB [HDA ATI SB], device 2: ALC887-VD Alt Analog [ALC887-VD Alt Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

So in the .asoundrc file go to the part that says:

pcm.!default {
  type asym
  playback.pcm "LoopAndOut"
  capture.pcm "hw:0,0"
}

and change capture.pcm "hw:0,0" to match your microphone's card; hw:SB,0 in my case.

Now you can record audio streams from your microphone with:

$ arecord -f cd -D default test.wav

or

$ ffmpeg -f alsa -i default test.wav

One may end the recording by killing the arecord process, e.g. with Ctrl+C or kill.

For more information about the file ~/.asoundrc look at:

Setting up Pulseaudio

Pulseaudio doesn't require creating a configuration file like .asoundrc as the pulseaudio package provides the tool parec that is used to record audio from input or output channels. In order to use it, it's necessary to know the device that is responsible for sending the audio stream to you speakers.

Start by listing the available devices with:

$ pactl list short sources

That should display a few devices indexed by a number. The following is a sample output from my computer.

0	alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
1	alsa_output.pci-0000_00_14.2.analog-stereo.monitor	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
2	alsa_input.pci-0000_00_14.2.analog-stereo	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
3	alsa_output.platform-snd_aloop.0.analog-stereo.monitor	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
4	alsa_input.platform-snd_aloop.0.analog-stereo	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED

There are two types of devices listed: input and output devices. In order to record the same audio that is played by the speakers, you should pick an output device, which is a device like 0, 1 or 3 from the listing above. In my case, I am using the analog connection, so I picked 1, alsa_output.pci-0000_00_14.2.analog-stereo.monitor.

Following that example, I can record audio played by my computer with:

$ parec --file-format=ogg -d alsa_output.pci-0000_00_14.2.analog-stereo.monitor test.ogg

or

$ parec --file-format=oga -d 1 test.ogg

or with ffmpeg

$ ffmpeg -f pulse -i 1 test.ogg

If I wanted to record audio from my microphone, I'd change the device (-d) option to 2 or alsa_input.pci-0000_00_14.2.analog-stereo.:

$ parec --file-format=oga -d alsa_input.pci-0000_00_14.2.analog-stereo test.ogg

or

$ parec --file-format=oga -d 2 test.ogg

Calling:

parec --list-file-formats

should list all available formats.

One may end the recording by killing the parec process, e.g. with Ctrl+C or kill.

Clone this wiki locally