Peak attenuation

Hello!

This is my first request: a tool to smoothly lower the level of a small selection by a desired amount, following a bell curve maybe. That would be very useful and effective for reducing short peaks and could also be used for longer sections in some cases. Using the envelope tool for that purpose seems tedious to me, compared to a hypothetical “Effects → smooth-reduce”.

Thank you

For reducing short, occasional peaks, I’d recommend using the Limiter effect: https://manual.audacityteam.org/man/limiter.html


To smoothly (bell-curve) lower a longer section, you can use the “Adjustable Fade” effect (https://manual.audacityteam.org/man/adjustable_fade.html), but you need to do it in two halves. After applying the effect to the first half, hold down the shift key and drag the “outside edge” across to select the second half of the region. I’ve demonstrated here on a generated tone so that you can see clearly what it does:

adjustable-fade.gif

Thank you for being so reactive and thorough! Nice!
I knew about the “Adjustable Fade”, I use it all the time, so much so that I came to dream about a tool that does it in one click, but I’m glad that “Adjustable fade” exists.

If you frequently want a “bell curve”, you could add one to the Adjustable Fade effect. You would need to add a bell curve function, and an option in the “Fade Type” dropdown list. The “Start / End” controls in this case would used as “Ends / Middle”.

That sounds perfect, what’s the procedure for doing something like that? I have no clue…

The “Adjustable Fade” effect is a “Nyquist Plug-in”.

“Nyquist” is an audio programming language that is included in Audacity. More information here: https://manual.audacityteam.org/man/nyquist.html
Nyquist programs are written in plain text.
For working with Nyquist in Windows, it is highly recommended to get a good plain text editor that supports "parentheses matching (such as NotePad++)

Nyquist scripts (programs) may be run in Audacity using the “Nyquist Prompt” effect: https://manual.audacityteam.org/man/nyquist_prompt.html
This is a good way to test Nyquist code.

For code that you will use frequently, Nyquist scripts may be made into plug-ins, which can be installed and are then available to run at any time like other effects.

Here is a Nyquist function that will create a single cycle of a sine wave that fits the current audio selection (lines beginning with a semicolon are “comments” and are ignored by Nyquist):

;version 4
;type process

(defun bell-curve ()
  (let ((step (hz-to-step (/ (get-duration 1))))
        (phase -90))
    (osc step 1 *sine-table* phase)))

;; Call the function.
(bell-curve)

This is close to what we want as the “gain” that will be applied to the audio. If we want to increase the level at the half way point, then we will need a curve that is the other way up - that is, we want the phase of the sine wave to be -90 rather than +90 degrees.

;version 4
;type process

(setf g0 1.0) ; gain at start / end of curve
(setf g1 0.5) ; gain at middle of curve

(defun bell-curve (g0 g1)
  (let ((step (hz-to-step (/ (get-duration 1))))
        (phase (if (> g0 g1) 90 -90)) ;phase -90 for bell, +90 for inverted bell
        (offset (if (> g0 g1) -1 1))) ;offset -1 for bell, +1 for inverted bell
    (mult 0.5
      (sum offset (osc step 1 *sine-table* phase)))))

;; Call the function.
(bell-curve g0 g1)

We also want to scale the curve according to the specified amount of gain.

Note that the above code gives us a curve that starts / end at 0, and goes up to +1 if g1 > g0 (bell curve) and goes down to -1 if g1 < g0 (inverted bell).
We need to scale the entire curve so that the height of the curve is equal to the difference of g0 and g1, and that the start value is g0.
For convenience, we can create a new function to do that, which I shall call “bell-curve-gain”

(defun bell-curve-gain (g0 g1)
  (let ((start g0)
        (mid-gain (abs (- g0 g1))))
    (sum start
        (mult mid-gain (bell-curve g0 g1)))))

To apply the gain curve to the track audio, we simply multiply (scale) the track audio by the gain curve:

(mult *track* (bell-curve-gain g0 g1))

Putting the whole thing together we get some code that can be run in the Nyquist Prompt:

;version 4
;type process

(setf g0 1.0) ; gain at start / end of curve
(setf g1 0.5) ; gain at middle of curve

(defun bell-curve (g0 g1)
  (let ((step (hz-to-step (/ (get-duration 1))))
        (phase (if (> g0 g1) 90 -90)) ;phase -90 for bell, +90 for inverted bell
        (offset (if (> g0 g1) -1 1))) ;offset -1 for bell, +1 for inverted bell
    (mult 0.5
      (sum offset (osc step 1 *sine-table* phase)))))


(defun bell-curve-gain (g0 g1)
  (let ((start g0)
        (mid-gain (abs (- g0 g1))))
    (sum start
        (mult mid-gain (bell-curve g0 g1)))))

;; Call the function.
(mult *track* (bell-curve-gain g0 g1))

The one thing that is obviously missing is that our gain values g0 and g1 are hard coded into the first few lines. It would be much nicer to have slider controls, so let’s add those:

;version 4
;type process

;control g0 "Start / End gain" float "" 1 0 2
;control g1 "Mid-gain" float "" 0.5 0 2

(defun bell-curve (g0 g1)
  (let ((step (hz-to-step (/ (get-duration 1))))
        (phase (if (> g0 g1) 90 -90)) ;phase -90 for bell, +90 for inverted bell
        (offset (if (> g0 g1) -1 1))) ;offset -1 for bell, +1 for inverted bell
    (mult 0.5
      (sum offset (osc step 1 *sine-table* phase)))))


(defun bell-curve-gain (g0 g1)
  (let ((start g0)
        (mid-gain (abs (- g0 g1))))
    (sum start
        (mult mid-gain (bell-curve g0 g1)))))

;; Call the function.
(mult *track* (bell-curve-gain g0 g1))

With the addition of a few more "plug-in headers, this code can easily be made into an installable plug-in. See here and have a go: https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference

Wow!! What can I say, thank you so much!
I never thought I would get an immediate answer that nails the problem, provides a detailed a solution, and teaches me something on the way.
I now have a plug-in that exactly fits my needs and discovered a whole new dimension to Audacity which makes it much more attractive, I’m definitely gonna try a lot of stuff with Nyquist programming.
I’m surprised on how few there are on the https://wiki.audacityteam.org/wiki/Download_Nyquist_Plug-ins#list
Is there a strict selection or something?

I don’t like to add plug-ins that don’t have instructions - as a minimum, there should be a brief description of what the plug-in does, and what each control does. Other than that, it’s just a matter of finding the time to check the plug-in has no major bugs, upload it and add the documentation to the wiki.

There are a lot more plug-ins here on the forum - mostly on the Nyquist boards: Nyquist - Audacity Forum