-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMuzakPlayer.cs
313 lines (284 loc) · 11 KB
/
MuzakPlayer.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace Muzak
{
public class MuzakPlayerEvent : UnityEvent<MuzakPlayerEvent.MuzakEventInfo>
{
public enum eEventType
{
PlayerPlay,
PlayerPause,
PlayerStop,
TrackLoopStarted,
TrackLoopEnded,
SequenceStarted,
SequenceSkipped,
SequenceEnded,
}
public struct MuzakEventInfo
{
public eEventType EventType;
public MuzakTrack Track;
public MuzakPlayer Player;
public int Channel;
public int Sequence;
}
}
public class MuzakPlayer : MonoBehaviour
{
public enum ePlayState
{
Stopped,
Stopping,
Paused,
Playing,
}
public MuzakPlayerEvent EventListener { get; private set; } = new MuzakPlayerEvent();
public ePlayState PlayState { get; private set; }
public double CurrentLoopTime { get; set; }
public MuzakTrack Track;
[Range(0, 1)]
public float Strength = 1;
public float FadeInTime = 1;
public AnimationCurve FadeInCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
public float FadeOutTime = 1;
public AnimationCurve FadeOutCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
public AudioSource AudioSourcePrototype;
private Coroutine m_currentCoroutine;
private Dictionary<MuzakSequence, (MuzakChannel, AudioSource)> m_sequenceSourceMappings = new Dictionary<MuzakSequence, (MuzakChannel, AudioSource)>();
public void TogglePlaying()
{
if(PlayState == ePlayState.Stopping || PlayState == ePlayState.Stopped)
{
Play();
}
else if (PlayState == ePlayState.Playing)
{
Stop();
}
}
public void Play()
{
if (!Track)
{
return;
}
PlayState = ePlayState.Playing;
if (m_currentCoroutine == null)
{
m_currentCoroutine = StartCoroutine(PlayTrackAsync(Track));
}
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.PlayerPlay,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
}
public void Pause()
{
PlayState = ePlayState.Paused;
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.PlayerPause,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
}
public void Stop()
{
PlayState = ePlayState.Stopping;
}
private AudioSource GetAudioSource(MuzakChannel channel, MuzakSequence sequence)
{
var channelIndex = Track.Channels.IndexOf(channel);
var sequenceIndex = Track.Channels[channelIndex].Sequences.IndexOf(sequence);
AudioSource source;
if (AudioSourcePrototype)
{
source = Instantiate(AudioSourcePrototype.gameObject)
.GetComponent<AudioSource>();
}
else
{
source = new GameObject($"MuzakTrackPlayer_{channel.Clip.name}")
.AddComponent<AudioSource>();
}
source.transform.SetParent(transform);
source.clip = channel.Clip;
source.outputAudioMixerGroup = Track.Mixer;
source.playOnAwake = false;
source.gameObject.hideFlags = HideFlags.HideAndDontSave;
return source;
}
private void StartSequence(MuzakChannel channel, MuzakSequence sequence, AudioSource source)
{
var channelIndex = Track.Channels.IndexOf(channel);
var sequenceIndex = Track.Channels[channelIndex].Sequences.IndexOf(sequence);
var roll = Random.value;
if (roll > sequence.Probability)
{
StartCoroutine(DelayedEvent(new MuzakPlayerEvent.MuzakEventInfo
{
Channel = channelIndex,
Sequence = sequenceIndex,
Track = Track,
EventType = MuzakPlayerEvent.eEventType.SequenceSkipped,
Player = this,
}, (float)sequence.StartTime));
return;
}
source.time = (float)sequence.Offset;
source.PlayScheduled(AudioSettings.dspTime + sequence.StartTime);
source.SetScheduledEndTime(AudioSettings.dspTime + sequence.StartTime + sequence.Duration);
StartCoroutine(DelayedEvent(new MuzakPlayerEvent.MuzakEventInfo
{
Channel = channelIndex,
Sequence = sequenceIndex,
Track = Track,
EventType = MuzakPlayerEvent.eEventType.SequenceStarted,
Player = this,
}, (float)sequence.StartTime));
StartCoroutine(DelayedEvent(new MuzakPlayerEvent.MuzakEventInfo
{
Channel = channelIndex,
Sequence = sequenceIndex,
Track = Track,
EventType = MuzakPlayerEvent.eEventType.SequenceEnded,
Player = this,
}, (float)(sequence.StartTime + sequence.Duration)));
}
private IEnumerator DelayedEvent(MuzakPlayerEvent.MuzakEventInfo eventInfo, float delay)
{
yield return new WaitForSeconds(delay);
EventListener.Invoke(eventInfo);
}
void OnDisable()
{
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.PlayerStop,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
}
IEnumerator PlayTrackAsync(MuzakTrack track)
{
while (PlayState != ePlayState.Playing)
{
yield return null;
}
var loopStartTime = AudioSettings.dspTime;
var playingness = 0f;
do
{
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.TrackLoopStarted,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
const double lookAhead = .0;
foreach (var channel in track.Channels)
{
foreach (var sequence in channel.Sequences)
{
AudioSource audioSource;
if (m_sequenceSourceMappings.TryGetValue(sequence, out var source) && source.Item2)
{
audioSource = source.Item2;
}
else
{
audioSource = GetAudioSource(channel, sequence);
}
if (audioSource == null)
{
continue;
}
m_sequenceSourceMappings[sequence] = (channel, audioSource);
StartSequence(channel, sequence, audioSource);
}
}
while (PlayState != ePlayState.Stopped && AudioSettings.dspTime < (loopStartTime + Track.Duration) - lookAhead)
{
var loopT = AudioSettings.dspTime - loopStartTime;
CurrentLoopTime = loopT;
// Depending on if we're starting or stopping change the transition amount
switch (PlayState)
{
case ePlayState.Playing:
playingness = Mathf.Clamp(playingness + Time.deltaTime, 0, FadeInTime);
if (playingness < FadeInTime)
{
Strength = FadeInCurve.Evaluate(playingness / FadeInTime);
}
else
{
Strength = 1;
}
break;
case ePlayState.Stopping:
playingness = Mathf.Clamp(playingness - Time.deltaTime, 0, FadeOutTime);
if (playingness > 0)
{
Strength = FadeOutCurve.Evaluate(playingness / FadeInTime);
}
else
{
Strength = 0;
PlayState = ePlayState.Stopped;
}
break;
}
foreach (var sequenceSourceMapping in m_sequenceSourceMappings)
{
var sequence = sequenceSourceMapping.Key;
var channel = sequenceSourceMapping.Value.Item1;
var source = sequenceSourceMapping.Value.Item2;
// Adjust volume
if (loopT < sequence.StartTime || loopT > sequence.StartTime + sequence.Duration)
{
source.volume = 0;
continue;
}
var sequenceTime = loopT - (float)sequence.StartTime;
source.volume = channel.Volume *
sequence.StrengthCurve.Evaluate(Strength) *
sequence.VolumeCurve.Evaluate((float)sequenceTime / (float)sequence.Duration);
}
yield return null;
}
loopStartTime = AudioSettings.dspTime + lookAhead;
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.TrackLoopEnded,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
}
while (PlayState != ePlayState.Stopped && track.Loop);
EventListener.Invoke(new MuzakPlayerEvent.MuzakEventInfo
{
EventType = MuzakPlayerEvent.eEventType.PlayerStop,
Player = this,
Track = Track,
Channel = -1,
Sequence = -1,
});
m_currentCoroutine = null;
}
}
}