;;; -*- syntax: common-lisp; package: clm; base: 10; mode:lisp -*-
;;;
;;; Moog style four pole filter
;;;   low pass, 24db/Oct, variable resonance, warm, analog sound ;-)
;;;   [all this digital wizardry and we're back where we started!]
;;; by Fernando Lopez-Lezcano, nando@ccrma.stanford.edu
;;; (translation into clm of Tim Stilson's c moog filter implementation)
;;;
;;; Filter some noise as an example
;;;

(definstrument filter-soundfile (start-time filename
					&key
					(freq-env '(0 400 1 400))
					(res-env '(0 0.7 1 0.7)))
  (let* ((in-file (open-input filename))
	 (in-file-dur (snd-duration in-file)))
    (multiple-value-bind (beg end)
	(get-beg-end start-time in-file-dur)
      (let* ((fenv (make-env :envelope freq-env
			     :start-time start-time
			     :duration in-file-dur))
	     (qenv (make-env :envelope res-env
			     :start-time start-time
			     :duration in-file-dur))
	     (f (make-moog-filter))
	     (reader (make-readin :file in-file)))
	(Run
	 (loop for i from beg to end do
	   (setf (moog-Q f)(env qenv)
		 (moog-frequency f)(env fenv))
	   (outa i (moog-filter f (readin reader)))))))))


(defmacro with-moog ((start-time
		      &key 
		      (freq-env '(0 9000 1 9000))
		      (res-env '(0 0.8 1 0.8))) &body body)
  `(sound-let ((temp1 () ,.body))
	      (filter-soundfile ,start-time temp1 :freq-env ,freq-env :res-env ,res-env)))

#|
(with-sound(:srate 44100)
  (with-moog (0 :freq-env '(0 4000 0.5 400 1 400) :res-env '(0 0.8 1 0.8)) 
	     (fm-violin 0 5 440 0.1)
	     (fm-violin 1 6 671.1 0.1)))
|#
