   (isa? (type s) ::sample))
 
 (defn- free-loaded-sample
-  [[[path args] buf]]
-  (if (server-connected?)
-    (do (buffer-free buf)
-        (dosync (alter loaded-samples*
-                       dissoc
-                       [path args])))))
+  [smpl]
+  (let [path (:path smpl)
+        args (:args smpl)]
+    (if (server-connected?)
+      (do (buffer-free smpl)
+          (swap! cached-samples* dissoc [path args])
+          (swap! loaded-samples* dissoc (:id smpl))))))
 
 (defn free-all-loaded-samples
   "Free all buffers associated with a loaded sample and the memory they
   consume. Also remove each sample from @loaded-samples once freed"
   []
-  (doseq [loaded-sample @loaded-samples*]
+  (doseq [loaded-sample (vals @loaded-samples*)]
     (free-loaded-sample loaded-sample)))
 
 (defn free-sample

      (out out-bus (* vol
                      (scaled-v-disk-in 2 buf rate loop?))))))

(defonce loaded-samples* (ref {}))

(defrecord Sample [id size n-channels rate allocated-on-server path args name])

(defn- load-sample*
  [path & args]
  (let [path (canonical-path path)
        f    (file path)]
    (when-not (.exists f)
      (throw (Exception. (str "Unable to load sample - file does not exist: " path))))
    (let [arg-map  (apply hash-map args)
          f-name   (or (:name args) (.getName f))
          start    (get arg-map :start 0)
          n-frames (get arg-map :size 0)
          buf      (buffer-alloc-read path start n-frames)]
      (let [sample (map->Sample
                    (assoc buf
                      :path path
                      :args args
                      :name f-name))]
        (dosync (alter loaded-samples* assoc [path args] sample))
        sample))))

(defn load-sample
  "Synchronously load a .wav or .aiff file into a memory buffer. Returns

   1]"
  [smpl & pargs] {:pre [(sample? smpl)]}
  (let [{:keys [path args]}     smpl
        {:keys [id n-channels]} (get @cached-samples* [path args])
        [target pos pargs]      (extract-target-pos-args pargs
                                                               (foundation-default-group)
                                                               :tail)]
