            (throw (js/Error. (str "Can't initialize media device. [" e "]")))))))
     c))
 
-(defn- peak-freq [arr nyquist]
+
+(defn- peak-freq [arr nyquist start-freq min-db]
   (let [buffer-length (.-length arr)
-        freq-to-index #(js/Math.round (* (/ % nyquist) buffer-length))
+        freq-to-index #(-> %
+                           (/ nyquist)
+                           (* buffer-length)
+                           js/Math.round)
         index-to-freq #(* % (/ nyquist buffer-length))]
-    (loop [i (freq-to-index 18500)
+    (loop [i (freq-to-index start-freq)
            index -1
            max (- (.-Infinity js/window))]
       (if (< i buffer-length)

          (if (< max val)
            (recur (inc i) i val)
            (recur (inc i) index max)))
        (when (< -80 max)
          (index-to-freq index))))))

(deftype AudioContext [js-context]
  Object

  (emit-sound [_ freq started-at]
     (let [oscillator (.createOscillator js-context)
           gain-node (.createGain js-context)
           gain (.-gain gain-node)
           duration 0.2
           ramp-duration 0.001]
       (.connect gain-node (.-destination js-context))
       (set! (.-value gain) 0)
       (set! (.-value (.-frequency oscillator)) freq)

  (:require [cljs.core.async :refer [<! put! chan]])
  (:require-macros [cljs.core.async.macros :refer [go]]))


(def audio-context-constructor (or js/window.AudioContext
                                   js/window.webkitAudiocontext))


(defn- <media-stream []
  (let [c (chan)]
    (go
      (let [navigator (.-navigator js/window)
