Queue.cs 9.8 KB

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