-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimingpoint.go
86 lines (79 loc) · 1.84 KB
/
timingpoint.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package hbxml
// TimingPoint is a struct that represents the timing points of a beatmap
type TimingPoint struct {
Inherited bool `xml:"inherited,attr"`
Offset int `xml:"offset,attr"`
BPM float64 `xml:"bpm,attr"`
SliderMultiplier float64 `xml:"sliderMultiplier,attr"`
TimeSignature int `xml:"timeSignature,attr"`
SampleSet int `xml:"sampleSet,attr"`
CustomSampleSet int `xml:"customSampleSet,attr"`
Volume int `xml:"volume,attr"`
Special bool `xml:"special,attr"`
}
func (b *Beatmap) GetTimingPoint(offset int) *TimingPoint {
for _, tp := range b.TimingPoints {
if tp.Offset == offset {
return &tp
}
}
return nil
}
// BPMs returns a slice of all the BPMs in the beatmap
func (b *Beatmap) BPMs() []float64 {
bpms := make([]float64, 0)
for _, tp := range b.TimingPoints {
if !tp.Inherited {
bpms = append(bpms, tp.BPM)
}
}
return bpms
}
// MedianBPM returns the median BPM of the beatmap
func (b *Beatmap) MedianBPM() float64 {
bpms := b.BPMs()
if len(bpms) == 0 {
return 0
}
return bpms[len(bpms)/2]
}
// AverageBPM returns the average BPM of the beatmap
func (b *Beatmap) AverageBPM() float64 {
bpms := b.BPMs()
if len(bpms) == 0 {
return 0
}
var sum float64
for _, bpm := range bpms {
sum += bpm
}
return sum / float64(len(bpms))
}
// HighestBPM returns the highest BPM of the beatmap
func (b *Beatmap) HighestBPM() float64 {
bpms := b.BPMs()
if len(bpms) == 0 {
return 0
}
highest := bpms[0]
for _, bpm := range bpms {
if bpm > highest {
highest = bpm
}
}
return highest
}
// LowestBPM returns the lowest BPM of the beatmap
func (b *Beatmap) LowestBPM() float64 {
bpms := b.BPMs()
if len(bpms) == 0 {
return 0
}
lowest := bpms[0]
for _, bpm := range bpms {
if bpm < lowest {
lowest = bpm
}
}
return lowest
}