My laptop speakers suck, so if a USB sound card is available, I'd rather use it. For this to work, I need the following to happen:
The first step is to write a couple udev rules to execute a
script. I'm not sure my rules are the best, but here they are:
I stuck them in
DRIVER=="snd-usb-audio", ACTION=="add", RUN+="/home/jesse/bin/select_alsa_default.sh"
SUBSYSTEM=="sound", ACTION=="remove", RUN+="/home/jesse/bin/select_alsa_default.sh"
/etc/udev/rules.d/50-snd-usb.rules
.
Next, I wrote the simple script. My idea was to use symlinks to
choose between two different /etc/asound.conf
files.
Here's the script:
#!/bin/bash
rm /etc/asound.conf
if [ "${ACTION}" == "add" ]
then
ln -s /etc/asound.conf.usb /etc/asound.conf
else
ln -s /etc/asound.conf.onboard /etc/asound.conf
fi
Finally, create the two asound.conf
files, asound.conf.onboard
and asound.conf.usb
.
These two files are exactly the same, except for the following
code at the bottom:
For
pcm.!default {
type plug
slave.pcm "hda-intel"
}
ctl.!default {
type hw
card "0"
}
asound.conf.usb
, we change slave.pcm
to "usb-audio"
and card
to
"1"
.
One last thing: my USB sound card has no volume control, so I want to control the volume using the multimedia keys on the laptop. Normally, no problem: I've already set the default card control, so if I had bound the keys to change, say, PCM, then it would change the PCM of either the USB card or the onboard card, as desired. But my onboard card has two PCMs, so I prefer to change its Master setting, while my USB card has no Master control at all.
So, here's my solution. I set the keys as follows:
This way, the multimedia keys try to control card 1 (the USB card).
If that fails (say, if the USB card is not present), then they
will instead control card 0.
XF86AudioLowerVolume amixer -c 1 set PCM 5%- || amixer
-c 0 set Master 5%- XF86AudioRaiseVolume amixer -c 1 set PCM 5%+ || amixer
-c 0 set Master 5%+ XF86AudioMute amixer -c 1 set PCM toggle || amixer
-c 0 set Master toggle
Because I use enlightenment, I used e16keyedit
to make
those changes. For other window managers, you'll have to use the
appropriate tool.
Viola.