   (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]
+    (get-value nil val (ot/now))))
+
+
+
+(defrecord PatternEnv [env-stages total-env-time is-looped start-time]
   PatternParam
-  (set-pattern-param [pat-env key val]
-    (-> (merge pat-env {key val})
+  (set-start-time [pat-env time]
+    (-> (assoc pat-env :start-time time)
         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))
+    (log/debug pat-env ", cur-time: " cur-time)
+    (let [elapsed-time (/ (- cur-time start-time) 1000.0)]
+      (if (or is-looped (< elapsed-time total-env-time))
+        (shp/signal-at env-stages (mod elapsed-time total-env-time))
         nil))))
 
-(defn pattern-env [env is-looped] (PatternEnv. nil nil (shp/env-stages env) is-looped))
-
+(defn pattern-env
+  [env is-looped]
+  (let [env-stages (shp/env-stages env)
+        total-env-time (shp/env-total-dur env-stages)]
+    (PatternEnv. env-stages total-env-time is-looped nil)))
+
+(defmethod print-method PatternEnv
+  [pat-env ^java.io.Writer w]
+  (.write w (pr-str "PatternEnv [stages:" (:env-stages pat-env)
+                    "total time:" (:total-env-time pat-env)
+                    "looped:" (:is-looped pat-env)
+                    "start:" (:start-time pat-env)
+                    "]")))
 
 
 

           {:synth  gabor
            :params {:out-bus        0
                     ; :freq           (cycle [440 220])
                     :freq            (pattern-env (ot/envelope [440 880] [4] :exp) false)
                     :sustain        0.1
                     :pan            0.0
                     :amp            0.1

                     :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))])
