using System; using System.Collections.Generic; using System.Text; using System.IO; using KillShit.Engine.Audio.FMOD; namespace KillShit.Engine.Audio { public static class AudioManager { public static FMOD.System AudioEngine = null; private static List SoundPool = new List ( ); public static bool bDisposed = false; public static void Init ( ) { FMOD.Factory.System_Create ( ref AudioEngine ); AudioEngine.init ( 32, FMOD.INITFLAG.NORMAL, System.IntPtr.Zero ); AudioEngine.set3DSettings ( 1, 15, 1 ); Engine.Renderer.Driver.Debug.AddText ( "Audio Manager Initialization Successful!", System.Drawing.Color.Green.ToArgb ( ) ); } public static void Dispose ( ) { foreach ( preCachedSound preCachedSound in SoundPool ) { if ( preCachedSound != null ) { preCachedSound.Dispose ( ); } } SoundPool.Clear ( ); if ( AudioEngine != null ) { AudioEngine.close ( ); } bDisposed = true; } public static void ClearPool ( TYPE Type ) { foreach ( preCachedSound preCachedSound in SoundPool ) { if ( preCachedSound != null && preCachedSound.enType == Type ) { preCachedSound.Dispose ( ); } } SoundPool.Clear ( ); } private static FMOD.VECTOR Vector3ToFMOD ( Microsoft.DirectX.Vector3 V ) { VECTOR V2; V2.x = V.X; V2.y = V.Y; V2.z = V.Z; return V2; } public static void Update ( Engine.Rendering.Camera Camera ) { if ( !bDisposed ) { VECTOR Pos = Vector3ToFMOD ( Camera.Position ); VECTOR Vel = Vector3ToFMOD ( new Microsoft.DirectX.Vector3 ( 0, 0, 0 ) ); VECTOR ViewDir = Vector3ToFMOD ( Camera.ViewDirection ); VECTOR Up = Vector3ToFMOD ( Camera.Up ); AudioEngine.set3DListenerAttributes ( 0, ref Pos, ref Vel, ref ViewDir, ref Up ); AudioEngine.update ( ); } } public static void addSound ( string Path, TYPE Type, Engine.Audio.FMOD.MODE Mode ) { preCachedSound tempSound = new preCachedSound ( Path, Type, Mode ); foreach ( preCachedSound preCachedSound in SoundPool ) { if ( tempSound.Equals ( preCachedSound ) ) { tempSound = null; return; } } DirectoryInfo diPath = new DirectoryInfo ( Path ); Engine.Renderer.Driver.Debug.AddText ( "Audio[Status] : '" + diPath.Parent + "/" + diPath.Name + "' pre-cached.", ( int ) Engine.Debug.MESSAGE_TYPE.INFORMATION ); SoundPool.Add ( tempSound ); diPath = null; } public static void playSound ( FMOD.CHANNELINDEX channelid, string Path, bool paused, ref FMOD.Channel Channel ) { AudioManager.AudioEngine.playSound ( channelid, getPreCachedSound ( Path ).sdSound, paused, ref Channel ); } public static void destroySound ( string Path ) { int Index = SoundPool.IndexOf ( getPreCachedSound ( Path ) ); if ( Index != -1 && !SoundPool [ Index ].bDisposed ) { SoundPool [ Index ].Dispose ( ); } } public static preCachedSound getPreCachedSound ( string Path ) { foreach ( preCachedSound preCachedSound in SoundPool ) { if ( Path == preCachedSound.strPath ) return preCachedSound; } return ( preCachedSound ) ( null ); } } public enum TYPE { Map, Music, Effect, Custom, } public class preCachedSound : IDisposable { public string strPath = null; public TYPE enType; public bool bDisposed = false; public FMOD.MODE enMode; public FMOD.Sound sdSound = null; public preCachedSound ( string Path, TYPE Type, FMOD.MODE Mode ) { strPath = Path; enType = Type; enMode = Mode; preCache ( ); } public override bool Equals ( object o ) { preCachedSound Clone = ( preCachedSound ) o; if ( Clone.strPath == this.strPath ) { Clone = null; return true; } else { Clone = null; return false; } } public void preCache ( ) { AudioManager.AudioEngine.createSound ( strPath, enMode, ref sdSound ); } public void Dispose ( ) { if ( sdSound != null ) { sdSound.release ( ); sdSound = null; bDisposed = true; } } public override int GetHashCode ( ) { return base.GetHashCode ( ); } } }