grimm-nixos-laptop/common/sound/pipewireLowLatency.nix

124 lines
3.4 KiB
Nix
Raw Normal View History

2024-05-06 23:22:32 +02:00
# source: https://github.com/fufexan/nix-gaming/raw/master/modules/pipewireLowLatency.nix
2024-05-07 12:19:14 +02:00
{ config
, pkgs
, lib
, ...
}:
let
2024-05-06 23:22:32 +02:00
inherit (lib.modules) mkIf;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.types) int;
inherit (lib.generators) toLua;
cfg = config.services.pipewire.lowLatency;
qr = "${toString cfg.quantum}/${toString cfg.rate}";
2024-05-07 12:19:14 +02:00
in
{
2024-05-06 23:22:32 +02:00
# low-latency PipeWire configuration
# extends the nixpkgs module
2024-05-07 12:19:14 +02:00
meta.maintainers = with lib.maintainers; [ fufexan ];
2024-05-06 23:22:32 +02:00
options = {
services.pipewire.lowLatency = {
enable = mkEnableOption ''
low latency for PipeWire. This will also set `services.pipewire.enable` and
`services.pipewire.wireplumber.enable` to true.
'';
quantum = mkOption {
description = "Minimum quantum to set";
type = int;
default = 64;
example = 32;
};
rate = mkOption {
description = "Rate to set";
type = int;
default = 48000;
example = 96000;
};
};
};
config = mkIf cfg.enable {
services.pipewire = {
# make sure PipeWire is enabled if the module is imported
# and low latency is enabledd
enable = true;
# write extra config
extraConfig.pipewire = {
"99-lowlatency" = {
context = {
properties.default.clock.min-quantum = cfg.quantum;
modules = [
{
name = "libpipewire-module-rtkit";
2024-05-07 12:19:14 +02:00
flags = [ "ifexists" "nofail" ];
2024-05-06 23:22:32 +02:00
args = {
nice.level = -15;
rt = {
prio = 88;
time.soft = 200000;
time.hard = 200000;
};
};
}
{
name = "libpipewire-module-protocol-pulse";
args = {
2024-05-07 12:19:14 +02:00
server.address = [ "unix:native" ];
2024-05-06 23:22:32 +02:00
pulse.min = {
req = qr;
quantum = qr;
frag = qr;
};
};
}
];
stream.properties = {
node.latency = qr;
resample.quality = 1;
};
};
};
};
# ensure WirePlumber is enabled explicitly (defaults to true while PW is enabled)
# and write extra config to ship low latency rules for alsa
wireplumber = {
enable = true;
2024-05-07 12:19:14 +02:00
configPackages =
let
# generate "matches" section of the rules
matches = toLua
2024-05-06 23:22:32 +02:00
{
2024-05-07 12:19:14 +02:00
multiline = false; # looks better while inline
indent = false;
} [ [ [ "node.name" "matches" "alsa_output.*" ] ] ]; # nested lists are to produce `{{{ }}}` in the output
# generate "apply_properties" section of the rules
apply_properties = toLua { } {
"audio.format" = "S32LE";
"audio.rate" = cfg.rate * 2;
"api.alsa.period-size" = 2;
};
in
[
(pkgs.writeTextDir "share/lowlatency.lua.d/99-alsa-lowlatency.lua" ''
-- Generated by nix-gaming
alsa_monitor.rules = {
{
matches = ${matches};
apply_properties = ${apply_properties};
}
2024-05-06 23:22:32 +02:00
}
2024-05-07 12:19:14 +02:00
'')
];
2024-05-06 23:22:32 +02:00
};
};
};
}