Posts tagged ‘automatic’

ALSA: Switch audio output to USB Headset

I recently purchased a USB headset.
Here’s how to get it working with ALSA, manually and automatically.

Manually:

(From ALSA ArchWiki)

Load module:

# modprobe snd_usb_audio

Verify that module is loaded:

$ lsmod | grep snd_usb_audio
snd_usb_audio          93519  3 
...

Find corresponding card ID:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
...
card 2: P780 [Plantronics GameCom 780], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

Set card ID as default audio device in ~/.asoundrc:

1
2
defaults.ctl.card 2
defaults.pcm.card 2

The manual method works fine if the headset is always plugged in.

Automatically:

It would be better if we could automatically change the default audio device based on whether the USB headset is plugged in or not. This can be achieved by using udev rules.

Add the following to /etc/udev/rules.d/00-local.rules:

# Set USB headset as default sound card when plugged in
KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"

# Restore default sound card when USB headset unplugged
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"

udev automatically detects changes to rules. However, you will need to unplug and replug the headset for changes to be be triggered.

Audio applications need to be restarted whenever default card changes. If anyone knows a way of switching audio devices without restarting applications (using ALSA, not Pulseaudio), please let me know.