Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*-========================================================================-_
  2.  |                               - XACT3D3 -                                |
  3.  |        Copyright (c) Microsoft Corporation.  All rights reserved.        |
  4.  |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
  5.  |VERSION:  0.1                         MODEL:   Unmanaged User-mode        |
  6.  |CONTRACT: N / A                       EXCEPT:  No Exceptions              |
  7.  |PARENT:   N / A                       MINREQ:  Win2000, Xbox360           |
  8.  |PROJECT:  XACT3D                      DIALECT: MS Visual C++ 7.0          |
  9.  |>------------------------------------------------------------------------<|
  10.  | DUTY: XACT 3D support                                                    |
  11.  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
  12.   NOTES:
  13.     1.  See X3DAudio.h for information regarding X3DAudio types.            */
  14.  
  15.  
  16. #ifndef __XACT3D3_H__
  17. #define __XACT3D3_H__
  18.  
  19. //--------------<D-E-F-I-N-I-T-I-O-N-S>-------------------------------------//
  20.     #include <x3daudio.h>
  21.     #include <xact3.h>
  22.  
  23.     #pragma warning(push)
  24.     #pragma warning(disable: 4701) // disable "local variable may be used without having been initialized" compile warning
  25.  
  26.     // Supported speaker positions, represented as azimuth angles.
  27.     //
  28.     // Here's a picture of the azimuth angles for the 8 cardinal points,
  29.     // seen from above.  The emitter's base position is at the origin 0.
  30.     //
  31.     //           FRONT
  32.     //             | 0  <-- azimuth
  33.     //             |
  34.     //    7pi/4 \  |  / pi/4
  35.     //           \ | /
  36.     // LEFT       \|/      RIGHT
  37.     // 3pi/2-------0-------pi/2
  38.     //            /|\
  39.     //           / | \
  40.     //    5pi/4 /  |  \ 3pi/4
  41.     //             |
  42.     //             | pi
  43.     //           BACK
  44.     //
  45.     #define LEFT_AZIMUTH                    (3*X3DAUDIO_PI/2)
  46.     #define RIGHT_AZIMUTH                   (X3DAUDIO_PI/2)
  47.     #define FRONT_LEFT_AZIMUTH              (7*X3DAUDIO_PI/4)
  48.     #define FRONT_RIGHT_AZIMUTH             (X3DAUDIO_PI/4)
  49.     #define FRONT_CENTER_AZIMUTH            0.0f
  50.     #define LOW_FREQUENCY_AZIMUTH           X3DAUDIO_2PI
  51.     #define BACK_LEFT_AZIMUTH               (5*X3DAUDIO_PI/4)
  52.     #define BACK_RIGHT_AZIMUTH              (3*X3DAUDIO_PI/4)
  53.     #define BACK_CENTER_AZIMUTH             X3DAUDIO_PI
  54.     #define FRONT_LEFT_OF_CENTER_AZIMUTH    (15*X3DAUDIO_PI/8)
  55.     #define FRONT_RIGHT_OF_CENTER_AZIMUTH   (X3DAUDIO_PI/8)
  56.  
  57.  
  58. //--------------<D-A-T-A---T-Y-P-E-S>---------------------------------------//
  59.     // Supported emitter channel layouts:
  60.     static const float aStereoLayout[] =
  61.     {
  62.         LEFT_AZIMUTH,
  63.         RIGHT_AZIMUTH
  64.     };
  65.     static const float a2Point1Layout[] =
  66.     {
  67.         LEFT_AZIMUTH,
  68.         RIGHT_AZIMUTH,
  69.         LOW_FREQUENCY_AZIMUTH
  70.     };
  71.     static const float aQuadLayout[] =
  72.     {
  73.         FRONT_LEFT_AZIMUTH,
  74.         FRONT_RIGHT_AZIMUTH,
  75.         BACK_LEFT_AZIMUTH,
  76.         BACK_RIGHT_AZIMUTH
  77.     };
  78.     static const float a4Point1Layout[] =
  79.     {
  80.         FRONT_LEFT_AZIMUTH,
  81.         FRONT_RIGHT_AZIMUTH,
  82.         LOW_FREQUENCY_AZIMUTH,
  83.         BACK_LEFT_AZIMUTH,
  84.         BACK_RIGHT_AZIMUTH
  85.     };
  86.     static const float a5Point1Layout[] =
  87.     {
  88.         FRONT_LEFT_AZIMUTH,
  89.         FRONT_RIGHT_AZIMUTH,
  90.         FRONT_CENTER_AZIMUTH,
  91.         LOW_FREQUENCY_AZIMUTH,
  92.         BACK_LEFT_AZIMUTH,
  93.         BACK_RIGHT_AZIMUTH
  94.     };
  95.     static const float a7Point1Layout[] =
  96.     {
  97.         FRONT_LEFT_AZIMUTH,
  98.         FRONT_RIGHT_AZIMUTH,
  99.         FRONT_CENTER_AZIMUTH,
  100.         LOW_FREQUENCY_AZIMUTH,
  101.         BACK_LEFT_AZIMUTH,
  102.         BACK_RIGHT_AZIMUTH,
  103.         LEFT_AZIMUTH,
  104.         RIGHT_AZIMUTH
  105.     };
  106.  
  107.  
  108. //--------------<F-U-N-C-T-I-O-N-S>-----------------------------------------//
  109.     ////
  110.     // DESCRIPTION:
  111.     //  Initializes the 3D API's:
  112.     //
  113.     // REMARKS:
  114.     //  This method only needs to be called once.
  115.     //  X3DAudio will be initialized such that its speaker channel mask
  116.     //  matches the format of the given XACT engine's final mix.
  117.     //
  118.     // PARAMETERS:
  119.     //  pEngine     - [in]  XACT engine
  120.     //  X3DInstance - [out] X3DAudio instance handle
  121.     //
  122.     // RETURN VALUE:
  123.     //  HResult error code
  124.     ////
  125.     EXTERN_C HRESULT inline XACT3DInitialize (__in IXACT3Engine* pEngine, __in X3DAUDIO_HANDLE X3DInstance)
  126.     {
  127.         HRESULT hr = S_OK;
  128.         if (pEngine == NULL) {
  129.             hr = E_POINTER;
  130.         }
  131.  
  132.         XACTVARIABLEVALUE nSpeedOfSound;
  133.         if (SUCCEEDED(hr)) {
  134.             XACTVARIABLEINDEX xactSpeedOfSoundID = pEngine->GetGlobalVariableIndex("SpeedOfSound");
  135.             hr = pEngine->GetGlobalVariable(xactSpeedOfSoundID, &nSpeedOfSound);
  136.         }
  137.  
  138.         if (SUCCEEDED(hr)) {
  139.             WAVEFORMATEXTENSIBLE wfxFinalMixFormat;
  140.             hr = pEngine->GetFinalMixFormat(&wfxFinalMixFormat);
  141.             if (SUCCEEDED(hr)) {
  142.                 X3DAudioInitialize(wfxFinalMixFormat.dwChannelMask, nSpeedOfSound, X3DInstance);
  143.             }
  144.         }
  145.         return hr;
  146.     }
  147.  
  148.  
  149.     ////
  150.     // DESCRIPTION:
  151.     //  Calculates DSP settings with respect to 3D parameters:
  152.     //
  153.     // REMARKS:
  154.     //  Note the following flags are always specified for XACT3D calculation:
  155.     //  X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER | X3DAUDIO_CALCULATE_EMITTER_ANGLE
  156.     //
  157.     //  This means the caller must set at least the following fields:
  158.     //    X3DAUDIO_LISTENER.OrientFront
  159.     //    X3DAUDIO_LISTENER.OrientTop
  160.     //    X3DAUDIO_LISTENER.Position
  161.     //    X3DAUDIO_LISTENER.Velocity
  162.     //
  163.     //    X3DAUDIO_EMITTER.OrientFront
  164.     //    X3DAUDIO_EMITTER.OrientTop, if emitter is multi-channel
  165.     //    X3DAUDIO_EMITTER.Position
  166.     //    X3DAUDIO_EMITTER.Velocity
  167.     //    X3DAUDIO_EMITTER.InnerRadius
  168.     //    X3DAUDIO_EMITTER.InnerRadiusAngle
  169.     //    X3DAUDIO_EMITTER.ChannelCount
  170.     //    X3DAUDIO_EMITTER.CurveDistanceScaler
  171.     //    X3DAUDIO_EMITTER.DopplerScaler
  172.     //
  173.     //    X3DAUDIO_DSP_SETTINGS.pMatrixCoefficients, the caller need only allocate space for SrcChannelCount*DstChannelCount elements
  174.     //    X3DAUDIO_DSP_SETTINGS.SrcChannelCount
  175.     //    X3DAUDIO_DSP_SETTINGS.DstChannelCount
  176.     //
  177.     //  If X3DAUDIO_EMITTER.pChannelAzimuths is left NULL for multi-channel emitters,
  178.     //  a default channel radius and channel azimuth array will be applied below.
  179.     //  Distance curves such as X3DAUDIO_EMITTER.pVolumeCurve should be
  180.     //  left NULL as XACT's native RPCs will be used to define DSP behaviour
  181.     //  with respect to normalized distance.
  182.     //
  183.     //  See X3DAudio.h for information regarding X3DAudio types.
  184.     //
  185.     // PARAMETERS:
  186.     //  X3DInstance  - [in]  X3DAudio instance handle, returned from XACT3DInitialize()
  187.     //  pListener    - [in]  point of 3D audio reception
  188.     //  pEmitter     - [in]  3D audio source
  189.     //  pDSPSettings - [out] receives calculation results, applied to an XACT cue via XACT3DApply()
  190.     //
  191.     // RETURN VALUE:
  192.     //  HResult error code
  193.     ////
  194.     EXTERN_C HRESULT inline XACT3DCalculate (__in X3DAUDIO_HANDLE X3DInstance, __in const X3DAUDIO_LISTENER* pListener, __inout X3DAUDIO_EMITTER* pEmitter, __inout X3DAUDIO_DSP_SETTINGS* pDSPSettings)
  195.     {
  196.         HRESULT hr = S_OK;
  197.         if (pListener == NULL || pEmitter == NULL || pDSPSettings == NULL) {
  198.             hr = E_POINTER;
  199.         }
  200.  
  201.         if (SUCCEEDED(hr)) {
  202.             if (pEmitter->ChannelCount > 1 && pEmitter->pChannelAzimuths == NULL) {
  203.                 pEmitter->ChannelRadius = 1.0f;
  204.  
  205.                 switch (pEmitter->ChannelCount) {
  206.                     case 2: pEmitter->pChannelAzimuths = (float*)&aStereoLayout[0]; break;
  207.                     case 3: pEmitter->pChannelAzimuths = (float*)&a2Point1Layout[0]; break;
  208.                     case 4: pEmitter->pChannelAzimuths = (float*)&aQuadLayout[0]; break;
  209.                     case 5: pEmitter->pChannelAzimuths = (float*)&a4Point1Layout[0]; break;
  210.                     case 6: pEmitter->pChannelAzimuths = (float*)&a5Point1Layout[0]; break;
  211.                     case 8: pEmitter->pChannelAzimuths = (float*)&a7Point1Layout[0]; break;
  212.                     default: hr = E_FAIL; break;
  213.                 }
  214.             }
  215.         }
  216.  
  217.         if (SUCCEEDED(hr)) {
  218.             static X3DAUDIO_DISTANCE_CURVE_POINT DefaultCurvePoints[2] = { 0.0f, 1.0f, 1.0f, 1.0f };
  219.             static X3DAUDIO_DISTANCE_CURVE       DefaultCurve          = { (X3DAUDIO_DISTANCE_CURVE_POINT*)&DefaultCurvePoints[0], 2 };
  220.             if (pEmitter->pVolumeCurve == NULL) {
  221.                 pEmitter->pVolumeCurve = &DefaultCurve;
  222.             }
  223.             if (pEmitter->pLFECurve == NULL) {
  224.                 pEmitter->pLFECurve = &DefaultCurve;
  225.             }
  226.  
  227.             X3DAudioCalculate(X3DInstance, pListener, pEmitter, (X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER | X3DAUDIO_CALCULATE_EMITTER_ANGLE), pDSPSettings);
  228.         }
  229.  
  230.         return hr;
  231.     }
  232.  
  233.  
  234.     ////
  235.     // DESCRIPTION:
  236.     //  Applies results from a call to XACT3DCalculate() to a cue.
  237.     //
  238.     // PARAMETERS:
  239.     //  pDSPSettings - [in] calculation results generated by XACT3DCalculate()
  240.     //  pCue         - [in] cue to which to apply pDSPSettings
  241.     //
  242.     // RETURN VALUE:
  243.     //  HResult error code
  244.     ////
  245.     EXTERN_C HRESULT inline XACT3DApply (__in const X3DAUDIO_DSP_SETTINGS* pDSPSettings, __in IXACT3Cue* pCue)
  246.     {
  247.         HRESULT hr = S_OK;
  248.         if (pDSPSettings == NULL || pCue == NULL) {
  249.             hr = E_POINTER;
  250.         }
  251.  
  252.         if (SUCCEEDED(hr)) {
  253.             hr = pCue->SetMatrixCoefficients(pDSPSettings->SrcChannelCount, pDSPSettings->DstChannelCount, pDSPSettings->pMatrixCoefficients);
  254.         }
  255.         if (SUCCEEDED(hr)) {
  256.             XACTVARIABLEINDEX xactDistanceID = pCue->GetVariableIndex("Distance");
  257.             hr = pCue->SetVariable(xactDistanceID, pDSPSettings->EmitterToListenerDistance);
  258.         }
  259.         if (SUCCEEDED(hr)) {
  260.             XACTVARIABLEINDEX xactDopplerID = pCue->GetVariableIndex("DopplerPitchScalar");
  261.             hr = pCue->SetVariable(xactDopplerID, pDSPSettings->DopplerFactor);
  262.         }
  263.         if (SUCCEEDED(hr)) {
  264.             XACTVARIABLEINDEX xactOrientationID = pCue->GetVariableIndex("OrientationAngle");
  265.             hr = pCue->SetVariable(xactOrientationID, pDSPSettings->EmitterToListenerAngle * (180.0f / X3DAUDIO_PI));
  266.         }
  267.  
  268.         return hr;
  269.     }
  270.  
  271.  
  272.     #pragma warning(pop)
  273.  
  274. #endif // __XACT3D3_H__
  275. //---------------------------------<-EOF->----------------------------------//
  276.