1. Overview
Although modern microphones may provide a decent level of noise reduction, there are often situations when the background noise still persists.
In this tutorial, we’ll learn how to enable real-time noise reduction using PulseAudio and other sound applications.
We’ll first examine how to cancel echoes from the sound. Then, we’ll study real-time noise reduction using a noise profile file. Finally, we’ll look at a GUI-based app that may simplify the setup.
2. Remove Acoustic Echoes
Acoustic echo is an unwanted added sound generated when the audio playing out of a speaker device is coupled back to the microphone. As a result, a user at the remote end may hear their voice.
PulseAudio provides module-echo-cancel to remove acoustic echos from the audio.
To enable it, we add the following line PulseAudio configuration file:
load-module module-echo-cancel
The configuration file is located in /etc/pulse/default.pa.
To apply the changes, we reload the PulseAudio using the following command:
$ pulseaudio -k
Now, the echo canceling module is loaded.
In case we’re using Ubuntu, we now should be able to select the new Noise Cancellation option from the Input Device section.
3. Noise Removal Using Noise Profile
We can remove background noise from audio by using the noise profile of our input sound.
Although this isn’t supported by PulseAudio out of the box, it can be configured using the ALSA modules.
Let’s look at how we can achieve it.
3.1. Create Audio Recording
To create the noise profile, we first need to have a short audio recording from the intended microphone device.
Let’s use the arecord command (ALSA sound recorder) for that:
$ arecord --format=cd noise.wav
Recording WAVE 'noise.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
This starts the audio recording and saves it to a file noise.wav.
3.2. Create a Noise Profile
Now that we have a short recording of our audio noise, let’s create the noise profile.
For that, we use the sox (sound exchange) package. It may not be installed by default so we may need to install it with the apt command:
$ sudo apt install sox
Once installed, let’s create the noise profile:
sox noise.wav -n noiseprof noise.prof
Here, the noise.wav file is the audio recording of a noise, while the noise.prof is the created noise profile file.
3.3. Create ALSA Loopback Device
Now, we need to apply the noise profile to our input device.
Since PulseAudio cannot directly connect audio software, the loopback device should be used as a proxy.
The following modprobe command creates an ALSA loopback device:
$ sudo modprobe snd_aloop
To verify that the device has been created correctly, we can run the aplay -l command and filter for the “Loopback” keyword with grep:
$ aplay -l | grep Loopback
card 2: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
card 2: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
We can see the loopback devices are on the list as we expect.
3.4. Find Input and Output Devices
Before we start a noise-free audio feed, let’s find our input and output audio devices. They’ll be needed later on.
For that, we use the pactl command:
$ pactl list sinks sources short
0 alsa_input.usb-046d_0825_3E93B490-02.mono-fallback module-alsa-card.c s16le 1ch 48000Hz RUNNING
1 alsa_output.pci-0000_00_1f.3.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz IDLE
2 alsa_input.pci-0000_00_1f.3.analog-stereo module-alsa-card.c s16le 2ch 44100Hz RUNNING
18 alsa_output.platform-snd_aloop.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
19 alsa_input.platform-snd_aloop.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
As we can see, we now have both input and output audio devices listed.
3.5. Create a Noise-Free Recording
Finally, let’s start the noise-free audio recording using the pacat command:
$ pacat -r -d $input --latency=1msec | sox -v 0.8 -b 16 -c 2 -e signed -t raw -r 44100 - -C 0.5 -b 16 -c 2 -e signed -r 44100 -t raw - noisered noise.prof 0.21 | pacat -p -d $output --latency=1msec
Although this command contains a huge number of options, most of them set up the sound format. The essential one is the noisered option, where we specify the noise profile file name that we created before.
Moreover, it’s important that we replace the $input with our microphone input device, which in our case is the alsa_input.pci-0000_00_1f.3.analog-stereo, while the $output device should be the loopback device, whose name is the alsa_output.platform-snd_aloop.0.analog-stereo.
We should now have the noise-free audio recording process running.
As a last step, we can start recording sound with the application of our choice, then run the pavucontrol command, change to the “Recording” tab, and set the audio device used for recording to “Monitor of Loopback Audio Device“.
4. Using the NoiseTorch App
Sometimes, adding noise reduction with ALSA is a tricky process.
There is a simpler alternative to this by using the open-source NoiseTorch App. It provides a graphical user interface (GUI) and takes much fewer steps to set up.
Let’s look at how we can install and use NoiseTorch.
4.1. Download and Setup
Firstly, we download the latest version of NoiseTorch using the wget command:
$ wget https://github.com/noisetorch/NoiseTorch/releases/download/v0.12.2/NoiseTorch_x64_<version>.tgz
Here, we insert the latest version in the
Secondly, to extract the archive, we use the tar command:
$ tar -C $HOME -h -xzf NoiseTorch_x64_<version>.tgz
This extracts the NoiseTorch packages to our home directory in ~/.local/bin.
Finally, we add the required permissions using the setcap command:
$ sudo setcap 'CAP_SYS_RESOURCE=+ep' ~/.local/bin/noisetorch
Now, the noisetorch executable is ready to run.
4.2. How to Use
To run Noisetorch, we use the following command:
$ ~/.local/bin/noisetorch
In the GUI interface, we select an input device and press the “Load NoiseTorch” button. The new device now should be available in the audio input device list.
5. Conclusion
In this article, we looked at how to enable real-time audio noise reduction. First, we enabled the module-echo-cancel on PulseAudio to remove echoes from audio. Then, we learned how to use a noise profile file with ALSA tools. Finally, we discovered the NoiseTorch application that provides GUI to simplify the setup.