AudioManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AudioManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Audio;
  13. using System.Collections.Generic;
  14. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// Component that manages audio playback for all cues.
  19. /// </summary>
  20. /// <remarks>
  21. /// Similar to a class found in the Net Rumble starter kit on the
  22. /// XNA Creators Club Online website (http://creators.xna.com).
  23. /// </remarks>
  24. public class AudioManager : GameComponent
  25. {
  26. #region Singleton
  27. /// <summary>
  28. /// The singleton for this type.
  29. /// </summary>
  30. private static AudioManager audioManager = null;
  31. #endregion
  32. #region Audio Data
  33. /// <summary>
  34. /// The audio engine used to play all cues.
  35. /// </summary>
  36. private AudioEngine audioEngine;
  37. /// <summary>
  38. /// The soundbank that contains all cues.
  39. /// </summary>
  40. private SoundBank soundBank;
  41. /// <summary>
  42. /// The wavebank with all wave files for this game.
  43. /// </summary>
  44. private WaveBank waveBank;
  45. #endregion
  46. #region Initialization Methods
  47. /// <summary>
  48. /// Constructs the manager for audio playback of all cues.
  49. /// </summary>
  50. /// <param name="game">The game that this component will be attached to.</param>
  51. /// <param name="settingsFile">The filename of the XACT settings file.</param>
  52. /// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
  53. /// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
  54. private AudioManager(Game game, string settingsFile, string waveBankFile,
  55. string soundBankFile)
  56. : base(game)
  57. {
  58. try
  59. {
  60. audioEngine = new AudioEngine(settingsFile);
  61. waveBank = new WaveBank(audioEngine, waveBankFile);
  62. soundBank = new SoundBank(audioEngine, soundBankFile);
  63. }
  64. catch (NoAudioHardwareException)
  65. {
  66. // silently fall back to silence
  67. audioEngine = null;
  68. waveBank = null;
  69. soundBank = null;
  70. }
  71. }
  72. /// <summary>
  73. /// Initialize the static AudioManager functionality.
  74. /// </summary>
  75. /// <param name="game">The game that this component will be attached to.</param>
  76. /// <param name="settingsFile">The filename of the XACT settings file.</param>
  77. /// <param name="waveBankFile">The filename of the XACT wavebank file.</param>
  78. /// <param name="soundBankFile">The filename of the XACT soundbank file.</param>
  79. public static void Initialize(Game game, string settingsFile,
  80. string waveBankFile, string soundBankFile)
  81. {
  82. audioManager = new AudioManager(game, settingsFile, waveBankFile,
  83. soundBankFile);
  84. if (game != null)
  85. {
  86. game.Components.Add(audioManager);
  87. }
  88. }
  89. #endregion
  90. #region Cue Methods
  91. /// <summary>
  92. /// Retrieve a cue by name.
  93. /// </summary>
  94. /// <param name="cueName">The name of the cue requested.</param>
  95. /// <returns>The cue corresponding to the name provided.</returns>
  96. public static Cue GetCue(string cueName)
  97. {
  98. if (String.IsNullOrEmpty(cueName) ||
  99. (audioManager == null) || (audioManager.audioEngine == null) ||
  100. (audioManager.soundBank == null) || (audioManager.waveBank == null))
  101. {
  102. return null;
  103. }
  104. return audioManager.soundBank.GetCue(cueName);
  105. }
  106. /// <summary>
  107. /// Plays a cue by name.
  108. /// </summary>
  109. /// <param name="cueName">The name of the cue to play.</param>
  110. public static void PlayCue(string cueName)
  111. {
  112. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  113. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  114. {
  115. //audioManager.soundBank.PlayCue(cueName);
  116. }
  117. }
  118. #endregion
  119. #region Music
  120. /// <summary>
  121. /// The cue for the music currently playing, if any.
  122. /// </summary>
  123. private Cue musicCue;
  124. /// <summary>
  125. /// Stack of music cue names, for layered music playback.
  126. /// </summary>
  127. private Stack<string> musicCueNameStack = new Stack<string>();
  128. /// <summary>
  129. /// Plays the desired music, clearing the stack of music cues.
  130. /// </summary>
  131. /// <param name="cueName">The name of the music cue to play.</param>
  132. public static void PlayMusic(string cueName)
  133. {
  134. // start the new music cue
  135. if (audioManager != null)
  136. {
  137. audioManager.musicCueNameStack.Clear();
  138. PushMusic(cueName);
  139. }
  140. }
  141. /// <summary>
  142. /// Plays the music for this game, adding it to the music stack.
  143. /// </summary>
  144. /// <param name="cueName">The name of the music cue to play.</param>
  145. public static void PushMusic(string cueName)
  146. {
  147. // start the new music cue
  148. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  149. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  150. {
  151. audioManager.musicCueNameStack.Push(cueName);
  152. if ((audioManager.musicCue == null) ||
  153. (audioManager.musicCue.Name != cueName))
  154. {
  155. if (audioManager.musicCue != null)
  156. {
  157. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  158. //audioManager.musicCue.Dispose();
  159. audioManager.musicCue = null;
  160. }
  161. audioManager.musicCue = GetCue(cueName);
  162. if (audioManager.musicCue != null)
  163. {
  164. audioManager.musicCue.Play();
  165. }
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// Stops the current music and plays the previous music on the stack.
  171. /// </summary>
  172. public static void PopMusic()
  173. {
  174. // start the new music cue
  175. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  176. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  177. {
  178. string cueName = null;
  179. if (audioManager.musicCueNameStack.Count > 0)
  180. {
  181. audioManager.musicCueNameStack.Pop();
  182. if (audioManager.musicCueNameStack.Count > 0)
  183. {
  184. cueName = audioManager.musicCueNameStack.Peek();
  185. }
  186. }
  187. if ((audioManager.musicCue == null) ||
  188. (audioManager.musicCue.Name != cueName))
  189. {
  190. if (audioManager.musicCue != null)
  191. {
  192. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  193. //audioManager.musicCue.Dispose();
  194. audioManager.musicCue = null;
  195. }
  196. if (!String.IsNullOrEmpty(cueName))
  197. {
  198. audioManager.musicCue = GetCue(cueName);
  199. if (audioManager.musicCue != null)
  200. {
  201. audioManager.musicCue.Play();
  202. }
  203. }
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Stop music playback, clearing the cue.
  209. /// </summary>
  210. public static void StopMusic()
  211. {
  212. if (audioManager != null)
  213. {
  214. audioManager.musicCueNameStack.Clear();
  215. if (audioManager.musicCue != null)
  216. {
  217. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  218. //audioManager.musicCue.Dispose();
  219. audioManager.musicCue = null;
  220. }
  221. }
  222. }
  223. #endregion
  224. #region Updating Methods
  225. /// <summary>
  226. /// Update the audio manager, particularly the engine.
  227. /// </summary>
  228. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  229. public override void Update(GameTime gameTime)
  230. {
  231. // update the audio engine
  232. if (audioEngine != null)
  233. {
  234. //audioEngine.Update();
  235. }
  236. //if ((musicCue != null) && musicCue.IsStopped)
  237. //{
  238. // AudioManager.PopMusic();
  239. //}
  240. base.Update(gameTime);
  241. }
  242. #endregion
  243. #region Instance Disposal Methods
  244. /// <summary>
  245. /// Clean up the component when it is disposing.
  246. /// </summary>
  247. protected override void Dispose(bool disposing)
  248. {
  249. try
  250. {
  251. if (disposing)
  252. {
  253. StopMusic();
  254. if (soundBank != null)
  255. {
  256. //soundBank.Dispose();
  257. soundBank = null;
  258. }
  259. if (waveBank != null)
  260. {
  261. //waveBank.Dispose();
  262. waveBank = null;
  263. }
  264. if (audioEngine != null)
  265. {
  266. //audioEngine.Dispose();
  267. audioEngine = null;
  268. }
  269. }
  270. }
  271. finally
  272. {
  273. base.Dispose(disposing);
  274. }
  275. }
  276. #endregion
  277. }
  278. }