/
usdx.go
330 lines (306 loc) · 7.25 KB
/
usdx.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package usdx
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"github.com/Patagonicus/usdx-reader/pkg/encoding"
"go.uber.org/zap"
)
// tags understood by USDX
var usdxTags = map[string]bool{
"TITLE": true,
"ARTIST": true,
"MP3": true,
"BPM": true,
"GAP": true,
"COVER": true,
"BACKGROUND": true,
"VIDEO": true,
"VIDEOGAP": true,
"GENRE": true,
"EDITION": true,
"CREATOR": true,
"LANGUAGE": true,
"YEAR": true,
"START": true,
"END": true,
"RESOLUTION": true,
"NOTESGAP": true,
"RELATIVE": true,
"ENCODING": true,
"PREVIEWSTART": true,
"MEDLEYSTARTBEAT": true,
"MEDLEYENDBEAT": true,
"CALCMEDLEY": true,
"DUETSINGERP1": true,
"DUETSINGERP2": true,
"P1": true,
"P2": true,
}
type Reader struct {
encodings map[string]Encoding
defaultEncoding Encoding
l *zap.Logger
}
func NewReader(l *zap.Logger) Reader {
m := make(map[string]Encoding)
for _, enc := range []Encoding{encoding.Auto, encoding.UTF8, encoding.CP1250, encoding.CP1252} {
name := enc.Name()
if _, ok := m[name]; ok {
l.Warn("duplicate encoding",
zap.String("name", name),
)
}
m[name] = enc
}
return Reader{
encodings: m,
defaultEncoding: encoding.Auto,
l: l,
}
}
func (r Reader) Read(in io.ReadSeeker, dir, sourceFile string) (Song, []error, error) {
l := r.l.With(
zap.String("dir", dir),
zap.String("sourceFile", sourceFile),
)
song := Song{
Dir: dir,
SourceFile: sourceFile,
Encoding: r.defaultEncoding,
Resolution: 4,
CalcMedley: true,
}
hasBOM, err := checkBOM(in)
if err != nil {
l.Warn("error detecting byte order mark",
zap.Error(err),
)
return song, nil, err
}
if hasBOM {
l.Debug("detected BOM, setting encoding to UTF8")
song.Encoding = encoding.UTF8
}
var warnings []error
seen := make(map[string]bool)
scanner := bufio.NewScanner(in)
for scanner.Scan() {
line := scanner.Text()
if !isTag(line) {
break
}
tag, value, err := getTagAndValue(line, song.Encoding)
if err != nil {
l.Warn("error decoding line",
zap.Error(err),
)
return song, warnings, err
}
if seen[tag] && usdxTags[tag] {
warnings = append(warnings, fmt.Errorf("duplicate tag '%v'", tag))
}
seen[tag] = true
switch tag {
case "DUETSINGERP1":
seen["P1"] = true
case "DUETSINGERP2":
seen["P2"] = true
case "P1":
seen["DUETSINGERP1"] = true
case "P2":
seen["DUETSINGERP2"] = true
}
err = nil
switch tag {
case "TITLE":
song.Title = value
case "ARTIST":
song.Artist = value
case "MP3":
song.SoundFile = value
case "BPM":
// I don't know why, but USDX replaces all commas before calling the parse method
song.BPM, err = parseFloat32I18n(strings.Replace(value, ",", ".", -1))
case "GAP":
song.Gap, err = parseFloat32I18n(value)
case "COVER":
song.CoverPath = value
case "BACKGROUND":
song.BackgroundPath = value
case "VIDEO":
song.VideoPath = value
case "VIDEOGAP":
song.VideoGap, err = parseFloat32I18n(value)
case "GENRE":
song.Genre = value
case "EDITION":
song.Edition = value
case "CREATOR":
song.Creator = value
case "LANGUAGE":
song.Language = value
case "YEAR":
// for some reason some files have a year tag with no value. Suppress that error.
if value != "" {
song.Year, err = parseInt(value)
}
case "START":
song.Start, err = parseFloat32I18n(value)
case "END":
song.End, err = parseInt(value)
case "RESOLUTION":
song.Resolution, err = parseInt(value)
case "NOTESGAP":
song.NotesGap, err = parseInt(value)
case "RELATIVE":
if strings.ToUpper(value) == "YES" {
song.Relative = true
}
case "ENCODING":
enc, ok := r.encodings[value]
if ok {
song.Encoding = enc
} else {
l.Warn("unknown encoding, keeping current on",
zap.String("name", value),
zap.String("current encoding", song.Encoding.Name()),
)
}
case "PREVIEWSTART":
song.PreviewStart, err = parseFloat32I18n(value)
case "MEDLEYSTARTBEAT":
if song.Relative {
l.Warn("ignoring medley start beat because relative is set",
zap.String("value", value),
)
} else {
song.MedleyStartBeat, err = parseInt(value)
}
case "MEDLEYENDBEAT":
if song.Relative {
l.Warn("ignoring medley end beat because relative is set",
zap.String("value", value),
)
} else {
song.MedleyEndBeat, err = parseInt(value)
}
case "CALCMEDLEY":
if strings.ToUpper(value) == "OFF" {
song.CalcMedley = false
}
case "DUETSINGERP1", "P1":
song.DuetSingerP1 = value
case "DUETSINGERP2", "P2":
song.DuetSingerP2 = value
default:
l.Warn("unknown tag",
zap.String("tag", tag),
zap.String("value", value),
)
song.CustomTags = append(song.CustomTags, Tag{
Tag: tag,
Content: value,
})
}
if err != nil {
l.Warn("failed to parse tag",
zap.String("tag", tag),
zap.String("value", value),
zap.Error(err),
)
warnings = append(warnings, fmt.Errorf("error adding tag '%v': %v", tag, err.Error()))
}
}
if scanner.Err() != nil {
return song, warnings, scanner.Err()
}
song.Notes = append(song.Notes, scanner.Text())
for scanner.Scan() {
song.Notes = append(song.Notes, scanner.Text())
}
return song, warnings, scanner.Err()
}
func isTag(line string) bool {
return strings.HasPrefix(line, "#")
}
func getTagAndValue(line string, decoder Encoding) (string, string, error) {
line = strings.TrimLeft(line, "#")
var tag, value string
sep := strings.IndexRune(line, ':')
if sep < 0 {
value = line
} else {
tag, value = line[:sep], line[sep+1:]
}
value, err := decoder.Decode(value)
return tag, value, err
}
func parseFloat32I18n(s string) (float32, error) {
// this is how USDX does it
val, err := strconv.ParseFloat(strings.Replace(trim(s), ",", ".", 1), 32)
return float32(val), err
}
func parseInt(s string) (int, error) {
return strconv.Atoi(trim(s))
}
func trim(s string) string {
return strings.TrimFunc(s, func(r rune) bool {
return unicode.IsSpace(r)
})
}
func checkBOM(r io.ReadSeeker) (bool, error) {
var bom [3]byte
_, err := io.ReadFull(r, bom[:])
if err != nil {
return false, err
}
if bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf {
return true, nil
}
_, err = r.Seek(0, io.SeekStart)
return false, err
}
type Song struct {
Dir string
SourceFile string
Title string
Artist string
SoundFile string
BPM float32
Gap float32
CoverPath string
BackgroundPath string
VideoPath string
VideoGap float32
Genre string
Edition string
Creator string
Language string
Year int
Start float32
End int
Resolution int
NotesGap int
Relative bool
Encoding Encoding
PreviewStart float32
MedleyStartBeat int
MedleyEndBeat int
CalcMedley bool
DuetSingerP1 string
DuetSingerP2 string
CustomTags []Tag
Notes []string
}
type Tag struct {
Tag string
Content string
}
type Encoding interface {
Name() string
Decode(s string) (string, error)
}