AudioSourceMgr.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Audio;
  4. public class AudioSourceMgr : MonoBehaviour
  5. {
  6. public AudioSourceMgr.Type SoundType
  7. {
  8. get
  9. {
  10. return this.m_eType;
  11. }
  12. }
  13. public float FadeVolume
  14. {
  15. get
  16. {
  17. return this.m_fFadeVolume;
  18. }
  19. }
  20. public string FileName { get; private set; }
  21. public bool isLastPlayCompatibilityMode { get; private set; }
  22. public void Init(AudioSourceMgr.Type f_eType, bool f_bThreeD, SoundMgr f_gcParent, Transform f_trDefParent)
  23. {
  24. this.m_eType = f_eType;
  25. this.m_bThreeD = f_bThreeD;
  26. this.m_bThreeDNow = f_bThreeD;
  27. this.m_gcSoundMgr = f_gcParent;
  28. this.m_fFadeVolume = 1f;
  29. this.m_trDefParent = f_trDefParent;
  30. this.m_bPlay = false;
  31. base.gameObject.transform.SetParent(f_trDefParent, false);
  32. if (f_trDefParent != null)
  33. {
  34. this.m_strLastParent = f_trDefParent.name;
  35. }
  36. this.audiosource = base.gameObject.GetComponentsInChildren<AudioSource>(true)[0];
  37. this.audiosource.playOnAwake = false;
  38. if (f_bThreeD)
  39. {
  40. this.audiosource.dopplerLevel = 0f;
  41. }
  42. this.ApplyThreeD();
  43. AudioMixerGroup outputAudioMixerGroup = null;
  44. switch (f_eType)
  45. {
  46. case AudioSourceMgr.Type.Bgm:
  47. outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.BGM];
  48. break;
  49. case AudioSourceMgr.Type.Se:
  50. outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.SE];
  51. break;
  52. case AudioSourceMgr.Type.System:
  53. outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.System];
  54. break;
  55. case AudioSourceMgr.Type.Env:
  56. outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Env];
  57. break;
  58. case AudioSourceMgr.Type.Voice:
  59. outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Voice];
  60. break;
  61. }
  62. this.audiosource.outputAudioMixerGroup = outputAudioMixerGroup;
  63. }
  64. public void Clear()
  65. {
  66. this.Stop();
  67. if (this.audiosource != null && this.audiosource.clip != null)
  68. {
  69. this.audiosource.clip = null;
  70. }
  71. if (this.m_ogg != null)
  72. {
  73. this.m_ogg.Close();
  74. this.m_ogg.Dispose();
  75. this.m_ogg = null;
  76. }
  77. this.Fase = 0;
  78. }
  79. public void OnDestroy()
  80. {
  81. this.Clear();
  82. }
  83. public void OnPreFinalize()
  84. {
  85. this.Clear();
  86. }
  87. public void BackToParent()
  88. {
  89. base.gameObject.transform.SetParent(this.m_trDefParent, false);
  90. if (this.m_trDefParent != null)
  91. {
  92. this.m_strLastParent = this.m_trDefParent.name;
  93. }
  94. }
  95. public void SetParent(Transform f_trNewAttachParent)
  96. {
  97. if (f_trNewAttachParent == null)
  98. {
  99. return;
  100. }
  101. base.gameObject.transform.SetParent(f_trNewAttachParent, false);
  102. if (f_trNewAttachParent != null)
  103. {
  104. this.m_strLastParent = f_trNewAttachParent.name;
  105. }
  106. }
  107. private void ApplyVolume()
  108. {
  109. if (this.audiosource == null)
  110. {
  111. Debug.LogWarning("AudioSourceは消えています。最後の親 " + this.m_strLastParent);
  112. return;
  113. }
  114. this.audiosource.volume = Mathf.Clamp(this.m_fFadeVolume, 0f, 1f);
  115. }
  116. public float Pitch
  117. {
  118. get
  119. {
  120. return this.audiosource.pitch;
  121. }
  122. set
  123. {
  124. this.audiosource.pitch = value;
  125. }
  126. }
  127. public void ApplyThreeD()
  128. {
  129. if (!this.m_bThreeD)
  130. {
  131. return;
  132. }
  133. bool threeD = this.m_gcSoundMgr.GetThreeD(this.m_eType);
  134. if (this.m_bThreeDNow == threeD)
  135. {
  136. return;
  137. }
  138. if (!threeD)
  139. {
  140. this.m_fBackPanLevel = this.audiosource.spatialBlend;
  141. this.m_bBackBypassEffects = this.audiosource.bypassEffects;
  142. this.m_bBackBypassListenerEffects = this.audiosource.bypassListenerEffects;
  143. this.m_bBackBypassReverbZones = this.audiosource.bypassReverbZones;
  144. this.audiosource.spatialBlend = 0f;
  145. this.audiosource.bypassEffects = true;
  146. this.audiosource.bypassListenerEffects = true;
  147. this.audiosource.bypassReverbZones = true;
  148. }
  149. else if (threeD)
  150. {
  151. this.audiosource.spatialBlend = this.m_fBackPanLevel;
  152. this.audiosource.bypassEffects = this.m_bBackBypassEffects;
  153. this.audiosource.bypassListenerEffects = this.m_bBackBypassListenerEffects;
  154. this.audiosource.bypassReverbZones = this.m_bBackBypassReverbZones;
  155. }
  156. this.m_bThreeDNow = threeD;
  157. }
  158. public bool LoadFromWf(AFileSystemBase fileSystem, string fileName, bool stream)
  159. {
  160. this.Clear();
  161. this.FileName = fileName;
  162. this.isLastPlayCompatibilityMode = this.m_gcSoundMgr.compatibilityMode;
  163. float[] array = this.aryAmpRate;
  164. float amp_late;
  165. if (this.m_gcSoundMgr.compatibilityMode || fileSystem.NativePointerToInterfaceFileSystem == GameUty.FileSystemOld.NativePointerToInterfaceFileSystem)
  166. {
  167. amp_late = ((!this.m_bDanceBGM) ? this.aryAmpRateOld[(int)this.m_eType] : 0.8f);
  168. }
  169. else
  170. {
  171. amp_late = ((!this.m_bDanceBGM) ? this.aryAmpRate[(int)this.m_eType] : 0.8f);
  172. }
  173. this.m_ogg = new SoundFileOgg(fileSystem, amp_late);
  174. this.m_ogg.Open(fileName, this.m_bThreeDNow, stream);
  175. if (!this.m_ogg.IsOpen())
  176. {
  177. Debug.LogError("サウンドファイルが開けませんでした。" + fileName);
  178. return false;
  179. }
  180. this.audiosource.clip = this.m_ogg.clip;
  181. this.Fase = 3;
  182. return true;
  183. }
  184. public void LoadPlayDanceBGM(string f_strFileName, float f_fFadeTime, bool f_bStreaming, bool f_bLoop = false)
  185. {
  186. AFileSystemBase fileSystem = GameUty.FileSystem;
  187. if (string.IsNullOrEmpty(f_strFileName))
  188. {
  189. Debug.LogWarning("サウンドファイル名が空です。" + f_strFileName);
  190. return;
  191. }
  192. if (!fileSystem.IsExistentFile(f_strFileName))
  193. {
  194. Debug.LogWarning("サウンドファイルが見つかりません。" + f_strFileName);
  195. return;
  196. }
  197. this.m_bDanceBGM = true;
  198. if (this.audiosource.outputAudioMixerGroup != this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance])
  199. {
  200. this.audiosource.outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance];
  201. }
  202. this.LoadFromWf(fileSystem, f_strFileName, f_bStreaming);
  203. this.ApplyVolume();
  204. this.Play(f_fFadeTime, f_bLoop);
  205. }
  206. public void LoadPlay(string f_strFileName, float f_fFadeTime, bool f_bStreaming, bool f_bLoop = false)
  207. {
  208. if (string.IsNullOrEmpty(f_strFileName))
  209. {
  210. Debug.LogWarning("サウンドファイル名が空です。" + f_strFileName);
  211. return;
  212. }
  213. AFileSystemBase[] array;
  214. if (this.m_eType == AudioSourceMgr.Type.Voice)
  215. {
  216. if (this.m_gcSoundMgr.compatibilityMode)
  217. {
  218. array = new AFileSystemBase[]
  219. {
  220. GameUty.FileSystemOld,
  221. GameUty.FileSystem
  222. };
  223. }
  224. else
  225. {
  226. array = new AFileSystemBase[]
  227. {
  228. GameUty.FileSystem,
  229. GameUty.FileSystemOld
  230. };
  231. }
  232. }
  233. else if (this.m_gcSoundMgr.compatibilityMode)
  234. {
  235. array = new AFileSystemBase[]
  236. {
  237. GameUty.FileSystemOld
  238. };
  239. }
  240. else
  241. {
  242. array = new AFileSystemBase[]
  243. {
  244. GameUty.FileSystem
  245. };
  246. }
  247. AFileSystemBase afileSystemBase = null;
  248. foreach (AFileSystemBase afileSystemBase2 in array)
  249. {
  250. if (afileSystemBase2.IsExistentFile(f_strFileName))
  251. {
  252. afileSystemBase = afileSystemBase2;
  253. break;
  254. }
  255. }
  256. if (afileSystemBase == null)
  257. {
  258. Debug.LogWarning("サウンドファイルが見つかりません。" + f_strFileName);
  259. return;
  260. }
  261. this.m_bDanceBGM = false;
  262. if (this.audiosource.outputAudioMixerGroup == this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance])
  263. {
  264. this.audiosource.outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.BGM];
  265. }
  266. this.LoadFromWf(afileSystemBase, f_strFileName, f_bStreaming);
  267. this.Play(f_fFadeTime, f_bLoop);
  268. }
  269. public void Play(float f_fFadeTime, bool loop = false)
  270. {
  271. this.m_bLoop = loop;
  272. this.m_fNextFadeTime = f_fFadeTime;
  273. if (this.Fase == 0)
  274. {
  275. Debug.LogError("サウンドは未だロードされていません。");
  276. return;
  277. }
  278. if (this.Fase == 1)
  279. {
  280. this.m_bPlay = true;
  281. this.Fase = 2;
  282. }
  283. else if (this.Fase == 3)
  284. {
  285. this.VAvgMax = 0.2f;
  286. this.ApplyVolume();
  287. this.Fade(1f, f_fFadeTime, false);
  288. this.audiosource.loop = loop;
  289. this.m_bPlay = true;
  290. this.audiosource.Play();
  291. }
  292. }
  293. public void PlayOneShot()
  294. {
  295. if (this.Fase == 0)
  296. {
  297. Debug.LogError("PlayOneShot サウンドは未だロードされていません。");
  298. }
  299. if (this.Fase == 1 || this.Fase == 2)
  300. {
  301. Debug.LogError("PlayOneShot サウンドがロード中です。待つ必要があります。");
  302. return;
  303. }
  304. this.VAvgMax = 0.2f;
  305. this.ApplyVolume();
  306. this.Fade(1f, 0f, false);
  307. this.audiosource.loop = false;
  308. this.m_bPlay = true;
  309. this.audiosource.PlayOneShot(this.audiosource.clip);
  310. }
  311. public void Stop()
  312. {
  313. this.VAvgMax = 1f;
  314. if (this.audiosource != null)
  315. {
  316. this.audiosource.Stop();
  317. }
  318. this.m_bFadeNow = false;
  319. this.m_fFadeVolume = 0f;
  320. this.m_bPlay = false;
  321. }
  322. public void Stop(float f_fFadeTime)
  323. {
  324. this.m_bPlay = false;
  325. this.Fade(0f, f_fFadeTime, true);
  326. }
  327. public void Fade(float f_fTargetVol, float f_fTime, bool f_bToStop)
  328. {
  329. if (f_fTime <= 0.01f)
  330. {
  331. this.m_fFadeVolume = f_fTargetVol;
  332. if (f_bToStop)
  333. {
  334. this.Stop();
  335. }
  336. this.ApplyVolume();
  337. return;
  338. }
  339. this.m_fFadeSubVolume = f_fTargetVol - this.m_fFadeVolume;
  340. if (this.m_fFadeSubVolume == 0f)
  341. {
  342. return;
  343. }
  344. this.m_fFadeTargetVolume = f_fTargetVol;
  345. this.m_fFadeTime = f_fTime;
  346. this.m_bFadeToStop = f_bToStop;
  347. this.m_bFadeNow = true;
  348. }
  349. public float GetVol()
  350. {
  351. this.audiosource.GetSpectrumData(this.vals, 0, FFTWindow.Blackman);
  352. float num = 0f;
  353. for (int i = 0; i < 256; i++)
  354. {
  355. num += this.vals[i];
  356. this.avgs[i] = this.avgs[i] * 0.9f + this.vals[i] * 0.1f;
  357. }
  358. this.VAvg = this.VAvg * 0.5f + num * 0.5f;
  359. this.VAvgMax *= 0.95f;
  360. if (this.VAvgMax < this.VAvg)
  361. {
  362. this.VAvgMax = this.VAvg;
  363. }
  364. return num * 17f;
  365. }
  366. public float GetLength()
  367. {
  368. if (this.m_ogg == null)
  369. {
  370. return 0f;
  371. }
  372. return this.m_ogg.GetLength();
  373. }
  374. public bool isPlay()
  375. {
  376. return this.Fase == 1 || (this.audiosource.isPlaying && this.m_bPlay);
  377. }
  378. public bool isLoop()
  379. {
  380. return this.audiosource.loop;
  381. }
  382. private void Update()
  383. {
  384. if ((this.Fase == 1 || this.Fase == 2) && this.Fase == 2)
  385. {
  386. if (this.www.isDone)
  387. {
  388. this.Fase = 3;
  389. UnityEngine.Object.Destroy(this.audiosource.clip);
  390. this.audiosource.clip = this.www.GetAudioClip(this.m_bThreeDNow, true);
  391. this.www.Dispose();
  392. this.Play(this.m_fNextFadeTime, this.m_bLoop);
  393. }
  394. else if (this.www.error != null)
  395. {
  396. this.Fase = 0;
  397. Debug.LogError("err audio " + this.url_);
  398. }
  399. }
  400. if (this.Fase == 3 && this.m_bFadeNow)
  401. {
  402. this.m_fFadeVolume += this.m_fFadeSubVolume * (Time.deltaTime / this.m_fFadeTime);
  403. if (this.m_fFadeSubVolume < 0f)
  404. {
  405. if (this.m_fFadeVolume <= this.m_fFadeTargetVolume)
  406. {
  407. this.m_fFadeVolume = this.m_fFadeTargetVolume;
  408. this.m_bFadeNow = false;
  409. if (this.m_bFadeToStop)
  410. {
  411. this.Stop();
  412. }
  413. }
  414. }
  415. else if (this.m_fFadeSubVolume > 0f && this.m_fFadeVolume >= this.m_fFadeTargetVolume)
  416. {
  417. this.m_fFadeVolume = this.m_fFadeTargetVolume;
  418. this.m_bFadeNow = false;
  419. if (this.m_bFadeToStop)
  420. {
  421. this.Stop();
  422. }
  423. }
  424. this.ApplyVolume();
  425. }
  426. }
  427. private void OnValidate()
  428. {
  429. if (this.m_ogg != null)
  430. {
  431. this.m_ogg.AmpRate = ((!this.m_bDanceBGM) ? this.aryAmpRate[(int)this.m_eType] : 0.8f);
  432. }
  433. }
  434. public float[] aryAmpRate = new float[]
  435. {
  436. 1f,
  437. 1f,
  438. 1f,
  439. 1f,
  440. 1f
  441. };
  442. public float[] aryAmpRateOld = new float[]
  443. {
  444. 0.3f,
  445. 0.8f,
  446. 0.5f,
  447. 0.15f,
  448. 2.8f
  449. };
  450. private AudioSourceMgr.Type m_eType;
  451. private SoundMgr m_gcSoundMgr;
  452. public AudioSource audiosource;
  453. private bool m_bFadeNow;
  454. private float m_fFadeVolume;
  455. private float m_fFadeTargetVolume;
  456. private float m_fFadeSubVolume;
  457. private float m_fFadeTime;
  458. private bool m_bFadeToStop;
  459. private bool m_bThreeD;
  460. private float m_fBackPanLevel = 1f;
  461. private bool m_bBackBypassEffects;
  462. private bool m_bBackBypassListenerEffects;
  463. private bool m_bBackBypassReverbZones;
  464. private bool m_bDanceBGM;
  465. private bool m_bThreeDNow;
  466. private WWW www;
  467. private int Fase;
  468. private string url_;
  469. public float[] vals = new float[256];
  470. public float[] avgs = new float[256];
  471. public float VAvg;
  472. public float VAvgMax = 1f;
  473. private SoundFileOgg m_ogg;
  474. private Transform m_trDefParent;
  475. private bool m_bLoop = true;
  476. private float m_fNextFadeTime;
  477. private string m_strLastParent = string.Empty;
  478. private bool m_bPlay;
  479. public enum Type
  480. {
  481. Bgm,
  482. Se,
  483. System,
  484. Env,
  485. Voice,
  486. Max
  487. }
  488. }