using System; using UnityEngine; using UnityEngine.Audio; public class AudioSourceMgr : MonoBehaviour { public AudioSourceMgr.Type SoundType { get { return this.m_eType; } set { this.m_eType = value; } } 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(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; case AudioSourceMgr.Type.VoiceHeroine: outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.VoiceHeroine]; break; case AudioSourceMgr.Type.VoiceSub: outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.VoiceSub]; break; case AudioSourceMgr.Type.VoiceExtra: outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.VoiceExtra]; break; case AudioSourceMgr.Type.VoiceMob: outputAudioMixerGroup = this.m_gcSoundMgr.mix_mgr[AudioMixerMgr.Group.VoiceMob]; 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 || this.m_eType == AudioSourceMgr.Type.VoiceHeroine || this.m_eType == AudioSourceMgr.Type.VoiceSub || this.m_eType == AudioSourceMgr.Type.VoiceExtra || this.m_eType == AudioSourceMgr.Type.VoiceMob) { 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, 1f, 1f, 1f, 1f }; public float[] aryAmpRateOld = new float[] { 0.3f, 0.8f, 0.5f, 0.15f, 2.8f, 2.8f, 2.8f, 2.8f, 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, VoiceHeroine, VoiceSub, VoiceExtra, VoiceMob, Max } }