Last active
December 20, 2015 21:09
-
-
Save claj/6195310 to your computer and use it in GitHub Desktop.
Find BPM by tapping. Belongs to this blog post: http://multimethods.blogspot.se/2013/08/small-app-for-finding-bpms-by-key-tap.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'seesaw.core) | |
;; the state, when was the last keypress? | |
(def last-tap (atom nil)) | |
(defn nanoseconds-diff-to-bpm [now then] | |
(let [nanosecs-in-a-minute (* 60 1000 1000 1000.0) | |
diff (- now then)] | |
(/ nanosecs-in-a-minute diff))) | |
(def ze-label (label "o")) | |
(def ze-frame (frame :title "BPM-counter" :content ze-label)) | |
(-> ze-frame pack! show!) | |
;; looking for changes of the state, process and presentes the BPM from this info | |
(add-watch last-tap :calculate-bpm-and-put-it-to-label | |
(fn [_ _ last-time now] | |
(if last-time | |
(config! ze-label :text (nanoseconds-diff-to-bpm now last-time))))) | |
;; attempts to put the current time in nanoseconds into the state | |
(defn tap! [] (reset! last-tap (System/nanoTime))) | |
;;when any key is pressed the window triggers fn tap! | |
(listen ze-frame :key-pressed (fn [e] (future (tap!)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment