Permalink
Browse files

Added bar to metronome

  • Loading branch information...
1 parent 6273f5d commit 67bd061d31df87376c08aa80d7980cd8350aae18 @soulflyer soulflyer committed Aug 20, 2012
Showing with 39 additions and 6 deletions.
  1. +39 −6 src/overtone/music/rhythm.clj
View
45 src/overtone/music/rhythm.clj
@@ -11,11 +11,19 @@
(metro-start [metro] [metro start-beat]
"Returns the start time of the metronome. Also restarts the metronome at
'start-beat' if given.")
+ (metro-bar-start [metro] [metro start-bar])
(metro-tick [metro]
"Returns the duration of one metronome 'tick' in milleseconds.")
+ (metro-tock [metro]
+ "Returns the duration of one bar in milliseconds.")
(metro-beat [metro] [metro beat]
"Returns the next beat number or the timestamp (in milliseconds) of the
given beat.")
+ (metro-bar [metro] [metro bar]
+ "Returns the next bar number or the timestamp (in milliseconds) of the
+ given bar")
+ (metro-bpb [metro] [metro new-bpb]
+ "Get the current beats per bar or change it to new-bpb")
(metro-bpm [metro] [metro new-bpm]
"Get the current bpm or change the bpm to 'new-bpm'."))))
@@ -37,25 +45,44 @@
; ([] (bar 1))
; ([b] (* (bar 1) (first @*signature) b)))
-(deftype Metronome [start bpm]
+(deftype Metronome [start bar-start bpm bpb]
IMetronome
(metro-start [metro] @start)
(metro-start [metro start-beat]
(let [new-start (- (now) (* start-beat (metro-tick metro)))]
(reset! start new-start)
new-start))
+ (metro-bar-start [metro] @bar-start)
+ (metro-bar-start [metro start-bar]
+ (let [new-bar-start (- (now) (* start-bar (metro-tock metro)))]
+ (reset! bar-start new-bar-start)
+ new-bar-start))
(metro-tick [metro] (beat-ms 1 @bpm))
+ (metro-tock [metro] (beat-ms @bpb @bpm))
(metro-beat [metro] (inc (long (/ (- (now) @start) (metro-tick metro)))))
(metro-beat [metro b] (+ (* b (metro-tick metro)) @start))
+ (metro-bar [metro] (inc (long (/ (- (now) @bar-start) (metro-tock metro)))))
+ (metro-bar [metro b] (+ (* b (metro-tock metro)) @bar-start))
(metro-bpm [metro] @bpm)
(metro-bpm [metro new-bpm]
- (let [cur-beat (metro-beat metro)
- new-tick (beat-ms 1 new-bpm)
- new-start (- (metro-beat metro cur-beat) (* new-tick cur-beat))]
+ (let [cur-beat (metro-beat metro)
+ cur-bar (metro-bar metro)
+ new-tick (beat-ms 1 new-bpm)
+ new-tock (* @bpb new-tick)
+ new-start (- (metro-beat metro cur-beat) (* new-tick cur-beat))
+ new-bar-start (- (metro-bar metro cur-bar) (* new-tock cur-bar))]
(reset! start new-start)
+ (reset! bar-start new-bar-start)
(reset! bpm new-bpm))
[:bpm new-bpm])
+ (metro-bpb [metro] @bpb)
+ (metro-bpb [metro new-bpb]
+ (let [cur-bar (metro-bar metro)
+ new-tock (beat-ms new-bpb @bpm)
+ new-bar-start (- (metro-bar metro cur-bar) (* new-tock cur-bar))]
+ (reset! bar-start new-bar-start)
+ (reset! bpb new-bpb)))
clojure.lang.ILookup
(valAt [this key] (.valAt this key nil))
@@ -79,15 +106,21 @@
with no arguments to get the next beat number, or pass it a beat number
to get the timestamp to play a note at that beat.
+ Metronome also works with bars. Set the number of beats per bar using
+ metro-bpb (defaults to 4). metro-bar returns a timestamp that can be used
+ to play a note relative to a specified bar.
+
(def m (metronome 128))
(m) ; => <next beat number>
(m 200) ; => <timestamp of beat 200>
(m :bpm) ; => return the current bpm val
(m :bpm 140) ; => set bpm to 140"
[bpm]
(let [start (atom (now))
- bpm (atom bpm)]
- (Metronome. start bpm)))
+ bar-start (atom @start)
+ bpm (atom bpm)
+ bpb (atom 4)]
+ (Metronome. start bar-start bpm bpb)))
;== Grooves
;

0 comments on commit 67bd061

Please sign in to comment.