Help with pwlv

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
jotti
Posts: 46
Joined: Sat Oct 06, 2007 3:19 pm
Operating System: Please select

Help with pwlv

Post by jotti » Sun Feb 13, 2011 8:10 am

I'm learning Nyquist. I did a plugin that would make a marked section silent with a steep fadeout in the beginning and a fadein at the end. The following image shows the effect:
Image
The left edge is ok, but at the right edge you see something strange. This is a magnified part of the right edge of another case:
Image
Some samples before the right edge, the fadein turns into a fadeout all of a sudden. It really is not audible, it's only 19 samples long, but I guess in some cases it will be audible.
I'm using the pwlv function to shape an envelope for the volume (or amplitude or whatyoucallit). Here's the whole code:

Code: Select all

;nyquist plug-in
;version 1
;type process
;name "Soft zeroing"
;action "Do the thing..."
;control fade "Fade" real "ms" 10 1 500
(setq percnt (/ (/ fade 1000.0) (/ len *sound-srate*)))
(setq a (pwlv 1.0 percnt 0.0 (- 1.0 percnt) 0.0 1.0 1.0))
(mult a s)
So I have a slider to set the length of the fadeout and fadein, between 1 and 500 milliseconds. Then some math and stuff to turn it into a percentage (um... from 0 to 1, not from 0 to 100, but you get the idea). The whole pwlv line should create the envelope for me going through (0, 1) (l, 0) (r, 0) and (1, 1) where l and r would be the end time of the fadeout and start time of the fadein.
My code fails at the far right edge. pwlv doesn't get it right. It adds a fading to zero as a last point. What's going on here?

edgar-rft
Posts: 347
Joined: Sun Jan 20, 2008 12:03 am
Operating System: Please select

Re: Help with pwlv

Post by edgar-rft » Sun Feb 13, 2011 10:04 am

To avoid nasty click-sounds at the beginning or end, a Nyquist envelope is defined as always to start and end with a zero-sample. Because your envelope ends with 1.0, Nyquist adds a fade to zero at the end.

The solution is described in the Nyquist manual under Piece-wise Linear Functions:
If you really want a linear ramp to reach its final value at the specified time, you need to make a signal that is one sample longer.
But this reads easier than it really is, because "pwl" envelopes are computed with *control-srate* (in Audacity always 44100/20=2205Hz), not with *sound-srate* (the sample rate of the Audacity track). That's the reason why the zero-fade at the end is more than one sample long. One sample in *control-srate* are several samples in *sound-srate*.

Below is a "fade" function where you can give "fade-out" (at the beginning) and "fade-in" (at the end) with times in seconds. If no "fade-in" time is given, then the "fade-in" at the end will be the same time as the "fade-out" at the beginning.

Code: Select all

(defun fade (sound fade-out &optional fade-in)
  (let* ((seconds  (/ len *sound-srate*))      ; length of sound in seconds
         (end-out  (/ fade-out seconds))       ; end of fade-out in Nyquist time
         (start-in (- 1.0 (if fade-in          ; start of fade-in in Nyquist time
                             (/ fade-in seconds)
                             end-out)))
         (len+1    (* (/ 1.0 len) (1+ len))))  ; Nyquist time at len + 1 sample
    (progv '(*control-srate*) (list *sound-srate*)
      (mult sound (pwl 0.0 1.0 end-out 0.0 start-in 0.0 1.0 1.0 len+1)))))
"progv" sets *control-srate* temporarily to *sound-srate*, so the "pwl" envelope gets computed with *sound-srate* precision.

Examples:

Code: Select all

(fade s 0.2 0.5)  ; 0.2 seconds fade-out, 0.5 seconds fade-in
(fade s 0.3)      ; 0.3 seconds fade-out, 0.3 seconds fade-in
Note that currently there is no check if "fade-in" + "fade-out" together are longer than the sound itself, so "pwl" will compute nonsense in this case.

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

Re: Help with pwlv

Post by steve » Sun Feb 13, 2011 1:02 pm

Am I correct in thinking that

Code: Select all

(progv '(*control-srate*) (list *sound-srate*) beh)
does the same as

Code: Select all

(control-srate-abs *sound-srate* beh)

edgar-rft
Posts: 347
Joined: Sun Jan 20, 2008 12:03 am
Operating System: Please select

Re: Help with pwlv

Post by edgar-rft » Sun Feb 13, 2011 1:19 pm

Yes, "nyquist.lsp" says:

Code: Select all

(defmacro control-srate-abs (r s)
  `(progv '(*CONTROL-SRATE*) (list ,r)
      ,s))
And (1.0/len)*(1+len) is the longwinded version of (1+len)/len, so here now simplified and with "control-srate-abs":

Code: Select all

(defun fade (sound fade-out &optional fade-in)
  (let* ((seconds  (/ len *sound-srate*))  ; length of sound in seconds
         (end-out  (/ fade-out seconds))   ; end of fade-out in Nyquist time
         (start-in (- 1.0 (if fade-in      ; start of fade-in in Nyquist time
                              (/ fade-in seconds)
                              end-out)))
         (len+1    (/ (1+ len) len)))      ; Nyquist time at len + 1 sample
    (control-srate-abs *sound-srate*
      (mult sound (pwl 0.0 1.0 end-out 0.0 start-in 0.0 1.0 1.0 len+1)))))
Last edited by edgar-rft on Sun Feb 13, 2011 1:48 pm, edited 2 times in total.

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

Re: Help with pwlv

Post by steve » Sun Feb 13, 2011 1:26 pm

edgar-rft wrote:Yes, "nyquist.lsp" says:....
Thanks Edgar.

jagatha
Posts: 2
Joined: Sun Mar 29, 2020 4:35 pm
Operating System: Windows 10

Re: Help with pwlv

Post by jagatha » Sun Mar 29, 2020 4:57 pm

Hi
I'm completely new to Nyquist and just starting to learn the plugins
I tried this code and the result is something like this. There is no fade out, silence, fadein
Any help would be highly appreciated.
Thanks
nyq_1.png
nyq_1.png (14.4 KiB) Viewed 2591 times

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

Re: Help with pwlv

Post by steve » Sun Mar 29, 2020 7:20 pm

Wow, this is an old thread.

Nyquist has evolved quite a lot over the last 9 years.
Here's an updated version using "version 4" syntax. I've commented everything to explain what each code line does:

Code: Select all

;nyquist plug-in
;version 4
;type process
;name "Soft zeroing"
;action "Do the thing..."
;control fade "Fade length" float "ms" 10 1 500

;; Fade length as proportion of selection
(setf fade (/ (/ fade 1000) (get-duration 1)))

;; Ensure that fade length is no more than half the duration
(setf fade (min (get-duration 0.5) fade))

;; Do fade with pwlv at same sample rate as the track
(control-srate-abs *sound-srate*
  (mult *track* (pwlv 1 fade 0 (- 1 fade) 0 1 1)))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

jagatha
Posts: 2
Joined: Sun Mar 29, 2020 4:35 pm
Operating System: Windows 10

Re: Help with pwlv

Post by jagatha » Mon Mar 30, 2020 1:37 am

Thank you so much steve,
this thread is so helpful to me, as Im learning the basics.
thank you,

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

Re: Help with pwlv

Post by steve » Mon Mar 30, 2020 9:05 pm

Just as a matter of interest, if you prefer a logarithmic fade, you could use PWEV rather than PWLV. The one thing to be careful of though, is that log zero is "undefined". To avoid log zero, create the gain envelope offset a little way above zero, then subtract the offset to bring the envelope back down to zero:

Code: Select all

;nyquist plug-in
;version 4
;type process
;name "Soft zeroing"
;action "Do the thing..."
;control fade "Fade length" float "ms" 10 1 500

;; Fade length as proportion of selection
(setf fade (/ (/ fade 1000) (get-duration 1)))

;; Ensure that fade length is no more than half the duration
(setf fade (min (get-duration 0.5) fade))

;; Log zero is "undefined", so we use a small "epsilon" value
(setf eps 0.01)

(control-srate-abs *sound-srate*
  (mult *track*
    (diff (pwev (+ 1 eps) fade eps (- 1 fade) eps 1 (+ 1 eps)) eps)))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply