AudioSourceMgr.cs 13 KB

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