Sound.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Sound.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 System.Collections.Generic;
  12. using System.Text;
  13. using System.IO;
  14. using Microsoft.Xna.Framework.Audio;
  15. #endregion
  16. namespace Spacewar
  17. {
  18. /// <summary>
  19. /// An enum for all of the spaceWar sounds
  20. /// </summary>
  21. public enum Sounds
  22. {
  23. /// <summary>
  24. /// Peashoot gun shot sound
  25. /// </summary>
  26. PeashooterFire,
  27. /// <summary>
  28. /// Entering hyperspace
  29. /// </summary>
  30. HyperspaceActivate,
  31. /// <summary>
  32. /// Returning from hyperspace
  33. /// </summary>
  34. HyperspaceReturn,
  35. /// <summary>
  36. /// Rocket weapon sound
  37. /// </summary>
  38. RocketFire,
  39. /// <summary>
  40. /// Rocket weapon final explosion sound
  41. /// </summary>
  42. RocketExplode,
  43. /// <summary>
  44. /// BFG weapon sound
  45. /// </summary>
  46. BFGFire,
  47. /// <summary>
  48. /// Menu Selection Sound
  49. /// </summary>
  50. MenuSelect,
  51. /// <summary>
  52. /// Menu Advance sound
  53. /// </summary>
  54. MenuAdvance,
  55. /// <summary>
  56. /// Menu Back Sound
  57. /// </summary>
  58. MenuBack,
  59. /// <summary>
  60. /// Bad Menu selection Sound
  61. /// </summary>
  62. MenuBadSelect,
  63. /// <summary>
  64. /// Menu Scroll sound
  65. /// </summary>
  66. MenuScroll,
  67. /// <summary>
  68. /// Alternate Menu selection sound
  69. /// </summary>
  70. MenuSelect2,
  71. /// <summary>
  72. /// Another Alternate Menu selection Sound
  73. /// </summary>
  74. MenuSelect3,
  75. /// <summary>
  76. /// Bonus weapon pickup sound
  77. /// </summary>
  78. WeaponPickup,
  79. /// <summary>
  80. /// Timer expiry sound
  81. /// </summary>
  82. CountDownExpire,
  83. /// <summary>
  84. /// Beep sound for last few seconds of a countdown
  85. /// </summary>
  86. CountDownWarning,
  87. /// <summary>
  88. /// Phase Activation sound
  89. /// </summary>
  90. PhaseActivate,
  91. /// <summary>
  92. /// Phase Expiration sound
  93. /// </summary>
  94. PhaseExpire,
  95. /// <summary>
  96. /// Engine sound for player1
  97. /// </summary>
  98. ThrustPlayer1,
  99. /// <summary>
  100. /// Engine sound for player2
  101. /// </summary>
  102. ThrustPlayer2,
  103. /// <summary>
  104. /// Title screen music
  105. /// </summary>
  106. TitleMusic,
  107. /// <summary>
  108. /// Ambient music for menu backgrounds
  109. /// </summary>
  110. MenuMusic,
  111. /// <summary>
  112. /// Counter when points or money ar tallying
  113. /// </summary>
  114. PointsTally,
  115. /// <summary>
  116. /// General explosion
  117. /// </summary>
  118. Explosion,
  119. /// <summary>
  120. /// Machine gun weapon sound
  121. /// </summary>
  122. MachineGunFire,
  123. /// <summary>
  124. /// Double machine gun weapon sound
  125. /// </summary>
  126. DoubleMachineGunFire,
  127. /// <summary>
  128. /// BFG Explosion sound
  129. /// </summary>
  130. BFGExplode,
  131. /// <summary>
  132. /// When ship is damaged
  133. /// </summary>
  134. DamageShip,
  135. /// <summary>
  136. /// When ship is destroyed
  137. /// </summary>
  138. ExplodeShip,
  139. }
  140. /// <summary>
  141. /// Abstracts away the sounds for a simple interface using the Sounds enum
  142. /// </summary>
  143. public static class Sound
  144. {
  145. private static AudioEngine engine;
  146. private static WaveBank wavebank;
  147. private static SoundBank soundbank;
  148. private static string[] cueNames = new string[]
  149. {
  150. "tx0_fire", //
  151. "hyperspace_activate", //
  152. "hyperspace_return", //
  153. "pdp3_fire",
  154. "pdp3_explode",
  155. "hax2_fire",
  156. "menu_select",
  157. "menu_advance", //Used all over - probbly need to choose where is back/select/advance etc
  158. "menu_back",
  159. "menu_bad_select",
  160. "menu_scroll",
  161. "menu_select2",
  162. "menu_select3",
  163. "weapon_pickup",
  164. "countdown_expire", //
  165. "countdown_warning", //
  166. "phase_activate",
  167. "phase_expire",
  168. "accel_player1", //
  169. "accel_player2", //
  170. "title_music", //sort of - not working!
  171. "menu_music", //sort of - not working
  172. "points_tally", //used on cash right now
  173. "explosion_generic",
  174. "pdp1_fire",
  175. "pdp2_fire",
  176. "hax2_impact",
  177. "damage_ship",
  178. "explosion_ship",
  179. };
  180. /// <summary>
  181. /// Plays a sound
  182. /// </summary>
  183. /// <param name="sound">Which sound to play</param>
  184. /// <returns>XACT cue to be used if you want to stop this particular looped sound. Can NOT be ignored. If the cue returned goes out of scope, the sound stops!!</returns>
  185. public static Cue Play(Sounds sound)
  186. {
  187. Cue returnValue = soundbank.GetCue(cueNames[(int)sound]);
  188. returnValue.Play();
  189. return returnValue;
  190. }
  191. /// <summary>
  192. /// Plays a sound
  193. /// </summary>
  194. /// <param name="sound">Which sound to play</param>
  195. /// <returns>Nothing! This cue will play through to completion and then free itself.</returns>
  196. public static void PlayCue(Sounds sound)
  197. {
  198. soundbank.PlayCue(cueNames[(int)sound]);
  199. }
  200. /// <summary>
  201. /// Pumps the AudioEngine to help it clean itself up
  202. /// </summary>
  203. public static void Update()
  204. {
  205. engine.Update();
  206. }
  207. /// <summary>
  208. /// Stops a previously playing cue
  209. /// </summary>
  210. /// <param name="cue">The cue to stop that you got returned from Play(sound)</param>
  211. public static void Stop(Cue cue)
  212. {
  213. cue.Stop(AudioStopOptions.Immediate);
  214. }
  215. /// <summary>
  216. /// Starts up the sound code
  217. /// </summary>
  218. public static void Initialize()
  219. {
  220. engine = new AudioEngine(SpacewarGame.Settings.MediaPath + @"audio\spacewar.xgs");
  221. wavebank = new WaveBank(engine, SpacewarGame.Settings.MediaPath + @"audio\spacewar.xwb");
  222. soundbank = new SoundBank(engine, SpacewarGame.Settings.MediaPath + @"audio\spacewar.xsb");
  223. }
  224. /// <summary>
  225. /// Shuts down the sound code tidily
  226. /// </summary>
  227. public static void Shutdown()
  228. {
  229. soundbank.Dispose();
  230. wavebank.Dispose();
  231. engine.Dispose();
  232. }
  233. }
  234. }