{ config, pkgs, ... }:

{
  security.rtkit.enable = true;

  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    jack.enable = true;
    wireplumber.enable = true;
  };

  services.pipewire.extraConfig.pipewire."92-low-latency" = {
    "context.properties" = {
      "default.clock.rate" = 48000;
      "default.clock.quantum" = 1024;
      "default.clock.min-quantum" = 512;
      "default.clock.max-quantum" = 2048;
    };
  };

  services.pipewire.wireplumber.extraConfig = {
    "monitor.alsa.rules" = [
      {
        matches = [ { "node.name" = "~alsa_output.*"; } ];
        actions = {
          update-props = {
            "session.suspend-timeout-seconds" = 0;
            "dither.method" = "rectangular";
          };
        };
      }
    ];
  };

  services.pipewire.extraConfig.pipewire."99-deepfilter-source" = {
    "context.modules" = [
      {
        name = "libpipewire-module-filter-chain";
        args = {
          "node.description" = "DeepFilter Noise Canceling Source";
          "media.name" = "DeepFilter Noise Canceling Source";
          "filter.graph" = {
            nodes = [
              {
                type = "ladspa";
                name = "deepfilter";
                plugin = "${pkgs.deepfilternet}/lib/ladspa/libdeep_filter_ladspa.so";
                label = "deep_filter_mono";
                control = { "Attenuation Limit (dB)" = 100; };
              }
            ];
          };
          "audio.rate" = 48000;
          "audio.position" = [ "MONO" ];
          "capture.props" = {
            "node.name" = "effect_input.deepfilter";
            "node.passive" = true;
          };
          "playback.props" = {
            "node.name" = "effect_output.deepfilter";
            "media.class" = "Audio/Source";
          };
        };
      }
    ];
  };
}
