This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
/
audio.go
195 lines (171 loc) · 4.03 KB
/
audio.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
package audio
import (
"io"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/mp3"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/silbinarywolf/gml-go/gml/assetman"
)
type SoundIndex int32
const (
sndUndefined SoundIndex = 0
SoundDirectoryBase = "sound"
)
// Play will play a sound
func (index SoundIndex) Play() {
if disableAudio {
return
}
if index == sndUndefined {
panic("Cannot play sound if not set")
}
sound := &soundManager.assetList[index]
if sound.audioPlayer == nil {
panic("Sound isn't loaded")
}
sound.audioPlayer.Rewind()
sound.audioPlayer.Play()
}
// Stop will stop playing a sound
func (index SoundIndex) Stop() {
if disableAudio {
return
}
if index == sndUndefined {
panic("Cannot stop sound if not set")
}
sound := &soundManager.assetList[index]
if sound.audioPlayer == nil {
panic("Sound isn't loaded")
}
sound.audioPlayer.Pause()
}
// IsPlaying will check if a sound is playing
func (index SoundIndex) IsPlaying() bool {
if disableAudio {
return true
}
if index == sndUndefined {
panic("IsPlaying: Cannot check if unset sound is playing")
}
sound := &soundManager.assetList[index]
return sound.audioPlayer.IsPlaying()
}
func (index SoundIndex) SetVolume(volume float64) {
if disableAudio {
return
}
if index == sndUndefined {
panic("SetVolume: Cannot check if unset sound is playing")
}
sound := &soundManager.assetList[index]
sound.audioPlayer.SetVolume(volume)
}
// Name of the sound asset
func (index SoundIndex) Name() string {
sound := &soundManager.assetList[index]
return sound.name
}
const (
sampleRate = 48000
)
type soundKind int32
const (
soundKindWAV soundKind = 1
soundKindMP3 soundKind = 2
)
var (
audioContext *audio.Context
)
type soundManagerData struct {
assetList []sound
assetNameToIndex map[string]SoundIndex
assetIndexToName []string
}
var (
soundManager = new(soundManagerData)
)
func init() {
assetman.Register(soundManager)
}
func (manager *soundManagerData) LoadAll() {
if audioContext != nil {
// Don't reinitialize. This can be called twice if multiple tests
// call TestBootstrap()
return
}
assetList := soundManager.assetNameToIndex
if len(assetList) == 0 {
// Don't initialize audio if there are no audio assets
return
}
var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
panic(err)
}
// Load all sounds
for _, soundIndex := range assetList {
loadSound(soundIndex)
}
}
func (manager *soundManagerData) ManifestJSON() (string, map[string]string) {
result := make(map[string]string)
assetList := manager.assetNameToIndex
for _, soundIndex := range assetList {
name := soundIndex.Name()
result[name] = name
}
return "sound", result
}
type sound struct {
audioPlayer *audio.Player
name string
}
type soundAsset struct {
Kind soundKind
Data []byte
}
func loadSound(index SoundIndex) {
name := soundManager.assetIndexToName[index]
soundAsset := debugLoadAndWriteSoundAsset(name)
if soundAsset == nil {
panic("missing sound: " + name)
}
var soundSteam io.ReadCloser
soundData := audio.BytesReadSeekCloser(soundAsset.Data)
switch soundAsset.Kind {
case soundKindWAV:
d, err := wav.Decode(audioContext, soundData)
if err != nil {
panic(err)
}
soundSteam = d
case soundKindMP3:
d, err := mp3.Decode(audioContext, soundData)
if err != nil {
panic(err)
}
soundSteam = d
default:
panic("invalid sound type")
}
if soundSteam == nil {
panic("soundStream is nil")
}
var err error
audioPlayer, err := audio.NewPlayer(audioContext, soundSteam)
if err != nil {
panic(err)
}
soundManager.assetList[index] = sound{
audioPlayer: audioPlayer,
name: name,
}
}
// InitSoundGeneratedData is used by code generated by gmlgo so you can query a sound by index or name
func InitSoundGeneratedData(indexToName []string, nameToIndex map[string]SoundIndex) {
soundManager.assetIndexToName = indexToName
soundManager.assetNameToIndex = nameToIndex
soundManager.assetList = make([]sound, len(soundManager.assetIndexToName))
}