logarithmic fade in/fade out

Help for Audacity on Windows.
Forum rules
ImageThis forum is for Audacity on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


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.
Post Reply
mmaa
Posts: 4
Joined: Wed May 08, 2013 5:28 am
Operating System: Please select

logarithmic fade in/fade out

Post by mmaa » Sun May 16, 2021 8:34 pm

Hi Everyone,

Does anyone know the exact mathematical formulae for logarithmic fade in/fade out (or exponential fade in/fade out) available among options of "adjustable fade" effect? Where can we find such formulae?

I appreciate your help in advance!

Best regards,
MM.

steve
Site Admin
Posts: 81627
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: logarithmic fade in/fade out

Post by steve » Sun May 16, 2021 8:50 pm

mmaa wrote:
Sun May 16, 2021 8:34 pm
Does anyone know the exact mathematical formulae for logarithmic fade in/fade out (or exponential fade in/fade out) available among options of "adjustable fade" effect? Where can we find such formulae?
Here's the code: https://github.com/audacity/audacity/bl ... le-fade.ny

The "Logarithmic In" option is the 6th option for the "preset" control (line 30). The option numbers start from zero, so "preset = 5".
This gets picked up in line 92:

Code: Select all

(5  (log-exp-curve 15.311 0))   ; Logarithmic In
which calls the function "log-exp-curve" with parameters 15.311 and 0.

"log-exp-curve" is defined in lines 182 to 190:

Code: Select all

;;; log or exponential curve scaled 0 to 1
;;; x is the minimum level in dB before scaling.
(defun log-exp-curve (x direction)
  (control-srate-abs *sound-srate*
    (let ((x (db-to-linear x)))
      ;; If direction=0 fade-in else fade-out
      (if (= direction 0)
        (setf env (pwev x 1 1))
        (setf env (pwev 1 1 x)))      
      (mult (/ (- 1 x))     ; normalize to 0 dB
        (diff env x)))))    ; drop down to silence
When called with (log-exp-curve 15.311 0), "direction"=0, which is the fade-in direction.
So the code that creates the fade curve is:

Code: Select all

(setf env (pwev x 1 1))
The "pwev" function is defined within Nyquist: http://www.cs.cmu.edu/~rbd/doc/nyquist/ ... l#index423
(pwev l1 t2 l2 t3 t3 ... tn ln) [LISP]
  • Creates a piece-wise exponential envelope with breakpoints at (0, l1), (t2, l2), etc., ending with (tn, ln). Otherwise, the behavior is like that of pwe.
and
(pwe t1 l1 t2 l2 ... tn) [LISP]
  • Creates a piece-wise exponential envelope with breakpoints at (0, 1), (t1, l1), (t2, l2), ... (tn, 1). Exponential segments means that the ratio of values from sample to sample is constant within the segment. (The current implementation actually takes the log of each value, computes a piece-wise exponential from the points using pwl, then exponentiates each resulting sample. A faster implementation is certainly possible!) Breakpoint values (lj) must be greater than zero. Otherwise, this function is similar to pwl, including stretch by *sustain*, mapping according to *warp*, sample rate based on *control-srate*, and "breakpoint munging" (see pwl described above). Default initial and final values are of dubious value with exponentials. See pwev below for the function you are probably looking for.
(emphasis mine)

The resulting "env" control signal is then multiplied by the selected audio.


Simplifying the code gives (approximately):

Code: Select all

(setf x 5.82841)
(setf env (pwev x 1 1))
(setf env (mult (/ (- 1 x)) (diff env x)))
(mult *track* env)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 81627
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: logarithmic fade in/fade out

Post by steve » Sun May 16, 2021 9:16 pm

steve wrote:
Sun May 16, 2021 8:50 pm
which calls the function "log-exp-curve" with parameters 15.311 and 0.
Q. Why "15.311"?
A. Because that value gives a similar amount of curvature as the "Cosine" curve.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

mmaa
Posts: 4
Joined: Wed May 08, 2013 5:28 am
Operating System: Please select

Re: logarithmic fade in/fade out

Post by mmaa » Mon May 17, 2021 5:41 pm

Thank you very much Steve for your comprehensive and very helpful answer!

Post Reply