-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAudioManager.cs
executable file
·452 lines (400 loc) · 15.5 KB
/
AudioManager.cs
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.Audio;
namespace UberAudio
{
//And audio event, mapping a audio settings to a string event
[Serializable]
public class AudioEvent
{
public string EventName;
public float RandomWeight = 1.0f;
public float Volume = 1.0f;
public AudioClip AudioClip = null;
public int Priority = 128;
public int GroupID = 0;
public bool Loop = false;
public bool StopWhenSourceDies = false;
public bool KeepLoopingWhenSourceDies = false;
public bool DoNotTrackSourceMovement = false;
public bool BypassEffects = false;
public bool BypassListenerEffects = false;
public bool BypassReverbZones = false;
public float DopplerLevel = 1.0f;
public float MaxDistance = 500.0f;
public float MinDistance = 1.0f;
public bool Mute = false;
public AudioMixerGroup OutputAudioMixerGroup = null;
public float PanStereo = 0.0f;
public float Pitch = 1.0f;
public bool Spatialize = false;
public AudioRolloffMode RolloffMode = AudioRolloffMode.Logarithmic;
public AnimationCurve CustomRolloffCurve;
public float SpatialBlend = 0.0f;
public float ReverbZoneMix = 1.0f;
[System.NonSerialized]
public string BankName;
//Used by the AudioEventEditor 'Clone'
//Duplicates an AudioEvent.
//TODO - Do this via reflection.
public AudioEvent Clone()
{
var ev = new AudioEvent();
ev.RandomWeight = RandomWeight;
ev.Volume = Volume;
ev.AudioClip = AudioClip;
ev.EventName = EventName;
ev.Loop = Loop;
ev.StopWhenSourceDies = StopWhenSourceDies;
ev.Priority = Priority;
ev.GroupID = GroupID;
ev.KeepLoopingWhenSourceDies = KeepLoopingWhenSourceDies;
ev.DoNotTrackSourceMovement = DoNotTrackSourceMovement;
ev.BypassEffects = BypassEffects;
ev.BypassListenerEffects = BypassListenerEffects;
ev.BypassReverbZones = BypassReverbZones;
ev.DopplerLevel = DopplerLevel;
ev.MaxDistance = MaxDistance;
ev.MinDistance = MinDistance;
ev.Mute = Mute;
ev.OutputAudioMixerGroup = OutputAudioMixerGroup;
ev.PanStereo = PanStereo;
ev.Pitch = Pitch;
ev.RolloffMode = RolloffMode;
ev.CustomRolloffCurve = CustomRolloffCurve;
ev.SpatialBlend = SpatialBlend;
ev.ReverbZoneMix = ReverbZoneMix;
ev.Spatialize = Spatialize;
return ev;
}
//Transfers audio settings to an audio source
public void TransferToAudioSource(AudioSource source)
{
source.loop = Loop;
source.volume = Volume;
source.clip = AudioClip;
source.bypassEffects = BypassEffects;
source.bypassListenerEffects = BypassListenerEffects;
source.bypassReverbZones = BypassReverbZones;
source.dopplerLevel = DopplerLevel;
source.maxDistance = MaxDistance;
source.minDistance = MinDistance;
source.mute = Mute;
source.outputAudioMixerGroup = OutputAudioMixerGroup;
source.panStereo = PanStereo;
source.pitch = Pitch;
source.priority = Priority;
source.rolloffMode = RolloffMode;
source.spatialBlend = SpatialBlend;
source.reverbZoneMix = ReverbZoneMix;
source.spatialize = Spatialize;
if(RolloffMode == AudioRolloffMode.Custom)
{
source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, CustomRolloffCurve);
}
}
//Transfers audio settings from an AudioSource. Only used in the editor
public void TransferFromAudioSource(AudioSource source)
{
Loop = source.loop;
Volume = source.volume;
AudioClip = source.clip;
BypassEffects = source.bypassEffects;
BypassListenerEffects = source.bypassListenerEffects;
BypassReverbZones = source.bypassReverbZones;
DopplerLevel = source.dopplerLevel;
MaxDistance = source.maxDistance;
MinDistance = source.minDistance;
Mute = source.mute;
OutputAudioMixerGroup = source.outputAudioMixerGroup;
PanStereo = source.panStereo;
Pitch = source.pitch;
Priority = source.priority;
RolloffMode = source.rolloffMode;
SpatialBlend = source.spatialBlend;
ReverbZoneMix = source.reverbZoneMix;
Spatialize = source.spatialize;
if(RolloffMode == AudioRolloffMode.Custom)
{
CustomRolloffCurve = source.GetCustomCurve(AudioSourceCurveType.CustomRolloff);
}
else
{
CustomRolloffCurve = null;
}
}
}
//The core UberAudio manager singleton
public class AudioManager : MonoBehaviour
{
static public AudioManager Instance
{
get; private set;
}
// Use to build an event name from segments
// See 'Play' for details
public static string MakeEvent(string s1, params object[] vars)
{
var outputString = s1.ToUpper();
foreach(var obj in vars)
{
string t = obj as string;
if(t!=null && t!="")
{
outputString += AudioEventSeparator;
outputString += t.ToUpper();
}
}
return outputString;
}
public bool LogMissingEvents = false;
public bool LogAllEvents = false;
public bool LogLookups = false;
public bool LogBankLoads = true;
public bool LogBankRefCounts = false;
//Set up our singleton.
public void Awake()
{
if(Instance!=null)
{
Destroy(gameObject);
}
else
{
Instance = this;
UnityEngine.Object.DontDestroyOnLoad(gameObject);
}
}
// Support for music. Not finished
public void PlayMusic(string eventName)
{
StopMusic();
Instance.CurrentMusicEvent = eventName;
}
// Support for music. Not finished
public void QueueMusic(string eventName)
{
Instance.MusicQueue.Enqueue(eventName);
}
// Support for music. Not finished
public void StopMusic()
{
MusicQueue.Clear();
if (MusicEmitter)
{
MusicEmitter.Stop();
MusicEmitter = null;
}
}
public void Update()
{
// Update the music queue
if(MusicQueue.Count>0)
{
if (string.IsNullOrEmpty(CurrentMusicEvent))
{
PlayMusic(MusicQueue.Dequeue());
}
}
//Delete any dead emitters
LinkedListNode<AudioEmitter> currentNode = ActiveEmitters.First;
while (currentNode != null)
{
if (currentNode.Value.Finished)
{
var node = currentNode;
currentNode = currentNode.Next;
GameObject.Destroy(node.Value.gameObject);
ActiveEmitters.Remove(node);
}
else
{
currentNode = currentNode.Next;
}
}
#if UNITY_EDITOR
if (LogBankRefCounts)
{
foreach(KeyValuePair<string, int> kvp in BankReferenceCounts)
{
Debug.Log(kvp.Key.ToString() + ":" + kvp.Value.ToString());
}
LogBankRefCounts = false;
}
#endif
}
// Play an audio event.
//
// audioEventName is an audio event name from an audio bank
// Event names are made of segments, ordered right to left in increasing order of specialisation
// Eg. 'FOOTSTEP', 'GRAVEL, FOOTSTEP', 'WOLF, GRAVEL, FOOTSTEP'
// The engine will strip the specialisations off, left to right, until it finds a match
// Use MakeEvent to construct these.
//
// source is a GameObject to attach to - if null, uses a global emitter, useful for 2D sounds
//
// If successful, returns an AudioEmitter attached to a new GameObject
public AudioEmitter Play(string audioEventName, GameObject source = null)
{
if(string.IsNullOrEmpty(audioEventName))
{
return null;
}
//If we're not attaching audio to another game object, assume this is a 2D sound and attach it to this manager
if(source==null)
{
source = this.gameObject;
}
if (LogAllEvents)
{
Debug.Log("AudioManager: Trying event '" + audioEventName + "'" + (source != null ? (" (source = '" + source.name + "')") : ""));
}
// Look up an event from this trigger
AudioEvent audioEvent = GetSoundForEvent(audioEventName);
if (audioEvent != null && audioEvent.AudioClip!=null)
{
var emitter = AudioEmitter.Create(source.transform, audioEvent);
emitter.Play();
ActiveEmitters.AddLast(emitter);
return emitter;
}
else if (LogMissingEvents)
{
Debug.Log("$$$$Missing sound event '" + audioEventName + "' (source = '" + source.name + "')");
}
return null;
}
public void Stop(AudioEmitter emitter)
{
if(emitter!=null)
{
emitter.Stop();
}
}
/// Recursively look up an event in the loaded banks, chopping bits off the audio event name until a match is found.
/// If there are multiple AudioEvents with the same trigger, does weighted randomisation.
AudioEvent GetSoundForEvent(string audioEventName)
{
if (LogLookups)
{
Debug.Log("$$$Looking for sound event " + audioEventName);
}
List<AudioEvent> events;
if (AudioEvents.TryGetValue(audioEventName, out events))
{
AudioEvent audioEvent;
//If we have more than one event, get a weighted, random one.
if (events.Count > 1)
{
audioEvent = GetRandomEvent(events);
}
else
{
audioEvent = events[0];
}
if(LogLookups)
{
Debug.Log("$$$Found sound event " + audioEventName);
}
return audioEvent;
}
else
{
var splitLocation = audioEventName.IndexOf(AudioEventSeparator);
if (splitLocation >= 0 && splitLocation + 1 < audioEventName.Length)
{
var newEv = audioEventName.Substring(splitLocation + 1);
return GetSoundForEvent(newEv);
}
}
return null;
}
// Uses the event weightings to get a random event from a group
public static AudioEvent GetRandomEvent(List<AudioEvent> events)
{
var totalWeight = 0.0f;
events.ForEach(e => totalWeight += e.RandomWeight);
var dieRoll = UnityEngine.Random.Range(0, totalWeight);
var currentWeight = 0.0;
foreach (var ev in events)
{
currentWeight += ev.RandomWeight;
if (dieRoll < currentWeight)
{
return ev;
}
}
return null;
}
// Internal - Used by AudioBankMounter
// If bankName is not loaded, loads it and sets the ref count to one
// Otherwise it just increments the ref count
public void LoadEventBank(string bankName)
{
if (BankReferenceCounts.ContainsKey(bankName))
{
BankReferenceCounts[bankName] += 1;
return;
}
BankReferenceCounts[bankName] = 1;
var bank = Resources.Load(bankName, typeof(AudioEventBank)) as AudioEventBank;
if (bank == null)
{
Debug.Log("Failed to load audio bank " + bankName);
return;
}
if (LogBankLoads)
{
Debug.Log("Loading bank " + bankName);
}
foreach (var se in bank.AudioEvents)
{
if (!AudioEvents.ContainsKey(se.EventName))
{
AudioEvents[se.EventName] = new List<AudioEvent>();
}
se.BankName = bankName;
AudioEvents[se.EventName].Add(se);
}
}
// Internal - Used by AudioBankMounter
// Decrements the reference count on the bank named bankName
// If it hits zero, unloads it
public void UnloadEventBank(string bankName)
{
if (BankReferenceCounts.ContainsKey(bankName))
{
BankReferenceCounts[bankName] -= 1;
if (BankReferenceCounts[bankName] <= 0)
{
BankReferenceCounts.Remove(bankName);
Debug.Log("AudioManager: Unload bank " + bankName);
var keysToDelete = new List<string>();
foreach (var kvp in AudioEvents)
{
kvp.Value.RemoveAll(e => e.BankName == bankName);
if (kvp.Value.Count == 0)
{
keysToDelete.Add(kvp.Key);
}
}
foreach (var k in keysToDelete)
{
AudioEvents.Remove(k);
}
}
}
}
Dictionary<string, List<AudioEvent>> AudioEvents = new Dictionary<string, List<AudioEvent>>();
LinkedList<AudioEmitter> ActiveEmitters = new LinkedList<AudioEmitter>();
Dictionary<string, int> BankReferenceCounts = new Dictionary<string, int>();
AudioEmitter MusicEmitter;
string CurrentMusicEvent;
Queue<string> MusicQueue = new Queue<string>();
//The seperator between removable parts of an event trigger
//If you change this, you'll need to change your audio banks!
static string AudioEventSeparator = ":";
}
}