
(define *srate* 44100)
(definstrument (fm-envelopes 
    start-time duration carrier-envelope modulator-envelope index-envelope amplitude-envelope)
  (let* ((start-sample (* start-time *srate*))
	 (end-sample (+ start-sample (* duration *srate*)))
	 (carrier (make-oscil :frequency 0))
	 (modulator (make-oscil :frequency 0))
	 (carrier-env (make-env :duration duration 
				:envelope carrier-envelope 
				:scaler (hz->radians 1)))
	 (modulator-env (make-env :duration duration 
				  :envelope modulator-envelope 
				  :scaler (hz->radians 1)))
	 (index-env (make-env :duration duration 
			      :envelope index-envelope))
	 (carrier-frequency 0.0)
	 (modulator-frequency 0.0)
	 (amp-env (make-env :duration duration 
			    :envelope amplitude-envelope)))
      (do ((sample-number  start-sample (+ sample-number  1)))
         ((> sample-number  end-sample))
	  (set! carrier-frequency (env carrier-env))
	  (set! modulator-frequency (env modulator-env))
	  (outa sample-number (* (env amp-env)
				 (oscil carrier (+ carrier-frequency  
						   (* (env index-env)
						      modulator-frequency
						      (oscil modulator modulator-frequency)))))))))

#|
(with-sound (:srate 44100 :statistics #t) 
(fm-envelopes 0 10 
	      '(0 440 0.5 440 1 220) 
	      '(0 220 0.3 220 0.5 110 1 130) 
	      '(0 0 0.1 4 0.5 2 1 1) 
	      '(0 0 0.5 1 1 0)))

(with-sound (:srate 44100 :statistics #t) 
(fm-envelopes 0 10 
	      '(0 440 1 440) 
	      '(0 230 0.5 220 1 220) 
	      '(0 4 1 0) 
	      '(0 0 0.5 1 1 0)))

(with-sound (:srate 44100 :statistics #t) 
  (fm-envelopes 0 10 
		'(0 440 1 440) 
		'(0 330 1 330) 
		'(0 4 1 0) 
		'(0 0 0.5 1 1 0)))
|#