 (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)

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

   result of concatenating all chunks, which, if the audio chunker
   works correctly, should be exactly the same."
  (fn [state owner]
    (chunker-test state))
  {:chunk-size  1000
   :chunks      []
   :input-data  []
   :data-fn     (fn [sample-rate]
                  (sine-wave 10 sample-rate 32768))})
