-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAudioSourceHapticPulser.cs
More file actions
216 lines (195 loc) · 7.14 KB
/
Copy pathAudioSourceHapticPulser.cs
File metadata and controls
216 lines (195 loc) · 7.14 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
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
namespace Zinnia.Haptics
{
using UnityEngine;
using UnityEngine.Events;
using System;
using System.Collections;
using Malimbe.MemberChangeMethod;
using Malimbe.MemberClearanceMethod;
using Malimbe.PropertySerializationAttribute;
using Malimbe.XmlDocumentationAttribute;
/// <summary>
/// Creates a haptic pattern based on the waveform of an <see cref="UnityEngine.AudioSource"/> and utilizes a <see cref="Haptics.HapticPulser"/> to create the effect.
/// </summary>
public class AudioSourceHapticPulser : RoutineHapticPulser
{
/// <summary>
/// The waveform to represent the haptic pattern.
/// </summary>
[Serialized, Cleared]
[field: DocumentedByXml]
public AudioSource AudioSource { get; set; }
/// <summary>
/// Observer added to <see cref="AudioSource"/>.
/// </summary>
protected AudioSourceDataObserver observer;
/// <summary>
/// The observed audio data.
/// </summary>
protected readonly AudioSourceDataObserver.EventData audioData = new AudioSourceDataObserver.EventData();
/// <inheritdoc />
public override bool IsActive()
{
return base.IsActive() && AudioSource != null;
}
/// <inheritdoc />
protected override void DoCancel()
{
RemoveDataObserver();
base.DoCancel();
}
/// <summary>
/// Enumerates through <see cref="AudioSource"/> and pulses for each amplitude of the wave.
/// </summary>
/// <returns>An Enumerator to manage the running of the Coroutine.</returns>
protected override IEnumerator HapticProcessRoutine()
{
AddDataObserver();
int outputSampleRate = AudioSettings.outputSampleRate;
while (AudioSource != null && AudioSource.isPlaying)
{
float currentSample = 0;
if (audioData.Data != null)
{
int sampleIndex = (int)((AudioSettings.dspTime - audioData.DspTime) * outputSampleRate) * audioData.Channels;
sampleIndex = Mathf.Min(sampleIndex, audioData.Data.Length - audioData.Channels);
for (int i = 0; i < audioData.Channels; ++i)
{
currentSample += Mathf.Abs(audioData.Data[sampleIndex + i]);
}
currentSample /= audioData.Channels;
}
HapticPulser.Intensity = currentSample * IntensityMultiplier;
HapticPulser.Begin();
yield return null;
}
RemoveDataObserver();
ResetIntensity();
}
/// <summary>
/// Adds a <see cref="AudioSourceHapticPulserDataObserver"/> to the <see cref="AudioSource"/>.
/// </summary>
protected virtual void AddDataObserver()
{
if (AudioSource == null)
{
return;
}
observer = AudioSource.gameObject.AddComponent<AudioSourceDataObserver>();
observer.DataObserved.AddListener(Receive);
}
/// <summary>
/// Remove the <see cref="AudioSourceHapticPulserDataObserver"/> from the <see cref="AudioSource"/>.
/// </summary>
protected virtual void RemoveDataObserver()
{
if (observer == null)
{
return;
}
observer.DataObserved.RemoveListener(Receive);
Destroy(observer);
observer = null;
}
/// <summary>
/// Receive audio data from <see cref="AudioSourceHapticPulserDataObserver"/>.
/// </summary>
protected virtual void Receive(AudioSourceDataObserver.EventData eventData)
{
audioData.Set(eventData);
}
/// <summary>
/// Called before <see cref="AudioSource"/> has been changed.
/// </summary>
[CalledBeforeChangeOf(nameof(AudioSource))]
protected virtual void OnBeforeAudioSourceChange()
{
if (hapticRoutine == null)
{
return;
}
RemoveDataObserver();
}
/// <summary>
/// Called after <see cref="AudioSource"/> has been changed.
/// </summary>
[CalledAfterChangeOf(nameof(AudioSource))]
protected virtual void OnAfterAudioSourceChange()
{
if (hapticRoutine == null)
{
return;
}
AddDataObserver();
}
}
/// <summary>
/// Observes the <see cref="AudioSource"/> and emits the audio data.
/// </summary>
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>
/// 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));
}
}
}