Kindly support DOTSLASHLINUX on Patreon to keep the website up and running while remaining ads free.

Basic ALSA Configuration

alsa
Firas Khalil Khana | 30/03/2017

In this article I’ll show you how to setup ALSA, with a working .asoundrc configuration file that supports multiple applications!


1- Installation


Gentoo Linux:

emerge --sync && emerge -av media-sound/alsa-utils


Void Linux:

xbps-install -Su && xbps-install -S alsa-utils


Arch Linux:

pacman -Syu alsa-utils



2- Unmuting Channels


Once ALSA is installed, we need to unmute the master channel for our sound card. We can do that using the ncurses interface of alsamixer (Use F6 to switch to your default soundcard, then press F5 to show all options):

┌─────────────────────────────────────────────────────────────────────────── AlsaMixer v1.1.4 ───────────────────────────────────────────────────────────────────────────┐
│ Card: HDA Intel PCH                                                                                                                            F1:  Help               │
│ Chip: IDT 92HD99BXX                                                                                                                            F2:  System information │
│ View: F3: Playback  F4: Capture  F5:[All]                                                                                                      F6:  Select sound card  │
│ Item: Master [dB gain: 0.00]                                                                                                                   Esc: Exit               │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│          ┌──┐         ┌──┐         ┌──┐         ┌──┐         ┌──┐         ┌──┐         ┌──┐         ┌──┐                      ┌──┐         ┌──┐                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          │▒▒│         │▒▒│         │▒▒│         │▒▒│         │  │         │  │         │  │         │▒▒│                      │  │         │  │                        │
│          ├──┤         ├──┤         ├──┤         ├──┤         └──┘         └──┘         ├──┤         └──┘       Enabled        └──┘         └──┘       Disabled         │
│          │OO│         │OO│         │OO│         │OO│                                   │MM│                                                                            │
│          └──┘         └──┘         └──┘         └──┘                                   └──┘        L    R                                                              │
│                                                                                                   CAPTURE                                                              │
│          100        100<>100     100<>100     100<>100       0<>0         0<>0          0         100<>100                    0<>0         0<>0                        │
│     <   Master   > Headphone     Speaker        Mic       Mic Boost    Mic Boost       Beep       Capture    Auto-Mute Mo Internal Mic Internal Mic Loopback Mix       │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
│                                                                                                                                                                        │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

If you can’t record sound using your internal microphone make sure that the Mic and Capture channels are unmuted (pressing the spacebar key on the Capture channel should switch from ——- to L R CAPTURE, this is necessary for the microphone to work).


3- ALSA Services


These are (alsa-restore, alsa-state, alsasound… etc). In most distros, these services are automatically enabled, however just to be safe, let’s see how we can enable and start them on:

Gentoo Linux (start and enable)

rc-service alsasound start
rc-update add alsasound boot


Void Linux (start and enable)

sv start alsa
ln -s /etc/sv/alsa /var/service/


Arch Linux (start and enable)

systemctl start alsa-restore.service
systemctl start alsa-state.service
systemctl enable alsa-restore.service
systemctl enable alsa-state.service

4- Default Sound Card & Sound from Multiple Applications


After you’ve done all the previous steps, ALSA should be working perfectly. Sound from multiple applications which is supported by alsa’s dmix plug-in is enabled by default. However, on some setups (like mine), the default sound card may not be your main sound card (for example HDMI and Intel HD Audio PCH):

aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: HDMI [HDA Intel HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: 92HD99BXX Analog [92HD99BXX Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0


The HDMI soundcard is given an index of 0, and your main PCH soundcard is given an index of 1. Thus the default card chosen by ALSA is the HDMI card. Therefore, you’ll hear nothing. To fix this, first we need to tell if the soundcard drivers and codecs are built into the kernel or left as modules.

In the latter, we can easily fix this problem by specifying our default sound card as a kernel parameter. Create a file in /etc/modprobe.d called alsa.conf or whatever_you_like.conf:

touch /etc/modprobe.d/alsa.conf


And put the following in it:

options snd-hda-intel index=1,0


This swaps the index numbers of both cards giving your main PCH sound card an index of 0, and your HDMI soundcard an index of 1.

However, if your soundcard drivers and codecs are built into the kernel. That previous line won’t do a thing. You have to tell ALSA when you’re starting an X session which card you want to use as your default soundcard. Therefore you need to create a file in your home directory called .asoundrc:

touch ~/.asoundrc


Now here’s a custom configuration that I use to swap both cards, and enable sound from multiple applications without sacrificing any sound quality whatsoever:

pcm.!default {
	type plug
	slave.pcm "dmixer"
}

pcm.dmixer  {
 	type dmix
 	ipc_key 1024
 	slave {
		pcm "hw:1,0"
		period_time 0
		period_size 1024
		buffer_size 4096
		rate 44100
	}
	bindings {
		0 0
		1 1
	}
}

ctl.dmixer {
	type hw
	card PCH
}

(Optional) Controlling Volume using shortcut keys on dwm

Let’s start by adding this to the commands section (Remeber we need to specify our default sound card in our previous case):

/* commands */
static const char *voldwcmd[]  = { "amixer", "-c", "PCH", "set", "Master", "1%-", NULL};
static const char *volupcmd[]  = { "amixer", "-c", "PCH", "set", "Master", "1%+", NULL};
static const char *volmcmd[]  = { "amixer", "-c", "PCH", "set", "Master", "toggle", NULL};


This defines 3 character arrays that will hold our volume controlling commands respectively.

Now, let’s map these commands to the hexadecimal code of your multimedia volume keys. To find out the hexadecimal code of your multimedia volume keys, fire up a terminal in a X session, focus on the shown window, press your shortcut keys and copy the codes. You should get something like this:

xev
KeyRelease event, serial 32, synthetic NO, window 0xe00001,
    root 0xd4, subw 0xe00002, time 4421968, (57,25), root:(1111,260),
    state 0x0, keycode 122 (keysym 0x1008ff11, XF86AudioLowerVolume), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyRelease event, serial 32, synthetic NO, window 0xe00001,
    root 0xd4, subw 0xe00002, time 4422488, (57,25), root:(1111,260),
    state 0x0, keycode 123 (keysym 0x1008ff13, XF86AudioRaiseVolume), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyPress event, serial 32, synthetic NO, window 0xe00001,
    root 0xd4, subw 0xe00002, time 4423054, (57,25), root:(1111,260),
    state 0x0, keycode 121 (keysym 0x1008ff12, XF86AudioMute), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False


Then we’ll add this entry to the modifiers section:

/* modifier                     key         function          argument */
{ 0,                            0x1008ff11,	   spawn,          {.v = voldwcmd } },
{ 0,             	            0x1008ff13,	   spawn,          {.v = volupcmd } },
{ 0,                            0x1008ff12,	   spawn,          {.v = volmcmd } },

4 Comments

5th

16/06/2017


Thank you a lot! It helped to setup ALSA on a freshly installed Void Linux. Just wanted to say thank you.

DOTSLASHLINUX

16/06/2017


@5th, you’re most welcome. Glad you found this article helpful!

Apko

11/07/2017


thank you very much

DOTSLASHLINUX

11/07/2017


@Apko, you’re most welcome! Thanks for passing by!

Leave A Comment