 (defn run-test
   [state]
   (swap! state assoc :processing? true)
-  (let [ctx          (a/audio-context)
-        sample-rate  (.-sampleRate ctx)
-        input-data   ((:data-fn @state) sample-rate)
-        input-buffer (let [buffer (.createBuffer ctx 1
-                                                 (count input-data)
-                                                 sample-rate)
-                           data   (.getChannelData buffer 0)]
-                       (doseq [[i x] (map-indexed vector input-data)]
-                         (aset data i x))
-                       buffer)
-        source       (doto (.createBufferSource ctx)
-                       (aset "buffer" input-buffer))
-        chunks       #js []
-        chunker      (c/audio-chunker {:ctx     ctx
-                                       :samples 4410
-                                       :on-chunk-ready
-                                       (fn [channel chunk]
-                                         (when (= 0 channel)
-                                           (.push chunks chunk)))})
-        dest         (.createMediaStreamDestination ctx)]
+  (let [ctx         (a/audio-context)
+        sample-rate (.-sampleRate ctx)
+        input-data  ((:data-fn @state) sample-rate)
+        chunk-size  (:chunk-size @state)
+        source      (a/create-buffer-source ctx 1 sample-rate
+                                            input-data)
+        chunks      #js []
+        chunker     (c/audio-chunker {:ctx     ctx
+                                      :samples chunk-size
+                                      :on-chunk-ready
+                                      (fn [channel chunk]
+                                        (when (= 0 channel)
+                                          (.push chunks chunk)))})
+        dest        (.createMediaStreamDestination ctx)]
     (c/connect-source chunker source)
     (c/connect-destination chunker dest)
     (set! (.-onended source)

                     :finished?   true
                     :sample-rate sample-rate
                     :input-data  input-data
                     :chunks      (apply concat (js->clj chunks))))))
    (.start source)))

(defn plot-chunker-test
  [state]
  (let [{:keys [processing?
                finished?
                sample-rate
                input-data
                chunks]} @state]
    (cond
      finished?   (plot-buffers (count input-data)
                                (str "Input Data "
                                     "(" (count input-data) " samples)")
                                input-data
                                (str "Concatenated Chunks "
                                     "(" (count chunks) " samples)")
                                chunks)
      processing? (html [:p "Processing..."])
      :else       (html [:button {:on-click #(run-test state)}
                         "Run test"]))))

(ns audio-utils.devcards.audio-chunker
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [devcards.core :refer-macros [defcard deftest dom-node]]
            [goog.string :as gstring]
            [goog.string.format]
            [sablono.core :refer-macros [html]]
            [audio-utils.audio-chunker :as c]
            [audio-utils.web-audio :as a]
