How to solve high audio latency?

My audio latency actually is 150ms in pulseeffects (using pulseeffects-legacy) I tried to fix that using a method in Archwiki, changing the values of default-fragments and default-fragment-size in daemon.conf

/etc/pulse/daemon.conf

default-fragments = 5
default-fragment-size-msec = 2

but it doesn’t work:

$ pacmd list-sinks | grep 'latency: [1-9]' 
	current latency: 91,98 ms
	fixed latency: 100,14 ms
	fixed latency: 100,14 ms

here’s my inxi -Fxz

$ inxi -Fxz
System:
  Kernel: 5.9.16-1-MANJARO x86_64 bits: 64 compiler: gcc v: 10.2.1 
  Desktop: GNOME 3.38.3 Distro: Manjaro Linux 
Machine:
  Type: Desktop Mobo: ASUSTeK model: PRIME B450M-GAMING/BR v: Rev X.0x 
  serial: <filter> UEFI: American Megatrends v: 2006 date: 11/13/2019 
CPU:
  Info: 6-Core model: AMD Ryzen 5 2600 bits: 64 type: MT MCP arch: Zen+ 
  rev: 2 L2 cache: 3 MiB 
  flags: avx avx2 lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm 
  bogomips: 81469 
  Speed: 1454 MHz min/max: 1550/3400 MHz boost: enabled Core speeds (MHz): 
  1: 1454 2: 3122 3: 1547 4: 1547 5: 1909 6: 1547 7: 1546 8: 1547 9: 1304 
  10: 3201 11: 1916 12: 1546 
Graphics:
  Device-1: AMD Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] 
  vendor: XFX Pine driver: amdgpu v: kernel bus ID: 08:00.0 
  Display: x11 server: X.org 1.20.10 driver: loaded: amdgpu,ati 
  unloaded: modesetting resolution: <missing: xdpyinfo> 
  OpenGL: renderer: Radeon RX 570 Series (POLARIS10 DRM 3.39.0 
  5.9.16-1-MANJARO LLVM 11.1.0) 
  v: 4.6 Mesa 20.3.4 direct render: Yes 
Audio:
  Device-1: AMD Ellesmere HDMI Audio [Radeon RX 470/480 / 570/580/590] 
  vendor: XFX Pine driver: snd_hda_intel v: kernel bus ID: 08:00.1 
  Device-2: AMD Family 17h HD Audio vendor: ASUSTeK driver: snd_hda_intel 
  v: kernel bus ID: 0a:00.3 
  Sound Server: ALSA v: k5.9.16-1-MANJARO 
Network:
  Device-1: Realtek RTL8111/8168/8411 PCI Express Gigabit Ethernet 
  vendor: ASUSTeK driver: r8169 v: kernel port: f000 bus ID: 07:00.0 
  IF: enp7s0 state: up speed: 100 Mbps duplex: full mac: <filter> 
Drives:
  Local Storage: total: 447.13 GiB used: 24.03 GiB (5.4%) 
  ID-1: /dev/sda vendor: Kingston model: SA400S37480G size: 447.13 GiB 
Partition:
  ID-1: / size: 48.97 GiB used: 12.61 GiB (25.8%) fs: ext4 dev: /dev/sda2 
  ID-2: /boot/efi size: 269.5 MiB used: 312 KiB (0.1%) fs: vfat 
  dev: /dev/sda1 
  ID-3: /home size: 389.64 GiB used: 11.42 GiB (2.9%) fs: ext4 
  dev: /dev/sda3 
Swap:
  ID-1: swap-1 type: file size: 1024 MiB used: 0 KiB (0.0%) file: /swapfile1 
Sensors:
  System Temperatures: cpu: 50.8 C mobo: N/A gpu: amdgpu temp: 50.0 C 
  Fan Speeds (RPM): N/A gpu: amdgpu fan: 1123 
Info:
  Processes: 328 Uptime: 3m Memory: 15.63 GiB used: 2.1 GiB (13.4%) 
  Init: systemd Compilers: gcc: 10.2.0 clang: 11.1.0 Packages: 1158 
  Shell: Zsh v: 5.8 inxi: 3.3.01

If you check an earlier section in the same Archwiki page - 4.3.1 Disabling timer-based scheduling (0/4)

Timer-based scheduling must be turned off in PulseAudio by modifying /etc/pulse/default.pa or changes to fragment size and quantity are ignored

You can use this command to add the option tsched=0 to Pulseaudio configuration

sudo sed -i 's/load-module module-udev-detect/load-module module-udev-detect tsched=0/g' /etc/pulse/default.pa

After this command my latency is the same, I have reboot my PC and used pulseaudio -k and pulseaudio --start too.

pacmd list-sinks | grep 'latency: [1-9]'
	fixed latency: 100,14 ms
	current latency: 92,00 ms
	fixed latency: 100,14 ms
	fixed latency: 100,14 ms

Archwiki had the correct command to restart PulseAudio

Restart the PulseAudio daemon (3/4)
$ systemctl --user restart pulseaudio.service

pulseaudio -k should not be used on a Manjaro system
I have posted explanations about this a number of times

No audio after update - #6 by nikgnomic

Audio output from firefox mixed with microphone - #6 by nikgnomic

I suggest you reboot system and check the systemd service and socket for Pulseaudio has restarted correctly

systemctl --user status pulseaudio*

and check the x11 modules are present, and audio inputs and outputs are not suspended

pacmd dump | grep -E 'x11|suspend'

If PulseAudio appears to be ok after restarting, please post response to these commands to check if timer-scheduling is disabled and pulseaudio has accepted correct values for fragments

pactl list short modules | grep udev

pulseaudio --dump-conf | grep fragment
$ systemctl --user status pulseaudio

● pulseaudio.service - Sound Service
     Loaded: loaded (/usr/lib/systemd/user/pulseaudio.service; disabled; vendor pres>
     Active: active (running) since Mon 2021-03-08 23:04:51 -03; 2min 43s ago
TriggeredBy: ● pulseaudio.socket
   Main PID: 29243 (pulseaudio)
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/pulseaudio.serv>
             ├─29243 /usr/bin/pulseaudio --daemonize=no --log-target=journal
             └─29247 /usr/lib/pulse/gsettings-helper

mar 08 23:04:51 manjaro systemd[1347]: Starting Sound Service...
mar 08 23:04:51 manjaro systemd[1347]: Started Sound Service.
mar 08 23:04:51 manjaro pulseaudio[29243]: Doing resync
mar 08 23:04:51 manjaro pulseaudio[29243]: Playback after capture (-3515), drop sink 488
mar 08 23:05:16 manjaro pulseaudio[29243]: GetManagedObjects() failed: org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
$ pacmd dump | grep -E 'x11|suspend'
load-module module-suspend-on-idle
suspend-sink alsa_output.pci-0000_08_00.1.hdmi-stereo-extra3 yes
suspend-sink alsa_output.pci-0000_0a_00.3.analog-stereo yes
suspend-sink alsa_output.pci-0000_0a_00.3.analog-stereo.echo-cancel yes
suspend-source alsa_output.pci-0000_08_00.1.hdmi-stereo-extra3.monitor yes
suspend-source alsa_output.pci-0000_0a_00.3.analog-stereo.monitor yes
suspend-source alsa_input.pci-0000_0a_00.3.analog-stereo yes
suspend-source noiseless yes
suspend-source alsa_output.pci-0000_0a_00.3.analog-stereo.echo-cancel.monitor yes

Yes, it is, everything fine except for the latency.

$ pactl list short modules | grep udev
5	module-udev-detect	tsched=0 	
6	module-alsa-card	device_id="0" name="pci-0000_08_00.1" card_name="alsa_card.pci-0000_08_00.1" namereg_fail=false tsched=no fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1"	
7	module-alsa-card	device_id="1" name="pci-0000_0a_00.3" card_name="alsa_card.pci-0000_0a_00.3" namereg_fail=false tsched=no fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1"	

$ pulseaudio --dump-conf | grep fragment
default-fragments = 4
default-fragment-size-msec = 25

today I tested if it could be a hardware problem and used a earphone, but the results are the same

pacmd list-sinks | grep 'latency: [1-9]'   
	current latency: 100,13 ms
	fixed latency: 100,14 ms
	current latency: 100,13 ms
	fixed latency: 100,14 ms

You have a source I do not recognise - noiseless
And a sink I did not expect to see - alsa_output.pci-0000_0a_00.3.analog-stereo.echo-cancel
But the two sinks I expected to see for pulseffects-legacy are absent - PulseEffects_apps and PulseEffects_mic

I suspect you have added module-echo-cancel to PulseAudio with the option source_name=noiseless but without the sink_name option

The loopback and/or ring buffer for module-echo-cancel could be the cause of your latency problem
The module may also be conflicting with audio processing within some packages
Disable audio post processing in certain applications - ArchWiki

Yes, I did it following a brazilian tutorial to reduce noise in microphone.

How do I fix that?

I have tried to remove the following lines from /etc/pulse/default.pa

load-module module-echo-cancel aec_args="analog_gain_control=0 digital_gain_control=0" source_name=noiseless
set-default-source noiseless

and then I reboot pulse with systemctl --user restart pulseaudio.service but the problem persist, I tried to reboot my computer too.

I don’t think the change in /etc/pulse/default.pa could be the cause of the problem because I’ve been using this method for noise reduction for about one year and I didn’t have any problem with audio latency, it was always about 30ms or 40ms. But after I formatted the computer four days ago this problem have appeared and I can’t solve it using the default method.

I was expecting from the OP to find that pulseffects-legacy had a lot of DSP effects turned on to cause high latency, which can usually be resolved by reducing the number of effects to reduce the cumulative latency

The fragmant settings in OP (2mS * 5 =10mS) are too optimistic, so PulseAudio would just ignore it. 10mS latency might be possible in JACK, but even that might be difficult for the onboard audio (Realtek ALC887) and latency would still increase when DSP effects are added to the audio chain

you should consider updating BIOS - PRIME-B450M-GAMING-BR - BIOS | asus.com/br
There have been a few reports on here of audio problems with B450 motherboards

Kernel v5.11 is said to have better Ryzen support

Current settings for fragments in post#5 appear to be consistent with 100mS latency - 25mS * 4
you might get lower latency with default-fragments = 2

The module name options are just for convenience. The naming was unusual to me for only using one name, but that was to make setting the default sink easier than using the full sink name

set-default-source alsa_input.pci-0000_0a_00.3.analog-stereo.echo-cancel

The option aec_args="analog_gain_control=0 digital_gain_control=0" would turn off any Automatic Gain Control
You would probably want to keep that in place to not have the microphone level being changed by the system

It might also help if you use aec_method='webrtc' instead of the default ‘speex’
or some of the other options for module-echo-cancel