Queue.cs 9.5 KB

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