-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsound.go
91 lines (76 loc) · 2.37 KB
/
sound.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
// Copyright © 2015-2018 Galvanized Logic Inc.
package vu
// sound.go wraps the audio package and controls all engine sounds.
import (
"log/slog"
)
// PlaySound plays the given sound at this entities location.
// - soundID : entity created with Eng.AddSound.
//
// Depends on Engine.AddSound.
func (e *Entity) PlaySound(eng *Engine, sound *Entity) {
if p := e.app.povs.get(e.eid); p != nil {
if s := e.app.sounds.get(sound.eid); s != nil {
e.app.sounds.play(eng, s, p)
}
return
}
slog.Error("PlaySound requires location", "entity", e.eid)
}
// SetListener sets the location of the sound listener to be this entity.
//
// Depends on Engine.AddSound.
func (e *Entity) SetListener() {
if p := e.app.povs.get(e.eid); p != nil {
e.app.sounds.setListener(e.eid)
return
}
slog.Error("SetListener requires location", "entity", e.eid)
}
// =============================================================================
// sounds: component manager for sound.
// sounds manages audio instances. Each sound must be loaded with sound data
// that has been bound to the audio card in order for the sound to be played.
type sounds struct {
list map[eID]*sound // loaded sounds assets.
listener eID // Pov listener location.
}
func (ss *sounds) get(eid eID) *sound { return ss.list[eid] }
// newSounds creates the sound component manager.
// Expected to be called once on startup.
func newSounds() *sounds {
ss := &sounds{}
ss.list = map[eID]*sound{} // Sounds ready to be played.
return ss
}
// create a new sound Entity.
func (ss *sounds) create(eids *entities, name string) (eid eID) {
return eids.create()
}
// assetLoaded associates a loaded sound asset with the given entity
func (ss *sounds) assetLoaded(eid eID, a asset) {
switch la := a.(type) {
case *sound:
ss.list[eid] = la
}
}
// play the given sound.
func (ss *sounds) play(eng *Engine, sound *sound, pov *pov) {
if sound != nil && pov != nil {
x, y, z := pov.at()
eng.ac.PlaySound(sound.sid, x, y, z)
}
}
// setListener saves the location of the listener pov.
// so that it can be set later on the main thread.
func (ss *sounds) setListener(pov eID) {
ss.listener = pov
}
// dispose of sound data associated with the given entity.
func (ss *sounds) dispose(eng *Engine, eid eID) {
if s := ss.list[eid]; s != nil {
delete(ss.list, eid)
// delete the sound resources.
eng.ac.DropSound(s.sid, s.did)
}
}