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

(defmacro set-formnt(filter freq r)
  `(let* ((freq ,freq)
	  (r ,r))
     (setf (mus-a2 (identity ,filter)) (- r)
	   (mus-b1 (identity ,filter)) (- (* 2.0 r (cos (hz->radians freq))))
	   (mus-b2 (identity ,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) (times->samples start-time duration)
    (let* ((noise (make-rand :frequency (* 0.5 *srate*) :amplitude amplitude))
	   (freq-env (make-env :envelope freq
			       :duration duration))
	   (r-env (make-env :envelope r
			    :duration duration))
	   (fmfilt (make-formant :frequency 440 :radius 0.99)))
      (run
       (loop for i from beg to end do
	     (set-formnt fmfilt (env freq-env) (env r-env))
	     (outa i (formant fmfilt (rand 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)))
