-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComponents.cs
More file actions
490 lines (426 loc) · 14.9 KB
/
Copy pathComponents.cs
File metadata and controls
490 lines (426 loc) · 14.9 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
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
using BeatSaberMarkupLanguage.Settings;
using IPA;
using IPA.Config;
using IPA.Loader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Audio;
namespace MusicSpatializer
{
public class SpeakerCreator : MonoBehaviour
{
public AudioClip dclip;
public GameObject speakerLeft;
public GameObject speakerRight;
public GameObject speakerResonance;
public GameObject speakerBass;
public float frontDistance = 20;
public float sideDistance = 15f;
public float volumeMultiplier = 1;
public AudioSplitter splitter;
public AudioMixerGroup mixerGroup;
public bool spatialize = true;
public bool resonance = true;
public bool bassBoost = false;
public bool doRotation = true;
public bool debugSpheres = false;
public Plugin pluginReference;
private GameObject rotationMarker;
private int rotationMarkerTries = 10;
// Start is called before the first frame update
void Start()
{
Create();
pluginReference.InjectAfterStart();
}
// Update is called once per frame
void Update()
{
//sideDistance += 0.01f;
//Create();
if (doRotation)
{
//rotate audio based on the chevron in 360 maps
if (rotationMarker != null)
{
//Console.WriteLine("found Chevron");
transform.rotation = rotationMarker.transform.rotation;
}
else
{
//Console.WriteLine("not found Chevron");
if (rotationMarkerTries > 0)
{
//rotationMarker = GameObject.Find("SpawnRotationChevron");
rotationMarker = GameObject.Find("FlyingGameHUD");
if (rotationMarker != null)
{
sideDistance = 7f;
PositionSpeakers();
}
rotationMarkerTries--;
}
}
}
if (splitter.gameObject.activeInHierarchy == false)
{
Remover();
}
}
void Remover()
{
if (splitter != null)
{
UnityEngine.Object.Destroy(splitter);
}
UnityEngine.Object.Destroy(this.gameObject);
pluginReference.InjectLooper(5);
}
void OnDestroy()
{
Remover();
}
void OnDisable()
{
Remover();
}
GameObject NewSpeaker(int channel)
{
GameObject speaker;
if (debugSpheres)
{
speaker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
}
else
{
speaker = new GameObject();
}
speaker.transform.parent = transform;
speaker.name = "Music Spatializer Speaker";
AudioReader reader = speaker.AddComponent<AudioReader>();
reader.channel = channel;
reader.splitter = splitter;
reader.mono = spatialize;
if (channel == -1 || channel == 21)
{
reader.allChannels = true;
}
AudioSource source = speaker.AddComponent<AudioSource>();
if (mixerGroup != null)
{
source.outputAudioMixerGroup = mixerGroup;
}
//Plugin.Log("AudioSource origninal: {0}", source.outputAudioMixerGroup.name);
source.spatialize = spatialize;
source.spatializePostEffects = spatialize;
source.bypassEffects = false;
source.bypassListenerEffects = true;
source.bypassReverbZones = true;
source.dopplerLevel = 0;
source.clip = dclip;
source.rolloffMode = AudioRolloffMode.Linear;
source.minDistance = 1000000;
source.maxDistance = 1000000;
source.spatialBlend = spatialize ? 1 : 0;
source.volume = 1;
reader.volume = (spatialize ? 0.275f : 0.4f) * volumeMultiplier;
source.priority = 0;
source.ignoreListenerPause = true;
source.Play();
if (channel == -1)
{
source.spatialize = false;
reader.volume = 0.2f * volumeMultiplier;
source.reverbZoneMix = 1.0f;
source.spatialBlend = 0;
source.bypassReverbZones = false;
//AudioLowPassFilter lowpass = speaker.AddComponent<AudioLowPassFilter>();
//lowpass.cutoffFrequency = 350;
Silencer silence = speaker.AddComponent<Silencer>();
AudioReverbZone reverb = speaker.AddComponent<AudioReverbZone>();
reverb.reverbPreset = AudioReverbPreset.Hangar;
}
if (channel == 21)
{
AudioLowPassFilter lowpass = speaker.AddComponent<AudioLowPassFilter>();
lowpass.cutoffFrequency = 300;
//source.spatialize = false;
reader.volume = 0.5f * volumeMultiplier;
}
//channel_filter chfilt = speaker.AddComponent<channel_filter>();
//chfilt.channel = channel;
return speaker;
}
void PositionSpeakers()
{
if (speakerLeft)
{
speakerLeft.transform.localPosition = new Vector3(-sideDistance, 1.5f, frontDistance);
}
if (speakerRight)
{
speakerRight.transform.localPosition = new Vector3(sideDistance, 1.5f, frontDistance);
}
if (speakerResonance)
{
speakerResonance.transform.localPosition = new Vector3(0, 3, 0);
}
if (speakerBass)
{
speakerBass.transform.localPosition = new Vector3(0, -3f, 10f);
}
}
void Create()
{
if (speakerLeft)
{
Destroy(speakerLeft);
}
if (speakerRight)
{
Destroy(speakerRight);
}
speakerLeft = NewSpeaker(0);
speakerRight = NewSpeaker(1);
if (resonance)
{
speakerResonance = NewSpeaker(-1);
}
if (bassBoost)
{
speakerBass = NewSpeaker(21);
}
PositionSpeakers();
}
}
public class AudioSplitter : MonoBehaviour
{
public float[][] channelData;
public bool[] beenUsed;
public int iteration = 0;
public bool ready = false;
AudioSource source;
//float lastTime = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (source == null)
{
source = gameObject.GetComponent<AudioSource>();
source.spatialBlend = 0;
source.reverbZoneMix = 0;
source.dopplerLevel = 0;
source.bypassEffects = false;
}
else
{
source.spatialBlend = 0;
source.reverbZoneMix = 0;
source.dopplerLevel = 0;
source.bypassEffects = false;
/*if (source.time == 0)
{
source.time = lastTime;
source.Play();
}
lastTime = source.time;*/
}
}
void OnAudioFilterRead(float[] data, int channels)
{
int dataLen = data.Length / channels;
if (channelData?.Length != channels)
{
channelData = new float[channels][];
beenUsed = new bool[channels];
}
int c = 0;
while (c < channels)
{
if (channelData[c]?.Length != dataLen)
{
channelData[c] = new float[dataLen];
}
beenUsed[c] = false;
c++;
}
iteration++;
int n = 0;
while (n < dataLen)
{
int i = 0;
while (i < channels)
{
channelData[i][n] = data[n * channels + i];
data[n * channels + i] = 0;
i++;
}
n++;
}
ready = true;
}
}
public class AudioReader : MonoBehaviour
{
public int channel = 0;
public bool allChannels = false;
public bool mono = true;
public float volume = 1.0f;
private int iteration = 0;
public AudioSplitter splitter;
AudioSource source;
// Start is called before the first frame update
void Start()
{
//Console.WriteLine("sample {0}", slitData[0]);
}
// Update is called once per frame
void Update()
{
if (source == null)
{
source = gameObject.GetComponent<AudioSource>();
}
else
{
if (source.isPlaying == false)
{
if (MusicSpatializer.Settings.Configuration.config.debugSpheres)
{
Plugin.log.Info("Speaker restarted");
}
source.Play();
}
}
/*
Console.WriteLine("======");
//Component[] comps = transform.GetComponents(typeof(Component));
Component[] comps = splitter.GetComponents(typeof(Component));
foreach (Component c in comps)
{
Type ctype = c.GetType();
Console.WriteLine("Component name: {0}", ctype.ToString());
}//*/
//AudioSource source=transform.GetComponent<AudioSource>();
//AudioSource mainSource=splitter.GetComponent<AudioSource>();
//Console.WriteLine("AudioSource vol:{0} active:{1} mute:{2}", source.volume, source.isPlaying, source.mute);
//Console.WriteLine("mainSource vol:{0} active:{1} mute:{2} priority:{3}", mainSource.volume, mainSource.isPlaying, mainSource.mute, mainSource.priority); ;
//source.Pause();
//source.Play();
//source.outputAudioMixerGroup = mainSource.outputAudioMixerGroup;
}
void OnAudioFilterRead(float[] data, int channels)
{
//Plugin.Log("reading audio {0} channels", channels);
if (splitter.ready == false)
{
return;
}
//only run if there is new data
if (splitter.iteration - iteration <= 0)
{
return;
}
iteration = splitter.iteration;
int dataLen = data.Length / channels;
int n = 0;
if (allChannels)
{
while (n < dataLen)
{
int i = 0;
while (i < channels)
{
data[n * channels + i] = splitter.channelData[i][n] * volume;
i++;
}
n++;
}
}
else
{
float[] slitData = splitter.channelData[channel];
while (n < dataLen)
{
if (mono)
{
int i = 0;
while (i < channels)
{
data[n * channels + i] = slitData[n] * volume;
i++;
}
}
else
{
data[n * channels + channel] = slitData[n] * volume;
}
n++;
}
}
}
}
public class Silencer : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnAudioFilterRead(float[] data, int channels)
{
int dataLen = data.Length / channels;
int n = 0;
while (n < dataLen)
{
int i = 0;
while (i < channels)
{
data[n * channels + i] = 0;
i++;
}
n++;
}
}
}
//this fixes a bug in the base game where the song AudioSource gets stopped by unity because it runs out of virtual channels due to too many hitsounds
public class MainSongAudioSourceRestarterBugFix : MonoBehaviour
{
private AudioTimeSyncController audioTimeSyncController;
private AudioSource source;
// Start is called before the first frame update
void Start()
{
audioTimeSyncController = GetComponent<AudioTimeSyncController>();
source = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (source != null && audioTimeSyncController != null)
{
if (audioTimeSyncController.state == AudioTimeSyncController.State.Playing && audioTimeSyncController.songTime > 0 && audioTimeSyncController.songTime < audioTimeSyncController.songEndTime - 0.5f && source.isPlaying == false && source.time == 0)
{
Plugin.log.Info("Song Time: " + audioTimeSyncController.songTime + " Song End Time: " + audioTimeSyncController.songEndTime);
source.time = audioTimeSyncController.songTime;
source.Play();
if (Plugin.log != null)
{
Plugin.log.Error("Main song AudioSource had to be restarted. This is a bug in the base game.");
}
}
}
}
}
}