-
Notifications
You must be signed in to change notification settings - Fork 2
/
beat.coffee
42 lines (34 loc) · 958 Bytes
/
beat.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class @Beat
constructor: (@bpm, @perMeasure) ->
@bps = @bpm/60
@startedAt = now()
@update()
@started = no
start: ->
@started = yes
@startedAt = now()
@update()
beat = 0
setTimeout =>
@intervals = [
accurateInterval 1000 / @bps, => stage.onBeat @beat()
accurateInterval 1000 * @perMeasure / @bps, => stage.onMeasure()
]
, 20
this
update: ->
@lastFrame = @now
@now = now()
@elapsed = @now - @startedAt
this
# Return the integer beat number zero-indexed of the current measure.
beat: ->
Math.floor (@elapsed * @bps) % @perMeasure
# Return how done we are with the current beat. 0.0 - 1.0
beatProgress: ->
(@elapsed * @bps) % 1
# Return how done we are with the current measure. 0.0 - 1.0
measureProgress: ->
((@elapsed * @bps) % @perMeasure) / @perMeasure
frameTime: ->
@now - @lastFrame