123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #region File Description
- //-----------------------------------------------------------------------------
- // AudioManager.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using System.Collections.Generic;
- #endregion
- namespace RolePlaying
- {
- /// <summary>
- /// Component that manages audio playback for all cues.
- /// </summary>
- /// <remarks>
- /// Similar to a class found in the Net Rumble starter kit on the
- /// XNA Creators Club Online website (http://creators.xna.com).
- /// </remarks>
- public class AudioManager : GameComponent
- {
- #region Singleton
- /// <summary>
- /// The singleton for this type.
- /// </summary>
- private static AudioManager audioManager = null;
- #endregion
- #region Audio Data
- /// <summary>
- /// The audio engine used to play all cues.
- /// </summary>
- private AudioEngine audioEngine;
- /// <summary>
- /// The soundbank that contains all cues.
- /// </summary>
- private SoundBank soundBank;
- /// <summary>
- /// The wavebank with all wave files for this game.
- /// </summary>
- private WaveBank waveBank;
- #endregion
- #region Initialization Methods
- /// <summary>
- /// Constructs the manager for audio playback of all cues.
- /// </summary>
- /// <param name="game">The game that this component will be attached to.</param>
- /// <param name="settingsFile">The filename of the XACT settings file.</param>
- /// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
- /// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
- private AudioManager(Game game, string settingsFile, string waveBankFile,
- string soundBankFile)
- : base(game)
- {
- try
- {
- audioEngine = new AudioEngine(settingsFile);
- waveBank = new WaveBank(audioEngine, waveBankFile);
- soundBank = new SoundBank(audioEngine, soundBankFile);
- }
- catch (NoAudioHardwareException)
- {
- // silently fall back to silence
- audioEngine = null;
- waveBank = null;
- soundBank = null;
- }
- }
- /// <summary>
- /// Initialize the static AudioManager functionality.
- /// </summary>
- /// <param name="game">The game that this component will be attached to.</param>
- /// <param name="settingsFile">The filename of the XACT settings file.</param>
- /// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
- /// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
- 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);
- }
- }
- #endregion
- #region Cue Methods
- /// <summary>
- /// Retrieve a cue by name.
- /// </summary>
- /// <param name="cueName">The name of the cue requested.</param>
- /// <returns>The cue corresponding to the name provided.</returns>
- public static Cue GetCue(string cueName)
- {
- if (String.IsNullOrEmpty(cueName) ||
- (audioManager == null) || (audioManager.audioEngine == null) ||
- (audioManager.soundBank == null) || (audioManager.waveBank == null))
- {
- return null;
- }
- return audioManager.soundBank.GetCue(cueName);
- }
- /// <summary>
- /// Plays a cue by name.
- /// </summary>
- /// <param name="cueName">The name of the cue to play.</param>
- public static void PlayCue(string cueName)
- {
- if ((audioManager != null) && (audioManager.audioEngine != null) &&
- (audioManager.soundBank != null) && (audioManager.waveBank != null))
- {
- //audioManager.soundBank.PlayCue(cueName);
- }
- }
- #endregion
- #region Music
- /// <summary>
- /// The cue for the music currently playing, if any.
- /// </summary>
- private Cue musicCue;
- /// <summary>
- /// Stack of music cue names, for layered music playback.
- /// </summary>
- private Stack<string> musicCueNameStack = new Stack<string>();
- /// <summary>
- /// Plays the desired music, clearing the stack of music cues.
- /// </summary>
- /// <param name="cueName">The name of the music cue to play.</param>
- public static void PlayMusic(string cueName)
- {
- // start the new music cue
- if (audioManager != null)
- {
- audioManager.musicCueNameStack.Clear();
- PushMusic(cueName);
- }
- }
- /// <summary>
- /// Plays the music for this game, adding it to the music stack.
- /// </summary>
- /// <param name="cueName">The name of the music cue to play.</param>
- public static void PushMusic(string cueName)
- {
- // start the new music cue
- if ((audioManager != null) && (audioManager.audioEngine != null) &&
- (audioManager.soundBank != null) && (audioManager.waveBank != null))
- {
- audioManager.musicCueNameStack.Push(cueName);
- if ((audioManager.musicCue == null) ||
- (audioManager.musicCue.Name != cueName))
- {
- if (audioManager.musicCue != null)
- {
- audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
- //audioManager.musicCue.Dispose();
- audioManager.musicCue = null;
- }
- audioManager.musicCue = GetCue(cueName);
- if (audioManager.musicCue != null)
- {
- audioManager.musicCue.Play();
- }
- }
- }
- }
- /// <summary>
- /// Stops the current music and plays the previous music on the stack.
- /// </summary>
- public static void PopMusic()
- {
- // start the new music cue
- if ((audioManager != null) && (audioManager.audioEngine != null) &&
- (audioManager.soundBank != null) && (audioManager.waveBank != null))
- {
- string cueName = null;
- if (audioManager.musicCueNameStack.Count > 0)
- {
- audioManager.musicCueNameStack.Pop();
- if (audioManager.musicCueNameStack.Count > 0)
- {
- cueName = audioManager.musicCueNameStack.Peek();
- }
- }
- if ((audioManager.musicCue == null) ||
- (audioManager.musicCue.Name != cueName))
- {
- if (audioManager.musicCue != null)
- {
- audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
- //audioManager.musicCue.Dispose();
- audioManager.musicCue = null;
- }
- if (!String.IsNullOrEmpty(cueName))
- {
- audioManager.musicCue = GetCue(cueName);
- if (audioManager.musicCue != null)
- {
- audioManager.musicCue.Play();
- }
- }
- }
- }
- }
- /// <summary>
- /// Stop music playback, clearing the cue.
- /// </summary>
- public static void StopMusic()
- {
- if (audioManager != null)
- {
- audioManager.musicCueNameStack.Clear();
- if (audioManager.musicCue != null)
- {
- audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
- //audioManager.musicCue.Dispose();
- audioManager.musicCue = null;
- }
- }
- }
- #endregion
- #region Updating Methods
- /// <summary>
- /// Update the audio manager, particularly the engine.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- public override void Update(GameTime gameTime)
- {
- // update the audio engine
- if (audioEngine != null)
- {
- //audioEngine.Update();
- }
- //if ((musicCue != null) && musicCue.IsStopped)
- //{
- // AudioManager.PopMusic();
- //}
- base.Update(gameTime);
- }
- #endregion
- #region Instance Disposal Methods
- /// <summary>
- /// Clean up the component when it is disposing.
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- try
- {
- if (disposing)
- {
- StopMusic();
- if (soundBank != null)
- {
- //soundBank.Dispose();
- soundBank = null;
- }
- if (waveBank != null)
- {
- //waveBank.Dispose();
- waveBank = null;
- }
- if (audioEngine != null)
- {
- //audioEngine.Dispose();
- audioEngine = null;
- }
- }
- }
- finally
- {
- base.Dispose(disposing);
- }
- }
- #endregion
- }
- }
|