Crackling pipewire

I have had an issue with pipewire for a year or so. It works perfectly fine on my laptop and most of the time it works on my tower but sometimes the sound crackles , e.g. I can listen for one second of fine music then it stops, emits some garbage noise and resumes fine. Sometimes I would nixos-rebuild and it fixes the isse (or so it seems). Crackling looks random. Speaker is my screen via HDMI.
I tried to ask for help on pipewire some time ago but the advice there proved unhelpful (something about my quantum rates, it’s in my config later), just learned about pw-top, which shows errors:

$ journalctl -b0 --user  -u pipewire

Sep 18 00:09:43 jedha pipewire[138612]: Cannot connect to server socket err = No such file or directory
Sep 18 00:09:43 jedha pipewire[138612]: Cannot connect to server request channel
Sep 18 00:09:43 jedha pipewire[138612]: jack server is not running or cannot be started
Sep 18 00:09:43 jedha pipewire[138612]: JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Sep 18 00:09:43 jedha pipewire[138612]: JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Sep 18 00:09:43 jedha pipewire[138612]: jack-device 0x564020f5c558: can't open client: Connection refused

I dont know why errors mention jack as it is disabled in my nixos config:

{
    enable = true;

  # Enable the (deprecated) media-session session manager instead of wireplumber
  media-session.enable = true;
  wireplumber.enable = false;

  # Disable everything that causes pipewire to interact with alsa devices
  alsa.enable = false;
  pulse.enable = true;
  jack.enable = false;
    config.pipewire = {
      "properties" = {
		default.clock.allowed-rates = [ 44100 48000 96000 ];
		"log.level" = 4;
		"default.clock.quantum"       = 256;
		"default.clock.min-quantum"   = 256;
		"default.clock.max-quantum"   = 256;
      };
    };
  }

The correct way to restart pipewire seems to be systemctl --user restart pipewire{,-pulse}.socket.
Then I’ve tried different things about systemd but I’ve noticed weird issues, possibly due to nixos. like systemctl status --user pipewire-pulse.service showing success while the journalctl output showed errors:

[W][02108.868351] mod.protocol-pulse | [        server.c:  570 start_unix_server()] server 0x55c040244080: socket '/run/user/1000/pulse/native' is in use
[W][02108.868439] mod.protocol-pulse | [        server.c:  996 servers_create_and_start()] pulse-server 0x55c040243cb0: failed to start server on 'unix:/run/user/1000/pulse/native': Address already in use
[E][02108.868455] mod.protocol-pulse | [  pulse-server.c: 5594 pw_protocol_pulse_new()] 0x55c040243cb0: no servers could be started: Address already in use
[E][02108.868525] pw.conf      | [          conf.c:  560 load_module()] 0x55c0401fe740: could not load mandatory module "libpipewire-module-protocol-pulse": Address already in use
[E][02108.868638] default      | [      pipewire.c:  125 main()] failed to create context: Address already in use

I get erros just launching basic commands too:

$ pipewire-media-session 
[W][01401.660839] ms.core      | [ media-session.c: 2211 core_error()] error id:0 seq:1295 res:-32 (Broken pipe): connection error
dbus[138573]: Attempted to unregister path (path[0] = MediaEndpoint path[1] = A2DPSink) which isn't registered
dbus[138573]: Attempted to unregister path (path[0] = MediaEndpoint path[1] = A2DPSink) which isn't registered
$ pipewire-media-session pulse-bridge
[E][01434.722045] pw.conf      | [          conf.c:  386 pw_conf_save_state()] can't rename temp file 'restore-stream.tmp': No such file or directory
[E][01434.722170] ms.mod.restore-stream | [restore-stream.c:  116 remove_idle_timeout()] can't save restore-stream state: No such file or directory
^Cdbus[138668]: Attempted to unregister path (path[0] = MediaEndpoint path[1] = A2DPSink) which isn't registered

I confess I know nothing about audio on linux, it always seemed magical to me (10 years ago, removing pulseaudio fixed my bugs, then pulseaudio started working well and we are moving to pipewire now). Pipewire solved a few pulseaudio issues I had but this peculiar issue is damning: sometimes it works, sometimes sound crackles. Any hint/help appreciated. I ask here since nixos seems to create several files in /etc/pipewire (maybe that’s upstream defaults) but I thought I would ask here first.

Do you have pulseaudio also running?

I’m new, and this is 2 years old, I understand.
But I also have issues with crackling audio, exactly as described here, on both the stable 23.11 and unstable 24.05. Switching to PulseAudio doesn’t seem to help. I’ve found no one else with this issue, does anyone have any idea on what might be going on?

Something to do with sound defaults, with pipewire from what I’ve gathered you just need to play with these values keep them as small as possible but gradually rise them until no audio issues.:

default.clock.allowed-rates = [ 44100 48000 96000 ]
default.clock.quantum = 32
default.clock.min-quantum = 32
default.clock.max-quantum = 1024

I haven’t had much luck and the audio settings options keep changing per release.

https://nixos.wiki/wiki/PipeWire
https://wiki.nixos.org/wiki/PipeWire

Thank you.

I have managed to solve my crackling, but the solution seems to be different from system to system, and it can also affect multiple linux distros, not just NixOS.
For those who may also have this issue, here are some things that might help (along with the response from Pheoxy):

  • Using the latest linux kernel in your configuration.nix.
  • BIOS Upgrade.
  • In the strange event where you use Realtek audio and have your PC connected via ethernet, disabling wifi/the wireless card can fix it. This can be done through desktop settings.