;;; -*- syntax: Lisp; font-size: 16; line-numbers: no; -*-
;;; -*- syntax: common-lisp; package: clm; base: 10; mode:lisp -*-
(load "nrev.scm")

(define *srate* 44100)

(definstrument (space file onset duration &key 
                 (distance-env '(0 1 100 10)) 
                 (amplitude-env '(0 1 100 1))
                 (degree-env '(0 45 50 0 100 90)) 
                 (reverb-amount .01))
  (let* ((beg (floor (* onset *srate*)))
         (end (+ beg (floor (* *srate* duration))))
         (loc (make-locsig :degree 0 :distance 1 :reverb reverb-amount))
         (rdA (make-readin :file file))
         (dist-env (make-env distance-env :duration duration))
         (amp-env (make-env amplitude-env :duration duration))
         (deg-env (make-env (list 0 1 (/ 1.0 90.0) 0 100 (/ 1.0 90.0)) :duration duration))
         (dist-scaler 0.0))
    (do ((i beg (+ i 1)))
        ((= i end))
      (let ((rdval (* (readin rdA) (env amp-env)))
            (degval (env deg-env))
            (distval (env dist-env)))
        (set! dist-scaler (/ 1.0 distval))
        (set! (locsig-ref loc 0) (* (- 1.0 degval) dist-scaler))
        (if (> (mus-channels *output*) 1) (set! (locsig-ref loc 1) (* degval dist-scaler)))
        (when *reverb* (set! (locsig-reverb-ref loc 0) (* reverb-amount (sqrt dist-scaler))))
        (locsig loc i rdval)))))

#|
(with-sound (:output "space.aiff"
             :channels 2
             :scaled-to .99
             :statistics #t
             :srate 44100
             :reverb nrev)
  (space "/Users/danielhensel/1-4.wav" 0 4
         :degree-env '(0 0 1 90)))
|#
