;; -*- Lisp -*-

;;; Drone instrument

(definstrument Drone (start-time duration frequency amp-1
		      ampfun synth ampat ampdc amtrev deg dis rvibamt rvibfreq)
  (multiple-value-bind (beg end) (get-beg-end start-time duration)
    (let* ((waveform (load-synthesis-table synth (make-table)))
	   (amplitude (* amp-1 .25))
	   (freq-in-Hz (in-Hz frequency))
	   (s (make-table-lookup :frequency frequency :wave-table waveform))
	   (amp-env (make-func :envelope (divseg ampfun 25 (* 100 (/ ampat duration)) 75 (- 100 (* 100 (/ ampdc duration))))
			       :scaler amplitude :start-time start-time :duration duration))
	   (ran-vib (make-randh :frequency rvibfreq 
				:amplitude (* rvibamt freq-in-Hz)))
	   (loc (make-locsig :degree deg :distance dis :revscale amtrev)))
      (Run 
       (loop for i from beg to end do
	 (locsig loc i (* (env amp-env) (table-lookup s (+ (randh ran-vib))))))))))

