AudioManager.cs 9.8 KB

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