   ([]
    (sync-to-midi-clock nil))
   ([name-filter]
-   (open-inputs-if-needed!)
-   (let [clock-sources (atom #{})
-         clock-finder (fn [msg]
-                        (when (= (:status msg) :timing-clock)
-                          (swap! clock-sources conj (:device msg))))]
-     (add-global-handler! clock-finder)
+   (let [clock-finder (watch-for-clock-sources)]
      (Thread/sleep 300)
-     (remove-global-handler! clock-finder)
-     (let [result (filter-devices @clock-sources name-filter)]
+     (let [result (filter-devices (finder-current-sources clock-finder) name-filter)]
+       (finder-finished clock-finder)
        (case (count result)
          0 (throw (IllegalArgumentException. (str "No MIDI clock sources " (describe-name-filter name-filter)
                                                   "were found.")))

      (filter #(or (re-find pattern (:name %1))
                   (re-find pattern (:description %1)))
              devices))))

(defn sync-to-midi-clock
  "Returns a sync function that will cause the beats-per-minute

(defn create-midi-port-filter
  "Return a filter which selects midi inputs and outputs we actually
  want to use. If this is a Mac, and the Humatic MIDI extension has
  been installed, create a filter which will accept only its versions
  of ports which it offers in parallel with the standard
  implementation. Otherwise, return a filter which accepts all ports."
  []
  (if (and (mac?) (mmj-installed?))
    (fn [device]
      (let [mmj-descriptions (set (map :description (filter #(mmj-device? %)
                                                            (overtone.midi/midi-sources))))]
        (or (mmj-device? device)
            ;; MMJ returns devices whose descriptions match the names of the
            ;; broken devices returned by the broken Java MIDI implementation.
            (not (mmj-descriptions (:name device))))))
    identity))

;; TODO: This and open-outputs will have to change once hot-plugging works.
