-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
227 lines (177 loc) · 3.92 KB
/
repository.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
package main
import (
"github.com/dhowden/tag"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
type Sample struct {
Id int `json:"id"`
Path string `json:"path"`
Title string `json:"title"`
Group string `json:"group"`
Metadata tag.Metadata `json:"-"`
isPlaying bool
cmd *exec.Cmd
err error
}
func (self *Sample) Start(playCmd string) (chan bool, error) {
// Make stopped callback channel
done := make(chan bool, 1)
log.Println("Playing file:", self.Path)
// Start playback
self.cmd = exec.Command(playCmd, self.Path)
err := self.cmd.Start()
if err != nil {
done <- false
return done, err
}
self.isPlaying = true
go func() {
// Wait until finished, reset state
self.err = self.cmd.Wait()
if self.err != nil {
log.Println("Could not play sample:", self.err)
}
self.isPlaying = false
done <- true
}()
return done, nil
}
func (self *Sample) Stop() error {
if self.isPlaying == false {
return fmt.Errorf("Sample not playing")
}
err := self.cmd.Process.Kill()
if err != nil {
log.Println("Could not kill player:", err)
return err
}
return nil
}
func (self *Sample) _makeTitle() {
if self.Metadata != nil && self.Metadata.Title() != "" && self.Metadata.Artist() != "" {
self.Title = fmt.Sprintf("%v (%v)", self.Metadata.Title(), self.Metadata.Artist())
} else if self.Metadata != nil && self.Metadata.Title() != "" {
self.Title = self.Metadata.Title()
} else {
self.Title = _stripSuffix(path.Base(self.Path))
}
}
func (self *Sample) _makeGroup(basePath string) {
rel := strings.Replace(self.Path, basePath, "", 1)
t := strings.Split(rel, "/")
if len(t) > 1 {
self.Group = t[1]
}
}
type Repository struct {
Path string
samplesCache []*Sample
}
func NewRepository(path string) *Repository {
repo := &Repository{
Path: path,
}
return repo
}
func _stripSuffix(path string) string {
t := strings.Split(path, ".")
return strings.Join(t[:len(t)-1], ".")
}
func _hasAudioSuffix(path string) bool {
suffices := []string{
".wav", ".mp3", ".ogg", ".flac",
}
for _, suffix := range suffices {
if strings.HasSuffix(path, suffix) {
return true
}
}
return false
}
func (self *Repository) listFiles() ([]string, error) {
log.Println("Searching files in:", self.Path)
files := []string{}
err := filepath.Walk(self.Path, func(path string, f os.FileInfo, err error) error {
// Filter at by suffix
if _hasAudioSuffix(path) {
files = append(files, path)
}
return nil
})
if err != nil {
return []string{}, err
}
return files, nil
}
func (self *Repository) Update() error {
files, err := self.listFiles()
if err != nil {
return err
}
self.samplesCache = []*Sample{}
for id, filename := range files {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Println("Could not open file:", err)
continue
}
// Read metadata
meta, _ := tag.ReadFrom(file)
sample := &Sample{
Id: id,
Path: filename,
Metadata: meta,
}
sample._makeTitle()
sample._makeGroup(self.Path)
self.samplesCache = append(self.samplesCache, sample)
}
return nil
}
func (self *Repository) _enumerateDuplicateTitles() {
}
func (self *Repository) AllSamples() []*Sample {
return self.samplesCache
}
func (self *Repository) Samples(group string) []*Sample {
samples := []*Sample{}
for _, s := range self.samplesCache {
if group == "" || group == "*" {
samples = append(samples, s)
continue
}
if s.Group == group {
samples = append(samples, s)
}
}
return samples
}
func (self *Repository) Groups() []string {
groups := []string{}
for _, s := range self.samplesCache {
hasGroup := false
for _, g := range groups {
if s.Group == g {
hasGroup = true
break
}
}
if !hasGroup {
groups = append(groups, s.Group)
}
}
return groups
}
func (self *Repository) GetSampleById(id int) *Sample {
if id >= len(self.samplesCache) {
return nil
}
return self.samplesCache[id]
}