# XAudio2 3D Sound

## Recommended Posts

I initialized XAudio2 and I'm able to play audio files normally, now I'm looking to play the sound in 3D space.

I'm using the following code to set the position of the emitter (model position) and the position of the listener (camera position), not sure what changes should I make to get it to work:

void updateAudioPosition()
{
X3DAUDIO_VECTOR p;

// Set the position of the listener in 3D space
p.x = 0.0f;
p.y = 0.0f;
p.z = 0.0f;
xAudioListener.Position = p;

// Set the position of the emitter in 3D space
p.x = 500.0f;
p.y = 100.0f;
p.z = 400.0f;
xAudioEmitter.Position = p;

DWORD dwCalcFlags = X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER
| X3DAUDIO_CALCULATE_LPF_DIRECT | X3DAUDIO_CALCULATE_LPF_REVERB
| X3DAUDIO_CALCULATE_REVERB;

X3DAudioCalculate(X3DInstance, &xAudioListener, &xAudioEmitter, dwCalcFlags, &DSPSettings);
pSourceVoice->SetFrequencyRatio(DSPSettings.DopplerFactor);
pSourceVoice->SetOutputMatrix(NULL, 1, 1, &DSPSettings.ReverbLevel);
}

void render_frame()
{
// Code to render the scene here...
updateAudioPosition();
}

##### Share on other sites

Your emitter is very far away from the listener.

I abandoned XAudio2 because it had too many bugs.

##### Share on other sites

@Dawoodoz: That's not the problem, I'm getting the following exception:

Unhandled exception at 0x773715de in XAudio Test.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1.

on line:

X3DAudioCalculate(X3DInstance, &xAudioListener, &xAudioEmitter, dwCalcFlags, &DSPSettings);

##### Share on other sites

0xcdcdcdd1 = 0xcdcdcdcd + 4 byte

It looks like one of the pointers are uninitialized memory from the debug environment.

Make sure that all pointers are initialized to Null before allocating.

##### Share on other sites
Got rid of the exception, now I'm trying to setup the basic XAudio2 3D Sound.

Tried the following code:
pXAudio2->GetDeviceDetails(0, &details);

emitter.Position = D3DXVECTOR3(5.0f, 2.0f, 0.0f);
X3DAUDIO_CONE emitterCone;
emitter.pCone = &(emitterCone);
emitter.pCone->InnerAngle = 0.0f;
// Setting the inner cone angles to X3DAUDIO_2PI and
// outer cone other than 0 causes
// the emitter to act like a point emitter using the
// INNER cone settings only.
emitter.pCone->OuterAngle = 0.0f;
// Setting the outer cone angles to zero causes
// the emitter to act like a point emitter using the
// OUTER cone settings only.
emitter.pCone->InnerVolume = 0.0f;
emitter.pCone->OuterVolume = 1.0f;
emitter.pCone->InnerLPF = 0.0f;
emitter.pCone->OuterLPF = 1.0f;
emitter.pCone->InnerReverb = 0.0f;
emitter.pCone->OuterReverb = 1.0f;

emitter.OrientFront = D3DXVECTOR3( 0, 0, 1 );
emitter.OrientTop = D3DXVECTOR3( 0, 1, 0 );
emitter.ChannelCount = 8;
FLOAT32 emitterAzimuths[8];
emitter.pChannelAzimuths = emitterAzimuths;

// Use of Inner radius allows for smoother transitions as
// a sound travels directly through, above, or below the listener.
// It also may be used to give elevation cues.

emitter.pVolumeCurve = NULL; //(X3DAUDIO_DISTANCE_CURVE*)&X3DAudioDefault_LinearCurve;
emitter.pLFECurve    = NULL; //(X3DAUDIO_DISTANCE_CURVE*)&Emitter_LFE_Curve;
emitter.pLPFDirectCurve = NULL; // use default curve
emitter.pLPFReverbCurve = NULL; // use default curve
emitter.pReverbCurve    = (X3DAUDIO_DISTANCE_CURVE*)&Emitter_Reverb_Curve;

listener.OrientFront = D3DXVECTOR3( 0, 0, 1 );
listener.OrientTop = D3DXVECTOR3( 0, 1, 0 );
listener.Position = D3DXVECTOR3(0, 0, 0);
listener.OrientFront = D3DXVECTOR3( 0, 0, 1 );
listener.OrientTop = D3DXVECTOR3( 0, 1, 0 );
listener.pCone = NULL; //(X3DAUDIO_CONE*)&Listener_DirectionalCone;
listener.Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

ZeroMemory(&dspSettings, sizeof(dspSettings));
dspSettings.SrcChannelCount = 1;
dspSettings.DstChannelCount = details.OutputFormat.Format.nChannels;
dspSettings.pMatrixCoefficients = matrixCoefficients;

DWORD dwCalcFlags = X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER
| X3DAUDIO_CALCULATE_LPF_DIRECT | X3DAUDIO_CALCULATE_LPF_REVERB
| X3DAUDIO_CALCULATE_REVERB;
X3DAudioCalculate(X3DInstance, &listener, &emitter, dwCalcFlags, &dspSettings);
pSourceVoice->SetFrequencyRatio(dspSettings.DopplerFactor);
pSourceVoice->SetOutputMatrix(NULL, 1, details.OutputFormat.Format.nChannels, dspSettings.pMatrixCoefficients);


I'm trying to play the sound in (x = 5.0f, y = 2.0f, z = 0.0f) which is the emitter position

and the listener position is located in (x = 0.0f, y = 0.0f, z = 0.0f)

I can hear the sound but it's not like in 3D space, whats wrong?

##### Share on other sites

I got it to work correctly after changing the parameters that I pass to "pSourceVoice->SetOutputMatrix".

Now, I notice when a model go a little far, I don't hear the sound well, what settings should I put to make the player hear the sound even if the model is far away?

I want the player to stop hearing the sound when the model is far ENOUGH.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628394
• Total Posts
2982431

• 10
• 9
• 19
• 24
• 9