Mycroft Community Forum

Fighting with pulseaudio AEC

I revisited Pulseaudio Webrtc AEC for a final showdown and due to effect this module can have on mental health this is probably a pyrrhic victory.

Unlike SpeexDSP EC, WebRTC AEC has drift compensation and actually seems to try and cancel the ‘far’ input noise from the ‘near’ input.
SpeexDSP EC is simpler as its just an EC attenuator rather than cancelation method as when it comes to loud close audio sources it actually still works.
WebRTC AEC falls down when the ratio of far input gets near to the levels of the near input.

No EC just playback with music noise, for reference. The alg also takes much longer to adapt than Speex.

https://drive.google.com/file/d/1-kA0vlgTNKCAZ9XwdJ9oSL5FoAtgGuMI/view?usp=sharing

PA EC but when it does its actually cancelation.

https://drive.google.com/file/d/1sjJ19WfdU7Vc-4PY7ONDwZps5XfaGzBT/view?usp=sharing

You can not have the volume too loud and here same again with volume turned up.

No EC just playback with music noise louder as a reference

https://drive.google.com/file/d/1H96OE78OYxKlpMGcqGDFWVFcsNUsgfmw/view?usp=sharing

PA EC Louder here you will tell things are starting to drop out and louder it only gets worse

https://drive.google.com/file/d/1twTxKbku2D4ZKEtN18f3YLD9RUHzUbDe/view?usp=sharing

Because of the drift compensation of webrtc and the possibility of canceling remote input its always been hugely interesting to me but its been a long winded fight to get it actually working on a Pi3.
What is confusing me is how susceptible it seems to be to latency and where is this supposed drift compensation but hey.

So I am not saying this is an exact config setup but if anyone wants to further this you can use the following as a start point as it took me some time to work out.

To blat latency to a minimum and do the following starting with /etc/pulse/daemon.conf

  ; rlimit-rttime = 200000

default-sample-format = s16le
default-sample-rate = 16000
; alternate-sample-rate = 48000
; default-sample-channels = 2
; default-channel-map = front-left,front-right

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

; enable-deferred-volume = yes

Then in /etc/pulse/default.pa there is a section for module-udev-detect

### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
load-module module-udev-detect tsched=0 fixed_latency_range=1
.else
### Use the static hardware detection module (for systems that lack udev support)

The the Webrtc AEC is just added to the end.

### Enable Echo/Noise-Cancellation
load-module module-echo-cancel use_master_format=1 aec_method=webrtc aec_args="analog_gain_control=0\ digital_g$
set-default-source echoCancel_source
set-default-sink echoCancel_sink

The above will give you a starting point and I began to understand that having some of the config=0 is just as important as including it if you wish to activate it.
The only missing options are mobile=0 which is a totally separate AEC for mobile Webrtc and has options for routing_mode=, comfort_noise=
I never got round to testing the mobile much but think its a lighter speex like attenuation EC with no drift compensation.

Pulse will just use the default card from you asound.conf so just a simple /etc/asound.conf will do especially on a Pi as you need a card like Speex with playback/capture on the same clock and hardware.
USB soundcard to respeaker 2 mic or something.

defaults.ctl.card 2; # Sets default device and control to third card (counting begins with 0).
defaults.pcm.card 2; # This does not change the data type.

Start your recording in a console.
parec --rate=16000 --format=s16le --channels=1 --file-format=wav rec.wav

In another start your far noise.
wget https://file-examples-com.github.io/uploads/2017/11/file_example_WAV_10MG.wav
paplay file_example_WAV_10MG.wav

Seems to help to resample to 16k

sox file_example_WAV_10MG.wav -r 16000 file_example_WAV_10MG-16k.wav