Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 9 additions & 6 deletions resource/dist/ModelExtras.ini
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ TailLightShadowIntensity = 80 # Opacity of projected groun
[SOUND]
SoundEffects = 1 # Master toggle for all ModelExtras sound effects
SoundMult = 0.6 # Multiplier for ModelExtras audio effects (0.0 to 1.0)
SoundEffects_GlobalAirbreakSound = 1 # Plays engine air brake sound for big vehicles (check [TABLE])
SoundEffects_GlobalEngineSound = 1 # Plays engine on/off sound
SoundEffects_GlobalIndicatorSound = 1 # Plays indicator on/off sound
SoundEffects_GlobalReverseSound = 1 # Plays reverse sound for big vehicles (check [TABLE])
SoundEffects_NonPlayerVehicles = 1 # Plays sound effects for traffic vehicles too
GlobalAirbreakSound = 1 # Plays engine air brake sound for big vehicles (check [TABLE])
GlobalEngineSound = 1 # Plays engine on/off sound
GlobalIndicatorSound = 1 # Plays indicator on/off sound
GlobalReverseSound = 1 # Plays reverse sound for big vehicles (check [TABLE])
DoorChimeSound = 1 # Plays open door warning chime sound for vehicles (check [TABLE])
DoorChimeOnlySelected = 1 # Open door warning chime for selected vehicles only (check [TABLE])
NonPlayerVehicles = 1 # Plays sound effects for traffic vehicles too

[CONFIG]
ModLoaderData = 1 # Loads per-vehicle data configs (.jsonc/.json/.eml/.ivfc) directly from ModLoader subfolders
Expand All @@ -101,5 +103,6 @@ SirenLightKey = 0x4C # Toggle emergency siren lig
SpotLightKey = 0x42 # Toggle police searchlight / spotlight on / off (Default: B [0x42])

[TABLE]
SoundEffects_BigVehicleModels = "403, 406, 407, 408, 414, 416, 427, 428, 431, 433, 437, 443, 455, 456, 486, 498, 499, 508, 514, 515, 524, 525, 528, 530, 532, 544, 552, 573, 574, 578, 588, 601, 609" # Vehicle model IDs for air brake and reverse warning sounds
BigVehicleModels = "403, 406, 407, 408, 414, 416, 427, 428, 431, 433, 437, 443, 455, 456, 486, 498, 499, 508, 514, 515, 524, 525, 528, 530, 532, 544, 552, 573, 574, 578, 588, 601, 609" # Vehicle model IDs for air brake and reverse warning sounds
BackFireEffect_VehicleModels = "402, 411, 415, 424, 429, 434, 439, 451, 461, 468, 475, 477, 480, 494, 496, 502, 503, 504, 506, 521, 522, 535, 541, 558, 559, 560, 561, 562, 565, 581, 587, 589, 603, 604" # Vehicle model IDs for sports exhaust backfire flames
DoorChime_VehicleModels = "400, 401, 402, 405, 409, 410, 411, 413, 415, 416, 418, 420, 421, 422, 426, 429, 436, 440, 445, 451, 458, 459, 470, 477, 480, 482, 489, 490, 492, 496, 500, 505, 506, 507, 516, 517, 526, 527, 529, 533, 540, 541, 546, 547, 550, 551, 554, 558, 559, 560, 561, 562, 565, 579, 582, 585, 587, 589, 596, 597, 598, 599, 602, 603" # Allowed vehicle model IDs for open door warning chime sound
Binary file added resource/dist/ModelExtras/audio/door_chime.mp3
Binary file not shown.
10 changes: 10 additions & 0 deletions src/ModelExtrasAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,16 @@ bool ME_PlaySound(CVehicle *pVeh, const char *soundPath, float volume) {
void ME_StopVehicleSounds(CVehicle *pVeh) {
if (!pVeh) return;
auto &sData = SoundEffects::m_VehData.Get(pVeh);
if (sData.m_hDoorChimeStream) {
AudioMgr::StopLoopStream(sData.m_hDoorChimeStream);
sData.m_hDoorChimeStream = 0;
}
}

bool ME_IsDoorChimePlaying(CVehicle *pVeh) {
if (!pVeh) return false;
auto &sData = SoundEffects::m_VehData.Get(pVeh);
return sData.m_hDoorChimeStream != 0;
}


Expand Down
1 change: 1 addition & 0 deletions src/ModelExtrasAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ extern "C"
// Audio & Sounds
ME_WRAPPER bool ME_PlaySound(CVehicle *pVeh, const char *soundPath, float volume);
ME_WRAPPER void ME_StopVehicleSounds(CVehicle *pVeh);
ME_WRAPPER bool ME_IsDoorChimePlaying(CVehicle *pVeh);
ME_WRAPPER float ME_GetBrakePressure(CVehicle *pVeh);
ME_WRAPPER bool ME_PlayFileSound(const char *soundPath, float volume);
ME_WRAPPER void ME_PlaySwitchSound(CVehicle *pVeh);
Expand Down
304 changes: 206 additions & 98 deletions src/features/soundeffects.cpp

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/features/soundeffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ struct SoundEffectsData
unsigned int m_nLastReverseSoundTime = 0;
float m_fBrakePressure = 0.0f;
float m_fMaxPedal = 0.0f;
StreamHandle m_hDoorChimeStream = 0;
SoundEffectsData(CVehicle *pVeh) {}
~SoundEffectsData() {}
~SoundEffectsData()
{
if (m_hDoorChimeStream)
{
AudioMgr::StopLoopStream(m_hDoorChimeStream);
}
}
};

class SoundEffects : public CVehFeature<SoundEffectsData>
Expand Down
2 changes: 1 addition & 1 deletion src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void ModelExtras::Init()

for (CVehicle *pVeh : CPools::ms_pVehiclePool)
{
if (!pVeh || pVeh->m_fHealth <= 0.0f) continue;
if (!pVeh) continue;

for (auto *pFeature : s_ActiveVehicleFeatures)
{
Expand Down
88 changes: 60 additions & 28 deletions src/utils/audiomgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace BassAPI
using tBASS_ChannelSetAttribute = BOOL(WINAPI *)(DWORD, DWORD, float);
using tBASS_ChannelIsActive = DWORD(WINAPI *)(DWORD);
using tBASS_StreamFree = BOOL(WINAPI *)(HSTREAM);
using tBASS_Pause = BOOL(WINAPI *)();
using tBASS_Start = BOOL(WINAPI *)();

static tBASS_Init fnInit = nullptr;
static tBASS_StreamCreateFile fnStreamCreate = nullptr;
Expand All @@ -28,6 +30,8 @@ namespace BassAPI
static tBASS_ChannelSetAttribute fnChannelSetAttr = nullptr;
static tBASS_ChannelIsActive fnChannelIsActive = nullptr;
static tBASS_StreamFree fnStreamFree = nullptr;
static tBASS_Pause fnPause = nullptr;
static tBASS_Start fnStart = nullptr;

static bool bReady = false;

Expand All @@ -53,6 +57,8 @@ namespace BassAPI
fnChannelSetAttr = (tBASS_ChannelSetAttribute)GetProcAddress(hBass, "BASS_ChannelSetAttribute");
fnChannelIsActive = (tBASS_ChannelIsActive)GetProcAddress(hBass, "BASS_ChannelIsActive");
fnStreamFree = (tBASS_StreamFree)GetProcAddress(hBass, "BASS_StreamFree");
fnPause = (tBASS_Pause)GetProcAddress(hBass, "BASS_Pause");
fnStart = (tBASS_Start)GetProcAddress(hBass, "BASS_Start");

if (fnInit && fnStreamCreate && fnChannelPlay && fnChannelPause && fnChannelSetAttr && fnChannelIsActive && fnStreamFree)
{
Expand All @@ -70,8 +76,8 @@ static float gfSoundMult = 1.0f;

void AudioMgr::ReloadConfig()
{
gbSoundEffectsEnabled = gConfig.ReadBoolean("SOUND", "SoundEffects", gConfig.ReadBoolean("FEATURES", "SoundEffects", false));
gfSoundMult = gConfig.ReadFloat("SOUND", "SoundMult", gConfig.ReadFloat("TWEAKS", "SoundMult", 0.6f));
gbSoundEffectsEnabled = gConfig.ReadBoolean("SOUND", "SoundEffects", false);
gfSoundMult = gConfig.ReadFloat("SOUND", "SoundMult", 0.6f);
}

void AudioMgr::Init()
Expand All @@ -94,39 +100,36 @@ void AudioMgr::Init()
needToFree.clear();
};

Events::processScriptsEvent += []
Events::drawingEvent += []
{
static bool bWasPaused = false;
bool bIsPaused = CTimer::m_UserPause || CTimer::m_CodePause;
bool bIsPaused = !Util::IsWindowFocused() || *(bool *)0xBA67A4 || CTimer::m_UserPause || CTimer::m_CodePause;

if (bIsPaused != bWasPaused)
{
bWasPaused = bIsPaused;
if (BassAPI::bReady && BassAPI::fnChannelPause && BassAPI::fnChannelPlay && BassAPI::fnChannelIsActive)
if (BassAPI::bReady)
{
for (auto stream : needToFree)
if (bIsPaused)
{
if (stream)
if (BassAPI::fnPause)
{
if (bIsPaused)
{
if (BassAPI::fnChannelIsActive(stream) == 1 /* BASS_ACTIVE_PLAYING */)
{
BassAPI::fnChannelPause(stream);
}
}
else
{
if (BassAPI::fnChannelIsActive(stream) == 3 /* BASS_ACTIVE_PAUSED */)
{
BassAPI::fnChannelPlay(stream, FALSE);
}
}
BassAPI::fnPause();
}
}
else
{
if (BassAPI::fnStart)
{
BassAPI::fnStart();
}
}
}
}
};

Events::processScriptsEvent += []
{
static size_t prev = 0;
size_t cur = CTimer::m_snTimeInMilliseconds;

Expand Down Expand Up @@ -169,7 +172,15 @@ void AudioMgr::PlaySwitchSound(CEntity *pEntity)

bool AudioMgr::ShouldPlaySound()
{
return gbSoundEffectsEnabled;
if (!gbSoundEffectsEnabled)
{
return false;
}
if (!Util::IsWindowFocused() || *(bool *)0xBA67A4 || CTimer::m_UserPause || CTimer::m_CodePause)
{
return false;
}
return true;
}

void AudioMgr::Play3DSound(const std::string &path, const CVector &worldPos, CEntity *pEntity, float baseVolume, float maxDistance)
Expand Down Expand Up @@ -203,7 +214,7 @@ void AudioMgr::Play3DSound(const std::string &path, const CVector &worldPos, CEn
CVector toSound = worldPos - TheCamera.GetPosition();
CVector camRight = TheCamera.m_mCameraMatrix.right;
float rightDot = (toSound.x * camRight.x + toSound.y * camRight.y + toSound.z * camRight.z) / dist;
pan = std::clamp(rightDot, -1.0f, 1.0f);
pan = std::clamp(-rightDot, -1.0f, 1.0f);
}

// Calibrated volume scaling with in-game SFX master volume (0xBA6797)
Expand Down Expand Up @@ -293,7 +304,7 @@ std::string AudioMgr::GetSirenAudioPath(int modeIndex)
return "";
}

StreamHandle AudioMgr::PlaySirenStream(const std::string &path, const CVector &worldPos, float baseVolume, float maxDistance)
StreamHandle AudioMgr::PlayLoopStream(const std::string &path, const CVector &worldPos, float baseVolume, float maxDistance)
{
if (path.empty() || !BassAPI::bReady || !BassAPI::fnStreamCreate || !BassAPI::fnChannelPlay)
{
Expand Down Expand Up @@ -321,7 +332,7 @@ StreamHandle AudioMgr::PlaySirenStream(const std::string &path, const CVector &w
CVector toSound = worldPos - TheCamera.GetPosition();
CVector camRight = TheCamera.m_mCameraMatrix.right;
float rightDot = (toSound.x * camRight.x + toSound.y * camRight.y + toSound.z * camRight.z) / dist;
pan = std::clamp(rightDot, -1.0f, 1.0f);
pan = std::clamp(-rightDot, -1.0f, 1.0f);
}

constexpr float INV_64 = 1.0f / 64.0f;
Expand Down Expand Up @@ -349,7 +360,7 @@ StreamHandle AudioMgr::PlaySirenStream(const std::string &path, const CVector &w
return 0;
}

void AudioMgr::UpdateSirenStream(StreamHandle stream, const CVector &worldPos, float baseVolume, float maxDistance)
void AudioMgr::UpdateLoopStream(StreamHandle stream, const CVector &worldPos, float baseVolume, float maxDistance)
{
if (!stream || !BassAPI::bReady || !BassAPI::fnChannelSetAttr || !BassAPI::fnChannelIsActive)
{
Expand All @@ -361,6 +372,12 @@ void AudioMgr::UpdateSirenStream(StreamHandle stream, const CVector &worldPos, f
return;
}

if (!ShouldPlaySound())
{
BassAPI::fnChannelSetAttr(stream, 2 /* BASS_ATTRIB_VOL */, 0.0f);
return;
}

float distSq = MathUtil::DistanceSquared(worldPos, TheCamera.GetPosition());
float dist = std::sqrt(distSq);
const float nearDist = 5.0f;
Expand All @@ -377,7 +394,7 @@ void AudioMgr::UpdateSirenStream(StreamHandle stream, const CVector &worldPos, f
CVector toSound = worldPos - TheCamera.GetPosition();
CVector camRight = TheCamera.m_mCameraMatrix.right;
float rightDot = (toSound.x * camRight.x + toSound.y * camRight.y + toSound.z * camRight.z) / dist;
pan = std::clamp(rightDot, -1.0f, 1.0f);
pan = std::clamp(-rightDot, -1.0f, 1.0f);
}

constexpr float INV_64 = 1.0f / 64.0f;
Expand All @@ -388,7 +405,7 @@ void AudioMgr::UpdateSirenStream(StreamHandle stream, const CVector &worldPos, f
BassAPI::fnChannelSetAttr(stream, 3 /* BASS_ATTRIB_PAN */, pan);
}

void AudioMgr::StopSirenStream(StreamHandle &stream)
void AudioMgr::StopLoopStream(StreamHandle &stream)
{
if (!stream)
{
Expand All @@ -412,4 +429,19 @@ void AudioMgr::StopSirenStream(StreamHandle &stream)
needToFree.erase(it);
}
stream = 0;
}

StreamHandle AudioMgr::PlaySirenStream(const std::string &path, const CVector &worldPos, float baseVolume, float maxDistance)
{
return PlayLoopStream(path, worldPos, baseVolume, maxDistance);
}

void AudioMgr::UpdateSirenStream(StreamHandle stream, const CVector &worldPos, float baseVolume, float maxDistance)
{
UpdateLoopStream(stream, worldPos, baseVolume, maxDistance);
}

void AudioMgr::StopSirenStream(StreamHandle &stream)
{
StopLoopStream(stream);
}
3 changes: 3 additions & 0 deletions src/utils/audiomgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class AudioMgr
static void PlayFileSound(const std::string &path, float volume = 1.0f);
static void PlayClickSound();
static void PlaySwitchSound(CEntity *pEntity = nullptr);
static StreamHandle PlayLoopStream(const std::string &path, const CVector &worldPos, float baseVolume = 1.0f, float maxDistance = 40.0f);
static void UpdateLoopStream(StreamHandle stream, const CVector &worldPos, float baseVolume = 1.0f, float maxDistance = 40.0f);
static void StopLoopStream(StreamHandle &stream);
static StreamHandle PlaySirenStream(const std::string &path, const CVector &worldPos, float baseVolume = 1.0f, float maxDistance = 120.0f);
static void UpdateSirenStream(StreamHandle stream, const CVector &worldPos, float baseVolume = 1.0f, float maxDistance = 120.0f);
static void StopSirenStream(StreamHandle &stream);
Expand Down