Pulseaudio echo cancellation

Share your own howto's etc. Not for support questions!

Pulseaudio echo cancellation

Postby gradinaruvasile » 2016-05-11 07:07

Echo cancellation provides not only echo cancellation but background noise removal too. It does effective echo cancellation to provide good audio quality even if you use microphone+external speakers eliminating feedback.

Setting up the echo cancellation in Pulseaudio is done by loading the module-echo-cancel-module:
Code: Select all
pactl load-module module-echo-cancel aec_method=webrtc

And to make it permanent add this to the end of /etc/pulse/default.pa file:
Code: Select all
load-module module-echo-cancel aec_method=webrtc

This will provide an additional playback and one additional recording virtual device named like this:

Built-in Audio Analog Stereo (echo cancelled with Built-in Audio Analog Stereo)

The name by default contains your default input and output device's name, echo cancelled with itself.
It works like this:
You start the application and change the default output AND input device to the echo-cancelled stream either from within the application (if the application "sees" the Pulseaudio streams) or externally (this works for all applications) from pavucontrol/desktop volume control applet. I recommend using pavucontrol (pavucontrol package) for this.
Also there is a really nice system tray application called "pasystray" (pasystray package) that can do it from drop-down menus (also launching pavucontrol and pretty much every pulseaudio manager application).

- Echo cancellation works only if BOTH the recording and playback streams go through the echo cancel virtual device.
- If you set an application via the external method to use the echo cancel device, Pulseaudio will remember the setting for that application and next time it is launched it will automatically be played back through the echo cancel device.

Possible Issues:
- when the microphone has adjustable gain control and you use voice applications through pulseaudio the echo-cancel module it may happen that in certain applications the auto gain control is over-sensitive and leads to sudden gain changes which generate annoying background noises when the gain is suddenly increased.

The vailable controls can be easily found by using the terminal "alsamixer" application. Note that most likely after launch you will see only the "pulse" device, so you will have to change it to your hardware device by pressing F6, select your card (NOT the ones that have HDMI in their name) then press TAB to cycle to your "capture" tab. See if you have there an adjustable slider that has something like "Rear Mic Boost" (varies depending of your actual capture device name).

1. Disable the AGC in the echo-canceller module (certain applications will still provide AGC on their own, but might alleviate the issue with 2 overlapping AGC methods):
Code: Select all
pactl unload-module module-echo-cancel
pactl load-module module-echo-cancel aec_method=webrtc aec_args="analog_gain_control=0\ digital_gain_control=0"

Or permanent setting, appended to the end of /etc/default.pa:
Code: Select all
load-module module-echo-cancel aec_method=webrtc aec_args="analog_gain_control=0\ digital_gain_control=0"

http://thread.gmane.org/gmane.comp.audi ... eral/25742

2. Prevent Pulseaudio from accessing the echo canceller altogether - this will prevent everything from messing with the adjustable gain control, but this is a hacky solution and will have to be redone EVERY TIME PULSEAUDIO IS UPDATED.
This is done by finding and editing the input control files in /usr/share/pulseaudio/alsa-mixer/paths/ directory. There is no "one solution fits all" here, you have to locate the file that controls your specific input device.

I will use my settings as example. I have only a built in card and i use the "Rear Mic" (physically the rear input on the desktop) for microphone.
I have:
Code: Select all
$ ls -1 /usr/share/pulseaudio/alsa-mixer/paths/

So i will have to edit analog-input-rear-mic.conf to access settings. The control i have to disable is "Rear Mic Boost"
So i will locate its section that looks like this:
Code: Select all
[Element Rear Mic Boost]
required-any = any
switch = select
volume = merge
override-map.1 = all
override-map.2 = all-left,all-right

And i will have to change the
Code: Select all
volume = merge

Code: Select all
volume = zero

Then restart pulseaudio with
Code: Select all
pulseaudio -k
(this command kills pulseaudio and, by virtue of its default settings, will respawn automatically).
*IMPORTANT* Restarting pulseaudio will have the following side effects: All applications that use pulseaudio and are running when it is restarted will be unable to play sound until they are restarted too.
The only exceptions i know of is pavucontrol and the volume control applet that will wait until the server is restarted and re-establish connection to it, but pretty much all other applications will lose playback capabilities until they are restarted.

Update: For me, i had to apply the fix nr 2 to get rid of Google Hangout's aggressive AGC on my home computer (caused loud background noise when it was silence around me). Fix nr. 1. was not enough as it seems certain applications will do AGC on their own and abuse the mic boost features. Chrome is such an application as it does the same on plain webrtc sites too not just their own hangouts.
Applying the 2nd fix restricted the AGC range to the "Capture" element's, not using the boost element.
Note that certain sound cards dont have adjustable boost on mic (most USB headsets) and those will not have thi issue.
User avatar
Posts: 935
Joined: 2010-01-31 22:03
Location: Cluj, Romania

Re: Pulseaudio echo cancellation

Postby darkbasic » 2017-10-26 20:27

https://blogs.gnome.org/uraeus/2010/10/ ... -on-linux/

Currently the echo cancellation code can deal with sources and sinks that share a common clock, such as those found on the same sound card.

For devices that don’t share a common clock, we currently don’t have accurate enough timings from pulseaudio yet in order to implement dynamic resampling. Most echo cancelling algorithms are extremely sensitive to drift and fail as soon as resampling is slightly inaccurate. This means that if you record sound using a USB microphone on your webcam for instance, there is a good chance the card will drift and the echo cancellation will cease to work.

Is it still true? What's the best way to test if echo cancellation is working?
Posts: 2
Joined: 2017-10-26 20:09

Re: Pulseaudio echo cancellation

Postby darkbasic » 2017-10-26 20:38

Just tried: it works flawlessly even when using multiple devices :mrgreen:
Posts: 2
Joined: 2017-10-26 20:09

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 2 guests