;;;
;;; Simple demonstration of two pole filter
;;; nando, October 28 1996
;;;

(defmacro b1-from-r-freq (r freq) `(- (* 2.0 ,r (cos (in-hz ,freq)))))
(defmacro b2-from-r (r) `(* ,r ,r))

(definstrument twopole(start-time duration amplitude
				  &key
				  (freq '(0 20 1 10000))
				  (r '(0 0.5 1 0.5)))
  (multiple-value-bind (beg end) (get-beg-end start-time duration)
    (let* ((noise (make-randh :frequency (* 0.49 sampling-rate) 
			      :amplitude amplitude))
	   (freq-env (make-env :envelope freq))
	   (r-env (make-env :envelope r))
	   (ppfilt (make-ppolar :r 0.5 :frequency 440.0)))
      (Run
       (loop for i from beg to end do
	     (let* ((freq0 (env freq-env))
		    (r0 (env r-env)))
	       (setf (smpflt-b1 ppfilt) (b1-from-r-freq r0 freq0))
	       (setf (smpflt-b2 ppfilt) (b2-from-r r0))
	       (outa i (ppolar ppfilt (randh noise)))))))))


;;; Frequency sweep, no resonance (r=0.3...0.5...0.7)
;;; (with-sound()(twopole 0 5 0.01 :r '(0 0.3 1 0.3) :freq '(0 10 0.1 10 0.9 10000 1 10000)))
;;; (with-sound()(twopole 0 5 0.01 :r '(0 0.5 1 0.5) :freq '(0 10 0.1 10 0.9 10000 1 10000)))

;;; Fixed frequency, resonance sweep
;;; (with-sound()(twopole 0 5 0.001 :r '(0 0.1 0.1 0.1 0.9 0.9999 1 0.9999) :freq '(0 440 1 440)))
