-
Notifications
You must be signed in to change notification settings - Fork 26
/
audio.go
314 lines (290 loc) · 6.7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package steven
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"time"
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/audio"
"github.com/thinkofdeath/steven/console"
"github.com/thinkofdeath/steven/format"
"github.com/thinkofdeath/steven/resource"
)
type soundCategory string
const (
sndAmbient soundCategory = "ambient"
sndWeather soundCategory = "weather"
sndPlayer soundCategory = "player"
sndNeutral soundCategory = "neutral"
sndHostile soundCategory = "hostile"
sndBlock soundCategory = "block"
sndMusic soundCategory = "music"
sndRecord soundCategory = "record"
genericSoundDoc = `
mu_vol_%[1]s controls the volume of sounds with the type of %[1]s.
The value should be between 0 and 100.
`
)
var (
loadedSounds = map[pluginKey]audio.SoundBuffer{}
soundList []audio.Sound
soundInfo = map[string]soundData{}
soundRandom = rand.New(rand.NewSource(time.Now().Unix()))
currentMusic []music
muVolMaster = console.NewIntVar("mu_vol_master", 100, console.Serializable, console.Mutable).
Doc(`
mu_vol_master controls the master volume. This value effects
all types of sounds. The value should be between 0 and 100.
`)
volVars = map[soundCategory]*console.IntVar{}
soundCategories = []soundCategory{
sndMusic,
sndRecord,
sndWeather,
sndBlock,
sndHostile,
sndNeutral,
sndPlayer,
sndAmbient,
}
)
func init() {
for _, ty := range soundCategories {
volVars[ty] = console.NewIntVar("mu_vol_"+string(ty), 100, console.Serializable, console.Mutable).
Doc(fmt.Sprintf(genericSoundDoc, ty)).Callback(refreshSound)
}
muVolMaster.Callback(refreshSound)
}
type music struct {
audio.Music
cat soundCategory
vol float64
cb func()
}
func refreshSound() {
for _, m := range currentMusic {
vol := m.vol
vol *= float64(muVolMaster.Value()) / 100
if v, ok := volVars[m.cat]; ok {
vol *= float64(v.Value()) / 100
}
m.SetVolume(vol)
}
}
func PlaySoundAt(name string, vol, pitch float64, v mgl32.Vec3) {
snd, ok := soundInfo[name]
if !ok {
return
}
playSoundInternal(snd.Category, snd.Sounds[soundRandom.Intn(len(snd.Sounds))], vol, pitch, true, v, nil)
}
func PlaySound(name string) {
snd, ok := soundInfo[name]
if !ok {
return
}
playSoundInternal(snd.Category, snd.Sounds[soundRandom.Intn(len(snd.Sounds))], 1, 1, false, mgl32.Vec3{}, nil)
}
// note: callback only valid for streams
func PlaySoundCallback(name string, cb func()) {
snd, ok := soundInfo[name]
if !ok {
return
}
playSoundInternal(snd.Category, snd.Sounds[soundRandom.Intn(len(snd.Sounds))], 1, 1, false, mgl32.Vec3{}, cb)
}
func playSoundInternal(cat soundCategory, snd sound, vol, pitch float64, rel bool, pos mgl32.Vec3, cb func()) {
vol *= snd.Volume * 100
baseVol := vol
vol *= float64(muVolMaster.Value()) / 100
if v, ok := volVars[cat]; ok {
vol *= float64(v.Value()) / 100
}
if vol <= 0 {
if cb != nil {
go func() { syncChan <- cb }()
}
return
}
name := snd.Name
key := pluginKey{"minecraft", name}
sb, ok := loadedSounds[key]
if !ok {
f, err := resource.Open("minecraft", "sounds/"+name+".ogg")
if err != nil {
v, ok := assets.Objects[fmt.Sprintf("minecraft/sounds/%s.ogg", name)]
if !ok {
console.Text("Missing sound %s", key)
if cb != nil {
cb()
}
return
}
loc := fmt.Sprintf("./resources/%s", hashPath(v.Hash))
f, err = os.Open(loc)
if err != nil {
console.Text("Missing sound %s", key)
if cb != nil {
cb()
}
return
}
}
if snd.Stream {
m := audio.NewMusic(f)
m.SetVolume(vol)
m.SetPitch(pitch)
m.Play()
currentMusic = append(currentMusic, music{Music: m, cb: cb, cat: cat, vol: baseVol})
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
sb = audio.NewSoundBufferData(data)
loadedSounds[key] = sb
}
var s audio.Sound
n := true
for _, sn := range soundList {
if sn.Status() == audio.StatStopped {
s = sn
n = false
break
}
}
if n {
if len(soundList) >= 100 {
console.Component(
format.Build("WARN: Skipping playing sound due to limit").
Color(format.Yellow).Create(),
)
return
}
s = audio.NewSound()
soundList = append(soundList, s)
}
s.SetBuffer(sb)
s.SetVolume(vol)
s.SetMinDistance(5)
s.SetAttenuation(0.008)
s.SetPitch(pitch)
s.Play()
if rel {
s.SetRelative(true)
s.SetPosition(pos.X(), pos.Y(), pos.Z())
} else {
s.SetRelative(false)
}
}
func tickAudio() {
tmp := currentMusic
currentMusic = currentMusic[:0]
for _, m := range tmp {
if m.Status() == audio.StatStopped {
if m.cb != nil {
m.cb()
}
m.Free()
} else {
currentMusic = append(currentMusic, m)
}
}
}
func StopAllMusic() {
for _, m := range currentMusic {
m.Stop()
m.Free()
}
currentMusic = currentMusic[:0]
}
type soundData struct {
Category soundCategory
Sounds []sound
}
type sound struct {
Name string
Stream bool
Volume float64
Type string
}
func (s *sound) UnmarshalJSON(b []byte) error {
type js struct {
Name string
Stream bool
Volume *float64
Type string
}
s.Volume = 1
if b[0] == '"' {
return json.Unmarshal(b, &s.Name)
}
var val js
err := json.Unmarshal(b, &val)
s.Name = val.Name
s.Stream = val.Stream
s.Type = val.Type
if val.Volume != nil {
s.Volume = *val.Volume
}
return err
}
func loadSoundData() {
v := assets.Objects["minecraft/sounds.json"]
loc := fmt.Sprintf("./resources/%s", hashPath(v.Hash))
f, err := os.Open(loc)
if err != nil {
panic(err)
}
snds, _ := resource.OpenAll("minecraft", "sounds.json")
snds = append([]io.ReadCloser{f}, snds...)
for _, s := range snds {
func() {
defer s.Close()
data := map[string]soundData{}
err := json.NewDecoder(s).Decode(&data)
if err != nil {
panic(err)
}
for k, v := range data {
soundInfo[k] = v
}
}()
}
for k, v := range soundInfo {
soundInfo[k] = flattenSoundData(v)
}
}
func flattenSoundData(v soundData) soundData {
var out []sound
for _, snd := range v.Sounds {
if snd.Type != "event" {
out = append(out, snd)
continue
}
other := flattenSoundData(soundInfo[snd.Name])
for _, os := range other.Sounds {
out = append(out, os)
}
}
v.Sounds = out
return v
}