Skip to content
This repository
Browse code

Added bar to metronome

  • Loading branch information...
commit 67bd061d31df87376c08aa80d7980cd8350aae18 1 parent 6273f5d
Iain soulflyer authored

Showing 1 changed file with 39 additions and 6 deletions. Show diff stats Hide diff stats

  1. +39 6 src/overtone/music/rhythm.clj
45 src/overtone/music/rhythm.clj
@@ -11,11 +11,19 @@
11 11 (metro-start [metro] [metro start-beat]
12 12 "Returns the start time of the metronome. Also restarts the metronome at
13 13 'start-beat' if given.")
  14 + (metro-bar-start [metro] [metro start-bar])
14 15 (metro-tick [metro]
15 16 "Returns the duration of one metronome 'tick' in milleseconds.")
  17 + (metro-tock [metro]
  18 + "Returns the duration of one bar in milliseconds.")
16 19 (metro-beat [metro] [metro beat]
17 20 "Returns the next beat number or the timestamp (in milliseconds) of the
18 21 given beat.")
  22 + (metro-bar [metro] [metro bar]
  23 + "Returns the next bar number or the timestamp (in milliseconds) of the
  24 + given bar")
  25 + (metro-bpb [metro] [metro new-bpb]
  26 + "Get the current beats per bar or change it to new-bpb")
19 27 (metro-bpm [metro] [metro new-bpm]
20 28 "Get the current bpm or change the bpm to 'new-bpm'."))))
21 29
@@ -37,7 +45,7 @@
37 45 ; ([] (bar 1))
38 46 ; ([b] (* (bar 1) (first @*signature) b)))
39 47
40   -(deftype Metronome [start bpm]
  48 +(deftype Metronome [start bar-start bpm bpb]
41 49
42 50 IMetronome
43 51 (metro-start [metro] @start)
@@ -45,17 +53,36 @@
45 53 (let [new-start (- (now) (* start-beat (metro-tick metro)))]
46 54 (reset! start new-start)
47 55 new-start))
  56 + (metro-bar-start [metro] @bar-start)
  57 + (metro-bar-start [metro start-bar]
  58 + (let [new-bar-start (- (now) (* start-bar (metro-tock metro)))]
  59 + (reset! bar-start new-bar-start)
  60 + new-bar-start))
48 61 (metro-tick [metro] (beat-ms 1 @bpm))
  62 + (metro-tock [metro] (beat-ms @bpb @bpm))
49 63 (metro-beat [metro] (inc (long (/ (- (now) @start) (metro-tick metro)))))
50 64 (metro-beat [metro b] (+ (* b (metro-tick metro)) @start))
  65 + (metro-bar [metro] (inc (long (/ (- (now) @bar-start) (metro-tock metro)))))
  66 + (metro-bar [metro b] (+ (* b (metro-tock metro)) @bar-start))
51 67 (metro-bpm [metro] @bpm)
52 68 (metro-bpm [metro new-bpm]
53   - (let [cur-beat (metro-beat metro)
54   - new-tick (beat-ms 1 new-bpm)
55   - new-start (- (metro-beat metro cur-beat) (* new-tick cur-beat))]
  69 + (let [cur-beat (metro-beat metro)
  70 + cur-bar (metro-bar metro)
  71 + new-tick (beat-ms 1 new-bpm)
  72 + new-tock (* @bpb new-tick)
  73 + new-start (- (metro-beat metro cur-beat) (* new-tick cur-beat))
  74 + new-bar-start (- (metro-bar metro cur-bar) (* new-tock cur-bar))]
56 75 (reset! start new-start)
  76 + (reset! bar-start new-bar-start)
57 77 (reset! bpm new-bpm))
58 78 [:bpm new-bpm])
  79 + (metro-bpb [metro] @bpb)
  80 + (metro-bpb [metro new-bpb]
  81 + (let [cur-bar (metro-bar metro)
  82 + new-tock (beat-ms new-bpb @bpm)
  83 + new-bar-start (- (metro-bar metro cur-bar) (* new-tock cur-bar))]
  84 + (reset! bar-start new-bar-start)
  85 + (reset! bpb new-bpb)))
59 86
60 87 clojure.lang.ILookup
61 88 (valAt [this key] (.valAt this key nil))
@@ -79,6 +106,10 @@
79 106 with no arguments to get the next beat number, or pass it a beat number
80 107 to get the timestamp to play a note at that beat.
81 108
  109 + Metronome also works with bars. Set the number of beats per bar using
  110 + metro-bpb (defaults to 4). metro-bar returns a timestamp that can be used
  111 + to play a note relative to a specified bar.
  112 +
82 113 (def m (metronome 128))
83 114 (m) ; => <next beat number>
84 115 (m 200) ; => <timestamp of beat 200>
@@ -86,8 +117,10 @@
86 117 (m :bpm 140) ; => set bpm to 140"
87 118 [bpm]
88 119 (let [start (atom (now))
89   - bpm (atom bpm)]
90   - (Metronome. start bpm)))
  120 + bar-start (atom @start)
  121 + bpm (atom bpm)
  122 + bpb (atom 4)]
  123 + (Metronome. start bar-start bpm bpb)))
91 124
92 125 ;== Grooves
93 126 ;

0 comments on commit 67bd061

Please sign in to comment.
Something went wrong with that request. Please try again.