AudioManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Collections.Generic;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Audio;
  13. #endregion
  14. namespace Audio3D
  15. {
  16. /// <summary>
  17. /// Audio manager keeps track of what 3D sounds are playing, updating
  18. /// their settings as the camera and entities move around the world, and
  19. /// automatically disposing sound effect instances after they finish playing.
  20. /// </summary>
  21. public class AudioManager : Microsoft.Xna.Framework.GameComponent
  22. {
  23. #region Fields
  24. // List of all the sound effects that will be loaded into this manager.
  25. static string[] soundNames =
  26. {
  27. "CatSound0",
  28. "CatSound1",
  29. "CatSound2",
  30. "DogSound",
  31. };
  32. // The listener describes the ear which is hearing 3D sounds.
  33. // This is usually set to match the camera.
  34. public AudioListener Listener
  35. {
  36. get { return listener; }
  37. }
  38. AudioListener listener = new AudioListener();
  39. // The emitter describes an entity which is making a 3D sound.
  40. AudioEmitter emitter = new AudioEmitter();
  41. // Store all the sound effects that are available to be played.
  42. Dictionary<string, SoundEffect> soundEffects = new Dictionary<string, SoundEffect>();
  43. // Keep track of all the 3D sounds that are currently playing.
  44. List<ActiveSound> activeSounds = new List<ActiveSound>();
  45. #endregion
  46. public AudioManager(Game game)
  47. : base(game)
  48. { }
  49. /// <summary>
  50. /// Initializes the audio manager.
  51. /// </summary>
  52. public override void Initialize()
  53. {
  54. // Set the scale for 3D audio so it matches the scale of our game world.
  55. // DistanceScale controls how much sounds change volume as you move further away.
  56. // DopplerScale controls how much sounds change pitch as you move past them.
  57. SoundEffect.DistanceScale = 2000;
  58. SoundEffect.DopplerScale = 0.1f;
  59. // Load all the sound effects.
  60. foreach (string soundName in soundNames)
  61. {
  62. soundEffects.Add(soundName, Game.Content.Load<SoundEffect>(soundName));
  63. }
  64. base.Initialize();
  65. }
  66. /// <summary>
  67. /// Unloads the sound effect data.
  68. /// </summary>
  69. protected override void Dispose(bool disposing)
  70. {
  71. try
  72. {
  73. if (disposing)
  74. {
  75. foreach (SoundEffect soundEffect in soundEffects.Values)
  76. {
  77. soundEffect.Dispose();
  78. }
  79. soundEffects.Clear();
  80. }
  81. }
  82. finally
  83. {
  84. base.Dispose(disposing);
  85. }
  86. }
  87. /// <summary>
  88. /// Updates the state of the 3D audio system.
  89. /// </summary>
  90. public override void Update(GameTime gameTime)
  91. {
  92. // Loop over all the currently playing 3D sounds.
  93. int index = 0;
  94. while (index < activeSounds.Count)
  95. {
  96. ActiveSound activeSound = activeSounds[index];
  97. if (activeSound.Instance.State == SoundState.Stopped)
  98. {
  99. // If the sound has stopped playing, dispose it.
  100. activeSound.Instance.Dispose();
  101. // Remove it from the active list.
  102. activeSounds.RemoveAt(index);
  103. }
  104. else
  105. {
  106. // If the sound is still playing, update its 3D settings.
  107. Apply3D(activeSound);
  108. index++;
  109. }
  110. }
  111. base.Update(gameTime);
  112. }
  113. /// <summary>
  114. /// Triggers a new 3D sound.
  115. /// </summary>
  116. public SoundEffectInstance Play3DSound(string soundName, bool isLooped, IAudioEmitter emitter)
  117. {
  118. ActiveSound activeSound = new ActiveSound();
  119. // Fill in the instance and emitter fields.
  120. activeSound.Instance = soundEffects[soundName].CreateInstance();
  121. activeSound.Instance.IsLooped = isLooped;
  122. activeSound.Emitter = emitter;
  123. // Set the 3D position of this sound, and then play it.
  124. Apply3D(activeSound);
  125. activeSound.Instance.Play();
  126. // Remember that this sound is now active.
  127. activeSounds.Add(activeSound);
  128. return activeSound.Instance;
  129. }
  130. /// <summary>
  131. /// Updates the position and velocity settings of a 3D sound.
  132. /// </summary>
  133. private void Apply3D(ActiveSound activeSound)
  134. {
  135. emitter.Position = activeSound.Emitter.Position;
  136. emitter.Forward = activeSound.Emitter.Forward;
  137. emitter.Up = activeSound.Emitter.Up;
  138. emitter.Velocity = activeSound.Emitter.Velocity;
  139. activeSound.Instance.Apply3D(listener, emitter);
  140. }
  141. /// <summary>
  142. /// Internal helper class for keeping track of an active 3D sound,
  143. /// and remembering which emitter object it is attached to.
  144. /// </summary>
  145. private class ActiveSound
  146. {
  147. public SoundEffectInstance Instance;
  148. public IAudioEmitter Emitter;
  149. }
  150. }
  151. }