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