-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicBeat.go
69 lines (53 loc) · 1.22 KB
/
musicBeat.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gophengine
import (
"log/slog"
"math"
)
// MusicBeat is basically MusicBeatState in vanilla FNF.
type MusicBeat struct {
LastBeat float64
LastStep float64
CurBeat int
CurStep int
StepHitFunc func(int)
BeatHitFunc func(int)
c *Conductor
}
func NewMusicBeat(c *Conductor) *MusicBeat {
return &MusicBeat{
StepHitFunc: func(_ int) {},
BeatHitFunc: func(_ int) {},
c: c,
}
}
func (mb *MusicBeat) Update() {
oldStep := mb.CurStep
mb.UpdateCurStep()
mb.UpdateBeat()
if oldStep != mb.CurStep && mb.CurStep > 0 {
mb.StepHit()
}
}
func (mb *MusicBeat) UpdateBeat() {
mb.CurBeat = int(math.Floor(float64(mb.CurStep) / 4))
}
func (mb *MusicBeat) UpdateCurStep() {
lastChange := BPMChangeEvent{}
for _, bcm := range mb.c.BPMChangeMap {
if mb.c.SongPosition >= bcm.SongTime {
lastChange = bcm
}
}
mb.CurStep = lastChange.StepTime + int(math.Floor((mb.c.SongPosition-lastChange.SongTime)/mb.c.StepCrochet))
}
func (mb *MusicBeat) StepHit() {
if mb.CurStep%4 == 0 {
mb.BeatHit()
}
slog.Debug("[MusicBeat] StepHit", "CurStep", mb.CurStep)
mb.StepHitFunc(mb.CurStep)
}
func (mb *MusicBeat) BeatHit() {
slog.Debug("[MusicBeat] #### BeatHit", "CurBeat", mb.CurBeat)
mb.BeatHitFunc(mb.CurBeat)
}