-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoremiPlayer.cs
371 lines (287 loc) · 11.8 KB
/
DoremiPlayer.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Audio;
using DG.Tweening;
public class DoremiPlayer : MonoBehaviour
{
public DoremiMusic currentMusic;
public PlayerSetting playerSetting = PlayerSetting.Stopped;
private int currentChannel;
public AudioMixerGroup mixerGroup;
public AudioSource masterChannel;
public List<AudioSource> mainChannels;
public List<AudioSource> loopChannels;
public double dspTimeAtSongStart;
public float blendDuration = 0f;
public DoremiMusic CurrentMusic {
get {
return currentMusic;
}
set {
mainChannels.Clear();
loopChannels.Clear();
currentMusic = value;
if (currentMusic.loopEndSamples == -1) {
currentMusic.loopEndSamples = currentMusic.audio[0].samples;
}
// Clear the old music players
foreach (Transform child in transform) {
Destroy(child.gameObject);
}
// The master channel will help keep track of timing n stuff
// GameObject masterGO = new GameObject("Master Audio");
// masterChannel = masterGO.AddComponent<AudioSource>();
// masterChannel.clip = currentMusic.audio[0];
// masterChannel.volume = 0f;
// masterChannel.playOnAwake = false;
// Create a new clip for each subtrack
foreach (AudioClip clip in currentMusic.audio) {
GameObject newObj = new GameObject();
if (currentMusic.loopStartSamples > 0) {
AudioSource newSource = newObj.AddComponent<AudioSource>();
newSource.clip = SnipAudioClip(clip, currentMusic.loopStartSamples, currentMusic.loopEndSamples, SnipSetting.Intro);
newSource.loop = false;
newSource.volume = 1f;
newSource.outputAudioMixerGroup = mixerGroup;
mainChannels.Add(newSource);
Debug.Log("Intro clip has been created and should be playing");
}
GameObject loopObj = new GameObject();
AudioSource loopSource = loopObj.AddComponent<AudioSource>();
loopSource.clip = SnipAudioClip(clip, currentMusic.loopStartSamples, currentMusic.loopEndSamples, SnipSetting.Loop);
loopSource.loop = true;
loopSource.volume = 1f;
loopSource.outputAudioMixerGroup = mixerGroup;
loopSource.playOnAwake = false;
loopChannels.Add(loopSource);
loopObj.transform.SetParent(transform, false);
newObj.transform.SetParent(loopObj.transform, false);
}
}
}
public int CurrentPosition {
get {
if (dspTimeAtSongStart == 0) return -2;
int output = -1;
// First let's find out if we're in the loop part yet...
if (!InLoopPortion) {
//Debug.Log("INTRO");
if (mainChannels.Count > 0 && mainChannels[0] != null) {
output = mainChannels[0].timeSamples;
} else {
Debug.LogErrorFormat("Doremi Error: There are 0 main channels; there should be at least one!");
output = 69;
}
} else {
//Debug.Log("CURRENT POSITION IS IN LOOP");
if (mainChannels.Count > 0 && loopChannels.Count > 0 && mainChannels[0] != null && loopChannels[0] != null) {
output = mainChannels[0].clip.samples + loopChannels[0].timeSamples;
} else {
Debug.LogErrorFormat("Doremi Error: There are {0} main channels and {1} loop channels; there should be at least one of each!", mainChannels.Count, loopChannels.Count);
output = 69;
}
}
return output;
}
set {
Debug.LogWarning("Doremi Warning: You're trying to set the current position of the song. This doesn't work quite right.");
// input is sample number
Pause();
// If we're in the intro portion, just adjust
// the samples of the intro players
if (value < currentMusic.loopStartSamples) {
dspTimeAtSongStart = (float)AudioSettings.dspTime - (mainChannels[0].time);
foreach (AudioSource s in mainChannels) {
s.timeSamples = value;
}
}
// If we're in the loop portion, just adjust
// the samples of the loop players
else if (value >= currentMusic.loopStartSamples) {
foreach (AudioSource s in loopChannels) {
s.timeSamples = value - currentMusic.loopStartSamples;
}
}
Resume(remainPaused: true);
if (playerSetting == PlayerSetting.Paused) Pause();
}
}
public int CurrentChannel {
get {
return currentChannel;
}
set {
currentChannel = value;
for (int i = 0; i < mainChannels.Count; i++) {
Debug.LogFormat("Setting volume of channel {0}", i);
if (i == currentChannel) {
Debug.Log("This is the channel");
if (blendDuration <= 0) {
mainChannels[i].volume = 1f;
loopChannels[i].volume = 1f;
} else {
mainChannels[i].DOFade(1f, blendDuration);
loopChannels[i].DOFade(1f, blendDuration);
}
} else {
if (blendDuration <= 0) {
mainChannels[i].volume = 0f;
loopChannels[i].volume = 0f;
} else {
mainChannels[i].DOFade(0f, blendDuration);
loopChannels[i].DOFade(0f, blendDuration);
}
}
}
}
}
public float CurrentPositionFraction {
get {
if (dspTimeAtSongStart == 0) return 0f;
int cPos = CurrentPosition;
int finalPos = currentMusic.loopEndSamples;
float frac = (float)cPos / (float)finalPos;
return frac;
}
set {
int samples = (int)(value * currentMusic.loopEndSamples);
CurrentPosition = samples;
}
}
public bool InLoopPortion {
get {
bool inLoop = loopChannels[0].time > 0;
return inLoop;
}
}
private float secondsToLoopStart {
get {
return currentMusic.loopStartSamples / (float)AudioSettings.outputSampleRate;
}
}
private float timeWhereLoopStarts {
get {
return (float)dspTimeAtSongStart + secondsToLoopStart;
}
}
public void Stop() {
playerSetting = PlayerSetting.Stopped;
foreach (Transform child in transform) {
Destroy(child.gameObject);
}
mainChannels.Clear();
loopChannels.Clear();
dspTimeAtSongStart = 0;
}
public void Play(DoremiMusic m) {
playerSetting = PlayerSetting.Playing;
CurrentMusic = m;
int i = 0;
foreach (AudioSource a in mainChannels) {
a.Play();
}
i = 0;
foreach (AudioSource b in loopChannels) {
dspTimeAtSongStart = AudioSettings.dspTime;
b.PlayScheduled(timeWhereLoopStarts);
}
CurrentChannel = 0;
Debug.Log("Doremi - Playing music");
}
public void Pause() {
foreach (AudioSource a in mainChannels) {
a.Pause();
}
foreach (AudioSource l in loopChannels) {
if (InLoopPortion) {
Debug.Log("Pausing while in the loop");
l.Pause();
}
else {
Debug.Log("Pausing while not in the loop");
l.Stop();
}
}
playerSetting = PlayerSetting.Paused;
}
public void Resume(bool remainPaused = false) {
if (!remainPaused) playerSetting = PlayerSetting.Playing;
if (InLoopPortion) {
foreach (AudioSource a in loopChannels) {
a.UnPause();
}
} else {
// We're in the intro, so play the intro channels
foreach (AudioSource a in mainChannels) {
a.UnPause();
}
float currentDspTime = (float)AudioSettings.dspTime;
dspTimeAtSongStart = (float)AudioSettings.dspTime - (mainChannels[0].time);
// We need to reschedule the loop channels, so let's first cancel the current one...
foreach (AudioSource l in loopChannels) {
l.Stop();
l.PlayScheduled(timeWhereLoopStarts);
}
}
}
public void SetChannelVolume(int channel, float volume, float duration = 0f) {
mainChannels[channel].DOFade(volume, duration);
loopChannels[channel].DOFade(volume, duration);
}
public void FadeOut(float duration) {
foreach (AudioSource a in mainChannels) {
a.DOFade(0f, duration);
}
foreach (AudioSource b in loopChannels) {
b.DOFade(0f, duration);
}
}
public void StopMusic() {}
public void StartMusic() {}
private AudioClip SnipAudioClip(AudioClip clipIn, int startSamples, int endSamples, SnipSetting setting) {
int clipOutSamples;
if (setting == SnipSetting.Loop) {clipOutSamples = endSamples - startSamples;}
else {clipOutSamples = startSamples;}
AudioClip clipOut = AudioClip.Create("Look Segment", clipOutSamples, clipIn.channels, clipIn.frequency, false);
float[] samplesToCopy = new float[clipOut.samples * clipOut.channels];
int pointToGetData;
if (setting == SnipSetting.Loop) {pointToGetData = startSamples;}
else {pointToGetData = 0;}
clipIn.GetData(samplesToCopy, pointToGetData);
clipOut.SetData(samplesToCopy, 0);
return clipOut;
}
}
public enum SnipSetting {
Intro = 0,
Loop = 1
}
public enum PlayerSetting {
Stopped = 0,
Playing = 1,
Paused = 2
}
// [CustomEditor(typeof(DoremiPlayer))]
// public class DoremiPlayerEditor : Editor {
// SerializedProperty currentMusic;
// SerializedProperty mixerGroup;
// SerializedProperty currentPos;
// SerializedProperty currentPosFrac;
// void OnEnable() {
// currentMusic = serializedObject.FindProperty("currentMusic");
// mixerGroup = serializedObject.FindProperty("mixerGroup");
// currentPos = serializedObject.FindProperty("CurrentPosition");
// currentPosFrac = serializedObject.FindProperty("CurrentPositionFraction");
// }
// public override void OnInspectorGUI() {
// serializedObject.Update();
// EditorGUILayout.PropertyField(currentMusic);
// EditorGUILayout.PropertyField(mixerGroup);
// // Debug.Log(currentPos.exposedReferenceValue);
// EditorGUILayout.Slider("Position", currentPosFrac.floatValue, 0f, 1f);
// EditorGUILayout.LabelField("", $"{currentPos.intValue}/{((DoremiMusic)currentMusic.exposedReferenceValue).loopEndSamples}");
// serializedObject.ApplyModifiedProperties();
// }
// }