Torque3D Documentation / _generateds / sfxXAudioProvider.cpp

sfxXAudioProvider.cpp

Engine/source/sfx/xaudio/sfxXAudioProvider.cpp

More...

Classes:

class

Extended SFXDeviceInfo to also store some extra XAudio specific data.

Public Defines

define
define

Detailed Description

Public Defines

_WIN32_DCOM() 
XAUDIO_FLAGS() 0

Public Variables

 MODULE_END 
 MODULE_INIT 
 MODULE_SHUTDOWN 
SFXXAudioProvider * mProvider 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24// Note:  This must be defined before platform.h so that
 25// CoInitializeEx is properly included.
 26#define _WIN32_DCOM
 27#include <xaudio2.h>
 28
 29#include "sfx/xaudio/sfxXAudioDevice.h"
 30#include "sfx/sfxProvider.h"
 31#include "core/util/safeRelease.h"
 32#include "core/strings/unicode.h"
 33#include "core/strings/stringFunctions.h"
 34#include "console/console.h"
 35#include "core/module.h"
 36
 37
 38class SFXXAudioProvider : public SFXProvider
 39{
 40public:
 41
 42   SFXXAudioProvider()
 43      : SFXProvider( "XAudio" ) {}
 44   virtual ~SFXXAudioProvider();
 45
 46protected:
 47
 48   /// Extended SFXDeviceInfo to also store some 
 49   /// extra XAudio specific data.
 50   struct XADeviceInfo : SFXDeviceInfo
 51   {
 52      UINT32 deviceIndex;
 53
 54      XAUDIO2_DEVICE_ROLE role;
 55
 56      WAVEFORMATEXTENSIBLE format;
 57   };
 58
 59   /// Helper for creating the XAudio engine.
 60   static bool _createXAudio( IXAudio2 **xaudio );
 61
 62public:
 63
 64   // SFXProvider
 65   void init();
 66   SFXDevice* createDevice( const String& deviceName, bool useHardware, S32 maxBuffers );
 67
 68};
 69
 70MODULE_BEGIN( XAudio )
 71
 72   MODULE_INIT_BEFORE( SFX )
 73   MODULE_SHUTDOWN_AFTER( SFX )
 74   
 75   SFXXAudioProvider* mProvider;
 76   
 77   MODULE_INIT
 78   {
 79      mProvider = new SFXXAudioProvider;
 80   }
 81   
 82   MODULE_SHUTDOWN
 83   {
 84      delete mProvider;
 85   }
 86
 87MODULE_END;
 88
 89SFXXAudioProvider::~SFXXAudioProvider()
 90{
 91}
 92
 93void SFXXAudioProvider::init()
 94{
 95   // Create a temp XAudio object for device enumeration.
 96   IXAudio2 *xAudio = NULL;
 97   if ( !_createXAudio( &xAudio ) )
 98   {
 99      Con::errorf( "SFXXAudioProvider::init() - XAudio2 failed to load!" );
100      return;
101   }
102
103   // Add the devices to the info list.
104   UINT32 count = 0;
105   xAudio->GetDeviceCount( &count );
106   for ( UINT32 i = 0; i < count; i++ )
107   {
108      XAUDIO2_DEVICE_DETAILS details;
109      HRESULT hr = xAudio->GetDeviceDetails( i, &details );
110      if ( FAILED( hr ) )
111         continue;
112
113      // Add a device to the info list.
114      XADeviceInfo* info = new XADeviceInfo;
115      info->deviceIndex = i;
116      info->driver = String( "XAudio" );
117      info->name = String( details.DisplayName );
118      info->hasHardware = false;
119      info->maxBuffers = 64;
120      info->role = details.Role;
121      info->format = details.OutputFormat;
122      mDeviceInfo.push_back( info );
123   }
124
125   // We're done with XAudio for now.
126   SAFE_RELEASE( xAudio );
127
128   // If we have no devices... we're done.
129   if ( mDeviceInfo.empty() )
130   {
131      Con::errorf( "SFXXAudioProvider::init() - No valid XAudio2 devices found!" );
132      return;
133   }
134
135   // If we got this far then we should be able to
136   // safely create a device for XAudio.
137   regProvider( this );
138}
139
140bool SFXXAudioProvider::_createXAudio( IXAudio2 **xaudio )
141{
142   // In debug builds enable the debug version 
143   // of the XAudio engine.
144   #ifdef TORQUE_DEBUG
145      #define XAUDIO_FLAGS XAUDIO2_DEBUG_ENGINE
146   #else
147      #define XAUDIO_FLAGS 0
148   #endif
149
150   // This must be called first... it doesn't hurt to 
151   // call it more than once.
152   CoInitialize( NULL );
153
154   // Try creating the xaudio engine.
155   HRESULT hr = XAudio2Create( xaudio, XAUDIO_FLAGS, XAUDIO2_DEFAULT_PROCESSOR );
156
157   return SUCCEEDED( hr ) && (*xaudio);
158}
159
160SFXDevice* SFXXAudioProvider::createDevice( const String& deviceName, bool useHardware, S32 maxBuffers )
161{
162   String devName;
163
164   devName = deviceName;
165
166   XADeviceInfo* info = dynamic_cast< XADeviceInfo* >( _findDeviceInfo( devName ) );
167
168   // Do we find one to create?
169   if ( info )
170   {
171      // Create the XAudio object to pass to the device.
172      IXAudio2 *xAudio = NULL;
173      if ( !_createXAudio( &xAudio ) )
174      {
175         Con::errorf( "SFXXAudioProvider::createDevice() - XAudio2 failed to load!" );
176         return NULL;
177      }
178
179      return new SFXXAudioDevice(   this,
180                                    devName,
181                                    xAudio, 
182                                    info->deviceIndex,
183                                    info->format.dwChannelMask,
184                                    maxBuffers );
185   }
186
187   // We didn't find a matching valid device.
188   return NULL;
189}
190