From a2cb35148c40c39fce067dbb16c8694ee6afc557 Mon Sep 17 00:00:00 2001 From: Katherina Walshe-Grey Date: Thu, 12 Sep 2024 11:38:17 +0100 Subject: [PATCH] pipewire-low-latency: split to reusable module --- hosts/kilgharrah/default.nix | 2 ++ services/default.nix | 1 + services/pipewire-low-latency.nix | 58 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 services/pipewire-low-latency.nix diff --git a/hosts/kilgharrah/default.nix b/hosts/kilgharrah/default.nix index 6372b16..4418b4a 100644 --- a/hosts/kilgharrah/default.nix +++ b/hosts/kilgharrah/default.nix @@ -24,6 +24,8 @@ console.keyMap = "uk"; services.xserver.xkb.layout = "gb"; + qenya.services.pipewire.lowLatency.enable = true; + age.secrets.user-password-kilgharrah-qenya.file = ../../secrets/user-password-kilgharrah-qenya.age; users.users.qenya.hashedPasswordFile = config.age.secrets.user-password-kilgharrah-qenya.path; users.users.qenya.extraGroups = [ "wheel" ]; diff --git a/services/default.nix b/services/default.nix index beecd73..fddd93c 100644 --- a/services/default.nix +++ b/services/default.nix @@ -1,5 +1,6 @@ { imports = [ ./forgejo.nix + ./pipewire-low-latency.nix ]; } \ No newline at end of file diff --git a/services/pipewire-low-latency.nix b/services/pipewire-low-latency.nix new file mode 100644 index 0000000..0ba2709 --- /dev/null +++ b/services/pipewire-low-latency.nix @@ -0,0 +1,58 @@ +{ config, lib, pkgs, ... }: + +let + inherit (lib) mkIf mkEnableOption; + cfg = config.qenya.services.pipewire.lowLatency; +in +{ + options.qenya.services.pipewire.lowLatency = { + enable = mkEnableOption "config to decrease sound latency (increasing CPU load) for e.g. streaming"; + # TODO: might be an idea to have the numbers be configurable + }; + + config = mkIf cfg.enable { + # TODO: needs more testing + services.pipewire.extraConfig = { + pipewire."92-low-latency" = { + context.properties = { + default.clock.rate = 48000; + default.clock.quantum = 32; + default.clock.min-quantum = 32; + default.clock.max-quantum = 32; + }; + }; + pipewire-pulse."92-low-latency" = { + context.modules = [ + { + name = "libpipewire-module-protocol-pulse"; + args = { + pulse.min.req = "32/48000"; + pulse.default.req = "32/48000"; + pulse.max.req = "32/48000"; + pulse.min.quantum = "32/48000"; + pulse.max.quantum = "32/48000"; + }; + } + ]; + stream.properties = { + node.latency = "32/48000"; + resample.quality = 1; + }; + }; + }; + # Available from NixOS 24.11. Lifted from https://nixos.wiki/wiki/PipeWire - probably need to adjust numbers + # services.pipewire.wireplumber.extraLuaConfig.main."99-alsa-lowlatency" = '' + # alsa_monitor.rules = { + # { + # matches = {{{ "node.name", "matches", "alsa_output.*" }}}; + # apply_properties = { + # ["audio.format"] = "S32LE", + # ["audio.rate"] = "96000", -- for USB soundcards it should be twice your desired rate + # ["api.alsa.period-size"] = 2, -- defaults to 1024, tweak by trial-and-error + # -- ["api.alsa.disable-batch"] = true, -- generally, USB soundcards use the batch mode + # }, + # }, + # } + # ''; + }; +}