Help with pwlv

For all discussions about Nyquist code. Request new plug-ins.
Post your new plug-in or test-drive the latest ones here.
Archived plug-ins are here.

Help with pwlv

Permanent link to this post Posted 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?
jotti
 
Posts: 35
Joined: Sat Oct 06, 2007 3:19 pm

Re: Help with pwlv

Permanent link to this post Posted 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.
edgar-rft
 
Posts: 347
Joined: Sun Jan 20, 2008 12:03 am

Re: Help with pwlv

Permanent link to this post Posted 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)
steve
Senior Forum Staff
 
Posts: 32940
Joined: Sat Dec 01, 2007 11:43 am

Re: Help with pwlv

Permanent link to this post Posted 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.
edgar-rft
 
Posts: 347
Joined: Sun Jan 20, 2008 12:03 am

Re: Help with pwlv

Permanent link to this post Posted by steve » Sun Feb 13, 2011 1:26 pm

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

Thanks Edgar.
steve
Senior Forum Staff
 
Posts: 32940
Joined: Sat Dec 01, 2007 11:43 am


Return to Nyquist



Who is online

Users browsing this forum: No registered users and 5 guests