Alsa USB-Audio hotplug

2

My new USB audio speaker appeared as the second device in Alsa. I managed to make it the primary one:

$ cat /proc/asound/modules 
 0 snd_usb_audio
 1 snd_hda_intel
$ cat /proc/asound/cards
 0 [Speaker        ]: USB-Audio - uBoom Q Speaker
                      uBoom Q Speaker at usb-0000:00:1d.1-1, full speed
 1 [Intel          ]: HDA-Intel - HDA Intel
                      HDA Intel at 0x80000000 irq 16

by adding these lines to /etc/modprobe.d/alsa-base.conf:

options snd_hda_intel index=1
options snd_usb_audio index=0

Now, when I boot USB-Audio works by default everywhere. The problem is — when I disconnect it (or boot without it), I see

$ cat /proc/asound/modules 
 1 snd_hda_intel

and nothing works because there's no 0-th device:

$ aplay
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'

The thing I want is a way to re-assign device indexes. If the solution is a script — I'm going to create a udev rule with it.

Currently, I change 'index=?' back to the default '0,1', invoke alsa force-reload and it helps. Any better way?

Note: there's no asoundconf in my distro (KUbuntu Lucid) :(

linux
alsa
hotplug
usb-audio
asked on Super User Aug 5, 2010 by kolypto • edited Jan 7, 2016 by G-Man Says 'Reinstate Monica'

4 Answers

2

try installling asoundconf-gtk but take care of https://bugs.launchpad.net/ubuntu/+source/asoundconf-gtk/+bug/575058

answered on Super User Aug 29, 2010 by user47877
1

First, let's deal with Alsa devices' indexes. Plug in your USB Speaker and invoke sudo alsa force-reload so Alsa finds it.

List cards indexes and their drivers:

$ cat /proc/asound/modules 
 0 snd_hda_intel
 1 snd_usb_audio

Show more detailed info:

$ cat /proc/asound/cards
 0 [Intel          ]: HDA-Intel - HDA Intel
                      HDA Intel at 0x80000000 irq 16
 1 [Speaker        ]: USB-Audio - uBoom Q Speaker
                      uBoom Q Speaker at usb-0000:00:1d.0-1, full speed

We want to have 'Speaker' with index=0.

Add these lines to /etc/modprobe.d/alsa-base.conf to make USB Speaker the default device, if present:

options snd_hda_intel index=-2
options snd_usb_audio index=-1

This makes USB-Speaker more preferable than the Builtin-card. Issue sudo alsa force-reload, voila! Now you can see with cat /proc/asound/cards how cards appear & disappear when you plug in/off your USB-Speaker.


Here's another receipt how to choose which of the 2 available cards will the next launched application use.

Add these lines to /etc/asound.conf:

$ cat /etc/asound.conf.switch 
# CARD DEFINITIONS
#=== CARD 'card0'
pcm.card0-hw { type hw ; card 0 ; }
ctl.card0-hw { type hw ; card 0 ; }

pcm.card0 { type dmix ;
    ipc_key 4109 ; ipc_perm 0660 ;
    slave { pcm "hw:0,0"
        channels 2 ; period_size 1024 ; buffer_size 4096 ; rate 44100 ; period_time 0 ;
        }
    bindings { 0 0 ; 1 1 ; }
    }

#=== CARD 'card1'
pcm.card1-hw { type hw ; card 1 ; }
ctl.card1-hw { type hw ; card 1 ; }

pcm.card1 { type dmix ;
    ipc_key 1949 ; ipc_perm 0660 ;
    slave { pcm "hw:1,0"
        channels 2 ; period_size 1024 ; buffer_size 4096 ; rate 44100 ; period_time 0 ;
        }
    bindings { 0 0 ; 1 1 ; }
    }

This will create two 'dmix' playback devices: 'card0' & 'card1'. They will allow you to switch between them without problems.

Now, put this in ~/.asoundrc:

# Define new virtual devices
pcm.this                { type plug ; slave.pcm "card1" ; }
ctl.this                ctl.card1-hw
# Set default device
pcm.!default    pcm.this
ctl.!default    ctl.this

to switch to the second card. Replace all '1's with '0's to switch back. A small script will be handy :)

NOTE: In this manual we created several new Alsa devices: 'this', 'card0-hw', 'card0', 'card1-hw', 'card1'. You can refer to them:

$ amixer -D 'this' scontrols
Simple mixer control 'PCM',0

P.S. I'm not in common with how Alsa really works and may have made terrible mistakes :) Please correct me if there's a better way. But still, this works :)

answered on Super User Aug 31, 2010 by kolypto • edited Aug 31, 2010 by kolypto
0

I tried the simple edit of alsa-base.conf setting the onboard to -2 and the usb to -1 but this didn't work.

however, when I set BOTH to -1 (with the onboard listed first), then I could switch back and forth.

I do have to perform an alsa force-reload at each change, though.

answered on Super User Oct 13, 2013 by Steve Rinsler
0

I'm trying to resolve the same problem: automatically rerouting audio stream when an USB sound card is plugged into the system, so I decided to write a new rule for udev:

/etc/udev/rules.d/50-alsa.rules:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+="/usr/local/bin/alsa-switch.sh"

of course, you must replace the '*'s with numbers according to your device (use lsusb to know which ones)

The alsa-switch.sh script is very simple :

#!/bin/sh
ln -sf /path/to/custom/asoundrc /home/<user>/.asoundrc

Another rule may be added to revert the configuration when the device is removed (sorry, I'm still working on it)

answered on Super User Jan 6, 2016 by Daniele • edited Jan 7, 2016 by Daniele

User contributions licensed under CC BY-SA 3.0