User:Joca/Control sound destination

From XPUB & Lens-Based wiki

Playing sounds to specific speakers from 1 computer

Most audioplayers have the possibility to define a specific sound output. Each output points to a specific soundcard, but not necessarily to a specific speaker connected to this card. The jacks are separated physically, but the software just sees a single four-device.

Imagine that you have a sound card with 4 outputs. You want to play a sound one of those speakers. Then you can split this soundcard using ALSA plugins. These plugins extend the functionality of sound devices.

Create an asound.conf file

To use an ALSA plugin, you need to create a .asound.conf file in /etc.

touch /etc/asound.conf will do the job.

asound.conf

This configuration file is not required for ALSA to work properly. Most applications will work without them. These files are used to allow extra functionality, such as routing and sample-rate conversion, through the alsa-lib layer. The actual reason that most applications will work without these user-side custom config files is that usually a default install of alsa-lib provides a sufficiently capable setup consisting of hierarchical config files

Open asound.conf and write plugin

Open etc.asound.conf in a code editor. To split a multichannel soundcard we need to create so called virtual slave devices that represent the 4 speakers in our 4 channel soundcard.

First, find the name of your sound device. List all devices with aplay -L

The output looks like this:

The soundcard I want to split is :

hw:CARD=USB,DEV=0

Scarlett 2i4 USB, USB Audio

Now we go back to the code editor with asound.conf.

First we define the slave

pcm_slave.fourchannels {

   pcm "hw:USB,0" # put the name of the soundcard here using this format
   channels 4 # number of channels on the original soundcard
   rate 48000     # or whatever

}

This defines a slave without any parameters. It's nothing more than another alias for your sound device. Now we are going to define the specific speakers.

pcm.mono1 {

   type plug
   slave.pcm {
       type dshare
       ipc_key 20160316     # any random but unique number
       slave fourchannels # it's master!
       bindings [ 0 ] # connection to specific channel. (starting with 0)
   }

}

Do the same for channel 2 to 4, and change their names and bindings.

pcm.mono2 {

   type plug

   slave.pcm {

       type dshare

       ipc_key 20160316

       slave fourchannels

       bindings [ 1 ]

   }

}

pcm.mono3 {

   type plug

   slave.pcm {

       type dshare

       ipc_key 20160316

       slave fourchannels

       bindings [ 2 ]

   }

}

pcm.mono4 {

   type plug

   slave.pcm {

       type dshare

       ipc_key 20160316

       slave fourchannels

       bindings [ 3 ]

   }

}

Save asound.conf. And now try to play to a specific speaker:

aplay -f cd -D mono1ma test.wav

sources:

https://unix.stackexchange.com/questions/270083/using-aplay-and-choose-which-output-on-card-to-use

https://www.alsa-project.org/wiki/Asoundrc