Queue.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 AudioSource _audioSource;
  13. private Setting _activeSetting;
  14. private AudioMaster.SourceType _sourceType;
  15. private ClipManager _assignedClipManager;
  16. private float _timeGap = 2f;
  17. private float _lowerRandomGap = 0;
  18. private float _upperRandomGap = 2;
  19. private bool _randomGap = false;
  20. private int _activeClip = 0;
  21. private bool _isSetup = false;
  22. private float _silenceTimer = 0f;
  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. _audioSource = gameObject.AddComponent<AudioSource>();
  35. _audioSource.playOnAwake = false;
  36. _audioSource.volume = VolumeMaster.GetVolume(sourceType);
  37. VolumeMaster.onVolumeChange += ResetVolume;
  38. ApplySettings(SettingMaster.Defaults(sourceType));
  39. _isSetup = true;
  40. }
  41. [HideFromIl2Cpp]
  42. public void Setup(ClipManager assignedClipManager, float timeGap, Loop loopType, Setting sourceSetting)
  43. {
  44. _assignedClipManager = assignedClipManager;
  45. _timeGap = timeGap;
  46. _loop = loopType;
  47. _sourceType = AudioMaster.SourceType.Custom;
  48. _audioSource = gameObject.AddComponent<AudioSource>();
  49. _audioSource.playOnAwake = false;
  50. _audioSource.volume = VolumeMaster.GetVolume(sourceType);
  51. VolumeMaster.onVolumeChange += ResetVolume;
  52. ApplySettings(sourceSetting);
  53. _isSetup = true;
  54. }
  55. [HideFromIl2Cpp]
  56. public void SetRandomTimeGap(float lowestTimeGap, float highestTimeGap)
  57. {
  58. _randomGap = true;
  59. _lowerRandomGap = lowestTimeGap;
  60. _upperRandomGap = highestTimeGap;
  61. }
  62. public void SetFixedTimeGap(float timeGap)
  63. {
  64. _randomGap = false;
  65. _timeGap = timeGap;
  66. }
  67. [HideFromIl2Cpp]
  68. public void Play()
  69. {
  70. if(_assignedClipManager.clipCount == 0 || _isSetup == false)
  71. {
  72. return;
  73. }
  74. if (_playState == PlayState.Stopped)
  75. {
  76. _playState = PlayState.Playing;
  77. _audioSource.clip = _assignedClipManager.GetClipAtIndex(_activeClip).audioClip;
  78. _audioSource.PlayDelayed(0.6f);
  79. }
  80. else if (_playState == PlayState.Paused)
  81. {
  82. UnPause();
  83. }
  84. else if(_playState == PlayState.Playing)
  85. {
  86. Clip nextClip = GetNextClip();
  87. if(_audioSource.clip != nextClip.audioClip)
  88. {
  89. _audioSource.clip = nextClip.audioClip;
  90. }
  91. if (_playState == PlayState.Playing)
  92. {
  93. _audioSource.PlayDelayed(0.5f + _timeGap);
  94. }
  95. }
  96. }
  97. public void Update()
  98. {
  99. if (_playState == PlayState.Playing)
  100. {
  101. _silenceTimer += Time.deltaTime;
  102. if (_silenceTimer >= 1f)
  103. {
  104. if (!_audioSource.isPlaying)
  105. {
  106. Play();
  107. }
  108. _silenceTimer = 0f;
  109. }
  110. }
  111. }
  112. [HideFromIl2Cpp]
  113. public void Stop()
  114. {
  115. if (_playState == PlayState.Playing)
  116. {
  117. _playState = PlayState.Stopped;
  118. _audioSource.Stop();
  119. }
  120. }
  121. [HideFromIl2Cpp]
  122. public void UnPause()
  123. {
  124. if (_playState == PlayState.Paused)
  125. {
  126. _playState = PlayState.Playing;
  127. _audioSource.UnPause();
  128. }
  129. }
  130. [HideFromIl2Cpp]
  131. public void Pause()
  132. {
  133. if (_playState == PlayState.Playing)
  134. {
  135. _playState = PlayState.Paused;
  136. _audioSource.Pause();
  137. }
  138. }
  139. [HideFromIl2Cpp]
  140. public Clip GetNextClip()
  141. {
  142. if (_loop == Loop.None) // Keep current clip
  143. {
  144. Stop();
  145. }
  146. else if (_loop == Loop.Single) // Keep current clip
  147. {
  148. _activeClip = _activeClip; // duh
  149. }
  150. else if (_loop == Loop.Randomize)
  151. {
  152. int randomIndex = _activeClip;
  153. if(_assignedClipManager.clipCount != 1)
  154. {
  155. while (_activeClip == randomIndex)
  156. {
  157. randomIndex = UnityEngine.Random.Range(0, _assignedClipManager.clipCount);
  158. }
  159. }
  160. _activeClip = randomIndex;
  161. }
  162. else if (_loop == Loop.All) // _loop == Loop.All
  163. {
  164. if (_activeClip < _assignedClipManager.clipCount - 1) // Not at the end yet, increase by 1
  165. {
  166. _activeClip++;
  167. }
  168. else if (_activeClip >= _assignedClipManager.clipCount - 1)
  169. {
  170. _activeClip = 0;
  171. }
  172. }
  173. if (_randomGap)
  174. {
  175. _timeGap = UnityEngine.Random.Range(_lowerRandomGap, _upperRandomGap);
  176. }
  177. return _assignedClipManager.GetClipAtIndex(_activeClip);
  178. }
  179. [HideFromIl2Cpp]
  180. public void AssignClipManager(ClipManager assignedClipManager, float timeGap, Loop loopType)
  181. {
  182. Stop();
  183. _assignedClipManager = assignedClipManager;
  184. _timeGap = timeGap;
  185. _loop = loopType;
  186. _activeClip = 0;
  187. }
  188. [HideFromIl2Cpp]
  189. private void OnEnable()
  190. {
  191. VolumeMaster.onVolumeChange += ResetVolume;
  192. }
  193. [HideFromIl2Cpp]
  194. private void OnDisable()
  195. {
  196. VolumeMaster.onVolumeChange -= ResetVolume;
  197. }
  198. [HideFromIl2Cpp]
  199. private void OnDestroy()
  200. {
  201. VolumeMaster.onVolumeChange -= ResetVolume;
  202. }
  203. [HideFromIl2Cpp]
  204. private void ResetVolume()
  205. {
  206. if(_audioSource && _sourceType != AudioMaster.SourceType.Custom)
  207. {
  208. _audioSource.volume = VolumeMaster.GetVolume(_sourceType);
  209. }
  210. }
  211. [HideFromIl2Cpp]
  212. public void ApplySettings(Setting newSetting)
  213. {
  214. _activeSetting = newSetting;
  215. _sourceType = _activeSetting.sourceType;
  216. _audioSource.spread = _activeSetting.spread;
  217. _audioSource.panStereo = _activeSetting.panStereo;
  218. _audioSource.dopplerLevel = _activeSetting.dopplerLevel;
  219. _audioSource.maxDistance = _activeSetting.maxDistance;
  220. _audioSource.minDistance = _activeSetting.minDistance;
  221. _audioSource.pitch = _activeSetting.pitch;
  222. _audioSource.spatialBlend = _activeSetting.spatialBlend;
  223. _audioSource.spatialize = _activeSetting.spatialize;
  224. _audioSource.rolloffFactor = _activeSetting.rolloffFactor;
  225. _audioSource.maxVolume = _activeSetting.maxVolume;
  226. _audioSource.maxVolume = _activeSetting.minVolume;
  227. _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. }