-
Notifications
You must be signed in to change notification settings - Fork 4
/
lyrics_provider.go
48 lines (39 loc) · 1.07 KB
/
lyrics_provider.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
package metadata
import (
"encoding/json"
"github.com/LogicalOverflow/music-sync/playback"
"github.com/LogicalOverflow/music-sync/util"
"os"
"path/filepath"
)
// LyricsAtom describes a single word/element in the lyrics
type LyricsAtom struct {
Timestamp int64 `json:"timestamp"`
Caption string `json:"caption"`
}
// LyricsLine is a line in the lyrics, composed of LyricsAtoms
type LyricsLine []LyricsAtom
// LyricsProvider is used to get the lyrics for songs
type LyricsProvider interface {
CollectLyrics(song string) []LyricsLine
}
// GetLyricsProvider returns a new LyricsProvider
func GetLyricsProvider() LyricsProvider {
return basicLyricsProvider{}
}
type basicLyricsProvider struct{}
func (basicLyricsProvider) CollectLyrics(song string) []LyricsLine {
path := filepath.Join(playback.AudioDir, song+".json")
if !util.IsFile(path) {
return []LyricsLine{}
}
f, err := os.Open(path)
if err != nil {
return []LyricsLine{}
}
result := make([]LyricsLine, 0)
if err := json.NewDecoder(f).Decode(&result); err != nil {
return []LyricsLine{}
}
return result
}