Skip to content

Audio system setup

edulim edited this page Aug 25, 2020 · 11 revisions

For MIA, besides playing audio from video streams, audio files, etc, we want to record the audio played by the computer and add it to Anki cards, 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 need to install anything and you may 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

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 connect 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 we can check that we have 1 card with 2 loopback devices and 8 subdevices each using the commmand aplay -l.

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 can be seen, 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.

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 real sound card and the virtual loopback devices.

That file creates a binding between the playback at the sound card (hw:SB,0) and the input end of the loopback device (hw:Loopback,1,0). Don't forget to change "SB" with your actual card name. 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 cal, so I like to make a custom keybinding with sxhkd. that way so I press the same key combination to start and pause

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. So check the part that says:

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

and change capture.pcm "hw:SB,0" to match your input device's card. Input devices may be listed with the command:

$ arecord -l

which shows an output similar to aplay -l.

Then you can record with:

$ arecord -f cd -D default test.wav

or

$ ffmpeg -f alsa -i default test.wav

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

Clone this wiki locally