Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAEWeaponAudioEntity #753

Merged
merged 26 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3d50c25
Refactor `PlayGoggleSound`
Pirulax Jul 17, 2024
afb3278
`PlayMiniGunFireSounds`
Pirulax Jul 17, 2024
693e3d7
Refactor `PlayWeaponLoopSound`
Pirulax Jul 17, 2024
f6109eb
Refactor class var names, etc
Pirulax Jul 17, 2024
189aa68
Merge branch 'master' of github.com:gta-reversed/gta-reversed-modern …
Pirulax Jul 17, 2024
905faa4
Fix `CAEAudioUtility::AudioLog10`
Pirulax Jul 18, 2024
dcaff18
Fix `CAEAudioUtility::GetCurrentTimeInMS`
Pirulax Jul 18, 2024
2a1691a
Refactor `CAEAudioEnvironment::GetPositionRelativeToCamera`
Pirulax Jul 18, 2024
bb0e9a9
Refactor `CAEAudioEnvironment::GetDistanceAttenuation`
Pirulax Jul 18, 2024
de6e8a9
Fix initialization order bug
Pirulax Jul 18, 2024
8f6370a
`PlayGunSounds`
Pirulax Jul 18, 2024
8c1d06f
`ReportChainsawEvent`
Pirulax Jul 18, 2024
7ce3713
`UpdateParameters`
Pirulax Jul 21, 2024
e6c6a6c
Finishing touches
Pirulax Jul 21, 2024
e886b9e
`CAEGlobalWeaponAudioEntity::ServiceAmbientGunFire`
Pirulax Jul 22, 2024
85360c0
Merge branch 'master' of github.com:gta-reversed/gta-reversed-modern …
Pirulax Jul 22, 2024
5a1f071
fix imgui menu after update
Pirulax Jul 22, 2024
31bb809
Fix recursion
Pirulax Jul 22, 2024
887c79e
Fix `CWorld::ProcessLineOfSight`
Pirulax Jul 22, 2024
cb452c9
Fix `TeleportDebugModule` serialization
Pirulax Jul 22, 2024
8ddc6f1
`CAEGlobalWeaponAudioEntity::ServiceAmbientGunFire`: Fix waterfall ef…
Pirulax Jul 22, 2024
0caa8ef
Update source/game_sa/Audio/entities/AEExplosionAudioEntity.cpp
Pirulax Sep 21, 2024
bfe7ad3
Fix `GetPositionRelativeToCamera`
Pirulax Sep 22, 2024
b80a513
Merge branch 'reverse/CAEWeaponAudioEntity' of https://github.com/Pir…
Pirulax Sep 22, 2024
5bce95f
Move `gSoundDistAttenuationTable` into its own data header
Pirulax Sep 30, 2024
7d4e09d
Merge branch 'master' into reverse/CAEWeaponAudioEntity
Pirulax Oct 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions source/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ T& StaticRef(uintptr addr) {
return *reinterpret_cast<T*>(addr);
}

/*!
* @brief Use for scoped static variables (That is, static variables that are initialized in functions)
* @brief See `CAEGlobalWeaponAudioEntity::ServiceAmbientGunFire` for examples)
* @tparam T The type of the var
* @param varAddr
* @param flagsAddr
* @param flagsMask
* @param initVal
* @return
*/
template<typename T>
T& ScopedStaticRef(uintptr varAddr, uintptr flagsAddr, uint32 flagsMask, T&& initVal) {
auto& var = StaticRef<T>(varAddr);
auto& flags = StaticRef<uint32>(flagsAddr);
if (!(flags & flagsMask)) {
flags |= flagsMask;
var = initVal;
}
return var;
}

// TODO: Replace this with the one above
template<typename T, uintptr Addr>
T& StaticRef() {
Expand Down
11 changes: 8 additions & 3 deletions source/extensions/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,14 @@ constexpr auto IsFixBugs() {
}

/// Predicate to check if `value` is null
template<typename T>
requires(std::is_pointer_v<T>)
bool IsNull(T value) { return value == nullptr; }
struct IsNull {
template<typename T>
requires(std::is_pointer_v<T>)
bool operator()(T ptr) { return ptr == nullptr; }
};
//template<typename T>
// requires(std::is_pointer_v<T>)
//bool IsNull(T value) { return value == nullptr; }

/// Negate another predicate function
template<typename T>
Expand Down
30 changes: 16 additions & 14 deletions source/game_sa/Audio/AEAudioEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "AEAudioEnvironment.h"

sReverbEnvironment (&gAudioZoneToReverbEnvironmentMap)[NUM_AUDIO_ENVIRONMENTS] = *(sReverbEnvironment(*)[NUM_AUDIO_ENVIRONMENTS])0x8AD670;
float (&gSoundDistAttenuationTable)[NUM_SOUND_DIST_ATTENUATION_ENTRIES] = *(float(*)[NUM_SOUND_DIST_ATTENUATION_ENTRIES])0x8AC270;

#include "data/SoundAttenuationTable.h"

void CAEAudioEnvironment::InjectHooks() {
RH_ScopedClass(CAEAudioEnvironment);
Expand All @@ -17,29 +18,30 @@ void CAEAudioEnvironment::InjectHooks() {
}

// 0x4D7E40
float CAEAudioEnvironment::GetDopplerRelativeFrequency(float prevDist, float curDist, uint32 prevTime, uint32 curTime, float timeScale) {
const auto fDistDiff = curDist - prevDist;
if (TheCamera.Get_Just_Switched_Status())
float CAEAudioEnvironment::GetDopplerRelativeFrequency(float prevDist, float curDist, uint32 prevTime, uint32 curTime, float dopplerScale) {
if (TheCamera.Get_Just_Switched_Status()) {
return 1.0F;
}

if (timeScale == 0.0F || fDistDiff == 0.0F || curTime <= prevTime)
const auto deltaDist = curDist - prevDist;
if (dopplerScale == 0.0F || deltaDist == 0.0F || curTime <= prevTime) {
return 1.0F;
}

const auto fDoppler = fDistDiff * 1000.0F / static_cast<float>(curTime - prevTime) * timeScale;
if (std::fabs(fDoppler) >= 340.0F)
const auto doppler = deltaDist * 1000.0F / (float)(curTime - prevTime) * dopplerScale;
if (std::fabs(doppler) >= 340.0F) {
return 1.0F;
}

const auto fClamped = std::clamp(fDoppler, -35.0F, 35.0F);
return 340.0F / (fClamped + 340.0F);
return 340.0F / (std::clamp(doppler, -35.0F, 35.0F) + 340.0F);
}

// 0x4D7F20
float CAEAudioEnvironment::GetDistanceAttenuation(float dist) {
if (dist >= 128.0F)
return -100.0F;

auto iArrIndex = static_cast<uint32>(std::floor(dist * 10.0F));
return gSoundDistAttenuationTable[iArrIndex];
assert(dist >= 0.f);
return dist < ATTENUATION_TABLE_MAX_DIST
? gSoundDistAttenuationTable[(uint32)std::floor(dist / ATTENUATION_TABLE_RESOLUTION)]
: -100.f;
}

// 0x4D7F60
Expand Down
5 changes: 1 addition & 4 deletions source/game_sa/Audio/AEAudioEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CAEAudioEnvironment {
public:
static void InjectHooks();

static float GetDopplerRelativeFrequency(float prevDist, float curDist, uint32 prevTime, uint32 curTime, float timeScale);
static float GetDopplerRelativeFrequency(float prevDist, float curDist, uint32 prevTime, uint32 curTime, float dopplerScale);
static float GetDistanceAttenuation(float dist);
static float GetDirectionalMikeAttenuation(const CVector& soundDir);
static void GetReverbEnvironmentAndDepth(int8* reverbEnv, int32* depth);
Expand All @@ -23,6 +23,3 @@ class CAEAudioEnvironment {

static constexpr int32 NUM_AUDIO_ENVIRONMENTS = 68;
extern sReverbEnvironment (&gAudioZoneToReverbEnvironmentMap)[NUM_AUDIO_ENVIRONMENTS];

static constexpr int32 NUM_SOUND_DIST_ATTENUATION_ENTRIES = 1280;
extern float (&gSoundDistAttenuationTable)[NUM_SOUND_DIST_ATTENUATION_ENTRIES];
19 changes: 14 additions & 5 deletions source/game_sa/Audio/AEAudioUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#include "AEAudioUtility.h"

uint64& CAEAudioUtility::startTimeMs = *reinterpret_cast<uint64*>(0xb610f8);
auto& Frequency = StaticRef<LARGE_INTEGER>(0xB610F0);
uint64& startTimeMs = *reinterpret_cast<uint64*>(0xb610f8);
float (&CAEAudioUtility::m_sfLogLookup)[50][2] = *reinterpret_cast<float (*)[50][2]>(0xb61100);

// NOTE: For me all values were 0... The below values should be the correct ones:
Expand Down Expand Up @@ -84,16 +85,23 @@ float CAEAudioUtility::GetPiecewiseLinear(float x, int16 dataCount, float (*data

// 0x4d9e50
float CAEAudioUtility::AudioLog10(float p) {
return 0.00001f <= p ? std::log10f(p) : -5.0f;
return p >= 0.00001f
? std::log10f(p)
: -5.0f;
}

// REFACTORED
// 0x4d9e80
uint64 CAEAudioUtility::GetCurrentTimeInMS() {
using namespace std::chrono;
auto nowMs = time_point_cast<milliseconds>(high_resolution_clock::now());
auto value = duration_cast<milliseconds>(nowMs.time_since_epoch());
return static_cast<uint64>(value.count());
const auto nowMs = time_point_cast<milliseconds>(high_resolution_clock::now());
const auto value = duration_cast<milliseconds>(nowMs.time_since_epoch());
return static_cast<uint64>(value.count()) - startTimeMs;

//For some reason this doesn't work (original code):
//LARGE_INTEGER counter;
//QueryPerformanceCounter(&counter);
//return counter.QuadPart / Frequency.QuadPart * 1000 - startTimeMs;
}

// 0x4d9ef0
Expand Down Expand Up @@ -122,6 +130,7 @@ void CAEAudioUtility::StaticInitialise() {
m_sfLogLookup[1][1] = log10f(v);
}

VERIFY(QueryPerformanceFrequency(&Frequency));
startTimeMs = GetCurrentTimeInMS();
}

Expand Down
1 change: 0 additions & 1 deletion source/game_sa/Audio/AEAudioUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CAEAudioUtility {
}

private:
static uint64& startTimeMs;
static float (&m_sfLogLookup)[50][2];

private:
Expand Down
33 changes: 17 additions & 16 deletions source/game_sa/Audio/AESound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CAESound::CAESound(CAESound& sound) {
m_nSoundIdInSlot = sound.m_nSoundIdInSlot;
m_pBaseAudio = sound.m_pBaseAudio;
m_nEvent = sound.m_nEvent;
m_fMaxVolume = sound.m_fMaxVolume;
m_ClientVariable = sound.m_ClientVariable;
m_fVolume = sound.m_fVolume;
m_fSoundDistance = sound.m_fSoundDistance;
m_fSpeed = sound.m_fSpeed;
Expand Down Expand Up @@ -80,7 +80,7 @@ CAESound::CAESound(int16 bankSlotId, int16 sfxId, CAEAudioEntity* baseAudio, CVe
m_pBaseAudio = baseAudio;
m_vecPrevPosn = CVector(0.0f, 0.0f, 0.0f);
m_pPhysicalEntity = nullptr;
m_fMaxVolume = -1.0F;
m_ClientVariable = -1.0F;
m_nEvent = AE_UNDEFINED;
m_nLastFrameUpdate = 0;

Expand Down Expand Up @@ -116,7 +116,7 @@ CAESound& CAESound::operator=(const CAESound& sound) {
m_nSoundIdInSlot = sound.m_nSoundIdInSlot;
m_pBaseAudio = sound.m_pBaseAudio;
m_nEvent = sound.m_nEvent;
m_fMaxVolume = sound.m_fMaxVolume;
m_ClientVariable = sound.m_ClientVariable;
m_fVolume = sound.m_fVolume;
m_fSoundDistance = sound.m_fSoundDistance;
m_fSpeed = sound.m_fSpeed;
Expand Down Expand Up @@ -295,34 +295,35 @@ void CAESound::CalculateVolume() {
}

// 0x4EFE50
void CAESound::Initialise(int16 bankSlotId, int16 sfxId, CAEAudioEntity* baseAudio, CVector posn, float volume, float maxDistance, float speed, float timeScale,
uint8 ignoredServiceCycles, eSoundEnvironment environmentFlags, float speedVariability, int16 currPlayPosn)
void CAESound::Initialise(
int16 bankSlotId, int16 soundID, CAEAudioEntity* audioEntity, CVector pos, float volume, float rollOffFactor, float relativeFrequency, float doppler,
uint8 frameDelay, uint32 flags, float frequencyVariance, int16 playTime)
{
UnregisterWithPhysicalEntity();

m_nSoundIdInSlot = sfxId;
m_nSoundIdInSlot = soundID;
m_nBankSlotId = bankSlotId;
m_pBaseAudio = baseAudio;
m_pBaseAudio = audioEntity;
m_fVolume = volume;
m_fSoundDistance = maxDistance;
m_fSpeed = speed;
m_fSpeedVariability = speedVariability;
m_fSoundDistance = rollOffFactor;
m_fSpeed = relativeFrequency;
m_fSpeedVariability = frequencyVariance;
m_vecPrevPosn .Set(0.0F, 0.0F, 0.0F);
m_nEvent = AE_UNDEFINED;
m_fMaxVolume = -1.0F;
m_ClientVariable = -1.0F;
m_nLastFrameUpdate = 0;

SetPosition(posn);
SetPosition(pos);

m_fTimeScale = timeScale;
m_fTimeScale = doppler;
m_nSoundLength = -1;
m_nHasStarted = 0;
m_nPlayingState = eSoundState::SOUND_ACTIVE;
m_fSoundHeadRoom = 0.0F;
m_nIgnoredServiceCycles = ignoredServiceCycles;
m_nEnvironmentFlags = environmentFlags;
m_nIgnoredServiceCycles = frameDelay;
m_nEnvironmentFlags = flags;
m_nIsUsed = 1;
m_nCurrentPlayPosition = currPlayPosn;
m_nCurrentPlayPosition = playTime;
m_fFinalVolume = -100.0F;
m_fFrequency = 1.0F;
}
Expand Down
28 changes: 18 additions & 10 deletions source/game_sa/Audio/AESound.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class CAEAudioEntity;
class CEntity;

using tSoundID = int16;

enum eSoundEnvironment : uint16 {
SOUND_DEFAULT = 0x0,
SOUND_FRONT_END = 0x1,
Expand Down Expand Up @@ -39,8 +41,8 @@ class CAESound {
int16 m_nSoundIdInSlot;
CAEAudioEntity* m_pBaseAudio;
CEntity* m_pPhysicalEntity;
eAudioEvents m_nEvent;
float m_fMaxVolume;
int32 m_nEvent; // Not necessarily `eAudioEvents`, for ex. see `CAEWeaponAudioEntity`
float m_ClientVariable;
float m_fVolume;
float m_fSoundDistance;
float m_fSpeed;
Expand Down Expand Up @@ -98,14 +100,20 @@ class CAESound {

CAESound& operator=(const CAESound& sound);

void Initialise(int16 bankSlotId, int16 sfxId, CAEAudioEntity* baseAudio, CVector posn, float volume,
float maxDistance = 1.0f,
float speed = 1.0f,
float timeScale = 1.0f,
uint8 ignoredServiceCycles = 0,
eSoundEnvironment environmentFlags = static_cast<eSoundEnvironment>(0),
float speedVariability = 0,
int16 currPlayPosn = 0);
void Initialise(
int16 bankSlotId,
int16 soundID,
CAEAudioEntity* audioEntity,
CVector pos,
float volume,
float rollOffFactor = 1.f,
float relativeFrequency = 1.f, // Speed
float doppler = 1.f,
uint8 frameDelay = 0,
uint32 flags = 0,
float frequencyVariance = 0.f,
int16 playTime = 0
);

void UnregisterWithPhysicalEntity();
void StopSound();
Expand Down
5 changes: 4 additions & 1 deletion source/game_sa/Audio/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ void CAudioEngine::InjectHooks() {
bool CAudioEngine::Initialise() {
CLoadingScreen::Pause();

// NOTSA: Initialize AudioUtility before `AEAudioHardware` to avoid crash (Division by zero in `GetCurrentTimeInMS`)
CAEAudioUtility::StaticInitialise();

if (!AEAudioHardware.Initialise()) {
NOTSA_LOG_ERR("Failed to initialise Audio Hardware");
return false;
Expand Down Expand Up @@ -138,7 +141,7 @@ bool CAudioEngine::Initialise() {

m_FrontendAE.Initialise();
CAudioEngine::SetEffectsFaderScalingFactor(0.0f);
CAEAudioUtility::StaticInitialise();
//CAEAudioUtility::StaticInitialise(); // Initialized above
CAEPedAudioEntity::StaticInitialise();
CAEPedSpeechAudioEntity::StaticInitialise();
CAEVehicleAudioEntity::StaticInitialise();
Expand Down
Loading
Loading