Wednesday, January 11, 2006

ALSA Device Sharing

Users often experience "device busy" errors when two or more applications attempt to use the same sound card simultaneously via ALSA. The answer to this problem is to configure your applications to use an ALSA device name that is routed through the dmix plugin. For most systems, the "default" device is routed through dmix to the first sound card in the system. But what if you have a second sound card (such as headphones)? Most systems don't automatically have a device name that routes through dmix for the second card. Here's the solution. Put the following in your ~/.asoundrc file:

pcm.dmix2 {
type dmix
ipc_key 8543
ipc_key_add_uid true
ipc_perm 0666
slave {
pcm "hw:1,0"
# format S16_LE
# rate 16000

pcm.plugdmix2 {
type plug
slave.pcm "dmix2"

The "hw:1,0" tells dmix2 to route to the 1st device on the second card. Substitute what is appropriate for your system. The ipc_key number is completely arbitrary. Make up whatever number you like, so long as it is unique in the ALSA configuration. Now configure your applications to use device "plugdmix2". The plug pcm will automatically convert various formats for you to the format required by the device. If you want to learn more about .asoundrc files, here's a couple of links I can recommend:

Hardware/Software Mixing

PCM Plugins