FAQ026
From the ALSA wiki
How can I change the default ALSA device?
Ok, if you want to change the default device on your soundcard, you need to edit your ~/.asoundrc file and put something like this into it:
pcm.!default {
type hw
card 0
device 2
}
This selects hw:0,2 as the default device. If you want to do sample-rate conversion, etc, using the plug plugin, you can try this:
pcm.!default {
type plug {
slave.pcm "hw,0,2"
}
}
If you need to change the default soundcard you need to edit one of the ALSA configuration files (if they does not exist yet, you can create them, usually with superuser privileges):
/etc/asound.conffor system-wide options that affect all users~/.asoundrcfor options that affect only your own user account
To one of these files (probably /etc/asound.conf) add something similar to the following:
pcm.!default {
type hw
card 2
}
ctl.!default {
type hw
card 2
}
You can find out which number you need to put in the card line by looking at /proc/asound/cards. If this file doesn't exist or the contents are empty, it means ALSA is not working properly on your computer. Let's assume ALSA is working ok. For example:
$ cat /proc/asound/cards
0 [Dummy ]: Dummy - Dummy
Dummy 1
1 [VirMIDI ]: VirMIDI - VirMIDI
Virtual MIDI Card 1
2 [AudioPCI ]: ENS1371 - Ensoniq AudioPCI
Ensoniq AudioPCI ENS1371 at 0xe400, irq 11
In this example we can see that soundcard number 0 "Dummy" is a dummy soundcard (useful for certain types of testing), soundcard number 1 "VirMIDI" is a virtual soundcard (useful for testing MIDI programs), and last but most importantly, soundcard number 2 "AudioPCI" is the real soundcard. Question: How can you tell which soundcard is a real soundcard? Answer: real soundcards always have an irq. In this example, only soundcard 2 AudioPCI has an irq, so it must be the real soundcard.
So the general case is:
pcm.!default {
type hw
card X
device Y
}
Also if you ever want to change your default soundcard, you should change the ctl.!default device to that card:
ctl.!default {
type hw
card X
}
Note by mschiff: IMO its better to use the card name instead of the number because as you can see in FAQ042 the order of the cards can change if you reboot. I set my default soundcard like that:
Find out the name of the card:
$ cat /proc/asound/cards
0 [Live ]: EMU10K1 - SBLive! Value [CT4832]
SBLive! Value [CT4832] (rev.8, serial:0x80271102) at 0xb400, irq 201
1 [Bt878 ]: Bt87x - Brooktree Bt878
Brooktree Bt878 at 0xf9000000, irq 177
2 [V8237 ]: VIA8237 - VIA 8237
VIA 8237 with AD1980 at 0x1000, irq 209
(Last time my computer booted the "Live" card was card "2")
Set it as default in the ALSA configuration file:
$ cat /etc/asound.conf
# use Soundblaster Live as default device
# (from /proc/asound/cards)
#
#
pcm.!default {
type hw
card Live
}
ctl.!default {
type hw
card Live
}
You can also make the default device use any other defined pcm device:
pcm.foo {
type dmix
slave.pcm "hw:0,0"
}
pcm.!default {
type plug
slave.pcm "foo"
}
In this example the slave definition references the "foo" pcm device defined above.
"Real soundcards always have an IRQ" - this is not true for USB soundcards:
$ cat /proc/asound/cards
0 [External ]: USB-Audio - SB Live! 24-bit External
Creative Technology SB Live! 24-bit External at usb-0000:00:10.1-2, full speed
Look at /usr/share/alsa/alsa.conf and find parameters...
# defaults defaults.ctl.card 0 defaults.pcm.card 0
Change it to the card of your choice.
Is it possible to use/set environment variables to change the default ALSA device?
you can use ALSA_PCM_CARD or ALSA_CARD to set the default card, either by name or by number. If both are set, ALSA_PCM_CARD takes precedence. The device on the card can be set in the variable ALSA_PCM_DEVICE.
You can check aplay -L for this information. This is part of the output I see.
@args.CARD {
type string
default {
@func getenv
vars {
0 ALSA_PCM_CARD
1 ALSA_CARD
}
default {
@func refer
name 'defaults.pcm.card'
}
}
}
Now, under default, you see vars and the two variables (ALSA_PCM_CARD and ALSA_CARD) that are queried.