;;; Moving a source using dlocsig

(definstrument move (start-time file
				&key 
				(amp 1.0)
				(path (make-path :path '(-10 10 5 0 10 10)))
				(reverb-amount 0.01))
  (let* ((in-file (open-input file))
	 (in-file-dur (snd-duration in-file))
	 (in-file-sr (snd-srate in-file))
	 (in-file-chans (channels in-file))
	 (reader (make-readin :file in-file)))
    ;; warn user if input file not mono
    (if (/= in-file-chans 1)
	(warn "input file not mono (~s channels!)" in-file-chans))
    ;; warn user if sample rates don't match
    (if (/= in-file-sr sampling-rate)
	(warn "input file sampling rate ~s does not match output sampling rate ~s"
	      in-file-sr sampling-rate))
    ;; create the dlocsig unit generator
    (multiple-value-bind (dloc dbeg dend)
	(make-dlocsig :path path
		      :start-time start-time
		      :duration in-file-dur
		      :reverb-amount reverb-amount
		      :scaler amp)
      (run
       (loop for i from dbeg to dend do
	 (dlocsig dloc i (readin reader))))
      (close-input in-file))))


;;; Wrap a moving sound instrument around arbitrary clm code

(defmacro move-sound ((&key 
		       (path nil)
		       (srate sampling-rate)
		       (channels nil)) &body body)
  (if path
      `(sound-let ((to-move (:srate ,srate 
				    :channels (if ,channels ,channels 1)
				    :reverb dlocnrev
				    :decay-time *reverb-decay-time*) 
			    ,.body))
		  (format t "; moving sound...~%")
		  (move 0 to-move :path ,path))
    body))

#|
(with-sound(:channels 4 :srate 44100 :statistics t)
  (move-sound (:path (make-path :path '(-10 10 5 0 10 10)) :srate 44100)
	      (grani 0 10 5 "/usr/ccrma/web/CCRMA/Courses/SoundSynthesis/Lectures/4/Sounds/small-gong.snd")))
|#
