Fix PulseAudio loopback delay

Sort of a follow up (in spirit) to two of Jon’s previous posts regarding pulse audio loopback; I noticed that there was quite a bit of delay (~500ms to 1second) in the default configuration and began searching for a way to fix it. After some research I found an alternative way to achieve the loopback but with must less delay.

1. Install paman

First install the PulseAudio Manager application so that you can correctly identify the input device (i.e. your mic or line-in) and your output device (i.e. the sound card you are using).

sudo apt-get install paman

You can find the input sources under the Sources section and the output devices under the Sinks section of the Devices tab. Make note of the names of the two devices.

2. Unload any previous loopback modules

If you had followed Jon’s previous posts then you will need to unload the modules (and potentially change your PulseAudio configuration so they don’t get loaded again on next restart). This is to stop it from doubling all loopback sound.




3. Create an executable script

Create a script and copy the following command into it:

pacat -r --latency-msec=1 -d [input] | pacat -p --latency-msec=1 -d [output]

where [input] is the name of your input device found in step 1 and [output] is the name of the output device. In my case it would look like:

pacat -r --latency-msec=1 -d alsa_input.pci-0000_05_02.0.analog-stereo | pacat -p --latency-msec=1 -d alsa_output.pci-0000_05_02.0.analog-surround-51

4. Run script

By simply running the script now you should get correct loopback and with much less delay than using the default loopback module. Even better if you set this script to run at startup you won’t have to worry about it ever again.




14 Comments

  1. Thank you very much. I was searching a solution for this problem some time ago. Works really fine.

  2. # cat .pulse/default.pa
    .include /etc/pulse/default.pa
    load-module module-loopback latency_msec=80

    … doesn’t anyone consult the manual these days? 😉

    no need for root; this method is likely to pound on your machine HARD … esp at 1msec … most humans will not detect lag until it approaches or exceeds 100msec.

  3. I also use pacat in the way described on this page with tvtime, but I’ve found that there are still cases where latency is introduced if the play pacat instance gets behind. Once it gets behind it stays behind. To address this problem I’ve made a modification to pacat. The details are on my site:
    http://selliott.org/node/59

    C Anthony,
    I find that module-loopback still occasionally has unreasonable latency even if a low latency_msec is specified.

  4. I’ve refined my patch a bit and added another one. I’ve mentioned my patches on the pulseaudio mailing list:
    http://lists.freedesktop.org/archives/pulseaudio-discuss/2012-May/013588.html
    But the consensus sees to be that the problem should be fixed in tvtime. I can’t say I blame them, but anyone who is still experiencing latency with the pacat solution mentioned in this post are welcome to try my patches. They are named “pacat-drain-experimental.diff” and “pulseaudio-limited-watermark-experimental.diff”. You can read more about them here:
    http://selliott.org/node/59
    I’m happy to receive feedback.

  5. Alright, how do I unload the modules and change the config so that they don’t get loaded again on restart?

  6. Matteo :

    Also, what do you mean by “create an executable script”?

    You can create an executable script by making a new text file and placing this as the first line:

    #!/bin/sh
    (terminal commands go here)

    Then mark the file as executable by issuing the command
    chmod +x myscript

    Now you can run it like a normal program
    ./myscript

  7. This can rape your machine as C Anthony said. If you simply want line in playback, you should just install alsa-utils, type alsamixer -c 0 (or 1 test to find your sound card), and look for the input playback selector, (the only one without a volume slider) to choose your input device. Also don’t forget to raise the line in’s playback volume.

  8. I am using loopback modules and null-sinks to combine my game audio and my webcam microphone audio into 1 virtual stream which is then sent to twitch.tv suing ffmpeg. I am having a horrible delay from the audio/video of the game and when I say something. Example: i shoot my gun and at the same time i say into my microphone “bang”, there’s about 25 seconds from the time the video shows me shooting my gun and when you hear my voice say “bang”. How would I fix this, do you have any idea. If i set a latency-msec on the game audio loopback, wont it become out of sync with the game video then? Any ideas

  9. @venge
    I’d like to see that happen… sound cards (even on-board) usually come pre-built with the mixers needed for this kind of stuff, especially RealTek, Intel, creAtive, or Turtle Beach. I doubt any of my systems (including my 400MHz Celeron) would have a problem with it. Alsa doesn’t do anything at all for me, line-volume is raised and I get no playback… I need loopback for it to work. Oddly enough though the delay happened the 2nd time I booted my Wii (I play Brawl, so it’s not hard for me to detect latency), but worked perfectly fine on the initial boot. Not sure what kicked up the delay… “Volume Control” lies and reports 0.00 latency on everything.

  10. This doesn’t work for me. The following does:

    “pactl load-module module-loopback latency_msec=1”

    But “pacat -r –latency-msec=1 -d ,etc..” doesn’t. It just goes to an empty line in the prompt, like I’m echoing something.

1 Trackback / Pingback

  1. bluetooth audio lag fix in manjaro linux – Kenny’s Codeboard

Leave a Reply

Your email address will not be published.


*