PipeWire: Difference between revisions

imported>Fufexan
m Fixed config example
imported>Fufexan
Add low-latency config section
Line 41: Line 41:
   };
   };
};
};
</syntaxHighlight>
==Low-latency setup==
Audio production and rhythm games require lower latency audio than general applications. PipeWire can achieve the required latency with much less CPU usage compared to PulseAudio, with the appropriate configuration.
The minimum period size controls how small a buffer can be. The lower it is, the less latency there is. PipeWire has a value of 32 by default, which amounts to 1.33ms. It can be brought lower if needed:
<syntaxHighlight lang="nix">
services.pipewire = {
  config.pipewire = {
    "context.properties" = {
      "default.clock.min-quantum" = 16 # going lower may cause crackles and distorted audio
    };
  };
};
</syntaxHighlight>
===PulseAudio backend===
Applications using the Pulse backend have a separate configuration. The default minimum value is 1024, so it needs to be tweaked if low-latency audio is desired.
<syntaxHighlight lang="nix">
services.pipewire = {
  config.pipewire-pulse = {
    "context.modules" = [
      {
        name = "libpipewire-module-protocol-pulse";
        args = {
          "pulse.min.quantum" = 16; # controls minimum playback quant
          "pulse.min.req" = 16; # controls minimum recording quant
          "pulse.min.frag" = 16; # controls minimum fragment size
          "server.address" = [ "unix:native" ]; # the default address of the server
        };
      };
    ];
  };
};
</syntaxHighlight>
As a general rule, the values in <code>pipewire-pulse</code> should not be lower than the ones in <code>pipewire</code>.
===Controlling the ALSA devices===
It is possible to configure various aspects of soundcards through <code>/etc/pipewire/media-session.d/alsa-monitor.conf</code>. Since there's no config option for it in the module, the file needs to be manually written:
<syntaxHighlight lang="nix">
environment.etc."pipewire/media-session.d/alsa-monitor.conf".text = ''
  rules = [
    {
      matches = [ { node.name = alsa_output.* } ]
      actions = {
        update-props = {
          audio.format = "S16LE"
          audio.rate = 48000
          api.alsa.period-size = 160 # defaults to 1024, tweak by trial-and-error
          #api.alsa.disable-batch = true # generally, USB soundcards use the batch mode
        }
      }
    }
  ];
'';
</syntaxHighlight>
The <code>matches</code> attribute applies the <code>actions</code> to the devices/properties listed there. It is usually used with soundcard names, like shown in the config above. <code><alsa_device></code> can be one of the outputs of
<syntaxHighlight lang="bash">
$ pw-dump | grep node.name | grep alsa
</syntaxHighlight>
</syntaxHighlight>