(definstrument rev2 (startime dur &key (volume1 1.0) (volume2 1.0))
  ;; a combination of 2 different reverbs, each reading a different channel of *reverb*
  (flet ((prime (val)
	   (or (= val 2)
	       (and (oddp val)
		    (do ((i 3 (+ i 2))
			 (lim (sqrt val)))
			((or (= 0 (mod val i)) (> i lim))
			 (> i lim)))))))
    (let* (;; jc-reverb variables
	   (jallpass1 (make-all-pass -0.700 0.700 1051))
	   (jallpass2 (make-all-pass -0.700 0.700  337))
	   (jallpass3 (make-all-pass -0.700 0.700  113))
	   (jcomb1 (make-comb 0.742 4799))
	   (jcomb2 (make-comb 0.733 4999))
	   (jcomb3 (make-comb 0.715 5399))
	   (jcomb4 (make-comb 0.697 5801))
	   (chan2 (= (mus-channels *output*) 2))
	   (joutdel1 (make-delay (* .013 *srate*)))
	   (joutdel2 (if chan2 (make-delay (* .011 *srate*))))
	   ;; nrev variables
	   (srscale (/ *srate* 25641))
	   (val 0)
	   (dly-len (make-array 15 :element-type 'fixnum 
				:initial-contents '(1433 1601 1867 2053 2251 2399 347 113 37 59 53 43 37 29 19)))
	   (beg (floor (* startime *srate*)))
	   (end (+ beg (floor (* dur *srate*)))))
      ;; initialize nrev delay lengths
      (loop for i below 15 do
	(setf val (floor (* srscale (aref dly-len i))))
	(if (= 0 (mod val 2)) (incf val))
	(loop while (not (prime val)) do (incf val 2))
	(setf (aref dly-len i) val))
      ;; nrev gens
      (let ((ncomb1 (make-comb (* .822 1.09) (aref dly-len 0)))
	    (ncomb2 (make-comb (* .802 1.09) (aref dly-len 1)))
	    (ncomb3 (make-comb (* .773 1.09) (aref dly-len 2)))
	    (ncomb4 (make-comb (* .753 1.09) (aref dly-len 3)))
	    (ncomb5 (make-comb (* .753 1.09) (aref dly-len 4)))
	    (ncomb6 (make-comb (* .733 1.09) (aref dly-len 5)))
	    (nlow (make-one-pole 0.7 (- 0.7 1.0)))
	    (nallpass1 (make-all-pass -0.700 0.700 (aref dly-len 6)))
	    (nallpass2 (make-all-pass -0.700 0.700 (aref dly-len 7)))
	    (nallpass3 (make-all-pass -0.700 0.700 (aref dly-len 8)))
	    (nallpass4 (make-all-pass -0.700 0.700 (aref dly-len 9)))
	    (nallpass5 (make-all-pass -0.700 0.700 (aref dly-len 11)))
	    (nallpass6 (if chan2 (make-all-pass -0.700 0.700 (aref dly-len 12)))))
	(run
	 (loop for i from beg to end do
           (let* (;; jc-reverb generators
		  (in1rev (in-any i 0 *reverb*))
		  (jallpass-sum (all-pass jallpass3 (all-pass jallpass2 (all-pass jallpass1 in1rev))))
		  (jcomb-sum (+ (comb jcomb1 jallpass-sum)
				(comb jcomb2 jallpass-sum)
				(comb jcomb3 jallpass-sum)
				(comb jcomb4 jallpass-sum)))
		  (jouta (* volume1 (delay joutdel1 jcomb-sum)))
		  (joutb (if chan2 (* volume1 (delay joutdel2 jcomb-sum)) 0.0))
		  ;; nrev gens
		  (rev (* volume2 (in-any i 1 *reverb*)))
		  (outrev (all-pass nallpass4
			    (one-pole nlow
			      (all-pass nallpass3
				(all-pass nallpass2
				  (all-pass nallpass1
				    (+ (comb ncomb1 rev)
				       (comb ncomb2 rev)
				       (comb ncomb3 rev)
				       (comb ncomb4 rev)
				       (comb ncomb5 rev)
				       (comb ncomb6 rev))))))))
		  (nouta (all-pass nallpass5 outrev))
		  (noutb (if chan2 (all-pass nallpass6 outrev) 0.0)))
	     (outa i (+ jouta nouta))
	     (when chan2 (outb i (+ joutb noutb))))))))))

(definstrument simp (start duration freq amp which-reverb)
  ;; which-reverb determines whether this note is reverberated by jc-reverb or nrev
  (let* ((beg (floor (* *srate* start)))
	 (end (+ beg (floor (* *srate* duration))))
	 (os (make-oscil freq)))
    (run
     (loop for i from beg below end do
       (let ((val (* amp (oscil os))))
	 (outa i val)
	 (out-any i (* val .1) which-reverb *reverb*))))))

#|
(with-sound (:reverb-channels 2 :reverb rev2)
  (simp 0 .1 440 .1 0)
  (simp 1 .1 440 .1 1))
|#
