How to make Alsa pick a preferred sound device automatically?

197,855

Solution 1

Find your card with

$ cat /proc/asound/cards

To get valid ALSA card names, use aplay:

$ aplay -l

and then create /etc/asound.conf with following:

pcm.!default {
    type hw
    card 1
}

ctl.!default {
    type hw           
    card 1
}

Replace "card 1" with the number or name of your card determined above.

Alternatively, you can change ordering of your cards so your USB card will be card 0 and it will work without editing asound.conf.

Solution 2

this is the method for selecting default sound card in Alsa. You may want to install Alsa for this method to work if you are using Pulse Audio.

cat /proc/asound/modules

will list your sound modules .The output of the command will be like this (eg):

0 snd_hda_intel

1 snd_usb_intel

you can pretty easily understand which one is your usb sound card from above.

nano /etc/modprobe.d/alsa-base.conf

edit this alsa-base.conf in such a way that your preferred card has an index =-2 / 0 and the other card has index =-1 / 1 (stick with -2 and -1 )

options snd_hda_intel index=-1

options snd_usb_intel index=-2

in this case usb device is preferred device.

if you are having two differnt cards with same name from the output, like this:

options snd_hda_intel

options snd_hda_intel

issue this command to find out which is which:

cat /proc/asound/cards

then edit modules in this way:

options snd_hda_intel enable=1 index=0

options snd_hda_intel enable=0 index=1

a reboot may be necessary.so you might have to manually switch over your sound cards.

Solution 3

The answer from Matija Nalis only half worked for me (alsamixer changed default, but other things like aplay and firefox stubbornly stuck with the wrong default). This example from the debian wiki worked for me (on CentOS-6 laptop):

defaults.pcm.!card Generic_1
defaults.ctl.!card Generic_1
defaults.pcm.!device 0
defaults.ctl.!device 0

For the record: ALSA is poorly documented, and especially this very simple stuff like selecting which card to use is way too hard. Worse, it seems to change between systems. Also, I got that "Generic_1" tag from aplay -l where it comes up as "card 1" in the list. Other people seemed to be using it, so I did the same... what it means I cannot say.

Solution 4

Here is a variant of Matija Nalis and Tel's answers. This is what worked for me:

~/.asoundrc

defaults.pcm.!card 1
defaults.ctl.!card 1

(Odroid C0, Debian Jessie, HDMI audio as card 0 and USB Sound Card as card 1 according to aplay -l)

Note: other methods did not work for me, as /proc/asound/modules does not exist and there is no hdmi audio module to blacklist or put to low priority, it is embedded into the kernel. It seems I would have had to recompile the kernel without hdmi audio support to disable it completely.

Solution 5

While you can change the default alsa card by editing .asoundrc or the system asound.conf there are a couple of significant issues with this approach.

It's fragile, and requires application restarts to be honored.

If you want to switch cards on the fly then you really need to use a soundserver that abstracts the applications from the soundcard correctly such as pulseaudio.

Share:
197,855

Related videos on Youtube

Petr
Author by

Petr

Updated on September 18, 2022

Comments

  • Petr
    Petr almost 2 years

    I bought an USB sound card. I'd like to set up my Linux desktop so that it prefers the USB device, if it is plugged in and automatically switches as the device is (un)plugged. Is it possible, and how?

  • Petr
    Petr almost 11 years
    What do you mean by "modifying the output"? How can I modify the output?
  • Petr
    Petr almost 11 years
    I tried your suggestion, but when the USB card is unplugged, audio stops working completely, instead of switching to the built-in card.
  • Ashildr
    Ashildr almost 11 years
    i have made necessary changes in the answer.try this version.hope this helps.the methods may vary according to the flavour of linux you use.
  • Petr
    Petr almost 11 years
    Thank you. How can I verify the setup? I identified the cards, added the appropriate options, rebooted, but I don't see any change - still the internal (intel) card is the preferred one.
  • Ashildr
    Ashildr almost 11 years
    try >options snd-usb-intel enable=1 index=0 (to enable) >options snd-hda-intel enable=0 index=1 (to disable) or >options snd-hda-intel enable=0 index=0 >options snd-usb-intel enable=1 index=1 and reboot
  • Ashildr
    Ashildr almost 11 years
    i.e, enable usb disable the other one using enable =0 or 1,do this vice versa also.i'm not sure which will work for you.reboot is necessary.i wish u solve yor problem
  • Petr
    Petr almost 11 years
    I'm confused - why would I disable one of the sound cards? Then, if I unplug the USB one, I'd have to enable the internal one manually by changing the configuration file again?
  • Ashildr
    Ashildr almost 11 years
    the above method is to choose a particular order for multiple installed cards.And this indeed depends upon the flavour and version of linux being used in the system. for eg:some of the older linux systems use the "index" method we described above.where as newer linux systems tend to use another more recent method involving "slots".since you said there wasn't visible changes after changing the index ,i wanted to test wheather the "index" method worked at all.you can gain more insights into configuring advanced controls in alsa via this link.hence i asked to disable one device.
  • Ashildr
    Ashildr almost 11 years
  • pilona
    pilona almost 11 years
    This requires one to be superuser, which most people will have. Where should one look though for something that can be done as a regular user?
  • Rob Gibbons
    Rob Gibbons over 8 years
    Using a RPI2, this solution worked for me. Tried all the rest, but nothing worked until I did this one. (Had to use "card 0" on mine).
  • linuxdev2013
    linuxdev2013 almost 8 years
    sudo modprobe $soundcard_driver would do what you desire for /proc/asound/modules
  • astooooooo
    astooooooo almost 8 years
    @linuxdev2013 how could you modprobe a driver that is not modprobe-able? As explained above, it is not a module, it is embedded in the kernel.
  • abchk1234
    abchk1234 about 7 years
    Using the above configuration, sound in youtube on palemoon did not work for me, this config worked: defaults.pcm.card 1 defaults.ctl.card 1 forum.palemoon.org/viewtopic.php?t=9661#p66016
  • alamin
    alamin about 7 years
    stucked for this one day! and you just saved my ass!
  • Max N
    Max N almost 7 years
    Some applications like Skype beta ignore that though and connect to the default ALSA card. That results in no audio for Skype when HDMI is the first device instead of HDA.
  • Szczepan Hołyszewski
    Szczepan Hołyszewski over 5 years
    DON'T use esoteric batch processing hackery in answers to normal people. Instead of writing a one-liner that extracts columns from output, describe in words how to find the desired information in the full output.
  • starbeamrainbowlabs
    starbeamrainbowlabs almost 5 years
    @SzczepanHołyszewski Absolutely. While offering it as an option is great, the output format has changed for me - and now it's just outputting Device.
  • Paul Slocum
    Paul Slocum over 4 years
    Doesn't work for me on the Raspberry Pi. It's not clear what the device "name" is from the aplay output, but I've tried everything the name could be and it always tells me the asound.conf is corrupt unless I just put "card 0" or "card 1"
  • ironchicken
    ironchicken over 4 years
    This worked for me too. Although I just used .!card 1 rather than Generic_1.
  • Vitaly Zdanevich
    Vitaly Zdanevich over 2 years
    /proc/asound/modules is not exists.