;nyquist plug-in
;version 3
;type process
;categories "http://lv2plug.in/ns/lv2core#GatePlugin"
;name "Pop Mute"
;action "Muting pops..."
;info "by Steve Daulton. http://audadcity.easyspacepro.com\nGNU General Public License version 2.\n\nMutes clicks and pops that are above the threshold level.\nThe amplitude of high level sounds (such as 'pops')\nare lowered to a 'residual' level set by the 'Mute Level'.\n

#|
Experimental version 0.2
To enable processing stereo tracks as "Linked Stereo", 
remove one semicolon from the start of the last 
'control' line (below) and add a semicolon to the
start of the line below it (setq stereo 1)
|#

;control thresh "Threshold" real "dB" -6 -24 0
;control floor "Mute Level" real "dB" -24 -100 0
;control atk "Attack" real "milliseconds" 10 0 100
;control rel "Release" real "milliseconds" 10 0 1000
;;control stereo "Process Stereo Tracks As" choice "Linked Stereo,2x Mono" 1
(setq stereo 1)

(setq floor (db-to-linear floor))
(setq thresh (db-to-linear thresh))
(setq atk (/ atk 1000.0))
(setq rel (/ rel 1000.0))
(setq look atk)
(setq err "")

(defun monopopmute (s-in look atk rel floor thresh)
  (let ((s-track (s-abs s-in)))
  (mult -1 s-in(clip(s-min (mult -1 floor)(diff(gate s-track look atk rel floor thresh)(1+ floor)))1.0))))
  
(defun 2xpopmute (s-in  look atk rel floor thresh)
  (let ((s-track (s-max (s-abs (aref s-in 0))(s-abs (aref s-in 1)))))
  (vector
    (mult -1(aref s-in 0)(clip(s-min (mult -1 floor)(diff(gate s-track look atk rel floor thresh)(1+ floor)))1.0))
    (mult -1(aref s-in 1)(clip(s-min (mult -1 floor)(diff(gate s-track look atk rel floor thresh)(1+ floor)))1.0)))))

(defun process (s-in  look atk rel floor thresh)
  (if (and (arrayp s-in)(= stereo 0))
    (2xpopmute s-in look atk rel floor thresh)
    (multichan-expand #'monopopmute s-in look atk rel floor thresh)))

(if (>(length err)0)
  (format nil err)
  (process s  look atk rel floor thresh))

