-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFmodEvent.cs
562 lines (488 loc) · 14 KB
/
FmodEvent.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using UnityEngine;
using System.IO;
using System;
using FMOD;
using FMOD.Studio;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using FMODUnity;
namespace LoFiPeople.FMOD
{
public class FmodEvent: IDisposable
{
public EventInstance Instance;
public EventDescription Description;
public readonly string EventPath;
event Action<FmodEvent> _onPlayStateChanged;
event Action<FmodEvent, string> _onMarker;
event Action<FmodEvent, string> _onSoundPlayed;
event Action<FmodEvent, string> _onSoundStopped;
event Func<FmodEvent, string, Sound> _onProgrammerSoundCreated;
global::FMOD.Studio.EVENT_CALLBACK _callbackSink = null;
Dictionary<string, int> _markers = null;
bool _markersLoaded = false;
#if ENABLE_IL2CPP
// Necessary because callbacks can only be static on AOT platforms
static Dictionary<IntPtr, FmodEvent> _callbacks = new Dictionary<IntPtr, FmodEvent>();
#endif
public event Action<FmodEvent> OnPlayStateChanged
{
add { _onPlayStateChanged += value; EnableCallbacks(); }
remove { _onPlayStateChanged -= value; DisableCallbacks(); }
}
public event Action<FmodEvent, string> OnMarker
{
add { _onMarker += value; EnableCallbacks(); }
remove { _onMarker -= value; DisableCallbacks(); }
}
public event Func<FmodEvent, string, Sound> OnProgrammerSoundCreated
{
add { _onProgrammerSoundCreated += value; EnableCallbacks(); }
remove { _onProgrammerSoundCreated -= value; DisableCallbacks(); }
}
public event Action<FmodEvent, string> OnSoundPlayed
{
add { _onSoundPlayed += value; EnableCallbacks(); }
remove { _onSoundPlayed -= value; DisableCallbacks(); }
}
public event Action<FmodEvent, string> OnSoundStopped
{
add { _onSoundStopped += value; EnableCallbacks(); }
remove { _onSoundStopped -= value; DisableCallbacks(); }
}
void EnableCallbacks()
{
if (_onPlayStateChanged == null &&
_onMarker == null &&
_onSoundPlayed == null &&
_onSoundStopped == null &&
_onProgrammerSoundCreated == null
)
return;
#if ENABLE_IL2CPP
lock (_callbacks)
{
if (!_callbacks.ContainsKey(this.Instance.handle))
_callbacks[this.Instance.handle] = this;
}
#endif
if (_callbackSink == null)
{
_callbackSink = new EVENT_CALLBACK(FmodEventCallback);
Do(Instance.setCallback(_callbackSink,
EVENT_CALLBACK_TYPE.STARTED |
EVENT_CALLBACK_TYPE.STARTING |
EVENT_CALLBACK_TYPE.RESTARTED |
EVENT_CALLBACK_TYPE.STOPPED |
EVENT_CALLBACK_TYPE.START_FAILED |
EVENT_CALLBACK_TYPE.TIMELINE_MARKER |
EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND |
EVENT_CALLBACK_TYPE.SOUND_PLAYED |
EVENT_CALLBACK_TYPE.SOUND_STOPPED
),
(p) => "Failed to set callback.");
}
}
void DisableCallbacks()
{
if (_onPlayStateChanged != null || _onMarker != null || _onProgrammerSoundCreated != null)
return;
#if ENABLE_IL2CPP
lock (_callbacks)
_callbacks.Remove(this.Instance.handle);
#endif
if (_callbackSink != null)
{
Do(Instance.setCallback(null),
(p) => "Failed to unset callback.");
_callbackSink = null;
}
}
#if ENABLE_IL2CPP
[AOT.MonoPInvokeCallback(typeof(global::FMOD.Studio.EVENT_CALLBACK))]
static global::FMOD.RESULT FmodEventCallback(global::FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instance, IntPtr parameterPtr)
{
if (!new EventInstance(instance).isValid())
{
UnityEngine.Debug.LogError("[FMOD] Received callback from invalid event instance.");
return RESULT.OK;
}
FmodEvent ev;
lock (_callbacks)
{
if (!_callbacks.TryGetValue(instance, out ev))
{
UnityEngine.Debug.LogError($"[FMOD] Received callback {type} from unknown event instance.");
return RESULT.OK;
}
}
return FmodEventCallbackHandler(type, parameterPtr, ev);
}
#else
[AOT.MonoPInvokeCallback(typeof(global::FMOD.Studio.EVENT_CALLBACK))]
global::FMOD.RESULT FmodEventCallback(global::FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instance, IntPtr parameterPtr)
{
return FmodEventCallbackHandler(type, parameterPtr, this);
}
#endif
static RESULT FmodEventCallbackHandler(EVENT_CALLBACK_TYPE type, IntPtr parameterPtr, FmodEvent ev)
{
if (ev == null)
{
UnityEngine.Debug.LogError($"[FMOD] Received callback {type} from unknown event instance.");
return RESULT.OK;
}
switch (type)
{
case EVENT_CALLBACK_TYPE.TIMELINE_MARKER:
if (ev._onMarker != null)
{
var parameter = (TIMELINE_MARKER_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(TIMELINE_MARKER_PROPERTIES));
ev._onMarker.Invoke(ev, parameter.name);
}
break;
case EVENT_CALLBACK_TYPE.SOUND_PLAYED:
if (ev._onSoundPlayed != null)
{
if (new Sound(parameterPtr).getName(out string soundName, 512) == RESULT.OK)
ev._onSoundPlayed.Invoke(ev, soundName);
}
break;
case EVENT_CALLBACK_TYPE.SOUND_STOPPED:
if (ev._onSoundStopped != null)
{
if (new Sound(parameterPtr).getName(out string soundName, 512) == RESULT.OK)
ev._onSoundStopped.Invoke(ev, soundName);
}
break;
case EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
if (ev._onProgrammerSoundCreated != null)
{
var parameter = (PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(PROGRAMMER_SOUND_PROPERTIES));
Sound sound = ev._onProgrammerSoundCreated.Invoke(ev, parameter.name);
if (sound.hasHandle())
{
parameter.sound = sound.handle;
parameter.subsoundIndex = -1;
Marshal.StructureToPtr(parameter, parameterPtr, false);
}
}
break;
default:
try
{
if (ev._onPlayStateChanged != null)
ev._onPlayStateChanged.Invoke(ev);
}
catch (NullReferenceException)
{
UnityEngine.Debug.Log("[FMOD] Something is null here, not sure what.");
}
break;
}
return RESULT.OK;
}
public FmodEvent(string fmodEventPath)
{
EventPath = fmodEventPath;
Instance = FMODUnity.RuntimeManager.CreateInstance(fmodEventPath);
Do(Instance.getDescription(out Description),
(p) => "Failed to get event description."
);
}
public void Release()
{
if (!Instance.isValid())
return;
Do(Instance.setCallback(null, EVENT_CALLBACK_TYPE.ALL), (p) => "Failed to remove callback");
_callbackSink = null;
#if ENABLE_IL2CPP
lock (_callbacks)
_callbacks.Remove(this.Instance.handle);
#endif
Do(Instance.release(), (p) => "Failed to release event instance.");
}
void IDisposable.Dispose()
{
this.Release();
}
~FmodEvent()
{
this.Release();
}
public float Time
{
get => this.TimeMilliseconds / 1000f;
set => this.TimeMilliseconds = Mathf.RoundToInt(value * 1000);
}
public int TimeMilliseconds
{
get
{
int ms;
Do(Instance.getTimelinePosition(out ms), (p) => "Failed to get timeline position");
return ms;
}
set
{
Do(Instance.setTimelinePosition(value), (p) => "Failed to set timeline position");
}
}
public PLAYBACK_STATE PlaybackState
{
get
{
PLAYBACK_STATE state;
Do(Instance.getPlaybackState(out state),
(p) => "Failed to get playback state.");
return state;
}
}
public Vector3 Position
{
//get { throw new NotImplementedException(); }
set
{
Instance.set3DAttributes(RuntimeUtils.To3DAttributes(value));
}
}
public FmodEvent SetPosition(Vector3 position)
{
this.Position = position;
return this;
}
public bool IsValid
{
get { return Instance.isValid(); }
}
public float Length
{
get => LengthMilliseconds / 1000f;
}
public int LengthMilliseconds
{
get
{
int length = 0;
Do(Description.getLength(out length), (p) => "Failed to get event timeline length.");
return length;
}
}
public FmodEvent Start()
{
Do(Instance.start(), (p) => "Failed to start event.");
return this;
}
public FmodEvent Pause()
{
IsPaused = true;
return this;
}
public bool IsPaused
{
get
{
bool paused = false;
Do(Instance.getPaused(out paused),
(p) => "Failed to check if event is paused.");
return paused;
}
set
{
Do(Instance.setPaused(value),
(p) => "Failed to change pause state.");
}
}
public FmodEvent Resume()
{
Do(Instance.setPaused(false),
(p) => "Failed to resume event.");
return this;
}
public FmodEvent Stop(bool hard = false)
{
Do(Instance.stop(hard ? global::FMOD.Studio.STOP_MODE.IMMEDIATE : global::FMOD.Studio.STOP_MODE.ALLOWFADEOUT),
(p) => "Failed to stop event instance.");
return this;
}
public FmodEvent LoadMarkers()
{
_markers = FmodMarker.Load(this.EventPath);
_markersLoaded = true;
return this;
}
public bool HasMarker(string marker)
{
if (!_markersLoaded)
LoadMarkers();
return _markers != null && _markers.ContainsKey(marker);
}
public FmodEvent JumpToMarker(string marker)
{
if (!IsValid)
return this;
if (!_markersLoaded)
LoadMarkers();
marker = marker.Trim();
if (_markers == null)
{
UnityEngine.Debug.LogError("[FMOD] No markers available. Try calling LoadMarkers() first");
return this;
}
int ms;
if (_markers.TryGetValue(marker, out ms))
{
this.TimeMilliseconds = ms;
}
else
UnityEngine.Debug.LogError($"[FMOD] Can't find marker named '{marker}'");
return this;
}
public float this[string name]
{
get {
float val, fval;
Do(Instance.getParameterByName(name, out val, out fval), (p) => string.Format("Failed to get parameter '{0}'.", p), name);
return fval;
}
set
{
Do(Instance.setParameterByName(name, value), (p) => string.Format("Failed to set parameter '{0}'.", p), name, FmodSeverity.Warning);
}
}
public float GetParamFinal(string name)
{
return this[name];
}
public float GetParamRaw(string name)
{
float val, fval;
Do(Instance.getParameterByName(name, out val, out fval), (p) => string.Format("Failed to get parameter '{0}'.", p), name);
return val;
}
public FmodEvent SetParam(string name, float value)
{
this[name] = value;
return this;
}
void Do(RESULT result, Func<string,string> error, FmodSeverity severity = FmodSeverity.Error)
{
FmodUtils.Check(result, error, null, this.EventPath, severity);
}
void Do(RESULT result, Func<string,string> error, string errorParam, FmodSeverity severity = FmodSeverity.Error)
{
FmodUtils.Check(result, error, errorParam, this.EventPath, severity);
}
public static void PlayOneShot(string eventPath)
{
FMODUnity.RuntimeManager.PlayOneShot(eventPath);
}
public static void PlayOneShot(string eventPath, Action<FmodEvent> init)
{
var ev = new FmodEvent(eventPath);
init(ev);
ev.Start().Release();
}
public static void PlayAndReleaseWhenDone(string eventPath, Action<FmodEvent> init)
{
var ev = new FmodEvent(eventPath);
init(ev);
ev.OnPlayStateChanged += e =>
{
if (e.PlaybackState == PLAYBACK_STATE.STOPPING)
e.Release();
};
ev.Start();
}
public static void KillAllInTransform(Transform transform, bool withChildren)
{
MonoBehaviour[] scripts = withChildren ?
transform.GetComponentsInChildren<MonoBehaviour>(true) :
transform.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour script in scripts)
{
KillAllInScript(script);
}
}
public static void KillAllInScript(MonoBehaviour script)
{
Type type = script.GetType();
PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
IEnumerable<FmodEvent> events =
props.Where(prop => typeof(FmodEvent).IsAssignableFrom(prop.PropertyType)).Select(prop => (FmodEvent)prop.GetValue(script, null))
.Union(
fields.Where(field => typeof(FmodEvent).IsAssignableFrom(field.FieldType)).Select(field => (FmodEvent)field.GetValue(script))
)
;
foreach (FmodEvent ev in events)
StopAndRelease(ev, true);
}
public static bool IsValidAndNotNull(FmodEvent ev)
{
return (ev != null && ev.IsValid);
}
public static bool IsValidAndPlaying(FmodEvent ev)
{
return IsValidAndNotNull(ev) && ev.PlaybackState != PLAYBACK_STATE.STOPPED;
}
public static bool Stop(FmodEvent ev, bool hard = false)
{
bool valid;
if (valid = IsValidAndNotNull(ev))
ev.Stop(hard);
return valid;
}
public static bool Release(FmodEvent ev)
{
bool valid;
if (valid = IsValidAndNotNull(ev))
ev.Release();
return valid;
}
public static bool StopAndRelease(FmodEvent ev, bool hard = false)
{
Stop(ev, hard);
return Release(ev);
}
}
[Serializable]
public class FmodMarker
{
public string name;
public double position;
public static string MarkerJsonFolder = "FMOD Markers";
public static Dictionary<string, int> Load(TextAsset markerJson)
{
FmodMarker[] markers = JsonUtils.DeserializeArray<FmodMarker>(markerJson.text);
var output = new Dictionary<string, int>();
for (int i = 0; i < markers.Length; i++)
output[markers[i].name] = Mathf.FloorToInt((float)(markers[i].position * 1000));
return output;
}
public static Dictionary<string, int> Load(string eventPath)
{
string resourcePath = string.Format("{0}/{1}", MarkerJsonFolder, Path.GetFileNameWithoutExtension(eventPath));
var jsonAsset = Resources.Load(resourcePath);
if (jsonAsset == null)
{
//UnityEngine.Debug.LogErrorFormat("[FMOD] Could not find marker file Resources/{0}.json", resourcePath);
return null;
}
return Load((TextAsset)jsonAsset);
}
}
[Serializable]
public class FmodException : Exception
{
public FmodException() { }
public FmodException(string message) : base(message) { }
public FmodException(string message, Exception inner) : base(message, inner) { }
protected FmodException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}