Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AudioEngine.Windows.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_WINDOWS
4 
5 using System;
6 
7 using SharpDX.MediaFoundation;
8 using SharpDX.Multimedia;
9 using SharpDX.X3DAudio;
10 using SharpDX.XAudio2;
11 
12 namespace SiliconStudio.Paradox.Audio
13 {
14  public partial class AudioEngine
15  {
16  internal XAudio2 XAudio2;
17 
18  internal X3DAudio X3DAudio;
19 
20  internal MasteringVoice MasteringVoice;
21 
22  private static bool mediaEngineStarted;
23 
24  #region Implementation of the IDisposable Interface
25 
26  private void DestroyImpl()
27  {
28  if (MasteringVoice != null)
29  {
30  MasteringVoice.Dispose();
31  MasteringVoice = null;
32  }
33 
34  if (XAudio2 != null)
35  {
36  XAudio2.StopEngine();
37  XAudio2.Dispose();
38  XAudio2 = null;
39  }
40 
41  PlatformSpecificDispose();
42 
43  if (mediaEngineStarted && nbOfAudioEngineInstances == 0)
44  {
45  MediaManager.Shutdown();
46  mediaEngineStarted = false;
47  }
48  }
49 
50  #endregion
51 
52  #region Audio Hardware Selection
53 
54  // This region is currently nor implemented nor exposed to the client
55 
56  private void AudioEngineImpl(AudioDevice device)
57  {
58  try
59  {
60  XAudio2 = new XAudio2();
61  X3DAudio = new X3DAudio(Speakers.Stereo); // only stereo mode currently supported
62 
63  XAudio2.CriticalError += XAudio2OnCriticalError;
64 
65  MasteringVoice = new MasteringVoice(XAudio2, 2, (int)AudioSampleRate); // Let XAudio choose an adequate sampling rate for the platform and the configuration but not number of channels [force to stereo 2-channels].
66 
67  if (!mediaEngineStarted)
68  {
69  // MediaManager.Startup(); <- MediaManager.Shutdown is buggy (do not set isStartUp to false) so we are forced to directly use MediaFactory.Startup here while sharpDX is not corrected.
70  MediaFactory.Startup(0x20070, 0);
71  mediaEngineStarted = true;
72  }
73 
74  PlatformSpecificInit();
75  }
76  catch (DllNotFoundException exp)
77  {
78  State = AudioEngineState.Invalidated;
79  Logger.Warning("One or more of the XAudio and MediaFoundation dlls were not found on the computer. " +
80  "Audio functionalities will not fully work for the current game instance." +
81  "To fix the problem install or repair the installation of XAudio and Media Foundation. [Exception details: {0}]", exp.Message);
82  }
83  catch (SharpDX.SharpDXException exp)
84  {
85  State = AudioEngineState.Invalidated;
86 
87  if (exp.ResultCode == XAudioErrorCodes.ErrorInvalidCall)
88  {
89  Logger.Warning("Initialization of the audio engine failed. This may be due to missing audio hardware or missing audio outputs. [Exception details: {0}]", exp.Message);
90  }
91  else if (exp.ResultCode == 0x8007007E)
92  {
93  Logger.Warning( "Audio engine initialization failed. This is probably due to missing dll on your computer. " +
94  "Please check that XAudio2 and MediaFoundation are correctly installed.[Exception details: {0}]", exp.Message);
95  }
96  else
97  {
98  Logger.Warning("Audio engine initialization failed. [Exception details: {0}]", exp.Message);
99  }
100  }
101  }
102 
103  private void XAudio2OnCriticalError(object sender, ErrorEventArgs errorEventArgs)
104  {
105  Logger.Error("XAudio2 critical error {0} ", errorEventArgs.ErrorCode);
106  }
107 
108  #endregion
109 
110  private void PauseAudioPlatformSpecific()
111  {
112  // nothing to do for windows
113  }
114 
115  private void ResumeAudioPlatformSpecific()
116  {
117  // nothing to do for windows
118  }
119  }
120 }
121 
122 #endif