   [derefed-pattern]
   (log/debug "Resetting pattern" (:name derefed-pattern) "to original synth and params")
   (assoc derefed-pattern :synth (:orig-synth derefed-pattern) :params (:orig-params derefed-pattern) :reset-flag false))
-
-(defn- set-start-time
-  ; Pattern is de-referenced since this function is called by swap!
-  [derefed-pattern time]
-  (assoc derefed-pattern :params (setup-params (:params derefed-pattern) :start-time time)))
 
 
 ;

  (let [val (-> (get-pattern pattern-key)
                deref
                (get-in [:params param-key]))]
    ; FIXME Function current-value isn't quite right since it ignores time
    (get-value nil val nil)))



(defrecord PatternEnv [pattern-key start-time env-stages is-looped]
  PatternParam
  (set-pattern-param [pat-env key val]
    (-> (merge pat-env {key val})
        map->PatternEnv))
  (get-pattern-param [pat-env cur-time]
    (if (some nil? (vals pat-env)) (log/error "PatternEnv has nil values: " pat-env))
    (let [elapsed-time (/ (- cur-time start-time) 1000.0)
          tot-env-time (shp/env-total-dur env-stages)]
      (if (or is-looped (< elapsed-time tot-env-time))
        (shp/signal-at env-stages (mod elapsed-time tot-env-time))
        nil))))

(defn pattern-env [env is-looped] (PatternEnv. nil nil (shp/env-stages env) is-looped))





                     :dur            50
                     :staging-period 2000}}}))
  (start-pattern (ot/now) :gabor1)
  (change-pattern :gabor1 [:freq (pattern-env (ot/envelope [440 880 660] [4 3] :exp) true)])
  (change-pattern :gabor1 [:sustain (pattern-env (ot/envelope [0.005 0.5 0.005] [3 5] :exp) true)])
  (change-pattern :gabor1 [:dur 25])
  (change-pattern :gabor1 [:dur 1000])
  (change-pattern :gabor1 [:sustain (map #(/ % (current-value :gabor1 :freq)) (range 10 0 -1))])
