157

Is it possible to do realtime noise-removal with PulseAudio, so the sound output that's coming out is smoother?

3
  • it probably is possible, though it would be intensive, and would require someone write a filter for it.
    – RolandiXor
    Dec 27, 2010 at 12:36
  • 3
    @Vadi - I have added an answer that might help you if you are still using Ubuntu or any other PulseAudio based Distro. Hope it works for you buddy. Apr 29, 2016 at 21:23
  • This post seems to be doing something similar
    – akhmed
    May 10, 2016 at 22:24

10 Answers 10

207

Pulseaudio module module-echo-cancel

I started reading a lot about PulseAudio and "hidden" options it had so I could find one that was similar to this question. The one I found was the noise-cancellation module, which is one that dramatically lowers any static noise on the microphone and even A LOT of the background noise, basically giving you the benefit of only recording your own voice with excellent quality (For audio recording for example). To do this follow this steps:

  1. sudo nano /etc/pulse/default.pa

  2. Add the following line anywhere on the file, but I recommend almost at the end where you will find a comment about Echo Cancellation stuff (~line 140):

    load-module module-echo-cancel
    
  3. Reload PulseAudio (pulseaudio -k) or simply restart the computer. You should be able to select the new Noise Cancellation option from the Input Device Section:

    screenshot

You can find more information about it on the Echo Cancel Module Page

Set input as default

If you wish to set as default the echo cancel device simply turn the above line into:

load-module module-echo-cancel source_name=logitechsource

and then at the bottom of the file add

set-default-source logitechsource

In this case I named the source logitechsource, but you can name it whatever you want and simply either restart pulseaudio.

Rename device

Lastly, if you do not want a super long name on the Sound Settings (When you want to select an input/output device). My suggestion is renaming the input device like this:

load-module module-echo-cancel source_name=logitechsource source_properties=device.description=LogitechHD

And again, restarting pulseaudio. The end result looks like this:

screenshot

UPDATE - Full documentation Found Here Thanks to clément

12
  • 18
    If you want to avoid the echo module from adjusting the volume slider automatically, you can set load-module module-echo-cancel aec_method=webrtc aec_args="analog_gain_control=0 digital_gain_control=1" as per gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/224 Oct 13, 2018 at 23:08
  • 4
    You can also load the module temporarily with pactl load-module module-echo-cancel
    – Madacol
    Sep 12, 2020 at 0:36
  • 5
    Wouldn't it be more appropriate to edit ~/.config/default.pa? Nov 17, 2020 at 3:33
  • 2
    Adding module-echo-cancel to pulse audio makes my voice sound slightly metallic in recordings. It does remove some of the background noise, but not all of it. Jan 25, 2021 at 13:09
  • 2
    The suggestion of @BipedalShark is good but since the user's configs will overwrite the system's configs, you will need to source the default configs at the top of your file with .include /etc/pulse/default.pa. Credit goes to this awesome answer. Also, I wrote my configs at ~/.config/pulse/default.pa.
    – Sufian
    Aug 24, 2021 at 10:54
18

This is an old question, but I had the same problem and after some Googling (where I mostly found people who agreed it wasn't possible) and reading some man pages, I have now developed a solution based on user2330377's idea.

First you need to create a noise profile for SoX. Just use any audio recording program to record a few seconds of noise, then cd into the directory you saved it to and do sox noise.wav -n noiseprof noise.prof.

Then you need to create an ALSA loopback device:

sudo modprobe snd_aloop

This is required because pulseaudio, unlike Jack, cannot directly connect audio software together; we will hence use the loopback device as a proxy.

Now you need to start paman and find the names of both your microphone (or other recording device) and of the loopback device we just created. Once those are found, you can execute the following command to start recording sound from your microphone, piping it through SoX and then playing it on the loopback device:

pacat -r -d alsa_input.pci-0000_00_14.2.analog-stereo --latency=1msec|sox -b 16 -e signed -c 2 -r 44100 -t raw - -b 16 -e signed -c 2 -r 44100 -t raw - noisered noise.prof 0.2|pacat -p -d alsa_output.2.analog-stereo --latency=1msec

(Where you need to substitute the correct device names for the -d parameters -- the input device for the first pacat invocation and the loopback device output for the second.)

There you go, almost done! As a last step, start recording sound with the application of your choice, then start up pavucontrol, change to the "Recording" tab and set the audio device used for recording (displayed as the grey button to the right) to "Monitor of Loopback Audio Device". You should now have a clear and noise-free recording!

1
8

So simple solution on ubuntu LTS or debian based distros this is an online script that activates noise cancellation feature that is already in pulse audio (the default audio on debian).

wget -qO - https://gist.githubusercontent.com/grigio/cb93c3e8710a6f045a3dd9456ec01799/raw/94f07c7d75bcf5dd9b08a9c3034844223ec6fbe1/fix-microphone-background-noise.sh | sudo bash && pulseaudio -k

you can also run these commands in secsetion (they are the same thing)

sudo cp /etc/pulse/default.pa /etc/pulse/default.pa.bak
sudo cat <<EOT >> /etc/pulse/default.pa
load-module module-echo-cancel source_name=noechosource sink_name=noechosink
set-default-source noechosource
set-default-sink noechosink
EOT
3
  • Hey how can I undo this? I executed it twice, and now I see duplicated inputs and both microphone and camera are gone! Jul 3, 2022 at 3:04
  • This one worked, thanks Karim! BTW, @OmarDulaimi - it's probably too late, but next time just take a look at the script :) It makes a backup in the first line, you had it in /etc/pulse/default.pa.bak, so the answer is: move the /etc/pulse/default.pa file somewhere (just in case, it's again a backup), then rename '/etc/pulse/default.pa.bak' to /etc/pulse/default.pa
    – konrados
    Jul 23, 2022 at 14:20
  • @OmarDulaimi just remove the additional lines you added. or restore the backup file created /etc/pulse/default.pa.bak and redo it
    – asgs
    Mar 8 at 9:32
5

I use NoiseTorch in Arch Linux , is easy to use and easy to install => NoiseTorch Github Link enter image description here

4

There is no any information on module documentation page about noise cancellation. There is only AEC (Acoustic Echo Cancellation) algorithm inside module-echo-cancel, which is have several implementations, like webrtc | speex.

Therefore you should buy headset|microphones with integrated noise cancellation feature as much as possible.

As I tested voice recording in ubuntu I found some particular qualities:

  • Skype, Telegram uses raw input from the default device (in my case front-in-mic|backward-mic jacks). If you need to cancel a noise in this apps, you should buys headsets|microphones only with integrated noise cancellation feature

  • If you need to use voice calls in browser, then you should keep in mind that browsers has it's own voice processing algorithms implementations, e.g. WebRTC.

  • Also web applications (sites like talky, hangouts, appear.in, etc) can have it's own voice processing algorithms implementations, despite the fact that they can be based on webrtc hangout-analysis

3

There is a noise suppression plugin for module-ladspa-sink:

Here you can can find a tutorial for installation on Fedora, and 2 sample recordings to get an idea about the effects of the plugin:

On Ubuntu:

  1. Download the linux_rnnoise_bin_x64.tar.gz
  2. From the archive, copy the bin/ladspa/librnnoise_ladspa.so e.g. to /usr/local/lib/librnnoise_ladspa.so or anywhere else.
  3. Find out the name of your input using pactl list sources short. For example my microphone is called alsa_input.usb-046d_0994_4A365E20-02.mono-fallback
  4. Run the commands bellow to activate the noise cancellation (before running replace the /usr/local/lib/librnnoise_ladspa.so and alsa_input.usb-046d_0994_4A365E20-02.mono-fallback to your values determined in the steps 2. and 3.):
    pacmd load-module module-null-sink sink_name=mic_denoised_out
    pacmd load-module module-ladspa-sink sink_name=mic_raw_in \
        sink_master=mic_denoised_out label=noise_suppressor_mono \
        plugin=/usr/local/lib/librnnoise_ladspa.so \
        control=50
    pacmd load-module module-loopback \
        source=alsa_input.usb-046d_0994_4A365E20-02.mono-fallback \
        sink=mic_raw_in channels=1 source_dont_move=true sink_dont_move=true
    pacmd load-module module-remap-source source_name=denoised \
        master=mic_denoised_out.monitor channels=1
  1. In your voice recording application you will configure audio input from Null output or Remapped monitor of null output and test (I used Skype Echo)

  2. To make the changes permanent, append the commands from the step 4 into /etc/pulse/default.pa, omitting the pacmd on the beginning of each line.

1

Investigation shows that there is no known way of doing real time noise reduction filtering with any Linux sub system. Some websites point to hardware you can buy which should do the trick much better than doing a software filter.

Alternatively if this is for a recording, you could pass the sound through Audacity and use the noise filter there.

2
  • Pity, I wonder why not. Thanks for investigating. Jan 8, 2011 at 12:16
  • Sure! There is no any information on module documentation page about noise cancellation. There is only AEC (Acoustic Echo Cancellation) algorithm inside module-echo-cancel, which is have several implementations, like webrtc | speex.
    – vskubriev
    Feb 14, 2018 at 8:16
1

Here's at leas tone way, implement webrtc-aec in the

http://wiki.gentoo.org/wiki/PulseAudio

webrtc-aec Yes Uses the webrtc.org AudioProcessing library for enhancing VoIP calls greatly in applications that support it by performing acoustic echo cancellation, analog gain control, noise suppression and other processing.

Here's a paper dated 2013 on the subject (noise removal with pulseaudio not webrtc-aec specifically) http://lac.linuxaudio.org/2013/papers/37.pdf

"We have presented rst results of a multi-channel noise/echo reduction solution built on top of PulseAudio and motivated the design decisions. The work has resulted in a number of improvements in the PulseAudio echo cancellation and signal-processing framework, which have been contributed during the version 3.0/4.0 development cycle and should facilitate future embedded Linux audio solutions. Further work includes optimizing code for audio stream mixing, more ecient resampling methods, and the implementation of an efficient AEC in the multi-channel processing pipeline."

4
  • What I had in mind for a quick had was to takle a noise profile and do real time noise removal using sox and then play back the audio output which I can then use as an input stream in pulse. I believe that pulse is able to create and redirect input and output streams in this manner. That is the playback stream can get redirected as an input stream to another application and the playback stream is per application not per device, so it need not be routed to device hardware. Jun 22, 2013 at 15:53
  • I'm giving this a go using pacmd to list pulseaudio devices, pacat to record and playback from them and sox to do the real time effects on the stream. Jun 22, 2013 at 16:18
  • Would you be interested in posting the scripts for it? Jun 23, 2013 at 20:51
  • IS that not what is used in PulseAudio plugin echo cancellation ? Jun 13, 2022 at 11:55
1

my amd laptop same problem the below code cancel my noise:

sudo nano /etc/pulse/default.pa

## add below code on bottom
### Enable Echo/Noise-Cancelation
load-module module-echo-cancel aec_method=webrtc aec_args="analog_gain_control=0 digital_gain_control=1" source_name=echoCancel_source sink_name=echoCancel_sink
set-default-source echoCancel_source
set-default-sink echoCancel_sink
### 2nd tips
.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

Hope someone will be helped

1

If you are on a distro that uses pipewire instead of pulseaudio (e.g. PopOS 22.04+) you can create an echo cancelling source/mic through the following steps:

  1. cp /usr/share/pipewire/pipewire.conf ~/.config/pipewire/pipewire.conf (create the directory if it doesn't exist already).

  2. Add this snippet from the docs to the active module so that you have the following in your pipewire.conf:

     context.modules = [
    
      # ... Lots of other modules loaded here
    
      {   name = libpipewire-module-echo-cancel
          args = {
              # library.name  = aec/libspa-aec-webrtc
              # node.latency = 1024/48000
              source.props = {
                 node.name = "Echo Cancellation Source"
              }
              sink.props = {
                 node.name = "Echo Cancellation Sink"
              }
          }
      }
    ]
    
  3. Restart pipewire: systemctl --user restart pipewire.service

You can listen to the results by recording your voice using something like simplescreenrecorder or by running this command gst-launch-1.0 pulsesrc ! pulsesink (make sure you have headphones on or the microphone will pick up the sound form the speaker causing unpleasant feedback noise).

I personally find that the noise cancelling from the echo cancelling modules makes my voice sound more natural compared to using a program capable of more aggressive noise cancelling such as NoiseTorch. However, echo cancelling is mostly good for removing your fan noise and similar whereas NoiseTorch can automatically mute your microphone when not speaking in addition to removing some noise when speaking.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .