-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar.go
61 lines (48 loc) · 1.16 KB
/
bar.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
package balafon
import (
"fmt"
"strings"
"time"
"github.com/mgnsk/balafon/internal/constants"
)
// Bar is a single bar of events.
type Bar struct {
Events []Event
timeSig [2]uint8
}
// SetTimeSig sets the timesig for testing.
func (b *Bar) SetTimeSig(num, denom uint8) {
b.timeSig = [2]uint8{num, denom}
}
func (b *Bar) String() string {
var s strings.Builder
s.WriteString(fmt.Sprintf("time: %d/%d", b.timeSig[0], b.timeSig[1]))
if len(b.Events) > 0 {
s.WriteString("\nevents:\n")
for _, ev := range b.Events {
s.WriteString(ev.String())
s.WriteString("\n")
}
}
return s.String()
}
// IsZeroDuration returns whether the bar consists of only zero duration events.
func (b *Bar) IsZeroDuration() bool {
for _, ev := range b.Events {
if ev.Duration > 0 {
return false
}
}
return true
}
// Cap returns the bar's capacity in ticks.
func (b *Bar) Cap() uint32 {
return uint32(b.timeSig[0]) * (uint32(constants.TicksPerWhole) / uint32(b.timeSig[1]))
}
// Duration returns the bar's duration.
func (b *Bar) Duration(tempo float64) time.Duration {
if b.IsZeroDuration() {
return 0
}
return constants.TicksPerQuarter.Duration(tempo, b.Cap())
}