From ef4cb698e444162c1ebff9053b382f958ceda36d Mon Sep 17 00:00:00 2001
From: Glatzemann <glatzemann@users.noreply.github.com>
Date: Tue, 13 Nov 2012 10:18:16 +0000
Subject: [PATCH] removed some Debugging code from SoundSystem.Windows.XAudio
 and optimized error handling for Windows 8

---
 Samples/SimpleNoContent/Game1.cs              |  9 ++
 .../ANX.SoundSystem.Windows.XAudio/Creator.cs | 89 ++++++++++++-------
 .../Properties/AssemblyInfo.cs                |  6 +-
 3 files changed, 69 insertions(+), 35 deletions(-)

diff --git a/Samples/SimpleNoContent/Game1.cs b/Samples/SimpleNoContent/Game1.cs
index 47887e6a..c595c7df 100644
--- a/Samples/SimpleNoContent/Game1.cs
+++ b/Samples/SimpleNoContent/Game1.cs
@@ -18,6 +18,8 @@ namespace SimpleModernUI
         GraphicsDeviceManager graphics;
         SpriteBatch spriteBatch;
 
+        Texture2D texture;
+
         public Game1()
         {
             graphics = new GraphicsDeviceManager(this);
@@ -32,6 +34,9 @@ namespace SimpleModernUI
         protected override void LoadContent()
         {
             spriteBatch = new SpriteBatch(GraphicsDevice);
+
+            texture = new Texture2D(GraphicsDevice, 1, 1);
+            texture.SetData<Color>(new Color[] { Color.White });
         }
 
         protected override void UnloadContent()
@@ -54,6 +59,10 @@ namespace SimpleModernUI
         {
             GraphicsDevice.Clear(Color.CornflowerBlue);
 
+            spriteBatch.Begin();
+            spriteBatch.Draw(texture, new Rectangle(10, 10, 64, 64), Color.Green);
+            spriteBatch.End();
+
             base.Draw(gameTime);
         }
     }
diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs
index 8662df51..7c3370d3 100644
--- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs
+++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs
@@ -85,19 +85,7 @@ namespace ANX.SoundSystem.Windows.XAudio
             dopplerScale = 1f;
             speedOfSound = 343.5f;
 
-            try
-            {
-                device = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.AnyProcessor);
-            }
-            catch (Exception ex)
-            {
-                device = null;
-                //TODO: error handling
-                System.Diagnostics.Debugger.Break();
-            }
-
-            if (device != null)
-                MasteringVoice = new MasteringVoice(device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate);
+            InitializeDevice();
         }
 
 	    ~Creator()
@@ -106,39 +94,74 @@ namespace ANX.SoundSystem.Windows.XAudio
             {
                 MasteringVoice.DestroyVoice();
                 MasteringVoice.Dispose();
+                MasteringVoice = null;
             }
 
             if (device != null)
+            {
                 device.Dispose();
-
-            MasteringVoice = null;
-            device = null;
+                device = null;
+            }
         }
 	    #endregion
 
+        internal void InitializeDevice()
+        {
+            if (device == null)
+            {
+                try
+                {
+                    device = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.AnyProcessor);
+                }
+                catch (SharpDX.SharpDXException ex)
+                {
+                    if (ex.ResultCode == 0x80040154)
+                    {
+                        // couldn't initialize XAudio: "class not registered"
+                        device = null;
+                    }
+                    else
+                    {
+                        throw ex;
+                    }
+                }
+
+                if (device != null)
+                {
+                    MasteringVoice = new MasteringVoice(device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate);
+                }
+
+                AddInSystemFactory.Instance.PreventSystemChange(AddInType.SoundSystem);
+            }
+        }
+
 		public IAudioListener CreateAudioListener()
         {
-            PreventSystemChange();
+            InitializeDevice();
+
 			throw new NotImplementedException();
 		}
 
 		public IAudioEmitter CreateAudioEmitter()
         {
-            PreventSystemChange();
+            InitializeDevice();
+
 			throw new NotImplementedException();
 		}
 
 		#region CreateSoundEffect
 		public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream)
 		{
-			PreventSystemChange();
+            InitializeDevice();
+
 			return new XAudioSoundEffect(stream);
 		}
 
 		public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, int count, int sampleRate,
 			AudioChannels channels, int loopStart, int loopLength)
 		{
-			PreventSystemChange();
+            InitializeDevice();
+
 			return new XAudioSoundEffect(buffer, offset, count, sampleRate, channels, loopStart, loopLength);
 		}
 		#endregion
@@ -146,7 +169,8 @@ namespace ANX.SoundSystem.Windows.XAudio
 		#region CreateSoundEffectInstance
 		public ISoundEffectInstance CreateSoundEffectInstance(ISoundEffect nativeSoundEffect)
 		{
-			PreventSystemChange();
+            InitializeDevice();
+
 			return new XAudioSoundEffectInstance(device, nativeSoundEffect as XAudioSoundEffect);
 		}
         #endregion
@@ -154,46 +178,47 @@ namespace ANX.SoundSystem.Windows.XAudio
         #region CreateDynamicSoundEffectInstance
         public IDynamicSoundEffectInstance CreateDynamicSoundEffectInstance(int sampleRate, AudioChannels channels)
         {
-            PreventSystemChange();
+            InitializeDevice();
+
             return new XAudioDynamicSoundEffectInstance(device, sampleRate, channels);
         }
         #endregion
 
 		public IMicrophone CreateMicrophone(Microphone managedMicrophone)
         {
-            PreventSystemChange();
+            InitializeDevice();
+
 			throw new NotImplementedException();
 		}
 
 		public ReadOnlyCollection<Microphone> GetAllMicrophones()
         {
-            PreventSystemChange();
+            InitializeDevice();
+
 			throw new NotImplementedException();
 		}
 
 		public int GetDefaultMicrophone(ReadOnlyCollection<Microphone> allMicrophones)
         {
-            PreventSystemChange();
+            InitializeDevice();
+
 			throw new NotImplementedException();
 		}
 
         #region CreateSong
         public ISong CreateSong(Song parentSong, Uri uri)
         {
-            PreventSystemChange();
+            InitializeDevice();
+
             return new XAudioSong(device, uri);
         }
 
         public ISong CreateSong(Song parentSong, string filepath, int duration)
         {
-            PreventSystemChange();
+            InitializeDevice();
+
             return new XAudioSong(device, filepath, duration);
         }
         #endregion
-
-		private static void PreventSystemChange()
-		{
-			AddInSystemFactory.Instance.PreventSystemChange(AddInType.SoundSystem);
-		}
     }
 }
diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Properties/AssemblyInfo.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Properties/AssemblyInfo.cs
index cf123fda..4f1792cb 100644
--- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Properties/AssemblyInfo.cs
+++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Properties/AssemblyInfo.cs
@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("ANX.Framework Team")]
 [assembly: AssemblyProduct("ANX.SoundSystem.Windows.XAudio")]
-[assembly: AssemblyCopyright("Copyright © ANX.Framework Team 2011")]
+[assembly: AssemblyCopyright("Copyright © ANX.Framework Team 2011 - 2012")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 
 // übernehmen, indem Sie "*" eingeben:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.0.4.*")]
-[assembly: AssemblyFileVersion("0.0.4.0")]
+[assembly: AssemblyVersion("0.4.1.*")]
+[assembly: AssemblyFileVersion("0.4.1.0")]