Implementing XAct in Our XNA Code

Posted: January 14, 2010 in C#, Game Programming, XNA
Tags: , , ,

xna_logo

To play a sound without using XACT, you will use the SoundEffect or SoundEffectInstance classes. Using these classes is simpler than using XACT and the classes required by XACT. Because XACT is not available on Zune, the SoundEffect and SoundEffectInstance will be the only option for Zune games. SoundEffect and SoundEffectInstance are also available for Xbox and Windows XNA games.

In this tutorial im using singleton pattern, singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.




Fields in AuidoManager class

private AudioEngine audioEngine;  // The audio engine used to play all cues
private SoundBank soundBank;      // The soundbank that contains all cues

private WaveBank sfxWaveBank;     // sound effect (sfx) bank
private WaveBank bgmWaveBank;     // background music (bgm) bank

private AudioCategory bgmCategory;// bgm category
private AudioCategory sfxCategory;// sfx category

private float bgmVolume = 1.0f;   // bgm volume
private float sfxVolume = 1.0f;   // sfx volume

private Cue sfxMusicCue;          // current cue for sfx
private Cue bgmMusicCue;          // current cue for bgm




Constructor

        private AudioManager(Game game,
            string settingsFile,
            string seWaveBankFile,
            string bgmWaveBankFile,
            string soundBankFile)
            : base(game)
        {
            try
            {
                audioEngine = new AudioEngine(settingsFile);
                sfxWaveBank = new WaveBank(audioEngine, seWaveBankFile);
                bgmWaveBank = new WaveBank(audioEngine, bgmWaveBankFile, 0, 16);
                soundBank = new SoundBank(audioEngine, soundBankFile);
            }
            catch (NoAudioHardwareException)
            {
                // silently fall back to silence
                audioEngine = null;
                sfxWaveBank = null;
                bgmWaveBank = null;
                soundBank = null;
            }
        }




Initialization Method

public static void Initialize(Game game)
        {
            string settingsFile = @"Content\Gtg.xgs";
            string seWaveBankFile = @"Content\SeWaveBank.xwb";
            string bgmWaveFile = @"Content\BgmWaveBank.xwb";
            string soundBankFile = @"Content\SoundBank.xsb";

            audioManager = new AudioManager(game,
                settingsFile,
                seWaveBankFile,
                bgmWaveFile,
                soundBankFile);

            if (game != null)
                game.Components.Add(audioManager);

            audioManager.sfxCategory = audioManager.audioEngine.GetCategory("Default");
            audioManager.bgmCategory = audioManager.audioEngine.GetCategory("Music");

            audioManager.sfxCategory.SetVolume(audioManager.sfxVolume);
            audioManager.bgmCategory.SetVolume(audioManager.bgmVolume);

        }




For playing Cue

public static Cue GetSfxCue(string cueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.sfxWaveBank == null))
                return null;

            return audioManager.soundBank.GetCue(cueName);
        }

        public static void PlaySfxCue(string cueName)
        {
            if ((audioManager != null) && (audioManager.audioEngine != null) &&
                (audioManager.soundBank != null) && (audioManager.sfxWaveBank != null))
                audioManager.soundBank.PlayCue(cueName);

        }

        public static void PlaySfxMusic(string musicCueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.sfxWaveBank == null))
                return;

            if (audioManager.sfxMusicCue != null)
                audioManager.sfxMusicCue.Stop(AudioStopOptions.AsAuthored);

            audioManager.sfxMusicCue = GetSfxCue(musicCueName);

            if (audioManager.sfxMusicCue != null)
                audioManager.sfxMusicCue.Play();

        }




Fully source code

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace Audio
{

    // class for backround music and sound effect
    // using sigleton pattern
    public class AudioManager : GameComponent
    {
        #region Singleton

        private static AudioManager audioManager = null;

        #endregion

        #region Audio Data

        private AudioEngine audioEngine;  // The audio engine used to play all cues
        private SoundBank soundBank;      // The soundbank that contains all cues

        private WaveBank sfxWaveBank;     // sound effect (sfx) bank
        private WaveBank bgmWaveBank;     // background music (bgm) bank

        private AudioCategory bgmCategory;// bgm category
        private AudioCategory sfxCategory;// sfx category

        private float bgmVolume = 1.0f;   // bgm volume
        private float sfxVolume = 1.0f;   // sfx volume

        private Cue sfxMusicCue;          // current cue for sfx
        private Cue bgmMusicCue;          // current cue for bgm

        #endregion

        #region Initialization

        private AudioManager(Game game,
            string settingsFile,
            string seWaveBankFile,
            string bgmWaveBankFile,
            string soundBankFile)
            : base(game)
        {
            try
            {
                audioEngine = new AudioEngine(settingsFile);
                sfxWaveBank = new WaveBank(audioEngine, seWaveBankFile);
                bgmWaveBank = new WaveBank(audioEngine, bgmWaveBankFile, 0, 16);
                soundBank = new SoundBank(audioEngine, soundBankFile);
            }
            catch (NoAudioHardwareException)
            {
                // silently fall back to silence
                audioEngine = null;
                sfxWaveBank = null;
                bgmWaveBank = null;
                soundBank = null;
            }
        }

        //public static void Initialize(Game game, string settingsFile,
        //    string waveBankFile, string soundBankFile)
        //{
        //    audioManager = new AudioManager(game, settingsFile, waveBankFile, soundBankFile);

        //    if (game != null)
        //        game.Components.Add(audioManager);

        //}

        public static void Initialize(Game game)
        {
            string settingsFile = @"Content\Gtg.xgs";
            string seWaveBankFile = @"Content\SeWaveBank.xwb";
            string bgmWaveFile = @"Content\BgmWaveBank.xwb";
            string soundBankFile = @"Content\SoundBank.xsb";

            audioManager = new AudioManager(game,
                settingsFile,
                seWaveBankFile,
                bgmWaveFile,
                soundBankFile);

            if (game != null)
                game.Components.Add(audioManager);

            audioManager.sfxCategory = audioManager.audioEngine.GetCategory("Default");
            audioManager.bgmCategory = audioManager.audioEngine.GetCategory("Music");

            audioManager.sfxCategory.SetVolume(audioManager.sfxVolume);
            audioManager.bgmCategory.SetVolume(audioManager.bgmVolume);

        }

        #endregion

        #region Cue Methods

        public static Cue GetSfxCue(string cueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.sfxWaveBank == null))
                return null;

            return audioManager.soundBank.GetCue(cueName);
        }

        public static void PlaySfxCue(string cueName)
        {
            if ((audioManager != null) && (audioManager.audioEngine != null) &&
                (audioManager.soundBank != null) && (audioManager.sfxWaveBank != null))
                audioManager.soundBank.PlayCue(cueName);

        }

        public static void PlaySfxMusic(string musicCueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.sfxWaveBank == null))
                return;

            if (audioManager.sfxMusicCue != null)
                audioManager.sfxMusicCue.Stop(AudioStopOptions.AsAuthored);

            audioManager.sfxMusicCue = GetSfxCue(musicCueName);

            if (audioManager.sfxMusicCue != null)
                audioManager.sfxMusicCue.Play();

        }

        public static Cue GetBgmCue(string cueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.bgmWaveBank == null))
                return null;

            return audioManager.soundBank.GetCue(cueName);
        }

        public static void PlayBgmCue(string cueName)
        {
            if ((audioManager != null) && (audioManager.audioEngine != null) &&
                (audioManager.soundBank != null) && (audioManager.bgmWaveBank != null))
                audioManager.soundBank.PlayCue(cueName);
        }

        public static void PlayBgmMusic(string musicCueName)
        {
            if ((audioManager == null) || (audioManager.audioEngine == null) ||
                (audioManager.soundBank == null) || (audioManager.bgmWaveBank == null))
                return;

            if (audioManager.bgmMusicCue != null)
                audioManager.bgmMusicCue.Stop(AudioStopOptions.AsAuthored);

            audioManager.bgmMusicCue = GetBgmCue(musicCueName);

            if (audioManager.bgmMusicCue != null)
                audioManager.bgmMusicCue.Play();

        }

        #endregion

        #region Updating Methods

        public override void Update(GameTime gameTime)
        {
            // update the audio engine
            if (audioEngine != null)
                audioEngine.Update();

            base.Update(gameTime);
        }

        public void SetBgmVolume(float value)
        {
            audioManager.bgmVolume = value;

            if (value < 0.1f)
                audioManager.bgmVolume = 0.1f;
            else if (value > 10.0f)
                audioManager.bgmVolume = 10.0f;
        }

        public void SetSfxVolume(float value)
        {
            audioManager.sfxVolume = value;

            if (value < 0.1f)
                audioManager.sfxVolume = 0.1f;
            else if (value > 10.0f)
                audioManager.sfxVolume = 10.0f;
        }

        #endregion

        #region Instance Disposal Methods

        /// <summary>
        /// Clean up the component when it is disposing
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (soundBank != null)
                    {
                        soundBank.Dispose();
                        soundBank = null;
                    }

                    if (sfxWaveBank != null)
                    {
                        sfxWaveBank.Dispose();
                        sfxWaveBank = null;
                    }

                    if (bgmWaveBank != null)
                    {
                        bgmWaveBank.Dispose();
                        bgmWaveBank = null;
                    }

                    if (audioEngine != null)
                    {
                        audioEngine.Dispose();
                        audioEngine = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        #endregion
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s