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

(definstrument filter-noise (start-time duration amp 
					&key
					(noise-freq 10000)
					(freq-env '(0 -1 1 1))
					(res-env '(0 0 1 1)))
  (multiple-value-bind (beg end)
      (get-beg-end start-time duration)
    (let* ((fenv (make-env :envelope freq-env
			   :start-time start-time
			   :duration duration))
	   (qenv (make-env :envelope res-env
			   :start-time start-time
			   :duration duration))
	   (f (make-moog-filter :frequency 0
				:Q 0.9))
	   (noi (make-randi :frequency noise-freq :amplitude amp)))
      (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 (randi noi))))))))

#|
(with-sound(:statistics t :sndfile "/zap/test.snd" :srate 44100)
  (filter-noise 0 5 0.041 
		:res-env '(0 0.94 1 0.94) 
		:freq-env '(0 50 1 22050) 
		:noise-freq 22050))
|#
