(define *srate* 44100)

(definstrument (variable-filtered-pulses 
    start duration pulse-frequency-env center-frequency-env q-env amplitude-env)
  "clm instrument with variable filtered pulses"
  (let* ((start-sample (* start *srate*))
         (end-sample (+ start-sample (* duration *srate*)))
         (pulse-f-env (make-env :envelope pulse-frequency-env
                                :duration duration
                                :scaler (hz->radians 1)))
         (filter-f-env (make-env :envelope center-frequency-env
                                 :duration duration))
         (filter-q-env (make-env :envelope q-env
                                 :duration duration))
         (amp-env (make-env :envelope amplitude-env
                            :duration duration))
         (pulse (make-pulse-train :frequency 0))
         (resonator (make-formant :frequency 220 :radius 1)))
     (do ((counter  start-sample (+ counter 1)))
         ((> counter end-sample))
          (set! (mus-frequency resonator) (env filter-f-env)) ; change the resonator center frequency
          (set! (mus-scaler resonator) (env filter-q-env)) ; change the resonator radius (q/bandwidth)
          (outa counter (* (env amp-env)
                           (formant resonator (pulse-train pulse (env pulse-f-env))))))))


#|
(with-sound (:srate 44100 :output "variable-filtered-pulses1.aiff") 
(variable-filtered-pulses 0 10 '(0 110 1 440) '(0 880 1 440) '(0 0.1 1 0.99) '(0 0 0.5 1 1 0)))

; woodpecker :=)
(with-sound (:srate 44100 :output "variable-filtered-pulses2.aiff") 
(variable-filtered-pulses 0 10 '(0 10 1 5) '(0 880 1 440) '(0 0.99 1 0.99) '(0 0 0.5 50 1 0)))



