Virtual Audio Cable in Linux Ubuntu

 Posted by:   Posted on:   Updated on:  2017-11-18T21:32:59Z

Virtual audio connections under Linux Ubuntu. Can be done using both Pulseaudio and ALSA servers.

If you ever wanted to pipe an audio stream from an application to another, you probably know that there are some virtual sound card drivers available for Windows. But what about Linux? Well, there are a lot of options, some being more complicated than others.

You probably need to do audio piping to record an audio output or to use SDR tools which only support audio input. You don't have to use a real cable to wire the sound card output into line in. The software approach is better. There is more than one way to do this in Linux. We'll look into Pulseaudio and ALSA, the sound servers used by Ubuntu. Depending on what application you use, you may need to configure the virtual connection on one or the other server.
Virtual Audio Cable in Linux Ubuntu

Pulseaudio

It's very simple to configure. Nothing to install. Give it a try by running in a terminal:
pacmd load-module module-null-sink sink_name=Virtual_Sink sink_properties=device.description=Virtual_Sink
You should see no error after running this command. Do not run it as root. If you open Sound Settings, a new output device should have appeared, named Virtual_Sink.

To test this, open and application that outputs sound and select Virtual_Sink. If you use an application with Pulseaudio support but with no way of selecting the sound card, you can set it to output sound through the Virtual_Sink in the Sound Settings. Simply click on Virtual_Sink. Unfortunately, this sets the virtual sink as default output device for all applications.

Select the virtual sink in Sound Settings - Pulseaudio
Select the virtual sink in Sound Settings
To use this stream as sound input, you must use an application with Pulseaudio support and select the Monitor of Virtual_Sink as input. Pulseaudio creates a monitor device for each output device that can be used to record what's being outputted.

Select Monitor as input source - Pulseaudio
Select Monitor as input source
Unfortunately not all applications support Pulseaudio. Even worse, not all applications that support Pulseaudio provide a sound device selection dialog.

Before trying a different approach, if this worked for you, you should know that this setup is not permanent. All it takes is to restart Pulseaudio and the Virtual_Sink is lost. To make it permanent, open the configuration file (sudo gedit /etc/pulse/default.pa) and add the following line:
load-module module-null-sink sink_name=Virtual_Sink sink_properties=device.description=Virtual_Sink
Save and close. You can restart Pulseaudio. The Virtual Sink is still there.

ALSA

ALSA makes use of a virtual loopback module. It can be loaded with modprobe:
sudo modprobe snd_aloop
The new device has 8 I/O ports (a.k.a. subdevices). You can view them using aplay -l command. Example output:
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
In this example you can see that the virtual devices are hw:1,0 and hw:1,1. From here, things get simple: one application outputs sound to hw:1,0 and the other records that sound from hw:1,1. I guess the other way round works too. It should be noted that the parameters of the sound are controlled by the first application that opens one of the loopback devices.

Although Pulseaudio can connect to ALSA devices, I had no luck on getting sound from loopback input. Yet it worked from the monitor of loopback.

Sound output setting example - ALSA
Sound output setting example
The output of the application above (sent to hw:1,0) could be recorded in Audacity by selecting as input Loopback: PCM (hw:1,1): Line:0. I have no idea how Audacity creates those Line and Mic labels and how do they work exactly for a virtual device.

Neither this setup is permanent. The module snd_aloop must be loaded at boot time by adding it in /etc/modules file.

Links

7 comments :

  1. genial... depuis le temps que je cherchais une solution via ubuntu et mon sdr

    remerciement

    ReplyDelete
  2. Audacity should be using the standard device names which you can view on the terminal using either aplay -L or arecord -L

    ReplyDelete
  3. I'm using your recipe for streaming music from Ubuntu/Linux to Twitter App on an Android x86_64 under Virtualbox. So I can stream music via Twitter Spaces from now on. Thank you!

    ReplyDelete
    Replies
    1. Well, it seems there are unlimited uses of this method :))

      Delete
  4. I am running Ubuntu Budgie and I get No PulseAudio daemon running, or not running as session daemon.
    How can I fix this?

    ReplyDelete

Please read the comments policy before publishing your comment.