AudioManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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,
  74. settingsFile,
  75. waveBankFile,
  76. soundBankFile);
  77. if (game != null)
  78. {
  79. game.Components.Add(audioManager);
  80. }
  81. }
  82. /// <summary>
  83. /// Retrieve a cue by name.
  84. /// </summary>
  85. /// <param name="cueName">The name of the cue requested.</param>
  86. /// <returns>The cue corresponding to the name provided.</returns>
  87. public static Cue GetCue(string cueName)
  88. {
  89. if (String.IsNullOrEmpty(cueName) ||
  90. (audioManager == null) || (audioManager.audioEngine == null) ||
  91. (audioManager.soundBank == null) || (audioManager.waveBank == null))
  92. {
  93. return null;
  94. }
  95. return audioManager.soundBank.GetCue(cueName);
  96. }
  97. /// <summary>
  98. /// Plays a cue by name.
  99. /// </summary>
  100. /// <param name="cueName">The name of the cue to play.</param>
  101. public static void PlayCue(string cueName)
  102. {
  103. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  104. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  105. {
  106. //audioManager.soundBank.PlayCue(cueName);
  107. }
  108. }
  109. /// <summary>
  110. /// The cue for the music currently playing, if any.
  111. /// </summary>
  112. private Cue musicCue;
  113. /// <summary>
  114. /// Stack of music cue names, for layered music playback.
  115. /// </summary>
  116. private Stack<string> musicCueNameStack = new Stack<string>();
  117. /// <summary>
  118. /// Plays the desired music, clearing the stack of music cues.
  119. /// </summary>
  120. /// <param name="cueName">The name of the music cue to play.</param>
  121. public static void PlayMusic(string cueName)
  122. {
  123. // start the new music cue
  124. if (audioManager != null)
  125. {
  126. audioManager.musicCueNameStack.Clear();
  127. PushMusic(cueName);
  128. }
  129. }
  130. /// <summary>
  131. /// Plays the music for this game, adding it to the music stack.
  132. /// </summary>
  133. /// <param name="cueName">The name of the music cue to play.</param>
  134. public static void PushMusic(string cueName)
  135. {
  136. // start the new music cue
  137. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  138. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  139. {
  140. audioManager.musicCueNameStack.Push(cueName);
  141. if ((audioManager.musicCue == null) ||
  142. (audioManager.musicCue.Name != cueName))
  143. {
  144. if (audioManager.musicCue != null)
  145. {
  146. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  147. //audioManager.musicCue.Dispose();
  148. audioManager.musicCue = null;
  149. }
  150. audioManager.musicCue = GetCue(cueName);
  151. if (audioManager.musicCue != null)
  152. {
  153. audioManager.musicCue.Play();
  154. }
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Stops the current music and plays the previous music on the stack.
  160. /// </summary>
  161. public static void PopMusic()
  162. {
  163. // start the new music cue
  164. if ((audioManager != null) && (audioManager.audioEngine != null) &&
  165. (audioManager.soundBank != null) && (audioManager.waveBank != null))
  166. {
  167. string cueName = null;
  168. if (audioManager.musicCueNameStack.Count > 0)
  169. {
  170. audioManager.musicCueNameStack.Pop();
  171. if (audioManager.musicCueNameStack.Count > 0)
  172. {
  173. cueName = audioManager.musicCueNameStack.Peek();
  174. }
  175. }
  176. if ((audioManager.musicCue == null) ||
  177. (audioManager.musicCue.Name != cueName))
  178. {
  179. if (audioManager.musicCue != null)
  180. {
  181. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  182. //audioManager.musicCue.Dispose();
  183. audioManager.musicCue = null;
  184. }
  185. if (!String.IsNullOrEmpty(cueName))
  186. {
  187. audioManager.musicCue = GetCue(cueName);
  188. if (audioManager.musicCue != null)
  189. {
  190. audioManager.musicCue.Play();
  191. }
  192. }
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// Stop music playback, clearing the cue.
  198. /// </summary>
  199. public static void StopMusic()
  200. {
  201. if (audioManager != null)
  202. {
  203. audioManager.musicCueNameStack.Clear();
  204. if (audioManager.musicCue != null)
  205. {
  206. audioManager.musicCue.Stop(AudioStopOptions.AsAuthored);
  207. //audioManager.musicCue.Dispose();
  208. audioManager.musicCue = null;
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// Update the audio manager, particularly the engine.
  214. /// </summary>
  215. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  216. public override void Update(GameTime gameTime)
  217. {
  218. // update the audio engine
  219. if (audioEngine != null)
  220. {
  221. //audioEngine.Update();
  222. }
  223. //if ((musicCue != null) && musicCue.IsStopped)
  224. //{
  225. // AudioManager.PopMusic();
  226. //}
  227. base.Update(gameTime);
  228. }
  229. /// <summary>
  230. /// Clean up the component when it is disposing.
  231. /// </summary>
  232. protected override void Dispose(bool disposing)
  233. {
  234. try
  235. {
  236. if (disposing)
  237. {
  238. StopMusic();
  239. if (soundBank != null)
  240. {
  241. //soundBank.Dispose();
  242. soundBank = null;
  243. }
  244. if (waveBank != null)
  245. {
  246. //waveBank.Dispose();
  247. waveBank = null;
  248. }
  249. if (audioEngine != null)
  250. {
  251. //audioEngine.Dispose();
  252. audioEngine = null;
  253. }
  254. }
  255. }
  256. finally
  257. {
  258. base.Dispose(disposing);
  259. }
  260. }
  261. }
  262. }