; comb-filter.ins
; delay with feedback used for spectral effect (note how short the delay time is!)
; last updated lcb 02.20.2001

(definstrument comb-filter (source-filename &key 
					 (source-srate 1.0)
					 (start 0.0)
					 (duration nil)
					 (delay-samples 799) ; specified in samples
					 (feedback 0.7) ; should be < 1.0
					 (gain 0.8)     ; scales output
					 (decay-time 0.1)) ; lengthens output file
  (let* ((source-file (open-input* source-filename))
	 (source-file-duration (/ (sound-duration source-filename) 
				  (abs source-srate)))
	 (minimum-duration (if (null duration)
			       (+ decay-time source-file-duration)
			     (+ decay-time
				(min duration source-file-duration))))
	 (source-src (make-src :input source-file
			       :srate source-srate))
	 (echo (make-delay :size delay-samples))
	 (current-sample 0.0))
    (multiple-value-bind (beg end) (times->samples start minimum-duration)
      (run
       (loop for i from beg to end do
	 (setf current-sample (src source-src))
	 (outa i (* gain (+ current-sample (delay echo (+ current-sample
							 (* feedback (tap echo))))))))))
      (close-input source-file)))

#|
example with-sound call:

(with-sound (:statistics t :srate 44100 :scaled-to 0.9)  (comb-filter "/usr/ccrma/snd/cburns/trumpet1.snd"))
|#