-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.go
203 lines (176 loc) · 3.92 KB
/
chart.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package ChartoGopher
import (
"fmt"
"sync"
)
type Chart interface {
AddTimeSignatureChange(numerator int, denominator int, position int) error
AddTempoChange(bpm int, position int)
AddTrack(track *Track)
Write(writer Writer) (int, error)
GetSongInfoMap() (info map[string]interface{})
GetTracks() []*Track
GetSyncProperties() (properties []SyncProperty)
GetEvents() []event
}
type SongInfo struct {
//Song info
SongName string
Artist string
Charter string
Album string
Year string
Offset int
Resolution int
Player2 string
Difficulty int
PreviewStart int
PreviewEnd int
Length int
Genre string
MediaType string
//Music file associated with this chart.
MusicStream string
}
type chart struct {
SongInfo SongInfo
SyncTrack syncTrack
Tracks []*Track
Events []event
}
func NewChart(songInfo SongInfo, bpm int, timeSigNumerator int, timeSigDenominator int) (*chart, error) {
err := validateDenominator(timeSigDenominator, 0)
timeSig := []timeSignature{
{
Numerator: timeSigNumerator,
Denominator: timeSigDenominator,
Position: 0,
},
}
tempo := []tempo{
{
Bpm: bpm,
Position: 0,
},
}
return &chart{
Tracks: make([]*Track, 0),
Events: make([]event, 0),
SongInfo: songInfo,
SyncTrack: syncTrack{
TimeSignatures: timeSig,
Tempos: tempo,
},
}, err
}
func (c *chart) AddTimeSignatureChange(numerator int, denominator int, position int) (err error) {
err = validateDenominator(denominator, position)
if err != nil {
return
}
c.SyncTrack.TimeSignatures = append(c.SyncTrack.TimeSignatures, timeSignature{
Numerator: numerator,
Denominator: denominator,
Position: position,
})
return
}
func (c *chart) AddTempoChange(bpm int, position int) {
c.SyncTrack.Tempos = append(c.SyncTrack.Tempos, tempo{
Bpm: bpm,
Position: position,
})
}
func (c *chart) AddTrack(track *Track) {
c.Tracks = append(c.Tracks, track)
}
func (c *chart) Write(writer Writer) (int, error) {
c.setDefaults()
return writer.Write(c)
}
func (c *chart) setDefaults() {
info := &c.SongInfo
if info.Resolution == 0 {
info.Resolution = 192
}
}
func (c chart) GetSongInfoMap() (info map[string]interface{}) {
songInfo := c.SongInfo
info = map[string]interface{}{
"Offset": songInfo.Offset,
"Resolution": songInfo.Resolution,
"Difficulty": songInfo.Difficulty,
"PreviewStart": songInfo.PreviewStart,
"PreviewEnd": songInfo.PreviewEnd,
}
if songInfo.SongName != "" {
info["Name"] = songInfo.SongName
}
if songInfo.Artist != "" {
info["Artist"] = songInfo.Artist
}
if songInfo.Charter != "" {
info["Charter"] = songInfo.Charter
}
if songInfo.Album != "" {
info["Album"] = songInfo.Album
}
if songInfo.Year != "" {
info["Year"] = songInfo.Year
}
if songInfo.Player2 != "" {
info["Player2"] = songInfo.Player2
}
if songInfo.Length != 0 {
info["Length"] = songInfo.Length
}
if songInfo.Genre != "" {
info["Genre"] = songInfo.Genre
}
if songInfo.MediaType != "" {
info["MediaType"] = songInfo.MediaType
}
if songInfo.MusicStream != "" {
info["MusicStream"] = songInfo.MusicStream
}
return
}
func (c chart) GetSyncProperties() (properties []SyncProperty) {
properties = make([]SyncProperty, 0)
ch := make(chan SyncProperty)
var wg sync.WaitGroup
wg.Add(2)
go func() {
wg.Wait()
close(ch)
}()
go func() {
defer wg.Done()
for _, v := range c.SyncTrack.Tempos {
ch <- v
}
}()
go func() {
defer wg.Done()
for _, v := range c.SyncTrack.TimeSignatures {
ch <- v
}
}()
for prop := range ch {
properties = append(properties, prop)
}
return
}
func (c chart) GetTracks() []*Track {
return c.Tracks
}
func (c *chart) AddEvent(time int, eventType EventType, comments ...string) {
c.Events = append(c.Events, event{
Time: time,
Event: eventType,
Comment: fmt.Sprint(comments),
})
}
func (c chart) GetEvents() []event {
return c.Events
}