;;; Use the "type" parameter to specify the type of butterworth
;;; filter to use. 

(definstrument butter-filter (start-time dur amp file 
					 &key 
					 (type 1)
					 (frq-env '(0 100.0 1 200.))
					 (bw-env '(0 50 1 100)))
  (multiple-value-bind
      (beg end) (times->samples start-time dur)
    (let* ((fil (open-input* file))
	   (s (make-readin :file fil))
	   (f-env (make-env :envelope frq-env
			    :duration dur)) 
	   (b-env (make-env :envelope bw-env
			    :duration dur))
	   (b (cond ((= 1 type)
		     (make-butterbp 100 50))
		    ((= 2 type)
		     (make-butterbr 100 50))
		    ((= 3 type)
		     (make-butterhp 100))
		    ((= 4 type)
		     (make-butterlp 100)))))
      (run
       (loop for i from beg to end do
	     (let ((sig (* amp (readin s)))
		   (f-val (env f-env))
		   (b-val (env b-env)))
	       (outa i (cond ((= 1 type)
			      (set-butterbp b f-val b-val)
			      (butterbp b sig))
			     ((= 2 type)
			      (set-butterbr b f-val b-val)
			      (butterbr b sig))
			     ((= 3 type)
			      (set-butterhp b f-val)
			      (butterhp b sig))
			     ((= 4 type)
			      (set-butterlp b f-val)
			      (butterlp b sig))))))))))


#|

(with-sound (:srate 44100)
  (butter-filter 0.0 4.3 0.7 "/usr/ccrma/web/html/courses/220b/lectures/3/examples/cello-C4.snd" 
		 :type 1 
		 :frq-env '(0 50 1 1000)
		 :bw-env '(0 100 1 200)))

(with-sound (:srate 44100)
  (butter-filter 0.0 4.3 0.7 "/usr/ccrma/web/html/courses/220b/lectures/3/examples/cello-C4.snd" 
		 :type 1 
		 :frq-env '(0 50 1 1000)
		 :bw-env '(0 10 1 20)))

(with-sound (:srate 44100)
  (butter-filter 0.0 4.3 0.7 "/usr/ccrma/web/html/courses/220b/lectures/3/examples/cello-C4.snd" 
		 :type 3 
		 :frq-env '(0 50 1 10000)))

(with-sound (:srate 44100)
  (butter-filter 0.0 4.3 0.7 "/usr/ccrma/web/html/courses/220b/lectures/3/examples/cello-C4.snd" 
		 :type 4 
		 :frq-env '(0 50 1 10000)))

|#