Scripting noise reduction, Audacity 3.1.0, Windows 11, Python 3.8

Hi,

I am trying to make a Python (Anaconda, Spyder, Python 3.8) script that uses audacity noise reduction. Since I am not very skilled programmer i am not able to deduce the correct syntax from the commands list (https://alphamanual.audacityteam.org/man/Scripting_Reference).
Moreover, i have found that this particular function is currently not available for scripting.
I would therefore like to ask you for a little push (like minimal working example or something) in the right direction.

Many thanks for any help!

So I have never used Python scripting. Were you referring to this quote in the manual?

This effect is not currently available from scripting.

I believe that Noise Reduction does not currently accept passed arguments which would be the reason for that comment.

I just tried this using Macros and it seems that if you first get a Noise profile before running the Macro, then the following command can be used within the macro to do the actual noise reduction.

NoiseReduction:Use_Preset="<Factory Defaults>"

I am not sure how this works with Python. Perhaps someone with experience in this area would care to comment.

Hi,
thanks for the tip, I have no experience with the macro tools, but I will try it. The situation will be as you write - predefined “silence” file and other file to apply it to.
The thing is, that I need to do some preparations in Python anyway, so I it would be very comfortable to create a full automat O:-)

Pretty much the same as with macros.

One thing that you can do with Python that you can’t do with macros is to launch Audacity (for example, with Python’s “Popen” command), which can be useful in the case of Noise Reduction as it allows you to ensure that you are starting without a noise profile.

It looks like you have some experience with the Python-Audacity scripting. Could you please give me a code example of the noise reduction? I really am a beginner, so I need to see the syntax.
Many thanks

There’s a simple example of Python scripting here: https://github.com/audacity/audacity/blob/master/scripts/piped-work/pipe_test.py
(Click on the “Raw” button to get the plain text: https://raw.githubusercontent.com/audacity/audacity/master/scripts/piped-work/pipe_test.py)

The above example sends the commands:

  • Help: Command=Help
  • Help: Command=“GetInfo”
  • SetPreference: Name=GUI/Theme Value=classic Reload=1

Try substituting other commands.
The command reference is here: https://manual.audacityteam.org/man/scripting_reference.html
(To find this in the manual, look at the “Reference” section in the left column of the manual and click the link “All Commands”)

A good way to try out commands quickly is with the command-line utility “pipeclient”. See: https://github.com/audacity/audacity/blob/master/scripts/piped-work/pipeclient.py
pipeclient.py may also be used as a loadable module for writing your own Python scripts.

(Although the above scripts should work with Python 2 or Python 3, it is highly recommended to use Python 3.)

Thank you for the links, but there are of no help for me. I tried to understand them, but it is just too many new things at once.
Could you please complete the following?

### what do i need to import? ###
import ???

### creating some random data ###
import numpy as np
import random

N = 1000
x = np.zeros(N)
for i in range(N):
	x[i] = random.random()


###creating the audio file ###
import soundfile as sf

sf.write( "randomnoise.wav", x, 100)

### getting noise profile ###
???
 audacity -> get noise profile(randomnoise.wav,start = 0 s, end = 2 s)
???

### lowering the noise ###

???
audacity -> lower the noise (randomnoise.wav, start = 0 s, end = 10 s, 10dB, sensitivity = 6, bands = 3)
???

I appreciate the problem. You need to spend some time playing with some simple examples of sending commands from Python to Audacity.

Did you get pipe_test.py to work?
Can you see how pipe_test.py works?
Can you successfully modify pipe_test.py to make Audacity do other things? (See here for available commands: https://manual.audacityteam.org/man/scripting_reference.html)

Are you able to run commands using the pipeclient.py command-line interface?
Can you import a file into Audacity using the pipeclient.py command-line interface?
Can you create a noise profile using the pipeclient.py command-line interface?
Can you apply Noise Reduction using the pipeclient.py command-line interface?

I really appreciate your effort of educating me, but I really already got through all of this. The pipe_test.py is working (I need to restart Audacity after every call, but that is a minor problem), I can see see that the command is called through a series of functions, but no, I have not been able to get the logic behind

'Help: Command="GetInfo"'

as a derivative from the hierarchy Extra: Scriptables II → GetInfo: (enum Type (default:Commands) …)
Moreover I still don’t know if the command is even working for the scripting at all.
I understand that it is all clear for an experienced programmer, but I really am a beginner. Could you please PLEASE just finish the code I proposed? That would save me many days (maybe months) of time.

I’m not a Python expert, but this works for me.
“pipeclient.py” must be in the same location as this script so that it can be imported.

Note that I’m on Linux, so you will need to make some adjustments for Windows.
You will also need to adjust file paths and file names to suit your machine.

#!/usr/bin/env python3
# -*- coding: utf-8 -*

from os.path import join
import sys
import subprocess
import time

import pipeclient

# Check that we're using a recent Python
assert sys.version_info >= (3, 6)

# Path to Audacity executable
# (I'm using a development version on Linux)
audacity = "..path../audacity/build/bin/Release/audacity"

# Path to audio file folder
folder="..path.."
# noise sample
noise_sample = join(folder, "noise.wav")
# file to be processed
process_file = join(folder, "myfile.wav")
# output file
processed = join(folder, "output.wav")

# Launch Audacity
subprocess.Popen(f'{audacity}')
# Allow enough time for Audacity to open
time.sleep(5)

# Create a client object
client = pipeclient.PipeClient()

# Import the noise sample
client.write(f'Import2: Filename={noise_sample}')

# Get the noise profile
client.write('NoiseReduction:')

# Undo the import
client.write('Undo:')

# Get the file to process
client.write(f'Import2: Filename={process_file}')
print(client.read())

# Apply Noise Reduction
client.write('NoiseReduction:')

# Export the processed file
client.write(f'Export2: Filename={processed}')

Thank you! It works! :slight_smile:
For a potential future reader, the changed code for Windows is (noise file needs to be in the same folder as the script, added autoexit in the end):

#!/usr/bin/env python3
# -*- coding: utf-8 -*

from os.path import join
import sys
import subprocess
import time
import os

import pipeclient

# Check that we're using a recent Python
assert sys.version_info >= (3, 6)

# Path to Audacity executable
audacity = "C:\Program Files\Audacity\Audacity.exe"

# Path to audio file folder
folder="C:/Users/User/Documents/1/python/Odecty"
# noise sample
noise_sample = join("noise.wav")
# file to be processed
process_file = join(folder, "myfile.wav")
# output file
processed = join(folder, "output.wav")

# Launch Audacity
#subprocess.Popen(f'{audacity}')
os.startfile(audacity)

# Allow enough time for Audacity to open
time.sleep(3)

# Create a client object
client = pipeclient.PipeClient()

# Import the noise sample
client.write(f'Import2: Filename={noise_sample}')

# Get the noise profile
client.write('NoiseReduction:')

# Undo the import
client.write('Undo:')

# Get the file to process
client.write(f'Import2: Filename={process_file}')
print(client.read())

# Apply Noise Reduction
client.write('NoiseReduction:')

# Export the processed file
client.write(f'Export2: Filename={processed}')

# Turn Audacity off
client.write('Undo:')
client.write("exit")