123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- using System;
- using UnityEngine;
- using UnityEngine.Audio;
- public class AudioSourceMgr : MonoBehaviour
- {
- public AudioSourceMgr.Type SoundType
- {
- get
- {
- return this.m_eType;
- }
- }
- public float FadeVolume
- {
- get
- {
- return this.m_fFadeVolume;
- }
- }
- public string FileName { get; private set; }
- public bool isLastPlayCompatibilityMode { get; private set; }
- public void Init(AudioSourceMgr.Type f_eType, bool f_bThreeD, SoundMgr f_gcParent, Transform f_trDefParent)
- {
- this.m_eType = f_eType;
- this.m_bThreeD = f_bThreeD;
- this.m_bThreeDNow = f_bThreeD;
- this.m_gcSoundMgr = f_gcParent;
- this.m_fFadeVolume = 1f;
- this.m_trDefParent = f_trDefParent;
- this.m_bPlay = false;
- base.gameObject.transform.SetParent(f_trDefParent, false);
- if (f_trDefParent != null)
- {
- this.m_strLastParent = f_trDefParent.name;
- }
- this.audiosource = base.gameObject.GetComponentsInChildren<AudioSource>(true)[0];
- this.audiosource.playOnAwake = false;
- if (f_bThreeD)
- {
- this.audiosource.dopplerLevel = 0f;
- }
- this.ApplyThreeD();
- AudioMixerGroup outputAudioMixerGroup = null;
- switch (f_eType)
- {
- case AudioSourceMgr.Type.Bgm:
- outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.BGM];
- break;
- case AudioSourceMgr.Type.Se:
- outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.SE];
- break;
- case AudioSourceMgr.Type.System:
- outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.System];
- break;
- case AudioSourceMgr.Type.Env:
- outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Env];
- break;
- case AudioSourceMgr.Type.Voice:
- outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Voice];
- break;
- }
- this.audiosource.outputAudioMixerGroup = outputAudioMixerGroup;
- }
- public void Clear()
- {
- this.Stop();
- if (this.audiosource != null && this.audiosource.clip != null)
- {
- this.audiosource.clip = null;
- }
- if (this.m_ogg != null)
- {
- this.m_ogg.Close();
- this.m_ogg.Dispose();
- this.m_ogg = null;
- }
- this.Fase = 0;
- }
- public void OnDestroy()
- {
- this.Clear();
- }
- public void OnPreFinalize()
- {
- this.Clear();
- }
- public void BackToParent()
- {
- base.gameObject.transform.SetParent(this.m_trDefParent, false);
- if (this.m_trDefParent != null)
- {
- this.m_strLastParent = this.m_trDefParent.name;
- }
- }
- public void SetParent(Transform f_trNewAttachParent)
- {
- if (f_trNewAttachParent == null)
- {
- return;
- }
- base.gameObject.transform.SetParent(f_trNewAttachParent, false);
- if (f_trNewAttachParent != null)
- {
- this.m_strLastParent = f_trNewAttachParent.name;
- }
- }
- private void ApplyVolume()
- {
- if (this.audiosource == null)
- {
- Debug.LogWarning("AudioSourceは消えています。最後の親 " + this.m_strLastParent);
- return;
- }
- this.audiosource.volume = Mathf.Clamp(this.m_fFadeVolume, 0f, 1f);
- }
- public float Pitch
- {
- get
- {
- return this.audiosource.pitch;
- }
- set
- {
- this.audiosource.pitch = value;
- }
- }
- public void ApplyThreeD()
- {
- if (!this.m_bThreeD)
- {
- return;
- }
- bool threeD = this.m_gcSoundMgr.GetThreeD(this.m_eType);
- if (this.m_bThreeDNow == threeD)
- {
- return;
- }
- if (!threeD)
- {
- this.m_fBackPanLevel = this.audiosource.spatialBlend;
- this.m_bBackBypassEffects = this.audiosource.bypassEffects;
- this.m_bBackBypassListenerEffects = this.audiosource.bypassListenerEffects;
- this.m_bBackBypassReverbZones = this.audiosource.bypassReverbZones;
- this.audiosource.spatialBlend = 0f;
- this.audiosource.bypassEffects = true;
- this.audiosource.bypassListenerEffects = true;
- this.audiosource.bypassReverbZones = true;
- }
- else if (threeD)
- {
- this.audiosource.spatialBlend = this.m_fBackPanLevel;
- this.audiosource.bypassEffects = this.m_bBackBypassEffects;
- this.audiosource.bypassListenerEffects = this.m_bBackBypassListenerEffects;
- this.audiosource.bypassReverbZones = this.m_bBackBypassReverbZones;
- }
- this.m_bThreeDNow = threeD;
- }
- public bool LoadFromWf(AFileSystemBase fileSystem, string fileName, bool stream)
- {
- this.Clear();
- this.FileName = fileName;
- this.isLastPlayCompatibilityMode = this.m_gcSoundMgr.compatibilityMode;
- float[] array = this.aryAmpRate;
- float amp_late;
- if (this.m_gcSoundMgr.compatibilityMode || fileSystem.NativePointerToInterfaceFileSystem == GameUty.FileSystemOld.NativePointerToInterfaceFileSystem)
- {
- amp_late = ((!this.m_bDanceBGM) ? this.aryAmpRateOld[(int)this.m_eType] : 0.8f);
- }
- else
- {
- amp_late = ((!this.m_bDanceBGM) ? this.aryAmpRate[(int)this.m_eType] : 0.8f);
- }
- this.m_ogg = new SoundFileOgg(fileSystem, amp_late);
- this.m_ogg.Open(fileName, this.m_bThreeDNow, stream);
- if (!this.m_ogg.IsOpen())
- {
- Debug.LogError("サウンドファイルが開けませんでした。" + fileName);
- return false;
- }
- this.audiosource.clip = this.m_ogg.clip;
- this.Fase = 3;
- return true;
- }
- public void LoadPlayDanceBGM(string f_strFileName, float f_fFadeTime, bool f_bStreaming, bool f_bLoop = false)
- {
- AFileSystemBase fileSystem = GameUty.FileSystem;
- if (string.IsNullOrEmpty(f_strFileName))
- {
- Debug.LogWarning("サウンドファイル名が空です。" + f_strFileName);
- return;
- }
- if (!fileSystem.IsExistentFile(f_strFileName))
- {
- Debug.LogWarning("サウンドファイルが見つかりません。" + f_strFileName);
- return;
- }
- this.m_bDanceBGM = true;
- if (this.audiosource.outputAudioMixerGroup != this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance])
- {
- this.audiosource.outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance];
- }
- this.LoadFromWf(fileSystem, f_strFileName, f_bStreaming);
- this.ApplyVolume();
- this.Play(f_fFadeTime, f_bLoop);
- }
- public void LoadPlay(string f_strFileName, float f_fFadeTime, bool f_bStreaming, bool f_bLoop = false)
- {
- if (string.IsNullOrEmpty(f_strFileName))
- {
- Debug.LogWarning("サウンドファイル名が空です。" + f_strFileName);
- return;
- }
- AFileSystemBase[] array;
- if (this.m_eType == AudioSourceMgr.Type.Voice)
- {
- if (this.m_gcSoundMgr.compatibilityMode)
- {
- array = new AFileSystemBase[]
- {
- GameUty.FileSystemOld,
- GameUty.FileSystem
- };
- }
- else
- {
- array = new AFileSystemBase[]
- {
- GameUty.FileSystem,
- GameUty.FileSystemOld
- };
- }
- }
- else if (this.m_gcSoundMgr.compatibilityMode)
- {
- array = new AFileSystemBase[]
- {
- GameUty.FileSystemOld
- };
- }
- else
- {
- array = new AFileSystemBase[]
- {
- GameUty.FileSystem
- };
- }
- AFileSystemBase afileSystemBase = null;
- foreach (AFileSystemBase afileSystemBase2 in array)
- {
- if (afileSystemBase2.IsExistentFile(f_strFileName))
- {
- afileSystemBase = afileSystemBase2;
- break;
- }
- }
- if (afileSystemBase == null)
- {
- Debug.LogWarning("サウンドファイルが見つかりません。" + f_strFileName);
- return;
- }
- this.m_bDanceBGM = false;
- if (this.audiosource.outputAudioMixerGroup == this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.Dance])
- {
- this.audiosource.outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.BGM];
- }
- this.LoadFromWf(afileSystemBase, f_strFileName, f_bStreaming);
- this.Play(f_fFadeTime, f_bLoop);
- }
- public void Play(float f_fFadeTime, bool loop = false)
- {
- this.m_bLoop = loop;
- this.m_fNextFadeTime = f_fFadeTime;
- if (this.Fase == 0)
- {
- Debug.LogError("サウンドは未だロードされていません。");
- return;
- }
- if (this.Fase == 1)
- {
- this.m_bPlay = true;
- this.Fase = 2;
- }
- else if (this.Fase == 3)
- {
- this.VAvgMax = 0.2f;
- this.ApplyVolume();
- this.Fade(1f, f_fFadeTime, false);
- this.audiosource.loop = loop;
- this.m_bPlay = true;
- this.audiosource.Play();
- }
- }
- public void PlayOneShot()
- {
- if (this.Fase == 0)
- {
- Debug.LogError("PlayOneShot サウンドは未だロードされていません。");
- }
- if (this.Fase == 1 || this.Fase == 2)
- {
- Debug.LogError("PlayOneShot サウンドがロード中です。待つ必要があります。");
- return;
- }
- this.VAvgMax = 0.2f;
- this.ApplyVolume();
- this.Fade(1f, 0f, false);
- this.audiosource.loop = false;
- this.m_bPlay = true;
- this.audiosource.PlayOneShot(this.audiosource.clip);
- }
- public void Stop()
- {
- this.VAvgMax = 1f;
- if (this.audiosource != null)
- {
- this.audiosource.Stop();
- }
- this.m_bFadeNow = false;
- this.m_fFadeVolume = 0f;
- this.m_bPlay = false;
- }
- public void Stop(float f_fFadeTime)
- {
- this.m_bPlay = false;
- this.Fade(0f, f_fFadeTime, true);
- }
- public void Fade(float f_fTargetVol, float f_fTime, bool f_bToStop)
- {
- if (f_fTime <= 0.01f)
- {
- this.m_fFadeVolume = f_fTargetVol;
- if (f_bToStop)
- {
- this.Stop();
- }
- this.ApplyVolume();
- return;
- }
- this.m_fFadeSubVolume = f_fTargetVol - this.m_fFadeVolume;
- if (this.m_fFadeSubVolume == 0f)
- {
- return;
- }
- this.m_fFadeTargetVolume = f_fTargetVol;
- this.m_fFadeTime = f_fTime;
- this.m_bFadeToStop = f_bToStop;
- this.m_bFadeNow = true;
- }
- public float GetVol()
- {
- this.audiosource.GetSpectrumData(this.vals, 0, FFTWindow.Blackman);
- float num = 0f;
- for (int i = 0; i < 256; i++)
- {
- num += this.vals[i];
- this.avgs[i] = this.avgs[i] * 0.9f + this.vals[i] * 0.1f;
- }
- this.VAvg = this.VAvg * 0.5f + num * 0.5f;
- this.VAvgMax *= 0.95f;
- if (this.VAvgMax < this.VAvg)
- {
- this.VAvgMax = this.VAvg;
- }
- return num * 17f;
- }
- public float GetLength()
- {
- if (this.m_ogg == null)
- {
- return 0f;
- }
- return this.m_ogg.GetLength();
- }
- public bool isPlay()
- {
- return this.Fase == 1 || (this.audiosource.isPlaying && this.m_bPlay);
- }
- public bool isLoop()
- {
- return this.audiosource.loop;
- }
- private void Update()
- {
- if ((this.Fase == 1 || this.Fase == 2) && this.Fase == 2)
- {
- if (this.www.isDone)
- {
- this.Fase = 3;
- UnityEngine.Object.Destroy(this.audiosource.clip);
- this.audiosource.clip = this.www.GetAudioClip(this.m_bThreeDNow, true);
- this.www.Dispose();
- this.Play(this.m_fNextFadeTime, this.m_bLoop);
- }
- else if (this.www.error != null)
- {
- this.Fase = 0;
- Debug.LogError("err audio " + this.url_);
- }
- }
- if (this.Fase == 3 && this.m_bFadeNow)
- {
- this.m_fFadeVolume += this.m_fFadeSubVolume * (Time.deltaTime / this.m_fFadeTime);
- if (this.m_fFadeSubVolume < 0f)
- {
- if (this.m_fFadeVolume <= this.m_fFadeTargetVolume)
- {
- this.m_fFadeVolume = this.m_fFadeTargetVolume;
- this.m_bFadeNow = false;
- if (this.m_bFadeToStop)
- {
- this.Stop();
- }
- }
- }
- else if (this.m_fFadeSubVolume > 0f && this.m_fFadeVolume >= this.m_fFadeTargetVolume)
- {
- this.m_fFadeVolume = this.m_fFadeTargetVolume;
- this.m_bFadeNow = false;
- if (this.m_bFadeToStop)
- {
- this.Stop();
- }
- }
- this.ApplyVolume();
- }
- }
- private void OnValidate()
- {
- if (this.m_ogg != null)
- {
- this.m_ogg.AmpRate = ((!this.m_bDanceBGM) ? this.aryAmpRate[(int)this.m_eType] : 0.8f);
- }
- }
- public float[] aryAmpRate = new float[]
- {
- 1f,
- 1f,
- 1f,
- 1f,
- 1f
- };
- public float[] aryAmpRateOld = new float[]
- {
- 0.3f,
- 0.8f,
- 0.5f,
- 0.15f,
- 2.8f
- };
- private AudioSourceMgr.Type m_eType;
- private SoundMgr m_gcSoundMgr;
- public AudioSource audiosource;
- private bool m_bFadeNow;
- private float m_fFadeVolume;
- private float m_fFadeTargetVolume;
- private float m_fFadeSubVolume;
- private float m_fFadeTime;
- private bool m_bFadeToStop;
- private bool m_bThreeD;
- private float m_fBackPanLevel = 1f;
- private bool m_bBackBypassEffects;
- private bool m_bBackBypassListenerEffects;
- private bool m_bBackBypassReverbZones;
- private bool m_bDanceBGM;
- private bool m_bThreeDNow;
- private WWW www;
- private int Fase;
- private string url_;
- public float[] vals = new float[256];
- public float[] avgs = new float[256];
- public float VAvg;
- public float VAvgMax = 1f;
- private SoundFileOgg m_ogg;
- private Transform m_trDefParent;
- private bool m_bLoop = true;
- private float m_fNextFadeTime;
- private string m_strLastParent = string.Empty;
- private bool m_bPlay;
- public enum Type
- {
- Bgm,
- Se,
- System,
- Env,
- Voice,
- Max
- }
- }
|