;nyquist plug-in
;version 4
;type process
;name "Delay with bpm and panning..."
;action "Performing Delay with bpm and panning..."

;control decay "Decay" float "dB" -6 -24 0
;control bpm "Tempo" float "bpm" 120 1 500
;control notes "Note values:" choice "Half note (0.5),Quarter note (1),Eighth note (2),Sixteenth note (4),Thirty-second note (8),Sixty-fourth note (16)" 2
;control echoes "Number of echoes" int "times" 5 1 100
;control spread "Pan spread" float "move" 0.0 -1 1
;control options "Select aditional options for the delayed sound:" choice "Normal,Add high pass filter,Add low pass filter,Add band pass filter" 0
;control lower "Lower frequency for the filter:" int "hz" 300 20 10000
;control upper "Upper frequency for the filter:" int "hz" 3000 20 10000

(setq notes
  (case notes
    (0 0.5)
    (1 1)
    (2 2)
    (3 4)
    (4 8)
    (t 16)))
(setq delay (* 60 (/ (* bpm notes))))

(defun sig ()
(case options
(0 *track*)
(1 (highpass2 *track* upper 1))
(2 (lowpass2 *track* lower 1))
(t (highpass2 (lowpass2 *track* upper 1) lower 1))))

(defun stereo-delay (sig)
  (let ((out sig))
    (do ((i 1 (1+ i))
         (gain decay (+ gain decay))
         (where spread (* -1 where)))
        ((> i echoes) out)
      (setf out
        (sim out
             (at-abs (* i delay)
                (panning (loud gain (cue (sig))) where)))))))

(defun panning (stereo-sig where)
  (let* ((mono-gain (* where where))
         (where (/ (+ 1 where) 2.0))  ;range 0 to 1
         (mono-sig (mono-mix stereo-sig)))
    (sum (mult mono-gain (pan mono-sig where))
         (stereo-pan stereo-sig where))))

(defun stereo-pan (stereo-sig where)
  (setf gain  (- 2 (* 4 (abs (- where 0.5)))))
  (vector (mult gain (- 1 where) (aref stereo-sig 0))
          (mult gain where (aref stereo-sig 1))))

(defun mono-mix (stereo)
  (mult 0.5 (sum (aref stereo 0)
                 (aref stereo 1))))

(setf dur (get-duration 1))
(if (arrayp *track*)
    (let ((temp-result (stereo-delay *track*)))
      (multichan-expand #'extract-abs 0 dur temp-result))
    "Error.\
Stereo track required.")