Swith audio output to another USB device

From Deimos.fr / Bloc Notes Informatique
Jump to: navigation, search
Linux

Software version
Operating System Debian 7
Website
Last Update 23/05/2013
Others

1 Introduction

I've recently bought an Enermax keyboard (Caesar) and have input and output audio directly on the keyboard ! That's great and better when it works. Unfortunately, as I have a minimal OS, it doesn't work out of the box. So here is how I could achieve it.

I will purpose 2 way to make it work and I personally use the manual way.

2 Manual way

First of all, you need to see if you could find your USB Audio device (normally there is no reasons why you shouldn't see it) :

Command lsusb
> lsusb
[...]
Bus 003 Device 004: ID 0d8c:0105 C-Media Electronics, Inc. CM108 Audio Controller
[...]

Then check this module is loaded :

Command lsmod
> lsmod | grep snd_usb_audio
snd_usb_audio          84836  2

If it's not the case, add it to your module list and load it :

Command
echo "snd_usb_audio" >> /etc/modules
modprobe snd_usb_audio

Now find the corresponding card ID (you could also have it in AlsaMixer) :

Command aplay
> aplay -l
[...]
card 1: Device [USB Multimedia Audio Device], périphérique 0: USB Audio [USB Audio]  Subdevices: 0/1
  Subdevice #0: subdevice #0
[...]

My card number is 1 ! Nice, now let's add to our personal configuration (replace 1 at the end of the lines by your card number) :

Configuration File ~/.asoundrc
defaults.ctl.card 1
defaults.pcm.card 1

Now reboot to make it work !

3 Automatic way

For the automatic rules, we're going to play with udev. Simply create a rule for it :

Configuration File /etc/udev/rules.d/75_audio.rules
# Set USB device 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 device 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 will do the work to make it automatically work :-)

4 References

http://archlinux.me/w0ng/2012/07/06/alsa-switch-audio-usb-headset/