Queue.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Il2CppInterop.Runtime.Attributes;
  2. using MelonLoader;
  3. using System.Collections;
  4. using UnityEngine;
  5. namespace AudioMgr
  6. {
  7. public class Queue : MonoBehaviour
  8. {
  9. public Queue(IntPtr intPtr) : base(intPtr)
  10. {
  11. }
  12. private Dictionary<bool, AudioSource> _audioSources;
  13. private bool _toggleAudioSource = true;
  14. private Setting _activeSetting;
  15. private AudioMaster.SourceType _sourceType;
  16. private ClipManager _assignedClipManager;
  17. private float _timeGap = 2f;
  18. private float _lowerRandomGap = 0;
  19. private float _upperRandomGap = 2;
  20. private bool _randomGap = false;
  21. private int _currentClipIndex = 0;
  22. private object _timerLoop;
  23. public enum Loop { None, Single, All, Randomize };
  24. private Loop _loop = Loop.All;
  25. public enum PlayState { Stopped, Playing, Paused };
  26. private PlayState _playState = PlayState.Stopped;
  27. [HideFromIl2Cpp]
  28. public void Setup(ClipManager assignedClipManager, float timeGap, Loop loopType, AudioMaster.SourceType sourceType)
  29. {
  30. _assignedClipManager = assignedClipManager;
  31. _sourceType = sourceType;
  32. _timeGap = timeGap;
  33. _loop = loopType;
  34. _audioSources = new Dictionary<bool, AudioSource>();
  35. _audioSources.Add(true, gameObject.AddComponent<AudioSource>());
  36. _audioSources.Add(false, gameObject.AddComponent<AudioSource>());
  37. _audioSources[true].playOnAwake = false;
  38. _audioSources[false].playOnAwake = false;
  39. _audioSources[true].volume = VolumeMaster.GetVolume(sourceType);
  40. _audioSources[false].volume = VolumeMaster.GetVolume(sourceType);
  41. VolumeMaster.onVolumeChange += ResetVolume;
  42. ApplySettings(SettingMaster.Defaults(sourceType));
  43. }
  44. [HideFromIl2Cpp]
  45. public void Setup(ClipManager assignedClipManager, float timeGap, Loop loopType, Setting sourceSetting)
  46. {
  47. _assignedClipManager = assignedClipManager;
  48. _timeGap = timeGap;
  49. _loop = loopType;
  50. _sourceType = AudioMaster.SourceType.Custom;
  51. _audioSources = new Dictionary<bool, AudioSource>();
  52. _audioSources.Add(true, gameObject.AddComponent<AudioSource>());
  53. _audioSources.Add(false, gameObject.AddComponent<AudioSource>());
  54. _audioSources[true].playOnAwake = false;
  55. _audioSources[false].playOnAwake = false;
  56. _audioSources[true].volume = VolumeMaster.GetVolume(_sourceType);
  57. _audioSources[false].volume = VolumeMaster.GetVolume(_sourceType);
  58. VolumeMaster.onVolumeChange += ResetVolume;
  59. ApplySettings(sourceSetting);
  60. }
  61. [HideFromIl2Cpp]
  62. public int GetNextClip()
  63. {
  64. if (_loop == Loop.Single) // Keep current clip
  65. {
  66. return _currentClipIndex;
  67. }
  68. else if (_loop == Loop.Randomize)
  69. {
  70. int randomIndex = _currentClipIndex;
  71. while (randomIndex == _currentClipIndex)
  72. {
  73. randomIndex = UnityEngine.Random.Range(0, _assignedClipManager.clipCount - 1);
  74. }
  75. return randomIndex;
  76. }
  77. else if (_currentClipIndex < _assignedClipManager.clipCount - 1) // Not at the end yet, increase by 1
  78. {
  79. return _currentClipIndex + 1;
  80. }
  81. else if (_currentClipIndex >= _assignedClipManager.clipCount - 1)
  82. {
  83. if (_loop == Loop.All)
  84. {
  85. return 0;
  86. }
  87. else
  88. {
  89. return -1;
  90. }
  91. }
  92. return -1;
  93. }
  94. [HideFromIl2Cpp]
  95. public void SetRandomTimeGap(float lowestTimeGap, float highestTimeGap)
  96. {
  97. _randomGap = true;
  98. _lowerRandomGap = lowestTimeGap;
  99. _upperRandomGap = highestTimeGap;
  100. }
  101. public void SetFixedTimeGap(float timeGap)
  102. {
  103. _randomGap = false;
  104. _timeGap = timeGap;
  105. }
  106. [HideFromIl2Cpp]
  107. public void Play()
  108. {
  109. if (_playState == PlayState.Stopped && _assignedClipManager.clipCount > 0)
  110. {
  111. _playState = PlayState.Playing;
  112. _timerLoop = MelonCoroutines.Start(TimerLoop());
  113. }
  114. else if (_playState == PlayState.Paused)
  115. {
  116. AudioListener.pause = false;
  117. _playState = PlayState.Playing;
  118. }
  119. }
  120. [HideFromIl2Cpp]
  121. public void Stop()
  122. {
  123. if (_timerLoop != null)
  124. {
  125. MelonCoroutines.Stop(_timerLoop);
  126. _playState = PlayState.Stopped;
  127. _audioSources[true].Stop();
  128. _audioSources[false].Stop();
  129. }
  130. }
  131. [HideFromIl2Cpp]
  132. private IEnumerator TimerLoop()
  133. {
  134. double _startTime;
  135. double _timeToNext = 0;
  136. int _nextClip = 0;
  137. _startTime = AudioSettings.dspTime + 0.5;
  138. if (_randomGap)
  139. {
  140. _timeGap = UnityEngine.Random.Range(_lowerRandomGap, _upperRandomGap);
  141. }
  142. _audioSources[_toggleAudioSource].clip = _assignedClipManager.GetClipAtIndex(_currentClipIndex).audioClip;
  143. _audioSources[_toggleAudioSource].PlayScheduled(_startTime + _timeGap);
  144. _timeToNext = _startTime;
  145. while (true)
  146. {
  147. if (_playState == PlayState.Playing)
  148. {
  149. // Assign new clip
  150. _nextClip = GetNextClip();
  151. if (_nextClip < 0)
  152. {
  153. yield break;
  154. }
  155. if(_randomGap)
  156. {
  157. _timeGap = UnityEngine.Random.Range(_lowerRandomGap, _upperRandomGap);
  158. }
  159. _toggleAudioSource = !_toggleAudioSource;
  160. _timeToNext = _timeToNext + _assignedClipManager.GetClipAtIndex(_currentClipIndex).clipLength + _timeGap;
  161. _audioSources[_toggleAudioSource].clip = _assignedClipManager.GetClipAtIndex(_nextClip).audioClip;
  162. _audioSources[_toggleAudioSource].PlayScheduled(_timeToNext);
  163. while (AudioSettings.dspTime < _timeToNext + 0.05)
  164. {
  165. yield return null;
  166. }
  167. _currentClipIndex = _nextClip;
  168. }
  169. yield return null;
  170. }
  171. }
  172. [HideFromIl2Cpp]
  173. public void AssignClipManager(ClipManager assignedClipManager, float timeGap, Loop loopType)
  174. {
  175. Stop();
  176. _assignedClipManager = assignedClipManager;
  177. _timeGap = timeGap;
  178. _loop = loopType;
  179. _currentClipIndex = 0;
  180. }
  181. [HideFromIl2Cpp]
  182. private void OnEnable()
  183. {
  184. VolumeMaster.onVolumeChange += ResetVolume;
  185. }
  186. [HideFromIl2Cpp]
  187. private void OnDisable()
  188. {
  189. VolumeMaster.onVolumeChange -= ResetVolume;
  190. }
  191. [HideFromIl2Cpp]
  192. private void OnDestroy()
  193. {
  194. VolumeMaster.onVolumeChange -= ResetVolume;
  195. }
  196. [HideFromIl2Cpp]
  197. private void ResetVolume()
  198. {
  199. if(_sourceType != AudioMaster.SourceType.Custom)
  200. {
  201. _audioSources[_toggleAudioSource].volume = VolumeMaster.GetVolume(_sourceType);
  202. _audioSources[!_toggleAudioSource].volume = VolumeMaster.GetVolume(_sourceType);
  203. }
  204. }
  205. [HideFromIl2Cpp]
  206. public void ApplySettings(Setting newSetting)
  207. {
  208. ApplySettingsToSingle(newSetting, true);
  209. ApplySettingsToSingle(newSetting, false);
  210. }
  211. [HideFromIl2Cpp]
  212. private void ApplySettingsToSingle(Setting newSetting, bool audioSource)
  213. {
  214. _activeSetting = newSetting;
  215. _sourceType = _activeSetting.sourceType;
  216. _audioSources[audioSource].spread = _activeSetting.spread;
  217. _audioSources[audioSource].panStereo = _activeSetting.panStereo;
  218. _audioSources[audioSource].dopplerLevel = _activeSetting.dopplerLevel;
  219. _audioSources[audioSource].maxDistance = _activeSetting.maxDistance;
  220. _audioSources[audioSource].minDistance = _activeSetting.minDistance;
  221. _audioSources[audioSource].pitch = _activeSetting.pitch;
  222. _audioSources[audioSource].spatialBlend = _activeSetting.spatialBlend;
  223. _audioSources[audioSource].spatialize = _activeSetting.spatialize;
  224. _audioSources[audioSource].rolloffFactor = _activeSetting.rolloffFactor;
  225. _audioSources[audioSource].maxVolume = _activeSetting.maxVolume;
  226. _audioSources[audioSource].maxVolume = _activeSetting.minVolume;
  227. _audioSources[audioSource].rolloffMode = _activeSetting.rolloffMode;
  228. //_audioSources[audioSource].SetCustomCurve(AudioSourceCurveType.CustomRolloff, _activeSetting.rollOffCurve);
  229. ResetVolume();
  230. }
  231. [HideFromIl2Cpp]
  232. public Loop loop
  233. {
  234. get
  235. {
  236. return _loop;
  237. }
  238. set
  239. {
  240. _loop = value;
  241. }
  242. }
  243. [HideFromIl2Cpp]
  244. public float timeGap
  245. {
  246. get
  247. {
  248. return _timeGap;
  249. }
  250. set
  251. {
  252. _timeGap = value;
  253. }
  254. }
  255. [HideFromIl2Cpp]
  256. public PlayState playState
  257. {
  258. get
  259. {
  260. return _playState;
  261. }
  262. }
  263. [HideFromIl2Cpp]
  264. public AudioMaster.SourceType sourceType
  265. {
  266. get
  267. {
  268. return _sourceType;
  269. }
  270. set
  271. {
  272. _sourceType = value;
  273. ApplySettings(SettingMaster.Defaults(_sourceType));
  274. }
  275. }
  276. }
  277. }