How To Enable Echo / Noise Cancellation Of Microphone Input On Your Linux Desktop (PulseAudio)

PulseAudio echo cancel

PulseAudio comes with a module that can be used to perform acoustic echo cancellation of the microphone input, and some background noise reduction. This module is not enabled by default on most (if not all) Linux distributions, so this article will show you how to enable it.

I'll show you 2 ways of enabling the PulseAudio Echo Cancel module: one that makes it permanent so it's automatically started when you login to your desktop, and another one that allows you to enable echo cancellation on demand, when you need it.

This is not just to offer 2 alternatives, but also to work around a PulseAudio bug that happens for example when plugging in a headset after loading the Echo Cancel module, which causes this not to switch automatically to the new sink_master. Also, in my case, having module-echo-cancel load at startup does not work at all (I'm not sure why), but it can be loaded after logging in with no issues, using the second method below.

[[Edit]] For those that have multiple microphones attached to the computer, I've added a new section to this article which explains how to choose for which microphone to apply the echo / noise cancellation (the new section is called How to choose the microphone in setups with multiple microphones, to use with the PulseAudio module-echo-cancel).

How to enable the PulseAudio module-echo-cancel on startup


To always have echo / noise cancellation on your Linux desktop using PulseAudio, having module-echo-cancel loaded at startup, follow the steps below.

1. Edit /etc/pulse/default.pa as root with a text editor, like Nano (command line text editor) for example:

sudo nano /etc/pulse/default.pa

Scroll down to the end of this file and paste the following:

.ifexists module-echo-cancel.so
load-module module-echo-cancel aec_method=webrtc source_name=echocancel sink_name=echocancel1
set-default-source echocancel
set-default-sink echocancel1
.endif

This is what this does: if your system PulseAudio is compiled with the echo / noise cancellation module, load this module, use webrtc as the echo cancellation method (it should be better than the default speex), specify a source and sink names, then set that source and sink as default.

2. After saving the file, reload PulseAudio using this command:

pulseaudio -k

Or reboot your computer.

After this, look in your system's sound settings and the input and output devices should both have the default name, but with "echo cancelled with..." in parentheses, e.g. "Built-in Audio Analog Stereo (echo cancelled with Built-in Audio Analog Stereo)" like in the screenshot at the top of the article.

This should persist through reboots.

If this doesn't work for you, see the second method below, for how to load module-echo-cancel on demand.

How to enable the PulseAudio module-echo-cancel on demand


To have echo / noise cancellation of the microphone input only when you need it (by having an application menu entry to turn this on), follow the steps below.

1. Create a new file in your home folder called echocancel with the following contents:

#!/usr/bin/env bash
pactl unload-module module-echo-cancel
pactl load-module module-echo-cancel aec_method=webrtc source_name=echocancel sink_name=echocancel1
pacmd set-default-source echocancel
pacmd set-default-sink echocancel1

This unloads module-echo-cancel if it was already loaded, then loads this module with webrtc as the echo cancellation method (it should be better than the default speex), specify a source and sink names, then set that source and sink as default.

2. After saving the file, make it executable and copy it somewhere in your path. You can make it executable and place it in /usr/local/bin using this command:

sudo install echocancel /usr/local/bin

3. Create a file called echocancel.desktop in your home folder with the following contents:

[Desktop Entry]
Version=1.0
Name=Echo Cancel PulseAudio Module
Comment=Load the PulseAudio module-echo-cancel
Exec=echocancel
Icon=multimedia-volume-control
Type=Application
Categories=AudioVideo;Audio;

4. Save the file and copy it to ~/.local/share/applications/, e.g. do this from the command line using:

cp echocancel.desktop ~/.local/share/applications/

You should now see a new entry called Echo Cancel PulseAudio Module in your applications menu. When you click on it, it should load the echo / noise cancelling PulseAudio volume.

After you run this, look in your system's sound settings and the input and output devices should both have the default name, but with "echo cancelled with..." in parentheses, like in the screenshot at the top of the article.

In case you want to unload the echo cancel PulseAudio module you can run:

pactl unload-module module-echo-cancel

How to choose the microphone in setups with multiple microphones, to use with the PulseAudio module-echo-cancel


If your system has multiple microphones, this is how to apply the echo / noise cancellation to the microphone you want to use.

Start by listing the audio source names using:

LANG=C pacmd list-sources | grep name:

This should output something like this:

LANG=C pacmd list-sources | grep name:

name: <alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1.monitor>
name: <alsa_input.pci-0000_00_1b.0.analog-stereo>
name: <alsa_output.pci-0000_00_1b.0.analog-stereo.monitor>
name: <alsa_input.usb-046d_0819_FEB73FA0-02.mono-fallback>

Notice that in my example, 2 of the sources have "input" in their names. You'll need to figure out which of these is the microphone for which you want to apply the echo cancellation. 

Having the name of the microphone you want to use, you'll now need to edit the load-module module-echo-cancel line in either of the 2 methods used above (with either the method to enable the PulseAudio module-echo-cancel on startup, or the method to enable the PulseAudio module-echo-cancel on demand) to include source_master=<SOURCE_NAME>, replacing SOURCE_NAME with the name of the source name found as explained above (e.g.: alsa_input.pci-0000_00_1b.0.analog-stereo from my example from above).

So considering the instructions for enabling the PulseAudio module-echo-cancel on startup and enabling the PulseAudio module-echo-cancel on demand from above, you'll need to change the load-module module-echo-cancel line:

from:

  • for enabling PulseAudio module-echo-cancel on startup:

load-module module-echo-cancel aec_method=webrtc source_name=echocancel sink_name=echocancel1

  • for enabling PulseAudio module-echo-cancel on demand:

pactl load-module module-echo-cancel aec_method=webrtc source_name=echocancel sink_name=echocancel1


To this:

  • for enabling PulseAudio module-echo-cancel on startup:
load-module module-echo-cancel source_master=<SOURCE_NAME> aec_method=webrtc source_name=echocancel sink_name=echocancel1

  • for enabling PulseAudio module-echo-cancel on demand:

pactl load-module module-echo-cancel source_master=<SOURCE_NAME> aec_method=webrtc source_name=echocancel sink_name=echocancel1

For example:

pactl load-module module-echo-cancel source_master=alsa_input.usb-046d_0819_FEB73FA0-02.mono-fallback aec_method=webrtc source_name=echocancel sink_name=echocancel1

For more on this, see the PulseAudio module-echo-cancel documentation.