-
Notifications
You must be signed in to change notification settings - Fork 65
/
maudio.go
75 lines (65 loc) · 1.42 KB
/
maudio.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
package asset
import (
"korok.io/korok/audio/sine"
"strings"
"log"
)
type AudioManager struct {
repo map[string]idCount
}
func NewAudioManager() *AudioManager {
return &AudioManager{make(map[string]idCount)}
}
// Load loads a single Texture file.
func (am *AudioManager) Load(file string, stream bool) {
if typ := audioType(file); typ == sine.None {
log.Println("not implemented audio type:", file)
return
}
var rid, cnt uint16
if v, ok := am.repo[file]; ok {
cnt = v.cnt
rid = v.rid
} else {
id, _ := sine.R.LoadSound(file, audioType(file), sourceType(stream))
rid = id
}
am.repo[file] = idCount{rid, cnt+1}
log.Print("load file:", file)
}
// Unload delete raw Texture and any related SubTextures.
func (am *AudioManager) Unload(file string) {
if v, ok := am.repo[file]; ok {
if v.cnt > 1 {
am.repo[file] = idCount{v.rid, v.cnt -1}
} else {
delete(am.repo, file)
sine.R.UnloadSound(v.rid)
log.Println("refCont == 0, delete resoruce!!")
}
}
}
func (am *AudioManager) Get(file string) (id uint16, ok bool){
if v, ook := am.repo[file]; ook {
id = v.rid
ok = true
}
return
}
func audioType(name string) sine.FileType {
switch true {
case strings.HasSuffix(name, ".wav"):
return sine.WAV
case strings.HasSuffix(name, ".ogg"):
return sine.VORB
default:
return sine.None
}
}
func sourceType(stream bool) sine.SourceType {
if stream {
return sine.Stream
} else {
return sine.Static
}
}