Control Default ALSA Device via Environment Variable

Say you want to target audio to one of N (virtual) devices in ALSA for a tree of processes.  That is, you want to say "hey, all you lot, play out channels 2 and 3 of the sound card as though they were a separate card" and then tell another set of processes to do the same with channels 0 and 1.

There are instructions on how to set up the split devices, but how do you make those devices the default ALSA for those processes (without having to reach into each process to make it pass a variable along to its children to tell them what device to run)?  The various pre-defined ALSA_ environment variables are all hardware-level, they don't let you easily use virtual/split devices.

Turns out ~/.asoundrc allows you to read environment variables directly, so you just do something like this:

pcm.!default {
@func getenv
default front

After you create your split devices.  Now you can start your processes and target them to the correct virtual device with:

DISPLAY_ALSA_DEVICE=front my-subprocess

and most processes will simply pass the environment variable down to their child processes.  So far e.g. mplayer picks up the default perfectly well, but Wine seems unhappy with it.  It seems they have become "multi-sound card aware" and as a result are no longer just choosing the alsa default as the output.


Comments are closed.


Pingbacks are closed.