;;;
;;; An amplitude modulation instrument with wavetables
;;; (nando, Oct 14 '96)
;;; (nando, Jan 8 '01 -> clm2)

(definstrument amw (start-time duration carrier-freq modulator-freq amplitude
		   &key
		   (carrier-spectr '(1 0.5 2 0.2 5 0.1))
		   (modulator-spectr '(1 0.5 3 0.2 4 0.1))
		   (amp-env '(0 0 0.5 1 1 0)))
  (multiple-value-bind (beg end) (times->samples start-time duration)
    (let* ((wave-c (partials->wave carrier-spectr))
           (carrier (make-table-lookup :frequency carrier-freq :wave wave-c))
	   (wave-m (partials->wave modulator-spectr))
           (modulator (make-table-lookup :frequency modulator-freq :wave wave-m))
	   (envelope (make-env :envelope amp-env 
			       :scaler amplitude
			       :duration duration)))
      (run
       (loop for i from beg to end do
	     (outa i (* (env envelope)
			(* (table-lookup carrier)(+ 1.0 (table-lookup modulator))))))))))

;;; (with-sound() (loop for time from 0 by 1.1 
;;;		    for f from 10 by 10 below 150 
;;;		    with ae = '(0 0 0.1 1 0.9 1 1 0) do
;;;		    (amw time 1.2 440 f 0.1 :amp-env ae)))

