Basic ALSA Configuration

alsa
By Firas Khalil Khana on 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 --ask --update --newuse media-sound/alsa-utils


Void Linux:

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):

┌────────────────────────────────────────────────────────────────────────── AlsaMixer v1.1.3 ───────────────────────────────────────────────────────────────────────────┐
│ 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│            │MM│            │OO│                            │MM│                            │MM│                                                          │
│         └──┘            └──┘            └──┘                            └──┘                            └──┘                                                          │
│         100             0<>0          100<>100        100<>100          0<>0            0<>0             33                             0<>53                         │
│   <    Master     >   Headphone        Speaker           PCM             Mic          Mic Boost         Beep       Auto-Mute Mode  Internal Mic Bo Loopback Mixing    │
│                                                                                                                                                                       │
│                                                                                                                                                                       │
│                                                                                                                                                                       │
│                                                                                                                                                                       │
│                                                                                                                                                                       │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

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 plugin 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

Recommended Distros

Upcoming Articles

  • The Linux Kernel Configuration Guide Part 1
  • The Linux Kernel Configuration Guide Part 2
  • The Linux Kernel Configuration Guide Part 3
  • The Linux Kernel Configuration Guide Part 4
  • The Linux Kernel Configuration Guide Part 5
  • The Linux Kernel Configuration Guide Part 6
  • The Linux Kernel Configuration Guide Part 7

Suggestions

Apart from the comments' section, if you wanted to see an article on how to configure a certain application for a GNU/Linux distro, or wanted to share one (perhaps one that you've written), feel free to send me an email on:

firasuke@gmail.com