Guest User

Sound Stuff for GameDev

a guest
Jun 5th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2. /// Manages all sound instances.
  3. /// </summary>
  4. public static class Audio
  5. {
  6.     static XAudio2 device;
  7.     static MasteringVoice master;
  8.  
  9.     static List<SoundInstance> instances;
  10.  
  11.     /// <summary>
  12.     /// The XAudio2 device.
  13.     /// </summary>
  14.     internal static XAudio2 Device
  15.     {
  16.         get { return device; }
  17.     }
  18.  
  19.     /// <summary>
  20.     /// Initializes the audio device and master track.
  21.     /// </summary>
  22.     internal static void Initialize()
  23.     {
  24.         device = new XAudio2();
  25.         master = new MasteringVoice(device);
  26.  
  27.         instances = new List<SoundInstance>();
  28.     }
  29.  
  30.     /// <summary>
  31.     /// Releases all XA2 resources.
  32.     /// </summary>
  33.     internal static void Shutdown()
  34.     {
  35.         foreach(SoundInstance i in instances)
  36.             i.Dispose();
  37.         master.Dispose();
  38.         device.Dispose();
  39.     }
  40.  
  41.     /// <summary>
  42.     /// Registers a sound instance with the system.
  43.     /// </summary>
  44.     /// <param name="instance">Sound instance</param>
  45.     internal static void AddInstance(SoundInstance instance)
  46.     {
  47.         instances.Add(instance);
  48.     }
  49.  
  50.     /// <summary>
  51.     /// Disposes any sound instance that has stopped playing.
  52.     /// </summary>
  53.     internal static void Update()
  54.     {
  55.         List<SoundInstance> temp = new List<SoundInstance>(instances);
  56.         foreach(SoundInstance i in temp)
  57.             if(!i.Playing)
  58.             {
  59.                 i.Dispose();
  60.                 instances.Remove(i);
  61.             }
  62.     }
  63. }
  64.  
  65. /// <summary>
  66. /// Loads sounds from various files.
  67. /// </summary>
  68. internal class SoundLoader
  69. {
  70.     /// <summary>
  71.     /// Loads a .wav sound file.
  72.     /// </summary>
  73.     /// <param name="format">The decoded format will be sent here</param>
  74.     /// <param name="buffer">The data will be sent here</param>
  75.     /// <param name="soundName">The path to the WAV file</param>
  76.     internal static void LoadWAV(out WaveFormat format, out AudioBuffer buffer, string soundName)
  77.     {
  78.         WaveStream wave = new WaveStream(soundName);
  79.  
  80.         format = wave.Format;
  81.  
  82.         buffer = new AudioBuffer();
  83.         buffer.AudioData = wave;
  84.         buffer.AudioBytes = (int)wave.Length;
  85.         buffer.Flags = BufferFlags.EndOfStream;
  86.     }
  87. }
  88.  
  89. /// <summary>
  90. /// Manages the data for a single sound.
  91. /// </summary>
  92. public class Sound : IAsset
  93. {
  94.     WaveFormat format;
  95.     AudioBuffer buffer;
  96.  
  97.     /// <summary>
  98.     /// Loads a sound from a file.
  99.     /// </summary>
  100.     /// <param name="soundName">The path to the sound file</param>
  101.     /// <returns>Whether the sound loaded successfully</returns>
  102.     public bool Load(string soundName)
  103.     {
  104.         if(soundName.EndsWith(".wav"))
  105.             SoundLoader.LoadWAV(out format, out buffer, soundName);
  106.         else
  107.             return false;
  108.  
  109.         return true;
  110.     }
  111.  
  112.     /// <summary>
  113.     /// Plays the sound.
  114.     /// </summary>
  115.     public void Play()
  116.     {
  117.         Audio.AddInstance(new SoundInstance(format, buffer));
  118.     }
  119.  
  120.     /// <summary>
  121.     /// Unloads the sound from memory.
  122.     /// </summary>
  123.     public void Unload()
  124.     {
  125.         buffer.Dispose();
  126.     }
  127. }
  128.  
  129. /// <summary>
  130. /// Manages a single sound instance.
  131. /// </summary>
  132. public class SoundInstance
  133. {
  134.     SourceVoice source;
  135.     bool playing;
  136.  
  137.     /// <summary>
  138.     /// Whether the sound is currently playing.
  139.     /// </summary>
  140.     public bool Playing
  141.     {
  142.         get { return playing; }
  143.     }
  144.  
  145.     /// <summary>
  146.     /// Starts a new instance of a sound.
  147.     /// </summary>
  148.     /// <param name="format">Format of the sound</param>
  149.     /// <param name="buffer">Buffer holding sound data</param>
  150.     internal SoundInstance(WaveFormat format, AudioBuffer buffer)
  151.     {
  152.         source = new SourceVoice(Audio.Device, format);
  153.         source.BufferEnd += (s, e) => playing = false;
  154.  
  155.         source.Start();
  156.         source.SubmitSourceBuffer(buffer); // THIS IS WHERE THE EXCEPTION IS THROWN
  157.         playing = true;
  158.     }
  159.  
  160.     /// <summary>
  161.     /// Releases memory used by the instance.
  162.     /// </summary>
  163.     internal void Dispose()
  164.     {
  165.         source.Dispose();
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment