/
storage.go
67 lines (58 loc) · 1.14 KB
/
storage.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
// Package source models a single audio source
package source
import (
"gopkg.in/mix.v0/bind/spec"
"sync"
)
// Prepare a source by ensuring it is stored in memory.
func Prepare(src string) {
storageMutex.Lock()
defer storageMutex.Unlock()
if _, exists := storage[src]; !exists {
storage[src] = New(src)
}
}
// Get a source from storage
func Get(src string) *Source {
storageMutex.Lock()
defer storageMutex.Unlock()
if _, ok := storage[src]; ok {
return storage[src]
} else {
return nil
}
}
func GetLength(src string) spec.Tz {
source := Get(src)
if source != nil {
return source.Length()
} else {
return spec.Tz(0)
}
}
// Prune to keep only the sources in this list
func Prune(keep map[string]bool) {
storageMutex.Lock()
defer storageMutex.Unlock()
for key, _ := range storage {
if _, exists := keep[key]; !exists {
delete(storage, key)
}
}
}
// Count the number of sources in memory
func Count() int {
storageMutex.Lock()
defer storageMutex.Unlock()
return len(storage)
}
/*
*
private */
var (
storage map[string]*Source
storageMutex = &sync.Mutex{}
)
func init() {
storage = make(map[string]*Source, 0)
}