/* * Sound.cs * Authors: Shawn Chen * August Zinsser * * Copyright Matthew Belmonte 2007 */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; namespace tAC_Engine { /// /// Loads the soundbank, giving client classes the ability to play each loaded sound by name. Also stores volume settings. /// public static class SoundManager { private static AudioEngine mEngine; // Main interface to the sound drivers private static WaveBank mSoundEffectsWaveBank; // Holds raw sound effect waves private static WaveBank mMusicWaveBank; // '' music private static SoundBank mSoundBank; // Holds Cues private static bool mHaveSound; // False if there are problems interfacing with the hardware private static bool mMuteMusic = false; // Volume control private static bool mMuteSoundEffects = false; // Actually prevents sound effect cues from playing in the first place private static AudioCategory mSoundEffectsCategory; // All sound effects private static AudioCategory mMusicCategory; // All music tracks private static Cue mCurrentMusicCue; // Current music track playing public static bool MuteAll { set { mMuteMusic = value; mMuteSoundEffects = value; } get { return mMuteMusic && mMuteSoundEffects; } } public static bool MuteMusic { set { mMuteMusic = value; } get { return mMuteMusic; } } public static bool MuteSoundEffects { set { mMuteSoundEffects = value; } get { return mMuteSoundEffects; } } /// /// Setup the hardware interface and load the sound banks /// public static void Initialize() { try { mEngine = new AudioEngine("Content/Sounds/AudioProject.xgs"); mSoundEffectsWaveBank = new WaveBank(mEngine, "Content/Sounds/Wave Bank.xwb"); mMusicWaveBank = new WaveBank(mEngine, "Content/Sounds/MusicWaveBank.xwb"); mSoundBank = new SoundBank(mEngine, "Content/Sounds/Sound Bank.xsb"); mSoundEffectsCategory = mEngine.GetCategory("Default"); mMusicCategory = mEngine.GetCategory("Music"); mHaveSound = true; } catch { mHaveSound = false; System.Diagnostics.Debug.Assert(false, "There were problems initializing sound."); } } /// /// Play a loaded sound by its filename (no extension) /// /// /// public static Cue PlayEffect(string name) { if (mHaveSound && !mMuteSoundEffects) { Cue ret = mSoundBank.GetCue(name); mSoundBank.PlayCue(name); return ret; } return null; } /// /// Immediately stops the given sound. Can be music or a sound effect. /// /// public static void Stop(string name) { if (mHaveSound) { Cue cue = mSoundBank.GetCue(name); cue.Stop(AudioStopOptions.AsAuthored); } } /// /// Sets the music to the given file. Music will run at 0 volume if it is muted. /// /// public static void SetMusic(string name) { if (mCurrentMusicCue != null) { // If already set, do nothing if (mCurrentMusicCue.Name == name) return; else mCurrentMusicCue.Stop(AudioStopOptions.AsAuthored); } mCurrentMusicCue = mSoundBank.GetCue(name); mCurrentMusicCue.Play(); } /// /// Stops whatever music is playing. /// public static void StopMusic() { mCurrentMusicCue.Stop(AudioStopOptions.AsAuthored); } /// /// Pauses whatever music is playing. /// public static void PauseMusic() { mCurrentMusicCue.Pause(); } /// /// Resumes playback of the current music track. /// public static void ResumeMusic() { mCurrentMusicCue.Resume(); } /// /// Updates the sound manager /// public static void Update() { if (mHaveSound) { mEngine.Update(); } if (mMuteMusic) { // TODO: Fade Out mMusicCategory.SetVolume(0f); } else { // TODO: Fade In mMusicCategory.SetVolume(1f); } } /// /// Dispose sound resources /// public static void Dispose() { if (mHaveSound) { mSoundBank.Dispose(); mSoundEffectsWaveBank.Dispose(); mEngine.Dispose(); } } } }