         [overtone.sc.machinery.server comms connection]
         [overtone.helpers.audio-file]
         [overtone.sc.util :only [id-mapper]]))
+
+(defrecord BufferInfo [id size n-channels rate n-samples rate-scale duration])
 
 (defn buffer-info
   "Fetch the information for buffer associated with buf-id (either an integer or

  (let [path    (resolve-tilde-path path)
        f-ext   (file-extension path)
        arg-map (merge {:n-chans 2
                         :size 65536
                         :header (or f-ext "wav")
                         :samples "int16"}
                        (apply hash-map args))
        {:keys [n-chans size header samples]} arg-map
        buf (buffer size n-chans)]
    (snd "/b_write" (:id buf) path header samples -1 0 1)
    (with-meta
      (assoc buf
        :path path
        :open? (atom true))
      {:type ::buffer-out-stream})))

(derive ::buffer-out-stream ::file-buffer)

(defn buffer-out-stream?

          num-samples                (* n-frames n-chans)
          rate-scale                 (/ rate (server-sample-rate))
          duration                   (/ n-frames rate)]

      (map->BufferInfo
       {:id id
        :size n-frames
        :n-channels n-chans
        :rate rate
        :n-samples num-samples
        :rate-scale rate-scale
        :duration duration}))))

(defrecord Buffer [id size n-channels rate allocated-on-server])

(defn buffer
  "Synchronously allocate a new zero filled buffer for storing audio data with
  the specified size and num-channels."
  ([size] (buffer size 1))
  ([size num-channels]
     (let [id   (with-server-self-sync
                  (fn [uid]
                    (alloc-id :audio-buffer
                              1
                              (fn [id]
                                (snd "/b_alloc" id size num-channels)
                                (server-sync uid)))))
           info (buffer-info id)]

       (map->Buffer
        (assoc info
          :allocated-on-server (atom true))))))

(defrecord BufferFile [id size n-channels rate allocated-on-server path])

(defn buffer-alloc-read
  "Synchronously allocates a buffer with the same number of channels as the
