/
songs.go
109 lines (76 loc) · 1.7 KB
/
songs.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
package main
import (
"path/filepath"
"sync"
"strings"
"github.com/ascherkus/go-id3/src/id3"
"os"
"log"
"time"
)
type SongRecord struct {
fullpath string
filetype string
title string
artist string
}
type Playlist struct {
path string
modtime time.Time
subdirs bool
}
var Playlists []*Playlist
func getSongs() []SongRecord {
addfilechannel := make(chan SongRecord, 100)
var w sync.WaitGroup
for _, playlistRec := range(IcerayCfg.Playlist) {
fext := strings.ToLower(filepath.Ext(playlistRec.Path))
if fext == ".xspf" {
// process XML playlist file
} else if fext == ".m3u" {
// process m3u playlist file
} else {
w.Add(1)
go folderSearch(playlistRec.Path, playlistRec.Subdirs, addfilechannel, &w)
}
}
// wait for song search(es) to finish up
w.Wait()
close(addfilechannel)
var songs []SongRecord
for mfile := range addfilechannel {
// Read in MP3 info and save it
fd,err := os.Open(mfile.fullpath)
defer fd.Close()
if err != nil {
log.Println("Problem opening: " + mfile.fullpath)
continue
}
// Read in MP3 tags
mp3tags := id3.Read(fd)
if ( mp3tags == nil ) {
log.Println("Problems getting MP3 tags for " + mfile.fullpath)
continue
}
if mp3tags.Artist == "" {
log.Println("Artist tag missing for " + mfile.fullpath)
continue
}
if mp3tags.Name == "" {
log.Println("Song tag missing for " + mfile.fullpath)
continue
}
mfile.artist = mp3tags.Artist
mfile.title = mp3tags.Name
songs = append(songs,mfile)
}
if IcerayCfg.Music.Shuffle {
// Now (linear) shuffle it
sc := len(songs)
for i := range(songs) {
j := randGen.Intn(sc)
songs[i], songs[j] = songs[j], songs[i]
}
}
return songs
}