Fix sounds not playing properly

This commit is contained in:
GuglioIsStupid 2026-03-03 17:05:33 -05:00
parent 0f7feb0369
commit 6e888df699

View file

@ -14,19 +14,17 @@
#include <audioout.h> #include <audioout.h>
#endif #endif
#ifdef _WINDOWS64
#include "..\..\Minecraft.Client\Windows64\Windows64_App.h" #include "..\..\Minecraft.Client\Windows64\Windows64_App.h"
#endif
#define MINIAUDIO_IMPLEMENTATION #define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h" #include "miniaudio.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "..\Filesystem\Filesystem.h"
#include "../Filesystem/Filesystem.h"
#ifdef __ORBIS__ #ifdef __ORBIS__
#include <audioout.h> #include <audioout.h>
//#define __DISABLE_MILES__ // MGH disabled for now as it crashes if we call sceNpMatching2Initialize
#endif #endif
// take out Orbis until they are done // take out Orbis until they are done
@ -60,7 +58,7 @@ void SoundEngine::playMusicTick() {};
#else #else
#ifdef _WINDOWS64 // todo: make this just search in Media instead #ifdef _WINDOWS64
char SoundEngine::m_szSoundPath[]={"Windows64Media\\Sound\\"}; char SoundEngine::m_szSoundPath[]={"Windows64Media\\Sound\\"};
char SoundEngine::m_szMusicPath[]={"music\\"}; char SoundEngine::m_szMusicPath[]={"music\\"};
char SoundEngine::m_szRedistName[]={"redist64"}; char SoundEngine::m_szRedistName[]={"redist64"};
@ -211,7 +209,7 @@ void SoundEngine::updateMiniAudio()
{ {
if (m_validListenerCount == 1) if (m_validListenerCount == 1)
{ {
for (size_t i = 0; i < MAX_LOCAL_PLAYERS; i++) for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
{ {
if (m_ListenerA[i].bValid) if (m_ListenerA[i].bValid)
{ {
@ -272,23 +270,32 @@ void SoundEngine::updateMiniAudio()
{ {
if (m_validListenerCount > 1) if (m_validListenerCount > 1)
{ {
float closest = 10000.0f; float fClosest=10000.0f;
int iClosestListener=0;
for (size_t i = 0; i < MAX_LOCAL_PLAYERS; i++) float fClosestX=0.0f,fClosestY=0.0f,fClosestZ=0.0f,fDist;
for( int i = 0; i < MAX_LOCAL_PLAYERS; i++ )
{ {
if( m_ListenerA[i].bValid ) if( m_ListenerA[i].bValid )
{ {
float dx = fabs(m_ListenerA[i].vPosition.x - s->info.x); float x,y,z;
float dy = fabs(m_ListenerA[i].vPosition.y - s->info.y);
float dz = fabs(m_ListenerA[i].vPosition.z - s->info.z);
float dist = dx + dy + dz; x=fabs(m_ListenerA[i].vPosition.x-s->info.x);
if (dist < closest) y=fabs(m_ListenerA[i].vPosition.y-s->info.y);
closest = dist; z=fabs(m_ListenerA[i].vPosition.z-s->info.z);
fDist=x+y+z;
if(fDist<fClosest)
{
fClosest=fDist;
fClosestX=x;
fClosestY=y;
fClosestZ=z;
iClosestListener=i;
}
} }
} }
float realDist = sqrtf(closest * closest); float realDist = sqrtf((fClosestX*fClosestX)+(fClosestY*fClosestY)+(fClosestZ*fClosestZ));
ma_sound_set_position(&s->sound, 0, 0, realDist); ma_sound_set_position(&s->sound, 0, 0, realDist);
} }
else else
@ -305,10 +312,6 @@ void SoundEngine::updateMiniAudio()
} }
} }
//#define DISTORTION_TEST
#ifdef DISTORTION_TEST
static float fVal=0.0f;
#endif
///////////////////////////////////////////// /////////////////////////////////////////////
// //
// tick // tick
@ -325,14 +328,10 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
// update the listener positions // update the listener positions
int listenerCount = 0; int listenerCount = 0;
#ifdef DISTORTION_TEST
float fX,fY,fZ;
#endif
if( players ) if( players )
{ {
bool bListenerPostionSet = false; bool bListenerPostionSet = false;
for( int i = 0; i < MAX_LOCAL_PLAYERS; i++ )
for( size_t i = 0; i < MAX_LOCAL_PLAYERS; i++ )
{ {
if( players[i] != NULL ) if( players[i] != NULL )
{ {
@ -394,11 +393,9 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
SoundEngine::SoundEngine() SoundEngine::SoundEngine()
{ {
random = new Random(); random = new Random();
memset(&m_engine, 0, sizeof(ma_engine)); memset(&m_engine, 0, sizeof(ma_engine));
memset(&m_engineConfig, 0, sizeof(ma_engine_config)); memset(&m_engineConfig, 0, sizeof(ma_engine_config));
m_musicStreamActive = false; m_musicStreamActive = false;
m_StreamState=eMusicStreamState_Idle; m_StreamState=eMusicStreamState_Idle;
m_iMusicDelay=0; m_iMusicDelay=0;
m_validListenerCount=0; m_validListenerCount=0;
@ -457,11 +454,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
strcpy((char*)szSoundName, "Minecraft/"); strcpy((char*)szSoundName, "Minecraft/");
#ifdef DISTORTION_TEST
wstring name = wchSoundNames[eSoundType_MOB_ENDERDRAGON_GROWL];
#else
wstring name = wchSoundNames[iSound]; wstring name = wchSoundNames[iSound];
#endif
char* SoundName = (char*)ConvertSoundPathToName(name); char* SoundName = (char*)ConvertSoundPathToName(name);
strcat((char*)szSoundName, SoundName); strcat((char*)szSoundName, SoundName);
@ -480,13 +473,15 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
{ {
int count = 0; int count = 0;
for (size_t i = 1; i < 32; i++) for (int i = 1; i < 32; i++)
{ {
char numberedFolder[256]; char numberedFolder[256];
sprintf_s(numberedFolder, "%s%d", basePath, i); sprintf_s(numberedFolder, "%s%d", basePath, i);
bool exists = DirectoryExists(numberedFolder); DWORD attr = GetFileAttributesA(numberedFolder);
if(exists)
if (attr != INVALID_FILE_ATTRIBUTES &&
(attr & FILE_ATTRIBUTE_DIRECTORY))
{ {
count++; count++;
} }
@ -511,16 +506,23 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
char searchPattern[256]; char searchPattern[256];
sprintf_s(searchPattern, "%s\\*.wav", chosenFolder); sprintf_s(searchPattern, "%s\\*.wav", chosenFolder);
char foundFile[256]; WIN32_FIND_DATAA findData;
if (!GetFirstFileInDirectory(chosenFolder, foundFile, sizeof(foundFile))) HANDLE hFind = FindFirstFileA(searchPattern, &findData);
if (hFind == INVALID_HANDLE_VALUE)
{ {
app.DebugPrintf("No .wav or .mp3 files found in %s\n", chosenFolder); sprintf_s(searchPattern, "%s\\*.mp3", chosenFolder);
hFind = FindFirstFileA(searchPattern, &findData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf("No .wav or .mp3 files found in %s\n", chosenFolder);
return; return;
} }
}
sprintf_s(finalPath, "%s\\%s", chosenFolder, findData.cFileName);
sprintf_s(finalPath, "%s\\%s", chosenFolder, foundFile); FindClose(hFind);
} }
MiniAudioSound* s = new MiniAudioSound(); MiniAudioSound* s = new MiniAudioSound();
@ -529,6 +531,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
s->info.x = x; s->info.x = x;
s->info.y = y; s->info.y = y;
s->info.z = z; s->info.z = z;
s->info.volume = volume; s->info.volume = volume;
s->info.pitch = pitch; s->info.pitch = pitch;
s->info.bIs3D = true; s->info.bIs3D = true;
@ -920,6 +923,7 @@ int SoundEngine::OpenStreamThreadProc(void* lpParameter)
if (result != MA_SUCCESS) if (result != MA_SUCCESS)
{ {
app.DebugPrintf("SoundEngine::OpenStreamThreadProc - Failed to open stream: %s\n", soundEngine->m_szStreamName);
return 0; return 0;
} }
@ -947,6 +951,7 @@ void SoundEngine::playMusicTick()
// AP - moved to a separate function so it can be called from the mixer callback on Vita // AP - moved to a separate function so it can be called from the mixer callback on Vita
void SoundEngine::playMusicUpdate() void SoundEngine::playMusicUpdate()
{ {
//return;
static bool firstCall = true; static bool firstCall = true;
static float fMusicVol = 0.0f; static float fMusicVol = 0.0f;
if( firstCall ) if( firstCall )
@ -1178,6 +1183,8 @@ void SoundEngine::playMusicUpdate()
if (!m_musicStreamActive) if (!m_musicStreamActive)
{ {
const char* currentExt = strrchr(reinterpret_cast<char*>(m_szStreamName), '.'); const char* currentExt = strrchr(reinterpret_cast<char*>(m_szStreamName), '.');
if (currentExt && _stricmp(currentExt, ".wav") == 0)
{
const bool isCD = (m_musicID >= m_iStream_CD_1); const bool isCD = (m_musicID >= m_iStream_CD_1);
const char* folder = isCD ? "cds/" : "music/"; const char* folder = isCD ? "cds/" : "music/";
@ -1195,6 +1202,7 @@ void SoundEngine::playMusicUpdate()
break; break;
} }
} }
}
m_StreamState = eMusicStreamState_Idle; m_StreamState = eMusicStreamState_Idle;
break; break;
@ -1230,18 +1238,12 @@ void SoundEngine::playMusicUpdate()
} }
break; break;
case eMusicStreamState_Stop: case eMusicStreamState_Stop:
#ifdef _WINDOWS64
if (m_musicStreamActive) if (m_musicStreamActive)
{ {
ma_sound_stop(&m_musicStream); ma_sound_stop(&m_musicStream);
ma_sound_uninit(&m_musicStream); ma_sound_uninit(&m_musicStream);
m_musicStreamActive = false; m_musicStreamActive = false;
} }
#else
AIL_pause_stream(m_hStream, 1);
AIL_close_stream(m_hStream);
m_hStream = 0;
#endif
SetIsPlayingStreamingCDMusic(false); SetIsPlayingStreamingCDMusic(false);
SetIsPlayingStreamingGameMusic(false); SetIsPlayingStreamingGameMusic(false);
@ -1367,7 +1369,7 @@ void SoundEngine::playMusicUpdate()
int iClosestListener = 0; int iClosestListener = 0;
float fClosestDist = 1e6f; float fClosestDist = 1e6f;
for (size_t i = 0; i < MAX_LOCAL_PLAYERS; i++) for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
{ {
if (m_ListenerA[i].bValid) if (m_ListenerA[i].bValid)
{ {