Difference between revisions of "Using JACK and PipeWire"

From Looking Glass
Jump to navigation Jump to search
Line 1: Line 1:
 
QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.
 
QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.
  
In order for PipeWire to provide drop-in JACK support, you should (depending on your distro):
+
Normally, you can run applications with PipeWire's JACK support by running it with <code>pw-jack</code>, but this is not possible when using libvirt. Instead, you need PipeWire to provide drop-in JACK support. To do this:
 
* on Debian: run <code>sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig</code>
 
* on Debian: run <code>sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig</code>
 
* on Arch: install the [https://aur.archlinux.org/packages/pipewire-jack-dropin/ pipewire-jack-dropin] AUR package.
 
* on Arch: install the [https://aur.archlinux.org/packages/pipewire-jack-dropin/ pipewire-jack-dropin] AUR package.
Line 8: Line 8:
  
 
== Run VMs under your user ==
 
== Run VMs under your user ==
 +
 +
We will assuming your username is <code>example</code>, your user ID is 1000, and your virtual machine is called <code>win10</code>.
 +
 +
You can configure libvirt to run QEMU virtual machines under your user by adding the following line to <code>/etc/libvirt/qemu.conf</code>:
 +
 +
user = "example"
 +
 +
Then, you can run <code>virsh edit win10</code>, and perform the following changes:
 +
 +
# If the <code><domain></code> line doesn't already contain <code>xmlns:qemu</code>, add <code>xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'</code>. The line should look like:
 +
#: <syntaxhighlight lang="xml"><domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'></syntaxhighlight>
 +
# Add the following block to the <code><devices></code> section:
 +
#: <syntaxhighlight lang="xml">
 +
  <audio id="1" type="jack">
 +
    <input clientName="win10" connectPorts="input-name"/>
 +
    <output clientName="win10" connectPorts="output-name"/>
 +
  </audio>
 +
</syntaxhighlight>
 +
#: <code>input-name</code> and <code>output-name</code> should be replaced with a regular expression that matches the name of the JACK input and output ports. You can get the names by looking at your patchbay, or run <code>jack_lsp</code>.
 +
# Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before <code></domain></code>:
 +
#: <syntaxhighlight lang="xml">
 +
  <qemu:commandline>
 +
    <qemu:env name="PIPEWIRE_RUNTIME_DIR" value="/run/user/1000"/>
 +
    <qemu:env name="PIPEWIRE_LATENCY" value="512/48000"/>
 +
  </qemu:commandline>
 +
</syntaxhighlight>
 +
#: If you already have a <code><qemu:commandline></code> section, merge this block with it. Remember to change <code>1000</code> to your actual user ID. You can tune the latency and sampling rate as you like.
 +
 +
If you are using a distro with <code>apparmor</code>, you will need to configure new rules...
 +
 +
Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.
  
 
== Allow QEMU user access to your PipeWire socket ==
 
== Allow QEMU user access to your PipeWire socket ==

Revision as of 10:13, 1 September 2021

QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.

Normally, you can run applications with PipeWire's JACK support by running it with pw-jack, but this is not possible when using libvirt. Instead, you need PipeWire to provide drop-in JACK support. To do this:

  • on Debian: run sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig
  • on Arch: install the pipewire-jack-dropin AUR package.

Once this is done, you have two options. You could run your virtual machines under your user, or you could set it up so that the QEMU user has access to your PipeWire socket. The former is easier, while the latter is more secure.

Run VMs under your user

We will assuming your username is example, your user ID is 1000, and your virtual machine is called win10.

You can configure libvirt to run QEMU virtual machines under your user by adding the following line to /etc/libvirt/qemu.conf:

user = "example"

Then, you can run virsh edit win10, and perform the following changes:

  1. If the <domain> line doesn't already contain xmlns:qemu, add xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'. The line should look like:
    <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  2. Add the following block to the <devices> section:
      <audio id="1" type="jack">
        <input clientName="win10" connectPorts="input-name"/>
        <output clientName="win10" connectPorts="output-name"/>
      </audio>
    input-name and output-name should be replaced with a regular expression that matches the name of the JACK input and output ports. You can get the names by looking at your patchbay, or run jack_lsp.
  3. Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before </domain>:
      <qemu:commandline>
        <qemu:env name="PIPEWIRE_RUNTIME_DIR" value="/run/user/1000"/>
        <qemu:env name="PIPEWIRE_LATENCY" value="512/48000"/>
      </qemu:commandline>
    If you already have a <qemu:commandline> section, merge this block with it. Remember to change 1000 to your actual user ID. You can tune the latency and sampling rate as you like.

If you are using a distro with apparmor, you will need to configure new rules...

Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.

Allow QEMU user access to your PipeWire socket