SECRET OF CSS

How To Create Simple Virtual Audio Interfaces In Linux | by Tate Galbraith | Aug, 2022


Audio routing bliss without all the overhead

1*kkFEuN1cK2p39UMWhrrRdA
Photo by Kelly Sikkema on Unsplash

The old clunky days of dealing with broken audio drivers and devices in Linux are behind us. Nowadays, there are entire distributions dedicated to sound design and music production. You can grab most popular off-the-shelf distros and expect many audio interfaces to work just fine out of the gate.

But what happens when you need a bit more flexibility with audio routing and connections?

There are many large, complicated audio frameworks like Jack and PluseAudio for Linux. These have so many configurable options and moving parts it will make you nauseous. If all you want to do is spin up a few extra loopback interfaces, these are a bit overkill. Plus, you’ll likely spend more time setting them up than you will actually creating the interfaces you need.

In this article we’ll explore a simple way to create virtual interfaces using the audio subsystem common to many Linux distributions. The ALSA project provides a straightforward, simple way to manage audio interfaces and basic configuration. Let’s try it out.

Basic commands

If you’re already familiar with using the default ALSA utilities you can skip ahead. If you’re new to ALSA or need a refresher, run through these commands so you can quickly identify and manage connected sound devices.

If you haven’t installed the ALSA utilities package yet, you’ll want to do that first:

sudo apt install alsa-utils

Now let’s take a look at a few basic list commands. These commands show you the attached devices that your operating system can see.

Show all playback devices:

aplay -l

Show all recording devices:

arecord -l

These two commands show the playback and record devices available on your system. This is helpful for identifying the name, hardware ID and number of channels on a device. You can also check here to ensure that any peripheral devices (especially USB interfaces) are correctly connected.

The following command will bring up the mixing panel for ALSA. This allows you to tweak input and output levels and make small configuration changes to each device:

alsamixer
1*Pb2taR52EI7v6dvZGnh1Tw
The alsamixer panel.

Now that we have a few of the basics down, let’s check out how to actually create some loopback interfaces.

Loading the loopback module

In order to create virtual loopback interfaces, we’ll need to load a special module into the operating system for ALSA. This module is called snd-aloop and it allows you to send audio to one side of an interface and have it appear on the output of the other. This is kind of like a virtual audio cable that connects two components.

Using these virtual interfaces you can perform more tricky audio routing. You can route audio to an interface that will capture or manipulate the sound on the other side or simply duplicate it. There are a ton of possibilities when you have access to a relatively unlimited number of virtual interfaces.

Let’s load the module using the following command:

sudo modprobe snd-aloop

Now if you run arecord -l or aplay -l you should see the following additions:

card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
Subdevices: 7/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7

These two interfaces provide an input and an output for you to route and capture audio from. The first device listed (device 0) is the one you’ll route the audio input to. The second device (device 1) is where you can hear it from the first device. Everything you send to the first interface appears on the second.

If you wanted to route audio to the hardware ID of the first interface, you would do something like this:

hw:1,0,0

And if you wanted to reference the second interface to listen to it, you would do this:

hw:1,1,0

You can use both arecord and aplay to test this by playing a WAV file into the first interface and listening to it (recording it) on the second.

Customizing loopback interfaces

If you want to create more than just one default pair of loopback interfaces you can do this easily by creating a special configuration file.

Create the following file /etc/modprobe.d/alsa-loopback.conf and populate it with the following content:

option snd-aloop enable=1,1,1,1 index=0,1,2,3

This will create four separate pairs of loopback interfaces. You can modify the number of enabled interfaces by adding or removing a 1 after enable and adding index values accordingly.

If you enable this configuration, now would be a good idea to ensure the module gets loaded every single time at startup.

Run the following command to ensure the snd-aloop module gets loaded at boot:

echo "snd_aloop" > /etc/modules-load.d/snd-aloop.conf

Now each time you reboot you should have the snd-aloop module loaded with your custom number of interfaces present.

Trying it out

If you want to test out your newly minted virtual interfaces you can load up your favorite audio application and select them as input/output devices. If you’re working on the command-line you can always perform tests using the following commands as well.

If you need test files, you can try some of the free WAV files available at freesound.org.

The following will play a test file into the input loopback:

aplay -D hw:1,0,0 test_input.wav

And the following will record it out of the output loopback:

arecord -D hw:1,1,0 -f S16_LE -c 2 -r 48000 test_output.wav

Hopefully, you’ve now achieved audio routing nirvana without all the headaches of implementing a more complicated audio server framework. If you’re looking for more information on ALSA you can check out the official documentation available here.



News Credit

%d bloggers like this: