Extracting Noise Reduction Effect Functionality

Help for Audacity 2.x.x on Mac OS X.

ImageThis forum is for Audacity 2.x.x on Mac OS X 10.4 and later.
Please state which version of OS X you are using, the exact three-section version of Audacity from Audacity > About Audacity and whether you obtained the .dmg installer or the zip.

Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at https://www.audacityteam.org/download/.
The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.

Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by laertes » Tue Aug 29, 2017 1:08 am

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!
laertes
 
Posts: 7
Joined: Tue Aug 29, 2017 12:40 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by kozikowski » Tue Aug 29, 2017 1:44 am

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.

Koz
kozikowski
Forum Staff
 
Posts: 37713
Joined: Thu Aug 02, 2007 5:57 pm
Location: Los Angeles
Operating System: OS X 10.9 Mavericks

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by steve » Tue Aug 29, 2017 12:15 pm

laertes wrote: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)?

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).

laertes wrote:I've looked into using Sox, but it's not as capable as Audacity for this unfortunately.

In what way "not as capable"? SoX is certainly easier to script as it is designed as a command line application.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by laertes » Wed Aug 30, 2017 12:35 am

First off, thanks a lot for replying!

kozikowski wrote:
leartes wrote: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.


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.

kozikowski wrote:
leartes wrote: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.


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 ;)
laertes
 
Posts: 7
Joined: Tue Aug 29, 2017 12:40 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by laertes » Wed Aug 30, 2017 12:49 am

steve wrote:
laertes wrote: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)?

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).


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??

steve wrote:
laertes wrote:I've looked into using Sox, but it's not as capable as Audacity for this unfortunately.

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


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!!
laertes
 
Posts: 7
Joined: Tue Aug 29, 2017 12:40 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by steve » Wed Aug 30, 2017 10:51 am

laertes wrote: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 open source, so you are certainly welcome to try that approach. The code is here: https://github.com/audacity/audacity/bl ... uction.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.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by laertes » Thu Sep 07, 2017 10:51 pm

steve wrote:
laertes wrote: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 open source, so you are certainly welcome to try that approach. The code is here: https://github.com/audacity/audacity/bl ... uction.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 ;)
laertes
 
Posts: 7
Joined: Tue Aug 29, 2017 12:40 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by steve » Fri Sep 08, 2017 1:43 pm

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)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 44458
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by Paul L » Wed Sep 13, 2017 4:51 pm

laertes wrote:
steve wrote:
laertes wrote:I've looked into using Sox, but it's not as capable as Audacity for this unfortunately.

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


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.


Thank you!!


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.
Paul L
 
Posts: 865
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: Extracting Noise Reduction Effect Functionality

Permanent link to this post Posted by laertes » Thu Sep 14, 2017 5:02 am

steve wrote: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)


(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.

Cheers!
laertes
 
Posts: 7
Joined: Tue Aug 29, 2017 12:40 am
Operating System: OS X 10.11 El Capitan or later (macOS)

Next

Return to Mac OS X



Who is online

Users browsing this forum: No registered users and 2 guests