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