Optimizations

This solves an FPS problem when 10 or more entities take damage
This commit is contained in:
GabsPuNs
2026-05-28 22:22:22 -04:00
parent 338604d7c5
commit 8dececbf90
11 changed files with 230 additions and 480 deletions

View File

@@ -1,5 +1,4 @@
#include "SoundEngine.h"
#include "../Consoles_App.h"
#include "../../MultiplayerLocalPlayer.h"
#include "../../../Minecraft.World/net.minecraft.world.level.h"
#include "../../../Minecraft.World/leveldata.h"
@@ -18,9 +17,6 @@
#define MA_NO_WINMM
#define MINIAUDIO_IMPLEMENTATION
#include "../Libs/audio/miniaudio.h"
#include <vector>
#include <memory>
#include <mutex>
char SoundEngine::m_szMiscSoundPath[]={"Assets/Sounds/Misc/%s"};
char SoundEngine::m_szUISoundPath[]={"Assets/Sounds/UI/%s"};
@@ -142,9 +138,7 @@ void SoundEngine::SetStreamingSounds(int iOverworldMin, int iOverWorldMax, int i
// array to monitor recently played tracks
if(m_bHeardTrackA)
{
delete [] m_bHeardTrackA;
}
m_bHeardTrackA = new bool[iEndMax+1];
memset(m_bHeardTrackA, 0, sizeof(bool) * (iEndMax + 1));
@@ -272,19 +266,13 @@ inline void SoundEngine::getGameModeMusicID(Minecraft* pMinecraft, unsigned int
/*
// TODO(3UR): this is a part of minigames also in the future other minigame ids will need to be handled for now TU30 only checks for BATTLE
else if (pMinecraft->GetCustomGameMode() && CustomGameModeInst::GetId() == EMiniGameId::BATTLE) // @3UR: thanks https://github.com/GRAnimated/MinecraftLCE/blob/6947670d152582457bfe02bd909ee30a7ab7eb55/src/Minecraft.World/net/minecraft/world/level/gamemode/minigames/EMiniGameId.h#L3
{
m_musicID = getMusicID(eMusicType_Battle);
}
*/
else
{
m_musicID = getMusicID(eMusicType_Overworld);
}
}
else
{
m_musicID = getMusicID(eMusicType_Menu);
}
}
}
@@ -293,11 +281,6 @@ inline void SoundEngine::getGameModeMusicID(Minecraft* pMinecraft, unsigned int
// tick
//
/////////////////////////////////////////////
#ifdef __PSVITA__
static S32 running = AIL_ms_count();
#endif
void SoundEngine::tick(shared_ptr<Mob> *players, float a)
{
ConsoleSoundEngine::tick();
@@ -332,9 +315,7 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
listenerCount++;
}
else
{
m_ListenerA[i].bValid=false;
}
}
}
@@ -351,12 +332,7 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
}
m_validListenerCount = listenerCount;
#ifdef __PSVITA__
// AP - Show that a change has occurred so we know to update the values at the next Mixer callback
SoundEngine_Change = true;
#else
updateMiniAudio();
#endif
}
/////////////////////////////////////////////
@@ -398,10 +374,6 @@ SoundEngine::SoundEngine()
memset(CurrentSoundsPlaying, 0, sizeof(int) * (eSoundType_MAX + eSFX_MAX));
memset(m_ListenerA, 0, sizeof(AUDIO_LISTENER) * XUSER_MAX_COUNT);
#ifdef __ORBIS__
m_hBGMAudio=GetAudioBGMHandle();
#endif
}
SoundEngine::~SoundEngine()
@@ -457,73 +429,67 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
}
wstring name = wchSoundNames[iSound];
char* SoundName = (char*)ConvertSoundPathToName(name);
app.DebugPrintf(6,
"PlaySound - %d - Sounds/Misc/%s (%f %f %f, vol %f, pitch %f)\n",
iSound, SoundName, x, y, z, volume, pitch);
std::vector<std::string>* validPaths = nullptr;
auto it = m_sfxPathCache.find(iSound);
char basePath[256];
sprintf_s(basePath, m_szMiscSoundPath, SoundName);
if (it != m_sfxPathCache.end())
{
validPaths = &it->second;
app.DebugPrintf(6, "Cache - PlaySound - %d - Sounds/Misc/%s (%f %f %f, vol %f, pitch %f)\n",
iSound, SoundName, x, y, z, volume, pitch);
}
else
{
std::vector<std::string> foundPaths;
char finalPath[256];
sprintf_s(finalPath, "%s.wav", basePath);
app.DebugPrintf(6, "PlaySound - %d - Sounds/Misc/%s (%f %f %f, vol %f, pitch %f)\n",
iSound, SoundName, x, y, z, volume, pitch);
const char* extensions[] = { ".ogg", ".wav" };
size_t extCount = sizeof(extensions) / sizeof(extensions[0]);
bool found = false;
char basePath[256];
sprintf_s(basePath, m_szMiscSoundPath, SoundName);
for (size_t extIdx = 0; extIdx < extCount; extIdx++)
{
char basePlusExt[256];
sprintf_s(basePlusExt, "%s%s", basePath, extensions[extIdx]);
if (fs::exists(basePlusExt))
{
sprintf_s(finalPath, "%s", basePlusExt);
found = true;
break;
}
}
const char* extensions[] = { ".ogg", ".wav" };
size_t count = sizeof(extensions) / sizeof(extensions[0]);
if (!found)
{
int count = 0;
for (size_t i = 0; i < count; i++)
{
char path[256];
sprintf_s(path, "%s%s", basePath, extensions[i]);
if (fs::exists(path))
foundPaths.push_back(path);
}
for (size_t extIdx = 0; extIdx < extCount; extIdx++)
{
if (foundPaths.empty())
{
for (size_t i = 1; i < 32; i++)
{
char numberedPath[256];
sprintf_s(numberedPath, "%s%d%s", basePath, i, extensions[extIdx]);
if (fs::exists(numberedPath))
count = i;
}
}
{
for (size_t i2 = 0; i2 < count; i2++)
{
char path[256];
sprintf_s(path, "%s%d%s", basePath, i, extensions[i2]);
if (fs::exists(path))
foundPaths.push_back(path);
}
}
}
if (count > 0)
m_sfxPathCache[iSound] = foundPaths;
validPaths = &m_sfxPathCache[iSound];
if (validPaths->empty())
{
int chosen = (rand() % count) + 1;
for (size_t extIdx = 0; extIdx < extCount; extIdx++)
{
char numberedPath[256];
sprintf_s(numberedPath, "%s%d%s", basePath, chosen, extensions[extIdx]);
if (fs::exists(numberedPath))
{
sprintf_s(finalPath, "%s", numberedPath);
found = true;
break;
}
}
if (!found)
{
sprintf_s(finalPath, "%s%d.wav", basePath, chosen);
}
app.DebugPrintf("No sound file found: %s\n", basePath);
return;
}
}
}
if (validPaths->empty())
return;
int chosen = random->nextInt((int)validPaths->size());
const char* finalPath = (*validPaths)[chosen].c_str();
MiniAudioSound* s = new MiniAudioSound();
memset(&s->info, 0, sizeof(AUDIO_INFO));
@@ -541,7 +507,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
if (ma_sound_init_from_file(
&m_engine,
finalPath,
MA_SOUND_FLAG_ASYNC,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC,
nullptr,
nullptr,
&s->sound) != MA_SUCCESS)
@@ -576,39 +542,47 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
/////////////////////////////////////////////
void SoundEngine::playUI(int iSound, float volume, float pitch)
{
wstring name;
name = wchUISoundNames[iSound];
std::string finalPathStr;
auto it = m_uiPathCache.find(iSound);
wstring name = wchUISoundNames[iSound];
char* SoundName = (char*)ConvertSoundPathToName(name);
app.DebugPrintf(6,
"PlaySound - %d - Sounds/UI/%s (vol %f, pitch %f)\n",
iSound, SoundName, volume, pitch);
if (it != m_uiPathCache.end())
{
finalPathStr = it->second;
app.DebugPrintf(6, "Cache - PlaySound - %d - Sounds/UI/%s (vol %f, pitch %f)\n",
iSound, SoundName, volume, pitch);
}
else
{
app.DebugPrintf(6, "PlaySound - %d - Sounds/UI/%s (vol %f, pitch %f)\n",
iSound, SoundName, volume, pitch);
char basePath[256];
sprintf_s(basePath, m_szUISoundPath, SoundName);
char basePath[256];
sprintf_s(basePath, m_szUISoundPath, SoundName);
bool found = false;
char finalPath[256];
sprintf_s(finalPath, "%s.wav", basePath);
const char* extensions[] = { ".ogg", ".wav" };
size_t count = sizeof(extensions) / sizeof(extensions[0]);
for (size_t i = 0; i < count; i++)
{
char path[256];
sprintf_s(path, "%s%s", basePath, extensions[i]);
if (fs::exists(path))
{
finalPathStr = path;
found = true;
break;
}
}
if (!found)
app.DebugPrintf("No sound file found for UI sound: %s\n", basePath);
const char* extensions[] = { ".ogg", ".wav" };
size_t count = sizeof(extensions) / sizeof(extensions[0]);
bool found = false;
for (size_t i = 0; i < count; i++)
{
sprintf_s(finalPath, "%s%s", basePath, extensions[i]);
if (fs::exists(finalPath))
{
found = true;
break;
}
}
if (!found)
{
app.DebugPrintf("No sound file found for UI sound: %s\n", basePath);
return;
}
m_uiPathCache[iSound] = finalPathStr;
}
MiniAudioSound* s = new MiniAudioSound();
memset(&s->info, 0, sizeof(AUDIO_INFO));
@@ -620,14 +594,14 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
if (ma_sound_init_from_file(
&m_engine,
finalPath,
MA_SOUND_FLAG_ASYNC,
finalPathStr.c_str(),
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC,
nullptr,
nullptr,
&s->sound) != MA_SUCCESS)
{
delete s;
app.DebugPrintf("ma_sound_init_from_file failed: %s\n", finalPath);
app.DebugPrintf("Failed to initialize sound from file: %s\n", finalPathStr.c_str());
return;
}
@@ -669,13 +643,9 @@ void SoundEngine::playStreaming(const wstring& name, float x, float y , float z,
m_StreamingAudioInfo.pitch = pitch;
if(m_StreamState == eMusicStreamState_Playing)
{
m_StreamState = eMusicStreamState_Stop;
}
else if(m_StreamState == eMusicStreamState_Opening)
{
m_StreamState = eMusicStreamState_OpeningCancel;
}
if(name.empty())
{
@@ -716,13 +686,9 @@ void SoundEngine::playStreaming(const wstring& name, float x, float y , float z,
m_musicID = getMusicID(eMusicType_End);
}
else if(playerInNether)
{
m_musicID = getMusicID(eMusicType_Nether);
}
else
{
getGameModeMusicID(pMinecraft, i);
}
}
else
{
@@ -733,7 +699,6 @@ void SoundEngine::playStreaming(const wstring& name, float x, float y , float z,
}
}
int SoundEngine::GetRandomishTrack(int iStart,int iEnd)
{
// 4J-PB - make it more likely that we'll get a track we've not heard for a while, although repeating tracks sometimes is fine
@@ -756,9 +721,7 @@ int SoundEngine::GetRandomishTrack(int iStart,int iEnd)
app.DebugPrintf("Heard all tracks - resetting the tracking array\n");
for(size_t i=iStart;i<=iEnd;i++)
{
m_bHeardTrackA[i]=false;
}
}
// trying to get a track we haven't heard, but not too hard
@@ -774,9 +737,7 @@ int SoundEngine::GetRandomishTrack(int iStart,int iEnd)
break;
}
else
{
app.DebugPrintf("(%d) Skipping track %d already heard it recently\n",i,iVal);
}
}
app.DebugPrintf("Select track %d\n",iVal);
@@ -871,13 +832,9 @@ int SoundEngine::getMusicID(const wstring& name)
float SoundEngine::getMasterMusicVolume()
{
if( m_bSystemMusicPlaying )
{
return 0.0f;
}
else
{
return m_MasterMusicVolume;
}
}
/////////////////////////////////////////////
@@ -958,10 +915,7 @@ int SoundEngine::OpenStreamThreadProc(void* lpParameter)
/////////////////////////////////////////////
void SoundEngine::playMusicTick()
{
// AP - vita will update the music during the mixer callback
#ifndef __PSVITA__
playMusicUpdate();
#endif
}
// AP - moved to a separate function so it can be called from the mixer callback on Vita
@@ -991,28 +945,8 @@ void SoundEngine::playMusicUpdate()
if(m_musicID!=-1)
{
// start playing it
#if ( defined __PS3__ || defined __PSVITA__ || defined __ORBIS__ )
#ifdef __PS3__
// 4J-PB - Need to check if we are a patched BD build
if(app.GetBootedFromDiscPatch())
{
sprintf(m_szStreamName,"%s/%s",app.GetBDUsrDirPath(m_szMusicPath), m_szMusicPath );
app.DebugPrintf("SoundEngine::playMusicUpdate - (booted from disc patch) music path - %s",m_szStreamName);
}
else
{
sprintf(m_szStreamName,"%s/%s",getUsrDirPath(), m_szMusicPath );
}
#else
sprintf(m_szStreamName,"%s/%s",getUsrDirPath(), m_szMusicPath );
#endif
#else
strcpy_s((char *)m_szStreamName, _countof(m_szStreamName), m_szMusicPath);
#endif
// are we using a mash-up pack?
//if(pMinecraft && !pMinecraft->skins->isUsingDefaultSkin() && pMinecraft->skins->getSelected()->hasAudio())
if(Minecraft::GetInstance()->skins->getSelected()->hasAudio())
@@ -1033,25 +967,14 @@ void SoundEngine::playMusicUpdate()
SetIsPlayingStreamingGameMusic(true);
SetIsPlayingStreamingCDMusic(false);
m_StreamingAudioInfo.bIs3D=false;
#ifdef _XBOX_ONE
wstring &wstrSoundName=dlcAudioFile->GetSoundName(m_musicID);
wstring wstrFile=L"TPACK:\\Data\\" + wstrSoundName +L".wav";
std::wstring mountedPath = StorageManager.GetMountedPath(wstrFile);
wcstombs(m_szStreamName,mountedPath.c_str(),255);
#else
wstring &wstrSoundName=dlcAudioFile->GetSoundName(m_musicID);
char szName[255];
wcstombs_s(nullptr, szName, sizeof(szName), wstrSoundName.c_str(), _TRUNCATE);
#if defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
string strFile="TPACK:/Data/" + string(szName) + ".wav";
#else
string strFile="TPACK:\\Data\\" + string(szName) + ".wav";
#endif
std::string mountedPath = StorageManager.GetMountedPath(strFile);
strcpy_s(m_szStreamName,mountedPath.c_str());
#endif
}
else
{
@@ -1067,44 +990,7 @@ void SoundEngine::playMusicUpdate()
}
else
{
// 4J-PB - if this is a PS3 disc patch, we have to check if the music file is in the patch data
#ifdef __PS3__
if(app.GetBootedFromDiscPatch() && (m_musicID<m_iStream_CD_1))
{
// rebuild the path for the music
strcat_s((char *)m_szStreamName,m_szStreamFileA[m_musicID]);
strcat_s((char *)m_szStreamName,".wav");
// check if this is in the patch data
sprintf(m_szStreamName,"%s/%s",app.GetBDUsrDirPath(m_szStreamName), m_szMusicPath );
strcat((char *)m_szStreamName,m_szStreamFileA[m_musicID]);
strcat((char *)m_szStreamName,".wav");
SetIsPlayingStreamingGameMusic(true);
SetIsPlayingStreamingCDMusic(false);
m_StreamingAudioInfo.bIs3D=false;
}
else if(m_musicID<m_iStream_CD_1)
{
SetIsPlayingStreamingGameMusic(true);
SetIsPlayingStreamingCDMusic(false);
m_StreamingAudioInfo.bIs3D=false;
// build the name
strcat_s((char *)m_szStreamName,m_szStreamFileA[m_musicID]);
strcat_s((char *)m_szStreamName,".wav");
}
else
{
SetIsPlayingStreamingGameMusic(false);
SetIsPlayingStreamingCDMusic(true);
m_StreamingAudioInfo.bIs3D=true;
// build the name
strcat_s((char *)m_szStreamName, "CDs/");
strcat_s((char *)m_szStreamName,m_szStreamFileA[m_musicID]);
strcat_s((char *)m_szStreamName,".wav");
}
#else
// 4J-PB - if this is a PS3 disc patch, we have to check if the music file is in the patch data
if(m_musicID<m_iStream_CD_1)
{
SetIsPlayingStreamingGameMusic(true);
@@ -1121,8 +1007,6 @@ void SoundEngine::playMusicUpdate()
}
strcat_s((char *)m_szStreamName, _countof(m_szStreamName), m_szStreamFileA[m_musicID]);
strcat_s((char *)m_szStreamName, _countof(m_szStreamName), ".wav");
#endif
}
// wstring name = m_szStreamFileA[m_musicID];
@@ -1132,9 +1016,7 @@ void SoundEngine::playMusicUpdate()
FILE* pFile = nullptr;
if (fopen_s(&pFile, reinterpret_cast<char*>(m_szStreamName), "rb") == 0 && pFile)
{
fclose(pFile);
}
else
{
const char* extensions[] = { ".ogg", ".wav" };
@@ -1160,9 +1042,8 @@ void SoundEngine::playMusicUpdate()
if (!found)
{
if (dotPos != nullptr)
{
strcpy_s(dotPos, 5, ".wav");
}
app.DebugPrintf("WARNING: No audio file found for music ID %d (tried .ogg, .wav)\n", m_musicID);
return;
}
@@ -1216,9 +1097,7 @@ void SoundEngine::playMusicUpdate()
ma_sound_set_position(&m_musicStream, m_StreamingAudioInfo.x, m_StreamingAudioInfo.y, -m_StreamingAudioInfo.z);
}
else
{
ma_sound_set_spatialization_enabled(&m_musicStream, MA_FALSE);
}
ma_sound_set_pitch(&m_musicStream, m_StreamingAudioInfo.pitch);
@@ -1349,9 +1228,7 @@ void SoundEngine::playMusicUpdate()
}
/* //This is setting the music in loop
else if ( !playerInEnd && !playerInNether )
{
getGameModeMusicID(pMinecraft, i);
}
*/
// volume change required?
@@ -1394,9 +1271,7 @@ void SoundEngine::playMusicUpdate()
float relZ = m_StreamingAudioInfo.z - m_ListenerA[iClosestListener].vPosition.z;
if (m_musicStreamActive)
{
ma_sound_set_position(&m_musicStream, relX, relY, relZ);
}
}
}
@@ -1484,11 +1359,12 @@ char *SoundEngine::ConvertSoundPathToName(const wstring& name, bool bConvertSpac
for(unsigned int i = 0; i < name.length(); i++ )
{
wchar_t c = name[i];
if(c=='.') c='/';
if(c=='.')
c='/';
if(bConvertSpaces)
{
if(c==' ') c='_';
}
buf[i] = (char)c;
}
buf[name.length()] = 0;