/****************************************************************************
* Play 3DCue:
* Plays a 3D cue based on an index and position
*****************************************************************************/
void SoundController::Play3DCue( XACTINDEX iCueIndex, D3DXVECTOR3* pvPosition )
{
	if( bInitialized )
	{		
		emitter.Position = *pvPosition;
		
		// Play the cue as a 3D audio cue
		pSoundBank->Prepare( iCueIndex, 0, 0, &pCue );
		XACT3DCalculate( x3DInstance, &listener, &emitter, &dspSettings );
		XACT3DApply( &dspSettings, pCue );
		pCue->Play();
	}
}
/****************************************************************************
* Do Work:
* Must be called about once per frame to proces sound
*****************************************************************************/
void SoundController::DoWork()
{
	{
        listener.OrientFront = D3DXVECTOR3( 0, 0, 1 );
		listener.OrientTop = D3DXVECTOR3( 0, 1, 0 );
		listener.Position = D3DXVECTOR3( 0, 5, 0 );

        // Background music positioning
        emitter.Position = D3DXVECTOR3( 0, 0, 0 );
        XACT3DCalculate(x3DInstance, &listener, &emitter, &dspSettings);
        XACT3DApply(&dspSettings, pBackgroundMusic);
    }
	pXACT3Engine->DoWork();
}
void CSoundComponent::Apply3D(vector<IXACT3Cue*>& cue, X3DAUDIO_LISTENER& mListener, vector<X3DAUDIO_EMITTER>& mEmitter)
{
	// Set the listener information
	ZeroMemory(&listener,sizeof(listener));
	// set the XYZ of the camera
	listener.Position = mListener.Position;
	// set the direction the camera looks in
	// this must be a unit vector
	listener.OrientFront = mListener.OrientFront;
	// set the up-direction of the camera
	// again, this must be a unit vector and orthogonal to camLook
	listener.OrientTop = mListener.OrientFront;
	// assuming no doppler effect
	listener.Velocity = D3DXVECTOR3(0,0,0);

	// emitter ChannelCount and DSP Setting's SrcChannelCount must match
	for(int i = 0; i<mEmitter.size(); i++)
	{
		mEmitter[i].ChannelCount = dspSettings.SrcChannelCount;
	}

	const float MIN_SOUND_DIST = 3.0f;
	const float MAX_SOUND_DIST = 15.0f;
	// the falloff curve:
	// at range 0=>100%, MIN_SOUND_DIST=>100%, MAX_SOUND_DIST=>0%
	X3DAUDIO_DISTANCE_CURVE_POINT soundFalloffPt[] = { 0.0f, 1.0f, MIN_SOUND_DIST, 1.0f, MAX_SOUND_DIST, 0.0f  };
	X3DAUDIO_DISTANCE_CURVE       soundFalloff          = { &soundFalloffPt[0], 3 };
	
	for(int i = 0; i<mEmitter.size(); i++)
	{
		// set the falloff curve
		mEmitter[i].pVolumeCurve =(X3DAUDIO_DISTANCE_CURVE*)&soundFalloff;
		mEmitter[i].CurveDistanceScaler = 1.0; // this is a multiplier on all distances
		// computer the effects on the sound and apply it
		XACT3DCalculate(xact3dInstance, &listener, &mEmitter[i], &dspSettings );
		if(i < cue.size())
			XACT3DApply( &dspSettings, cue[i]);
		else
			FAILED("Cue not found");
	}
}