Extracting Noise Reduction Effect Functionality

Hello All,

In my project I’d like to apply the Noise Reduction effect to a large number of files. Ideally I’d like to do this with Audacity from the command line, which would allow me to call it from within something like a Python wrapper that iterates over each file, etc.

Some lingering questions that my research hasn’t been able to clear up for me:

  • Has anyone worked out how to modify Audacity like this (making it run as a command like routine, at least as far as the Noise Reduction effect is concerned)?

  • My C++ is a work in progress, but I’ve been working to understand the logical flow of the Noise Reduction effect’s source code. Does anyone understand this and mind please summarizing it (function1 → function2 → etc…)? Where does control enter that part of the code, where does it exit, etc.

  • The Audacity wiki offers a high-level explanation of the Noise Reduction algorithm, but it’s too vague to enable precise re-implementation in one’s own code. Has anyone seen a more precise (more mathematical) explanation of what the Noise Reduction effect is doing?

In general, any suggestions as to how one might best go about what I’m trying to do would be really super. I’ve looked into using Sox, but it’s not as capable as Audacity for this unfortunately.

Thank you!

I’d like to apply the Noise Reduction effect to a large number of files.

What sort of files and where did they come from? Knowing that isn’t as optional as you think.

The Audacity wiki offers a high-level explanation of the Noise Reduction algorithm, but it’s too vague to enable precise re-implementation in one’s own code.

Back down here on earth, You know the tool works in two distinct waves, right? First you have the Profile step where you drag-select a clean portion of the target noise. Step two is when you turn loose the actual program to do the reduction based on the profile in step 1.

This is let the bloodhound sniff the noise and then it goes charging off howling down the performance looking for a match.

It’s not a one-pass program and it falls over pretty quickly if the quality of the work isn’t good enough to get a good profile. One of the silly jokes is not to get any of your voice in the profile by accident or Noise Reduction will try to remove your voice, too. If you have a really crappy performance, you can’t tell where your voice is or is not.

It won’t process moving noises. That gives us the certified way to kill your performance by leaving a TV running quietly in the background. No way to get rid of that.

Automating the profile step would be a programming exercise.

As in the first sentence, if all your sound files are similar and strictly controlled, then yes, you can automate reduction. In real life that happens to nobody, ever.


There is a module called “mod-script-pipe” that provides an API for controlling Audacity from any scripting language (such as Python) that supports named pipes. There are no official binaries (executables) for the module, so you would need to build it from the source code. For more information, see: http://manual.audacityteam.org/man/scripting.html

Possibly an easier approach would be to use a keyboard macro program such as Autokey or Autohotkey. (Possible Mac equivalents are “Keyboard Maestro”, “Hammerspoon”, “Karabiner”, “BetterTouchTools”, but I’ve not used any of these).

In what way “not as capable”? SoX is certainly easier to script as it is designed as a command line application.

First off, thanks a lot for replying!

They’re all 5-20 sec long wav files recorded on cellphones of a person talking. PCM samples at 44.1 KHz. Mono (not stereo).The recordings were typically made inside retail shops and have a mix of stationary (non-time-varying) noise as well as sporadic non-stationary (time-varying) components.

Yes, I’ve used Audacity as you’ve mentioned…step (1) highlight a portion of the audio thought to be noise for the algorithm to build a noise model, and then (2) highlight the portion of the audio file (usually all of it) to apply the noise reduction algorithm to using the noise model created in (1). This works fairly well in a few experiments I’ve tried. The downside is of course that it’s not automated. As you said, if you accidentally identify part of the speech as noise in step (1), step (2) can be quite unsuccessful.

Assuming only stationary noise, if I could extract the Noise Reduction effect’s functionality into a callable command line routine I think I’d be in business. I could write a routine in Python to estimate a section of the file that’s background noise (by analyzing the FFT/Spectrogram and the energy levels to find a long non-speechlike section) and then use that to call the part of the Noise Reduction effect that builds the noise model, followed by calling the part that actually performs the noise reduction itself.

More generally, if this were possible I could potentially accommodate time-varying noise by identifying multiple sections of noise and calling the routine multiple times in some intelligent manner.

For all of this to work though, I need to isolate and compile the Noise Reduction effect processing into callable command line executable(s) but for someone new to the code and who’s not a C++ expert to begin with it’s daunting…any help would be great.

(Alternatively, if I could find a clearly articulated explanation of the algorithm I could just code it from scratch in python as well.)

Thank you again :wink:

Thank you very much for the “mod-script-pipe” suggestion. I have zero knowledge about “named pipes”, but I’ll look into it. I’ll also check out the Autokey, etc. suggestion. Cheers!

Conceptually, since all I really want is just a way to access the functionality of the Noise Reduction algorithm (i.e. identifying and building a noise model, and then applying that noise model to perform the actual noise reduction), I wonder if it is potentially easier to pull out that part of the code and build a C++ wrapper for it and just compile that into an executable that I could call from Python…it’s hard for me to judge this given my current poor understanding of the code’s structure.

Do you have any view on this??

Yeah, exactly…Sox seems to provide exactly the sort of command line functionality that I need. The sad thing though is that when I compare the final result of using Audacity vs Sox for several of my audio files it seems that the Audacity algorithm does a noticeably better job.

I’ve been tempted to try implementing Audacity’s algorithm as explained in the wiki page from scratch in Python, but the magic in these kinds of things is in the details and it may take me much longer to work out these details to get a good result vs. just figuring out how to extract the code to do what I need.

Hmm…any further suggestions would be great, including any high-level comments on how the Audacity code is structured so that I might be able to just pull out the part I need into a small customized command line routine(s) that only performs the noise reduction, and if that’s even feasible to begin with.

Thank you!!

It’s open source, so you are certainly welcome to try that approach. The code is here: https://github.com/audacity/audacity/blob/master/src/effects/NoiseReduction.cpp

Note that there are some things that can probably be improved:

  1. Setting Sensitivity to zero effectively disables the effect (not useful)
  2. Frequency smoothing bands blur “into” (rather than “our from”) the audio that is being retained, so increasing the smoothing bands causes more of the “good” audio to be removed.
  3. The code is overly complicated because the developer has left in a lot of unused experimental code.
  4. There is nothing special about the capitalized word “NEW” in code comments. That is just a thing that one of the Audacity developers does.

Thank you again for the help. I’ll definitely take these tips into consideration as I work with the code.

Would you mind bootstrapping me into the code a bit by helping me understand where the in the code the Noise Reduction functionality begins and where it ends, i.e. where does the “signal” enter that effect and exit that effect. I’m trying to figure out what part of the C++ code is the essential stuff for this so that I can pull it out and build some simple wrapper code around it. I’m trying to navigate this on my own, but it’s a bit confusing and any help anyone could offer would be greatly appreciated :wink:

Look at:
void EffectNoiseReduction::Worker::ProcessSamples

Depending on whether the bool “mDoProfile” is true or false, this either calls:
void EffectNoiseReduction::Worker::GatherStatistics(Statistics &statistics)

or calls:
void EffectNoiseReduction::Worker::ReduceNoise
(const Statistics &statistics, WaveTrack *outputTrack)

I examined the source code for Noise Reduction in sox, and it mentions that its algorithm for noise reduction was borrowed from Audacity! But an older version of it surely before noise reduction was much improved in version 2.1.0.

(sorry for the late reply…I missed that you had sent this)

This is very very helpful, Steve. I really appreciate it.

I’m digging through the code and hope you don’t mind if I ping you with a few more questions down the road if/when they arise.


That’s really interesting and good to be aware of. Thank you very much for the information!

ps sorry for the delayed reply…wasn’t aware of your post till now.

Would you happen to know if the current algorithm is accurately reflected by http://wiki.audacityteam.org/wiki/How_Audacity_Noise_Reduction_Works ? Going through the code and trying to understand it…

Thank you!

I am the one who rewrote the code and the Wiki page, and I say the Wiki is up to date.

Just came across this thread years later but I am very interested in undergoing a similar kind of project, i.e. using the noise reduction algorithm in audacity in my own GUI to be more efficient/run on simpler hardware. Did you ever end up getting a wrapper developed in Python for this? I have been working my way through the C++ source code but if you have any advice I would greatly appreciate it!