-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAudioSourceDataObserver.cs
More file actions
104 lines (93 loc) · 3.35 KB
/
Copy pathAudioSourceDataObserver.cs
File metadata and controls
104 lines (93 loc) · 3.35 KB
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
namespace Zinnia.Audio
{
using UnityEngine;
using UnityEngine.Events;
using System;
using Malimbe.PropertySerializationAttribute;
using Malimbe.XmlDocumentationAttribute;
/// <summary>
/// Observes the <see cref="AudioSource"/> and emits the audio data.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class AudioSourceDataObserver : MonoBehaviour
{
/// <summary>
/// Holds data about a <see cref="AudioSourceDataObserver"/> event.
/// </summary>
[Serializable]
public class EventData
{
/// <summary>
/// <see cref="AudioSettings.dspTime"/> of the last <see cref="OnAudioFilterRead"/>.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public double DspTime { get; set; }
/// <summary>
/// Audio data array of the last <see cref="OnAudioFilterRead"/>.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public float[] Data { get; set; }
/// <summary>
/// Number of channels of the last <see cref="OnAudioFilterRead"/>.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public int Channels { get; set; }
public EventData Set(EventData source)
{
return Set(source.DspTime, source.Data, source.Channels);
}
public EventData Set(double dspTime, float[] data, int channels)
{
DspTime = dspTime;
Data = data;
Channels = channels;
return this;
}
public void Clear()
{
Set(default, default, default);
}
}
/// <summary>
/// Defines the event with the <see cref="EventData"/>.
/// </summary>
[Serializable]
public class UnityEvent : UnityEvent<EventData> { }
/// <summary>
/// Emitted whenever the audio data is observed.
/// </summary>
[DocumentedByXml]
public UnityEvent DataObserved = new UnityEvent();
/// <summary>
/// The data to emit with an event.
/// </summary>
protected readonly EventData eventData = new EventData();
/// <summary>
/// Returns whether the <see cref="AudioSource"/> is player.
/// </summary>
public virtual bool IsAudioSourcePlaying() => audioSource != null && audioSource.isPlaying;
/// <summary>
/// The <see cref="AudioSource"/> to observe.
/// </summary>
protected AudioSource audioSource;
/// <summary>
/// Caches the <see cref="AudioSource"/>.
/// </summary>
protected virtual void Awake()
{
audioSource = GetComponent<AudioSource>();
}
/// <summary>
/// Emits audio data.
/// </summary>
/// <param name="data">An array of floats comprising the audio data.</param>
/// <param name="channels">An int that stores the number of channels of audio data passed to this delegate.</param>
protected virtual void OnAudioFilterRead(float[] data, int channels)
{
DataObserved?.Invoke(eventData.Set(AudioSettings.dspTime, data, channels));
}
}
}