FAQ050

From the ALSA wiki

Jump to: navigation, search

How can I make my USB device the default device upon plugin?

The short answer is to use either hotplug or udev (not sure about udev though).

The procedure outlined here does not replace FAQ040 and in fact falls short in several ways. So far I only got adding events and no removal events, hence change only works one way. Secondly, you probably will need to restart all sound applications so that they open the new default device.

A brief overview of what needs to be done:

  • Write several asound.conf files, one for each use case
  • Write a script to switch between them.
  • Instruct hotplug to execute a script for the usb device
  • Get the script to choose the appropriate sound card

Ok, here we go. I have an Intel and a USB Extigy sound card. I created two different versions of /etc/asound.cond and placed them in /etc/alsa/ as asound.intel and asound.extigy respectively:

/etc/alsa/asound.intel:

   # CARD DEFINITIONS
   pcm.intel       { type hw; card I82801DBICH4 }
   ctl.intel       { type hw; card I82801DBICH4 }
   
   pcm.intelModem  { type hw; card Modem }
   ctl.intelModem  { type hw; card Modem }
   
   pcm.extigy      { type hw; card Extigy; }
   ctl.extigy      { type hw; card Extigy; }
   
   pcm.!default pcm.intel
   ctl.!default ctl.intel

/etc/alsa/asound.extigy:

   # CARD DEFINITIONS
   pcm.intel       { type hw; card I82801DBICH4 }
   ctl.intel       { type hw; card I82801DBICH4 }
   
   pcm.intelModem  { type hw; card Modem }
   ctl.intelModem  { type hw; card Modem }
   
   pcm.extigy      { type hw; card Extigy; }
   ctl.extigy      { type hw; card Extigy; }
   
   pcm.!default pcm.extigy
   ctl.!default ctl.extigy

I wrote a small script that creates a link from /etc/asound.conf to /etc/alsa/asound.NAME

/usr/local/sbin/alsaswitch:

   #!/bin/bash
   
   usage(){
           echo usage: `basename $0` `ls /etc/alsa/ -1 | sed -e 's:/etc/alsa/::g' -e 's/asound.//g'`
   }
   
   if [ -n $1 ] && [ -e /etc/alsa/asound.$1 ];
   then
           ln -sf /etc/alsa/asound.$1 /etc/asound.conf
   else
           usage
   fi

If you don't want things to happen automatically, stop here. Whenever you have connected your device, just login as root and do alsaswitch extigy or whatever extension you gave to the file.

The next step is to get hotplug to do the job. Firstly, you need to create a usermap file to tell hotplug which script to execute for your device. This map resides in /etc/hotplug/usb/.

First, figure out your vendor and product ID. Either use lsusb, or look them up on the web. now if we just went ahead and made a usermap with only vendor and product id, we will get multiple calls of the script, one for each interface. I restricted my script to apply only to the control interface (bInterfaceSubClass 1) YMMV.

/etc/hotplug/usb/extigy.usermap:

   # usb module         match_flags idVendor idProduct bcdDevice_lo bcdDevice_hi bDeviceClass bDeviceSubClass bDeviceProtocol bInterfaceClass bInterfaceSubClass bInterfaceProtocol driver_info
   extigy  0x0103  0x041e 0x3000 0x0 0x0 0x0 0x0 0x0 0x0 0x001 0x0 0x0 0x0

Finally, we need the script itself I leave the commented out section as comments for what I tried. REMOVER is a variable that is set by hotplug, which supposingly points to a script that is executed when the device is removed. This did not work for me.

/etc/hotplug/usb/extigy:

   #!/bin/bash
   #for debugging
   #env >> /tmp/test.txt
   
   #determine which configuration is active, note that removal does not seem to work
   #echo -e "/usr/local/sbin/alsaswitch `ls -lFA /etc/asound.conf | sed 's:.*/etc/alsa/asound.\([^ ]*\)$:\1:'`" > $REMOVER
   #chmod a+x $REMOVER
   /usr/local/sbin/alsaswitch extigy;

Now all you need is to plug in your device. If you have coldplugging installed, this also works at boot time.

Personal tools