;;;
;;; Simple demonstration of formant filter
;;; nando, October 28 1996
;;;

(defmacro set-formnt(filter freq r)
  `(let* ((freq ,freq)
	  (r ,r))
     (setf (smpflt-a2 (frmnt-tz ,filter)) (- r)
	   (smpflt-b1 (frmnt-tp ,filter)) (- (* 2.0 r (cos (in-hz freq))))
	   (smpflt-b2 (frmnt-tp ,filter)) (* r r))))

(definstrument simp-formnt(start-time duration amplitude
				  &key
				  (freq '(0 20 1 10000))
				  (r '(0 0.707 1 0.707)))
  (multiple-value-bind (beg end) (get-beg-end start-time duration)
    (let* ((noise (make-randh :frequency (* 0.5 sampling-rate) :amplitude amplitude))
	   (freq-env (make-env :envelope freq
			       :start-time start-time
			       :duration duration))
	   (r-env (make-env :envelope r
			    :start-time start-time
			    :duration duration))
	   (fmfilt (make-formnt :frequency 440 :r 0.99)))
      (Run
       (loop for i from beg to end do
	     (set-formnt fmfilt (env freq-env) (env r-env))
	     (outa i (formnt fmfilt (randh noise))))))))

;;; (with-sound()(simp-formnt 0 2 0.1 :r '(0 0.999 1 0.999) :freq '(0 20 1 5000)))
;;; (with-sound()(simp-formnt 0 4 0.1 :r '(0 0.1 0.8 0.9999 1 0.9999) :freq '(0 800 1 800)))
