Page 2 of 2

Re: Help Building a Plugin

Posted: Mon May 03, 2010 1:09 pm
by Roquentin
Thanks for all your help and encouragement. I'm now convinced that I can actually handle doing some stuff in Nyquist (and that I don't have to reinvent the wheel to do it!). This boiled-down script is more or less exactly what I was looking for:

;nyquist plug-in
;version 1
;type process
;name "soft-knee brickwall limiter"

(defun limit (s) (mult s (diff 1.0 (mult 0.5 (s-abs s)))))
(limit s)

Re: Help Building a Plugin

Posted: Mon May 03, 2010 9:05 pm
by steve
I'm impressed - the soft clipping sounds better than my original plug-in :)
It does weird things on stereo files though.

For processing stereo files you could add this to the end:

Code: Select all

(if (arrayp s)
   (vector (limit (aref s 0))(limit (aref s 1)))
   (limit s))
but you would also need to rename the variable "s" within the function so as to differentiate it from "s".
For example:

Code: Select all

;nyquist plug-in
;version 1
;type process
;name "soft-knee brickwall limiter"

(defun limit (s-in) 
   (mult s-in (diff 1.0 (mult 0.5 (s-abs s-in)))))

(if (arrayp s)
   (vector (limit (aref s 0))(limit (aref s 1)))
   (limit s))
A shorter, but somewhat more cryptic way of writing this would be:

Code: Select all

(defun limit (s-in) 
   (mult s-in (diff 1.0 (mult 0.5 (s-abs s-in)))))

   (multichan-expand #'limit s)
(note the single quote after the #)

If this effect is used for "maximising" a recording, you will want to amplify the output to compensate for the reduction in peak gain.
Fortunately, this algorithm produces easily predictable attenuation (where the multiplier of (s-abs s) is greater than 0.5).

Here is a slight modification that allows the limiter threshold to be set in the range of 0 to -6 dB ( -6dB is the same as your code) and can also perform the necessary amplification to compensate for the peak attenuation.

Code: Select all

;nyquist plug-in
;version 1
;type process
;name "soft-clip brickwall limiter"
;info "Usually best to Normalize the audio before using this effect."
;control thresh "Threshold" real "dB" -3 -6 0
;control mu "Apply Make-Up Gain?" choice "Yes,No" 0

; limit max and min values of thresh
(setq thresh (min (max thresh -6) 0))

; convertthresh to linear scale
(setq thresh (dB-to-linear thresh))

(Let 
   ((make-up (if (= mu 0)(/ thresh)1)) ; set make up gain to inverse of thresh, or 1
   (thresh (- 1.0 thresh))) ; set thresh 
   
   (defun limit (s-in)   
     (mult s-in make-up (diff 1.0 (mult thresh (s-abs s-in)))))

  (multichan-expand #'limit s))

Re: Help Building a Plugin

Posted: Tue May 04, 2010 12:59 pm
by Roquentin
Wow, thanks Steve! I am going to have to spend some time to really understand some of the suggestions you made but they look like good ideas. My rudimentary scripting skills are going to have to do some growing here...

Re: Help Building a Plugin

Posted: Tue May 04, 2010 7:43 pm
by steve
Since you're interested in Nyquist programming it may be worth signing up to the Audacity-Nyquist mailing list.
It is a very low volume e-mail list that you send e-mails to and receive e-mails from other list subscribers. It's not actually used very much, but there are occasionally interesting things come up and it provides another avenue for asking questions.
https://lists.sourceforge.net/lists/lis ... ty-nyquist