Pulseaudio Microphone Loopback (Listen While Recording)

Before the tomatoes come flying my way, I quite like Pulseaudio and think it’s done a lot of nice things for the “user experience” of many Linux desktops. That being said, I got a new headset today that features two 3.5mm audio jacks: one for the headphones, the other for included boom microphone. However, when plugging in said device and using it with the headphones on, I find it rather difficult to use VoIP applications (like Teamspeak) when I can’t hear my own voice (narcissistic, right?). I lose track of what I’m saying when everything is all muffled. So, what’s needed is a microphone loopback, so I can hear myself through the headphones while I’m talking. To give credit where it’s due, I found the pieces for this at this link, but to remove the microphone loopback, it wants you to manually look up the module index number and then use a command to unload it. I’m lazy, so I dumped this into two executable files in /usr/local/bin.

First, a file called “loadmicloop”, that has a single line in it:

pactl load-module module-loopback latency_msec=1

Secondly, a file called “unloadmicloop”, that also has a single line in it:

pactl unload-module $(pactl list short | grep latency_msec=1 | awk '{print $1}')

The first one is pretty obvious: it tells pactl to load the loopback module with a 1 milisecond latency. The second one passes the results of a grep and awk of the short list of loaded Pulseaudio modules that’s been sliced to only be the index number of the looback module. That module number is then unloaded by “pactl unload-module”. This is handy, as I wouldn’t want it on all the time, and it’d be great to see that in future versions of the sound control panel for microphones, perhaps as a simple toggle on and off.

In all reality I should probably give this 10 more minutes and make the two commands “on” and “off” parameters to one executable, but, as mentioned, I’m lazy and it works for a dirty hack.

UPDATE: Thanks to Eli Barzilay in the comments for adding a small wrapper script for the on/off I mentioned above. I’ve modified it so that it should work on EL-based systems (RHEL/CentOS/SL):

#!/bin/sh
case "x$1" in
( xon | xstart ) pactl load-module module-loopback latency_msec=1 > $HOME/.paloop.mod
exit;;
( xoff | xstop ) pactl unload-module $(cat $HOME/.paloop.mod)
exit;;
( * ) echo "$0: use with on/off or start/stop" 1>&2; exit 1;;
esac
  1. Sweet, nice to know how to get the loopback/monitoring to fully with a command.

  2. Simple wrapper (hopefully the text won’t get butchered):

    #!/bin/sh
    case “x$1″ in
    ( xon | xstart ) pactl load-module module-loopback latency_msec=1
    exit;;
    ( xoff | xstop ) pactl unload-module \
    $(pactl list short | grep latency_msec=1 | awk ‘{print $1}’)
    exit;;
    ( * ) echo “$0: use with on/off or start/stop” 1>&2; exit 1;;
    esac

  3. Here’s my shellscript, it just checks if the loopback is already active, and toggles it based on its current state.

    If this turns the single quotes into ‘’, replace them with real single quotes when typing in this script. I added it to my top panel (I use the MATE desktop), and whenever i click it, it toggles states.

    #! /bin/bash
    # Toggle loopback from line in
    pactl unload-module $(pactl list short | grep latency_msec=1 | awk ‘{print $1}’) || pactl load-module module-loopback latency_msec=1

  4. I know, this is pretty old, but I use this to loopback music from my phone to my computer, which has much better audio quality.

    The only problem is: Thanks to pulseaudio it sounds like crap. I don’t know what exactly happens but lower frequencies seem to get completely distorted :/ Any suggestions?

Leave a Comment