Skip to content

Commit

Permalink
3D Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
chrystianfarias committed Jan 19, 2022
1 parent 4b1d453 commit 90efd1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
10 changes: 6 additions & 4 deletions GTAFmod/FMODAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ void FMODAudio::LoadBank(FMOD::Studio::System* fmodSystem, char* bank, char* abs

//Set 3D space
m_RpmEventInstance->setVolume(.4);
m_RpmEventInstance->setReverbLevel(0, 2);
m_RpmEventInstance->set3DAttributes(&m_Attributes);
m_RpmEventInstance->setParameterByID(m_RpmDesc.id, 0.0f);
m_RpmEventInstance->setParameterByID(m_LoadDesc.id, 1.0f);

m_Attributes = { { 0 } };
m_Attributes.forward.z = 1.0f;
m_Attributes.up.y = 1.0f;
CheckError(fmodSystem->setListenerAttributes(0, &m_Attributes), "Failed on set 3d ambient");
m_ListenerAttributes = { { 0 } };
m_ListenerAttributes.forward.x = 1.0f;
m_ListenerAttributes.up.z = 1.0f;

CheckError(fmodSystem->setListenerAttributes(0, &m_ListenerAttributes), "Failed on set 3d ambient");

m_bIsLoaded = true;
}
3 changes: 2 additions & 1 deletion GTAFmod/FMODAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class FMODAudio
FMOD::Studio::EventInstance* m_GearEventInstance;
FMOD_STUDIO_PARAMETER_DESCRIPTION m_RpmDesc;
FMOD_STUDIO_PARAMETER_DESCRIPTION m_LoadDesc;
FMOD_3D_ATTRIBUTES m_Attributes;
FMOD_3D_ATTRIBUTES m_Attributes;
FMOD_3D_ATTRIBUTES m_ListenerAttributes;

void LoadBank(FMOD::Studio::System* fmodSystem, char* bank, char* absolutePath);

Expand Down
36 changes: 32 additions & 4 deletions GTAFmod/GTAFmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fmod_studio.hpp"
#include "fmod_errors.h"
#include "Curve.h"
#include "CModelInfo.h"

#include <stdio.h>
#include <string>
Expand Down Expand Up @@ -245,14 +246,41 @@ class GTAFmod {
//Set 3D space position
CVector camPos = TheCamera.GetPosition();
CVector vehiclePos = vehicle->GetPosition();

audio->m_Attributes.position.x = vehiclePos.x - camPos.x;
audio->m_Attributes.position.y = vehiclePos.y - camPos.y;
audio->m_Attributes.position.z = vehiclePos.z - camPos.z;
CVector pos = vehiclePos - camPos;
CVector dirFor;
CVector dirUp;
CVector offsetFor = CVector(0, -1, 0);
CVector offsetUp = CVector(0, 0, 1);
CMatrix* matrix = vehicle->m_matrix;
RwV3dTransformPoint((RwV3d*)&dirFor, (RwV3d*)&offsetFor, (RwMatrix*)matrix);
RwV3dTransformPoint((RwV3d*)&dirUp, (RwV3d*)&offsetUp, (RwMatrix*)matrix);

audio->m_Attributes.position.x = vehiclePos.x;
audio->m_Attributes.position.y = vehiclePos.y;
audio->m_Attributes.position.z = vehiclePos.z;
audio->m_Attributes.velocity.x = vehicle->m_vecMoveSpeed.x;
audio->m_Attributes.velocity.y = vehicle->m_vecMoveSpeed.y;
audio->m_Attributes.velocity.z = vehicle->m_vecMoveSpeed.z;

audio->m_Attributes.forward.x = dirFor.x - vehiclePos.x;
audio->m_Attributes.forward.y = dirFor.y - vehiclePos.y;
audio->m_Attributes.forward.z = dirFor.z - vehiclePos.z;
audio->m_Attributes.up.x = dirUp.x - vehiclePos.x;
audio->m_Attributes.up.y = dirUp.y - vehiclePos.y;
audio->m_Attributes.up.z = dirUp.z - vehiclePos.z;

audio->m_RpmEventInstance->set3DAttributes(&audio->m_Attributes);
audio->m_BackFireEventInstance->set3DAttributes(&audio->m_Attributes);
audio->m_GearEventInstance->set3DAttributes(&audio->m_Attributes);

audio->m_ListenerAttributes.position.x = camPos.x;
audio->m_ListenerAttributes.position.y = camPos.y;
audio->m_ListenerAttributes.position.z = camPos.z;
audio->m_ListenerAttributes.forward = audio->m_Attributes.forward;
audio->m_ListenerAttributes.up = audio->m_Attributes.up;

fmodSystem->setListenerAttributes(0, &audio->m_ListenerAttributes);

//Get gas pedal
float gasPedal = abs(vehicle->m_fGasPedal);

Expand Down

0 comments on commit 90efd1e

Please sign in to comment.