forked from quasilyte/ge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_system.go
217 lines (182 loc) · 4.83 KB
/
audio_system.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
package ge
import (
"io"
"runtime"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
"github.com/hajimehoshi/ebiten/v2/audio/wav"
resource "github.com/quasilyte/ebitengine-resource"
)
const (
soundMapSize = 64
maxSoundMapID = soundMapSize * 8
)
type soundMap struct {
table [soundMapSize]byte
}
func (m *soundMap) Reset() {
m.table = [soundMapSize]byte{}
}
func (m *soundMap) IsSet(id uint) bool {
byteIndex := id / 8
if byteIndex < uint(len(m.table)) {
bitIndex := id % 8
return uint(m.table[byteIndex]&(1>>bitIndex)) != 0
}
return false
}
func (m *soundMap) Set(id uint) {
byteIndex := id / 8
if byteIndex < uint(len(m.table)) {
bitIndex := id % 8
m.table[byteIndex] = 1 << bitIndex
}
}
type AudioSystem struct {
loader *resource.Loader
currentMusic resource.Audio
audioContext *audio.Context
currentQueueSound resource.Audio
soundQueue []resource.AudioID
// This small bitset is used to track sounds with id<maxSoundMapID.
// These sounds will be "played" only once during a frame.
// Therefore, doing multiple PlaySound(id) during a single frame
// is more efficient.
soundMap soundMap
groupVolume [4]float64
muted bool
}
type audioResource struct {
player *audio.Player
group uint
volume float64
}
func (sys *AudioSystem) init(audioContext *audio.Context, l *resource.Loader) {
sys.loader = l
sys.audioContext = audioContext
sys.soundQueue = make([]resource.AudioID, 0, 4)
for i := range sys.groupVolume {
sys.groupVolume[i] = 1.0
}
if runtime.GOOS != "android" {
// Audio player factory has lazy initialization that may lead
// to a ~0.2s delay before the first sound can be played.
// To avoid that delay, we force that factory to initialize
// right now, before the game is started.
dummy := sys.audioContext.NewPlayerFromBytes(nil)
dummy.Rewind()
}
}
func (sys *AudioSystem) GetContext() *audio.Context {
return sys.audioContext
}
func (sys *AudioSystem) Update() {
sys.soundMap.Reset()
if sys.currentQueueSound.Player == nil {
if len(sys.soundQueue) == 0 {
// Nothing to play in the queue.
return
}
// Do a dequeue.
sys.currentQueueSound = sys.playSound(sys.soundQueue[0], 1)
for i, id := range sys.soundQueue[1:] {
sys.soundQueue[i] = id
}
sys.soundQueue = sys.soundQueue[:len(sys.soundQueue)-1]
return
}
if !sys.currentQueueSound.Player.IsPlaying() {
// Finished playing the current enqueued sound.
sys.currentQueueSound = resource.Audio{}
}
}
func (sys *AudioSystem) SetGroupVolume(groupID uint, multiplier float64) {
if groupID >= uint(len(sys.groupVolume)) {
panic("invalid group ID")
}
sys.groupVolume[groupID] = multiplier
}
func (sys *AudioSystem) DecodeWAV(r io.Reader) (*wav.Stream, error) {
return wav.Decode(sys.audioContext, r)
}
func (sys *AudioSystem) DecodeOGG(r io.Reader) (*vorbis.Stream, error) {
return vorbis.Decode(sys.audioContext, r)
}
func (sys *AudioSystem) PauseCurrentMusic() {
if sys.muted {
return
}
if sys.currentMusic.Player == nil {
return
}
sys.currentMusic.Player.Pause()
}
func (sys *AudioSystem) ContinueCurrentMusic() {
if sys.muted {
return
}
if sys.currentMusic.Player == nil || sys.currentMusic.Player.IsPlaying() {
return
}
sys.currentMusic.Player.SetVolume(sys.currentMusic.Volume * sys.groupVolume[sys.currentMusic.Group])
sys.currentMusic.Player.Play()
}
func (sys *AudioSystem) ContinueMusic(id resource.AudioID) {
if sys.muted {
return
}
sys.continueMusic(sys.loader.LoadAudio(id))
}
func (sys *AudioSystem) continueMusic(res resource.Audio) {
if res.Player.IsPlaying() {
return
}
sys.currentMusic = res
res.Player.SetVolume(res.Volume * sys.groupVolume[res.Group])
res.Player.Play()
}
func (sys *AudioSystem) PlayMusic(id resource.AudioID) {
if sys.muted {
return
}
res := sys.loader.LoadAudio(id)
if sys.currentMusic.Player != nil && res.Player == sys.currentMusic.Player && res.Player.IsPlaying() {
return
}
sys.currentMusic = res
res.Player.SetVolume(res.Volume * sys.groupVolume[res.Group])
res.Player.Rewind()
res.Player.Play()
}
func (sys *AudioSystem) ResetQueue() {
sys.soundQueue = sys.soundQueue[:0]
}
func (sys *AudioSystem) EnqueueSound(id resource.AudioID) {
if sys.muted {
return
}
sys.soundQueue = append(sys.soundQueue, id)
}
func (sys *AudioSystem) PlaySound(id resource.AudioID) {
sys.PlaySoundWithVolume(id, 1.0)
}
func (sys *AudioSystem) PlaySoundWithVolume(id resource.AudioID, vol float64) {
if sys.muted {
return
}
if sys.soundMap.IsSet(uint(id)) {
return
}
sys.soundMap.Set(uint(id))
sys.playSound(id, vol)
}
func (sys *AudioSystem) playSound(id resource.AudioID, vol float64) resource.Audio {
res := sys.loader.LoadWAV(id)
volumeMultiplier := sys.groupVolume[res.Group] * vol
if volumeMultiplier != 0 {
res.Player.SetVolume(res.Volume * volumeMultiplier)
res.Player.Rewind()
res.Player.Play()
}
return res
}