Page 1 of 1

logarithmic fade in/fade out

Posted: Sun May 16, 2021 8:34 pm
by mmaa
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.

Re: logarithmic fade in/fade out

Posted: Sun May 16, 2021 8:50 pm
by steve
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)

Re: logarithmic fade in/fade out

Posted: Sun May 16, 2021 9:16 pm
by steve
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.

Re: logarithmic fade in/fade out

Posted: Mon May 17, 2021 5:41 pm
by mmaa
Thank you very much Steve for your comprehensive and very helpful answer!