This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrc.go
79 lines (64 loc) · 1.45 KB
/
lrc.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
package main
import (
"strings"
)
type Lyric struct {
Timestamp float64
Content string
}
type Info map[string]string
type Lrc struct {
Info Info
Lyrics []Lyric
lrcType LyricType
}
func (lrc *Lrc) Parse(text string) {
lines := strings.Split(text, "\n")
lyrics := make([]Lyric, 0)
info := make(Info)
lrc.lrcType = LyricSynced
for _, line := range lines {
parsedLine := parseLine(line)
switch parsedLine := parsedLine.(type) {
case InfoLine:
info[parsedLine.Key] = parsedLine.Value
case TimeLine:
for _, timestamp := range parsedLine.Timestamps {
lyrics = append(lyrics, Lyric{Timestamp: timestamp, Content: parsedLine.Content})
}
case InvalidLine:
lyrics = append(lyrics, Lyric{Timestamp: 0, Content: line})
lrc.lrcType = LyricUnSynced
}
}
lrc.Info = info
lrc.Lyrics = lyrics
}
func (lrc *Lrc) Offset(offsetTime float64) {
for i := range lrc.Lyrics {
lrc.Lyrics[i].Timestamp += offsetTime
if lrc.Lyrics[i].Timestamp < 0 {
lrc.Lyrics[i].Timestamp = 0
}
}
}
func (lrc *Lrc) Clone() *Lrc {
clonedLrc := &Lrc{
Info: make(Info),
Lyrics: make([]Lyric, len(lrc.Lyrics)),
lrcType: lrc.lrcType,
}
for key, value := range lrc.Info {
clonedLrc.Info[key] = value
}
copy(clonedLrc.Lyrics, lrc.Lyrics)
return clonedLrc
}
func (lrc *Lrc) ToString() string {
content := ""
for _, lrcLine := range lrc.Lyrics {
content += lrcLine.Content + "\n"
}
content = strings.TrimSpace(content)
return content
}