 
 ;; A simple object which holds the values necessary to establish a link between an external
 ;; source of MIDI clock messages and the metronome driving the timing of a light show.
-(defrecord ClockSync [metronome midi-clock-source buffer traktor-info] 
+(defrecord ClockSync [metronome midi-clock-source timestamps means traktor-info] 
     IClockSync
     (sync-start [this]
-      (add-synced-metronome midi-clock-source metronome (fn [msg] (sync-handler msg buffer metronome traktor-info))))
+      (add-synced-metronome midi-clock-source metronome
+                            (fn [msg] (sync-handler msg timestamps means metronome traktor-info))))
     (sync-stop [this]
       (remove-synced-metronome midi-clock-source metronome))
     (sync-status [this]
       (dosync
-       (ensure buffer)
+       (ensure timestamps)
        (ensure traktor-info)
-       (let [n (count @buffer)
-             lag (when (pos? n) (- (now) (last @buffer)))
+       (let [n (count @timestamps)
+             lag (when (pos? n) (- (now) (last @timestamps)))
              current (and (= n max-clock-intervals)
                           (< lag 100))
              traktor-current (traktor-beat-phase-current @traktor-info)]


(def ^:private max-clock-intervals
  "How many MIDI clock pulses should be kept around for averaging?"
  12)

(def ^:private max-tempo-taps
  "How many tempo taps should be kept around for averaging?"

  "Handles MIDI communication, including syncing a show metronome to MIDI clock pulses."
  (:require [afterglow.rhythm :as rhythm]
            [amalloy.ring-buffer :refer [ring-buffer]]
            [clojure.math.numeric-tower :as math]
            [overtone.at-at :refer [now]]
            [overtone.midi :as midi]
            [taoensso.timbre :as timbre])
