   (generate-output-sample [this channel input-sample]
     (queue-in-buffer this channel input-sample)
     (add-to-rms this channel input-sample)
-    (let [threshold (db->amplitude (:threshold config))]
+    (let [threshold (db->amplitude (:threshold config))
+          hold      (time->samples (:hold config) (:sample-rate config))]
       (if (>= (calculate-rms this channel) threshold)
-        (dequeue-from-buffer this channel)
-        0.0)))
+        (do
+          (>> samples-held 0)
+          (dequeue-from-buffer this channel))
+        (if (<= (<< samples-held) hold)
+          (do
+            (aswap! samples-held inc)
+            input-sample)
+          0.0))))
 
   (add-to-rms [this channel sample]
     (aswap! rms-buffers update channel
             (fn [buffer]
-              (let [window (max (time->samples (:rms-window config)
-                                               (:sample-rate config))
-                                2)
+              (let [window (max 2 (time->samples (:rms-window config)
+                                                 (:sample-rate config)))
                     buffer (or buffer (rms/rms-buffer window))]
                 (rms/rms-push buffer sample)))))
 
   (calculate-rms [this channel]
-    (rms/rms ((aderef rms-buffers) channel)))
+    (rms/rms ((<< rms-buffers) channel)))
 
   (queue-in-buffer [this channel sample]
     (aswap! buffers update channel
             (fn [buffer]
-              (let [size   (max (time->samples (:look-ahead config)
-                                               (:sample-rate config))
-                                2)
+              (let [size   (max 2 (time->samples (:look-ahead config)
+                                                 (:sample-rate config)))
                     buffer (or buffer (rb/ring-buffer size))]
                 (conj buffer sample)))))
 
   (dequeue-from-buffer [this channel]
-    (let [buffer ((aderef buffers) channel)
+    (let [buffer ((<< buffers) channel)
           sample (peek buffer)]
       (pop buffer)
       sample)))

  (queue-in-buffer [this channel sample])
  (dequeue-from-buffer [this channel]))

(defrecord Gate [config next buffers rms-buffers]
  w/IWorkerAudioNode
  (connect [this destination]
    (reset! next destination))

(ns audio-utils.gate
  (:require [audio-utils.ring-buffer :as rb]
            [audio-utils.rms-buffer :as rms]
            [audio-utils.util :refer [aatom << >> aswap!
                                      db->amplitude time->samples]]
            [audio-utils.worker :as w]))

