feat(Windows64): miniaudio

This commit is contained in:
str1k3r
2026-07-23 11:57:04 -04:00
parent 51ee0d8e57
commit 2b051aa038
647 changed files with 103781 additions and 18 deletions
+665 -4
View File
@@ -16,7 +16,17 @@
#ifdef _WINDOWS64
#include "..\..\Minecraft.Client\Windows64\Windows64_App.h"
#include "..\..\Minecraft.Client\Windows64\Miles\include\imssapi.h"
std::vector<MiniAudioSound*> m_activeSounds;
#include "stb_vorbis.h"
#define MA_NO_DSOUND
#define MA_NO_WINMM
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include <vector>
#include <memory>
#include <mutex>
#include "..\Filesystem\Filesystem.h"
#endif
#ifdef __ORBIS__
@@ -93,11 +103,13 @@ char SoundEngine::m_szSoundPath[]={"Sound/"};
#endif
#ifndef _WINDOWS64
F32 AILCALLBACK custom_falloff_function (HSAMPLE S,
F32 distance,
F32 rolloff_factor,
F32 min_dist,
F32 max_dist);
#endif
char *SoundEngine::m_szStreamFileA[eStream_Max]=
{
@@ -155,6 +167,7 @@ char *SoundEngine::m_szStreamFileA[eStream_Max]=
// ErrorCallback
//
/////////////////////////////////////////////
#ifndef _WINDOWS64
void AILCALL ErrorCallback(S64 i_Id, char const* i_Details)
{
char *pchLastError=AIL_last_error();
@@ -169,6 +182,7 @@ void AILCALL ErrorCallback(S64 i_Id, char const* i_Details)
app.DebugPrintf("ErrorCallback - Details: %s\n", i_Details);
}
}
#endif
#ifdef __PSVITA__
// AP - this is the callback when the driver is about to mix. At this point the mutex is locked by Miles so we can now call all Miles functions without
@@ -201,6 +215,7 @@ void AILCALL MilesMixerCB(HDIGDRIVER dig)
/////////////////////////////////////////////
void SoundEngine::init(Options *pOptions)
{
#ifndef _WINDOWS64
app.DebugPrintf("---SoundEngine::init\n");
#ifdef __DISABLE_MILES__
return;
@@ -370,6 +385,31 @@ void SoundEngine::init(Options *pOptions)
// wait for 1 mix...
AIL_release_sample_handle(Sample);
#endif
#else
app.DebugPrintf("---SoundEngine::init\n");
m_engineConfig = ma_engine_config_init();
m_engineConfig.listenerCount = MAX_LOCAL_PLAYERS;
if (ma_engine_init(&m_engineConfig, &m_engine) != MA_SUCCESS)
{
app.DebugPrintf("Failed to initialize miniaudio engine\n");
return;
}
ma_engine_set_volume(&m_engine, 1.0f);
m_MasterMusicVolume = 1.0f;
m_MasterEffectsVolume = 1.0f;
m_validListenerCount = 1;
m_bSystemMusicPlaying = false;
app.DebugPrintf("---miniaudio initialized\n");
return;
#endif
}
#ifdef __ORBIS__
@@ -399,6 +439,7 @@ void SoundEngine::SetStreamingSounds(int iOverworldMin, int iOverWorldMax, int i
}
// AP - moved to a separate function so it can be called from the mixer callback on Vita
#ifndef _WINDOWS64
void SoundEngine::updateMiles()
{
#ifdef __PSVITA__
@@ -611,6 +652,110 @@ void SoundEngine::updateMiles()
}
AIL_complete_event_queue_processing();
}
#else
void SoundEngine::updateMiniAudio()
{
if (m_validListenerCount == 1)
{
for (size_t i = 0; i < MAX_LOCAL_PLAYERS; i++)
{
if (m_ListenerA[i].bValid)
{
ma_engine_listener_set_position(
&m_engine,
0,
m_ListenerA[i].vPosition.x,
m_ListenerA[i].vPosition.y,
m_ListenerA[i].vPosition.z);
ma_engine_listener_set_direction(
&m_engine,
0,
m_ListenerA[i].vOrientFront.x,
m_ListenerA[i].vOrientFront.y,
m_ListenerA[i].vOrientFront.z);
ma_engine_listener_set_world_up(
&m_engine,
0,
0.0f, 1.0f, 0.0f);
break;
}
}
}
else
{
ma_engine_listener_set_position(&m_engine, 0, 0.0f, 0.0f, 0.0f);
ma_engine_listener_set_direction(&m_engine, 0, 0.0f, 0.0f, 1.0f);
ma_engine_listener_set_world_up(&m_engine, 0, 0.0f, 1.0f, 0.0f);
}
for (auto it = m_activeSounds.begin(); it != m_activeSounds.end(); )
{
MiniAudioSound* s = *it;
if (!ma_sound_is_playing(&s->sound))
{
ma_sound_uninit(&s->sound);
delete s;
it = m_activeSounds.erase(it);
continue;
}
float finalVolume = s->info.volume * m_MasterEffectsVolume * SFX_VOLUME_MULTIPLIER;
if (finalVolume > SFX_MAX_GAIN)
finalVolume = SFX_MAX_GAIN;
ma_sound_set_volume(&s->sound, finalVolume);
ma_sound_set_pitch(&s->sound, s->info.pitch);
if (s->info.bIs3D)
{
if (m_validListenerCount > 1)
{
float fClosest=10000.0f;
int iClosestListener=0;
float fClosestX=0.0f,fClosestY=0.0f,fClosestZ=0.0f,fDist;
for( size_t i = 0; i < MAX_LOCAL_PLAYERS; i++ )
{
if( m_ListenerA[i].bValid )
{
float x,y,z;
x=fabs(m_ListenerA[i].vPosition.x-s->info.x);
y=fabs(m_ListenerA[i].vPosition.y-s->info.y);
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((fClosestX*fClosestX)+(fClosestY*fClosestY)+(fClosestZ*fClosestZ));
ma_sound_set_position(&s->sound, 0, 0, realDist);
}
else
{
ma_sound_set_position(
&s->sound,
s->info.x,
s->info.y,
s->info.z);
}
}
++it;
}
}
#endif
//#define DISTORTION_TEST
#ifdef DISTORTION_TEST
@@ -697,7 +842,11 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
LeaveCriticalSection(&SoundEngine_MixerMutex);
#else
#ifndef _WINDOWS64
updateMiles();
#else
updateMiniAudio();
#endif
#endif
}
@@ -709,7 +858,13 @@ void SoundEngine::tick(shared_ptr<Mob> *players, float a)
SoundEngine::SoundEngine()
{
random = new Random();
#ifndef _WINDOWS64
m_hStream=0;
#else
memset(&m_engine, 0, sizeof(ma_engine));
memset(&m_engineConfig, 0, sizeof(ma_engine_config));
m_musicStreamActive = false;
#endif
m_StreamState=eMusicStreamState_Idle;
m_iMusicDelay=0;
m_validListenerCount=0;
@@ -791,7 +946,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
strcat((char *)szSoundName,SoundName);
// app.DebugPrintf(6,"PlaySound - %d - %s - %s (%f %f %f, vol %f, pitch %f)\n",iSound, SoundName, szSoundName,x,y,z,volume,pitch);
#ifndef _WINDOWS64
AUDIO_INFO AudioInfo;
AudioInfo.x=x;
AudioInfo.y=y;
@@ -808,6 +963,124 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume, floa
S32 token = AIL_enqueue_event_start();
AIL_enqueue_event_buffer(&token, &AudioInfo, sizeof(AUDIO_INFO), 0);
AIL_enqueue_event_end_named(token, (char *)szSoundName);
#else
app.DebugPrintf(6,
"PlaySound - %d - %s - %s (%f %f %f, vol %f, pitch %f)\n",
iSound, SoundName, szSoundName, x, y, z, volume, pitch);
char basePath[256];
sprintf_s(basePath, "Windows64Media/Sound/%s", (char*)szSoundName);
char finalPath[256];
// Check path cache first to avoid expensive filesystem probing
auto cacheIt = m_soundPathCache.find(iSound);
if (cacheIt != m_soundPathCache.end())
{
const auto& paths = cacheIt->second;
if (paths.empty())
return; // previously probed, no files found
const std::string& chosen = paths[rand() % paths.size()];
sprintf_s(finalPath, "%s", chosen.c_str());
}
else
{
// Cache miss — probe filesystem and store results
std::vector<std::string> validPaths;
const char* extensions[] = { ".ogg", ".wav", ".mp3" };
size_t extCount = sizeof(extensions) / sizeof(extensions[0]);
bool found = false;
// Check base name with each extension (non-numbered)
for (size_t extIdx = 0; extIdx < extCount; extIdx++)
{
char basePlusExt[256];
sprintf_s(basePlusExt, "%s%s", basePath, extensions[extIdx]);
DWORD attr = GetFileAttributesA(basePlusExt);
if (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY))
{
validPaths.push_back(basePlusExt);
found = true;
break; // non-numbered: only one base file needed
}
}
if (!found)
{
// Check numbered variants (e.g. sound1.ogg, sound2.ogg, ...)
for (size_t extIdx = 0; extIdx < extCount; extIdx++)
{
for (int i = 1; i < 32; i++)
{
char numberedPath[256];
sprintf_s(numberedPath, "%s%d%s", basePath, i, extensions[extIdx]);
DWORD attr = GetFileAttributesA(numberedPath);
if (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY))
{
validPaths.push_back(numberedPath);
}
}
}
}
m_soundPathCache[iSound] = validPaths;
if (validPaths.empty())
{
sprintf_s(finalPath, "%s.wav", basePath); // fallback for debug print
}
else
{
const std::string& chosen = validPaths[rand() % validPaths.size()];
sprintf_s(finalPath, "%s", chosen.c_str());
}
}
MiniAudioSound* s = new MiniAudioSound();
memset(&s->info, 0, sizeof(AUDIO_INFO));
s->info.x = x;
s->info.y = y;
s->info.z = z;
s->info.volume = volume;
s->info.pitch = pitch;
s->info.bIs3D = true;
s->info.bUseSoundsPitchVal = false;
s->info.iSound = iSound + eSFX_MAX;
if (ma_sound_init_from_file(
&m_engine,
finalPath,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC,
NULL,
NULL,
&s->sound) != MA_SUCCESS)
{
app.DebugPrintf("Failed to initialize sound from file: %s\n", finalPath);
delete s;
return;
}
ma_sound_set_spatialization_enabled(&s->sound, MA_TRUE);
ma_sound_set_min_distance(&s->sound, SFX_3D_MIN_DISTANCE);
ma_sound_set_max_distance(&s->sound, SFX_3D_MAX_DISTANCE);
ma_sound_set_rolloff(&s->sound, SFX_3D_ROLLOFF);
float finalVolume = volume * m_MasterEffectsVolume * SFX_VOLUME_MULTIPLIER;
if (finalVolume > SFX_MAX_GAIN)
finalVolume = SFX_MAX_GAIN;
ma_sound_set_volume(&s->sound, finalVolume);
ma_sound_set_pitch(&s->sound, pitch);
ma_sound_set_position(&s->sound, x, y, z);
ma_sound_start(&s->sound);
m_activeSounds.push_back(s);
#endif
}
/////////////////////////////////////////////
@@ -819,6 +1092,9 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
{
U8 szSoundName[256];
wstring name;
#ifdef _WINDOWS64
const char* soundDir;
#endif
// we have some game sounds played as UI sounds...
// Not the best way to do this, but it seems to only be the portal sounds
@@ -831,6 +1107,9 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
// build the name
strcpy((char *)szSoundName,"Minecraft/");
name = wchSoundNames[iSound];
#ifdef _WINDOWS64
soundDir = "Minecraft";
#endif
}
else
{
@@ -841,6 +1120,9 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
// build the name
strcpy((char *)szSoundName,"Minecraft/UI/");
name = wchUISoundNames[iSound];
#ifdef _WINDOWS64
soundDir = "Minecraft/UI";
#endif
}
char *SoundName = (char *)ConvertSoundPathToName(name);
@@ -848,6 +1130,7 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
// app.DebugPrintf("UI: Playing %s, volume %f, pitch %f\n",SoundName,volume,pitch);
//app.DebugPrintf("PlaySound - %d - %s\n",iSound, SoundName);
#ifndef _WINDOWS64
AUDIO_INFO AudioInfo;
memset(&AudioInfo,0,sizeof(AUDIO_INFO));
@@ -870,6 +1153,82 @@ void SoundEngine::playUI(int iSound, float volume, float pitch)
S32 token = AIL_enqueue_event_start();
AIL_enqueue_event_buffer(&token, &AudioInfo, sizeof(AUDIO_INFO), 0);
AIL_enqueue_event_end_named(token, (char *)szSoundName);
#else
char basePath[256];
sprintf_s(basePath, "Windows64Media/Sound/%s/%s", soundDir, ConvertSoundPathToName(name));
char finalPath[256];
// Check UI sound path cache first
auto cacheIt = m_uiSoundPathCache.find(iSound);
if (cacheIt != m_uiSoundPathCache.end())
{
if (cacheIt->second.empty())
{
app.DebugPrintf("No sound file found for UI sound (cached): %s\n", basePath);
return;
}
sprintf_s(finalPath, "%s", cacheIt->second.c_str());
}
else
{
// Cache miss — probe filesystem and store result
const char* extensions[] = { ".ogg", ".wav", ".mp3" };
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 (FileExists(finalPath))
{
found = true;
break;
}
}
if (!found)
{
m_uiSoundPathCache[iSound] = ""; // cache negative result
app.DebugPrintf("No sound file found for UI sound: %s\n", basePath);
return;
}
m_uiSoundPathCache[iSound] = finalPath;
}
MiniAudioSound* s = new MiniAudioSound();
memset(&s->info, 0, sizeof(AUDIO_INFO));
s->info.volume = volume;
s->info.pitch = pitch;
s->info.bIs3D = false;
s->info.bUseSoundsPitchVal = true;
if (ma_sound_init_from_file(
&m_engine,
finalPath,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC,
NULL,
NULL,
&s->sound) != MA_SUCCESS)
{
delete s;
app.DebugPrintf("ma_sound_init_from_file failed: %s\n", finalPath);
return;
}
ma_sound_set_spatialization_enabled(&s->sound, MA_FALSE);
float finalVolume = volume * m_MasterEffectsVolume;
if (finalVolume > 1.0f)
finalVolume = 1.0f;
printf("UI Sound volume set to %f\nEffects volume: %f\n", finalVolume, m_MasterEffectsVolume);
ma_sound_set_volume(&s->sound, finalVolume);
ma_sound_set_pitch(&s->sound, pitch);
ma_sound_start(&s->sound);
m_activeSounds.push_back(s);
#endif
}
/////////////////////////////////////////////
@@ -1133,12 +1492,42 @@ int SoundEngine::OpenStreamThreadProc( void* lpParameter )
return 0;
#endif
SoundEngine *soundEngine = (SoundEngine *)lpParameter;
#ifndef _WINDOWS64
soundEngine->m_hStream = AIL_open_stream(soundEngine->m_hDriver,soundEngine->m_szStreamName,0);
if(soundEngine->m_hStream==0)
{
app.DebugPrintf("SoundEngine::OpenStreamThreadProc - Could not open - %s\n",soundEngine->m_szStreamName);
}
#else
const char* ext = strrchr(soundEngine->m_szStreamName, '.');
if (soundEngine->m_musicStreamActive)
{
ma_sound_stop(&soundEngine->m_musicStream);
ma_sound_uninit(&soundEngine->m_musicStream);
soundEngine->m_musicStreamActive = false;
}
ma_result result = ma_sound_init_from_file(
&soundEngine->m_engine,
soundEngine->m_szStreamName,
MA_SOUND_FLAG_STREAM,
NULL,
NULL,
&soundEngine->m_musicStream);
if (result != MA_SUCCESS)
{
app.DebugPrintf("SoundEngine::OpenStreamThreadProc - Failed to open stream: %s\n", soundEngine->m_szStreamName);
return 0;
}
ma_sound_set_spatialization_enabled(&soundEngine->m_musicStream, MA_FALSE);
ma_sound_set_looping(&soundEngine->m_musicStream, MA_FALSE);
soundEngine->m_musicStreamActive = true;
#endif
return 0;
}
@@ -1158,6 +1547,7 @@ void SoundEngine::playMusicTick()
// AP - moved to a separate function so it can be called from the mixer callback on Vita
void SoundEngine::playMusicUpdate()
{
#ifndef _WINDOWS64
//return;
static bool firstCall = true;
static float fMusicVol = 0.0f;
@@ -1340,10 +1730,150 @@ void SoundEngine::playMusicUpdate()
m_openStreamThread->Run();
m_StreamState = eMusicStreamState_Opening;
}
#else
static float fMusicVol = 0.0f;
fMusicVol = getMasterMusicVolume();
switch(m_StreamState)
{
case eMusicStreamState_Idle:
// start a stream playing
if (m_iMusicDelay > 0)
{
m_iMusicDelay--;
return;
}
if (m_musicStreamActive)
{
app.DebugPrintf("WARNING: m_musicStreamActive already true in Idle state, resetting to Playing\n");
m_StreamState = eMusicStreamState_Playing;
return;
}
if(m_musicID!=-1)
{
// start playing it
strcpy((char *)m_szStreamName,m_szMusicPath);
// are we using a mash-up pack?
//if(pMinecraft && !pMinecraft->skins->isUsingDefaultSkin() && pMinecraft->skins->getSelected()->hasAudio())
if(Minecraft::GetInstance()->skins->getSelected()->hasAudio())
{
// It's a mash-up - need to use the DLC path for the music
TexturePack *pTexPack=Minecraft::GetInstance()->skins->getSelected();
DLCTexturePack *pDLCTexPack=(DLCTexturePack *)pTexPack;
DLCPack *pack = pDLCTexPack->getDLCInfoParentPack();
DLCAudioFile *dlcAudioFile = (DLCAudioFile *) pack->getFile(DLCManager::e_DLCType_Audio, 0);
app.DebugPrintf("Mashup pack \n");
// build the name
// if the music ID is beyond the end of the texture pack music files, then it's a CD
if(m_musicID<m_iStream_CD_1)
{
SetIsPlayingStreamingGameMusic(true);
SetIsPlayingStreamingCDMusic(false);
m_MusicType=eMusicType_Game;
m_StreamingAudioInfo.bIs3D=false;
wstring &wstrSoundName=dlcAudioFile->GetSoundName(m_musicID);
char szName[255];
wcstombs(szName,wstrSoundName.c_str(),255);
string strFile="TPACK:\\Data\\" + string(szName) + ".wav";
std::string mountedPath = StorageManager.GetMountedPath(strFile);
strcpy(m_szStreamName,mountedPath.c_str());
}
else
{
SetIsPlayingStreamingGameMusic(false);
SetIsPlayingStreamingCDMusic(true);
m_MusicType=eMusicType_CD;
m_StreamingAudioInfo.bIs3D=true;
// Need to adjust to index into the cds in the game's m_szStreamFileA
strcat((char *)m_szStreamName,"cds/");
strcat((char *)m_szStreamName,m_szStreamFileA[m_musicID-m_iStream_CD_1+eStream_CD_1]);
strcat((char *)m_szStreamName,".wav");
}
}
else
{
if(m_musicID<m_iStream_CD_1)
{
SetIsPlayingStreamingGameMusic(true);
SetIsPlayingStreamingCDMusic(false);
m_MusicType=eMusicType_Game;
m_StreamingAudioInfo.bIs3D=false;
// build the name
strcat((char *)m_szStreamName,"music/");
}
else
{
SetIsPlayingStreamingGameMusic(false);
SetIsPlayingStreamingCDMusic(true);
m_MusicType=eMusicType_CD;
m_StreamingAudioInfo.bIs3D=true;
// build the name
strcat((char *)m_szStreamName,"cds/");
}
strcat((char *)m_szStreamName,m_szStreamFileA[m_musicID]);
strcat((char *)m_szStreamName,".wav");
}
FILE* pFile = NULL;
if (fopen_s(&pFile, reinterpret_cast<char*>(m_szStreamName), "rb") == 0 && pFile)
{
fclose(pFile);
}
else
{
const char* extensions[] = { ".ogg", ".mp3", ".wav" };
size_t extCount = sizeof(extensions) / sizeof(extensions[0]);
bool found = false;
char* dotPos = strrchr(reinterpret_cast<char*>(m_szStreamName), '.');
if (dotPos != NULL && (dotPos - reinterpret_cast<char*>(m_szStreamName)) < 250)
{
for (size_t i = 0; i < extCount; i++)
{
strcpy_s(dotPos, 5, extensions[i]);
if (fopen_s(&pFile, reinterpret_cast<char*>(m_szStreamName), "rb") == 0 && pFile)
{
fclose(pFile);
found = true;
break;
}
}
}
if (!found)
{
if (dotPos != NULL)
{
strcpy_s(dotPos, 5, ".wav");
}
app.DebugPrintf("WARNING: No audio file found for music ID %d (tried .ogg, .mp3, .wav)\n", m_musicID);
return;
}
}
app.DebugPrintf("Starting streaming - %s\n",m_szStreamName);
m_openStreamThread = new C4JThread(OpenStreamThreadProc, this, "OpenStreamThreadProc");
m_openStreamThread->Run();
m_StreamState = eMusicStreamState_Opening;
}
#endif
break;
case eMusicStreamState_Opening:
// If the open stream thread is complete, then we are ready to proceed to actually playing
#ifndef _WINDOWS64
if( !m_openStreamThread->isRunning() )
{
delete m_openStreamThread;
@@ -1409,6 +1939,63 @@ void SoundEngine::playMusicUpdate()
m_StreamState=eMusicStreamState_Playing;
}
#else
if( !m_openStreamThread->isRunning() )
{
delete m_openStreamThread;
m_openStreamThread = NULL;
app.DebugPrintf("OpenStreamThreadProc finished. m_musicStreamActive=%d\n", m_musicStreamActive);
if (!m_musicStreamActive)
{
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 char* folder = isCD ? "cds/" : "music/";
int n = sprintf_s(reinterpret_cast<char*>(m_szStreamName), 512, "%s%s%s.wav", m_szMusicPath, folder, m_szStreamFileA[m_musicID]);
if (n > 0)
{
FILE* pFile = NULL;
if (fopen_s(&pFile, reinterpret_cast<char*>(m_szStreamName), "rb") == 0 && pFile)
{
fclose(pFile);
m_openStreamThread = new C4JThread(OpenStreamThreadProc, this, "OpenStreamThreadProc");
m_openStreamThread->Run();
break;
}
}
}
m_StreamState = eMusicStreamState_Idle;
break;
}
if (m_StreamingAudioInfo.bIs3D)
{
ma_sound_set_spatialization_enabled(&m_musicStream, MA_TRUE);
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);
float finalVolume = m_StreamingAudioInfo.volume * getMasterMusicVolume();
ma_sound_set_volume(&m_musicStream, finalVolume);
ma_result startResult = ma_sound_start(&m_musicStream);
app.DebugPrintf("ma_sound_start result: %d\n", startResult);
m_StreamState=eMusicStreamState_Playing;
}
#endif
break;
case eMusicStreamState_OpeningCancel:
if( !m_openStreamThread->isRunning() )
@@ -1420,18 +2007,46 @@ void SoundEngine::playMusicUpdate()
break;
case eMusicStreamState_Stop:
// should gradually take the volume down in steps
#ifndef _WINDOWS64
AIL_pause_stream(m_hStream,1);
AIL_close_stream(m_hStream);
m_hStream=0;
SetIsPlayingStreamingCDMusic(false);
SetIsPlayingStreamingGameMusic(false);
m_StreamState=eMusicStreamState_Idle;
#else
if (m_musicStreamActive)
{
ma_sound_stop(&m_musicStream);
ma_sound_uninit(&m_musicStream);
m_musicStreamActive = false;
}
SetIsPlayingStreamingCDMusic(false);
SetIsPlayingStreamingGameMusic(false);
m_StreamState = eMusicStreamState_Idle;
#endif
break;
case eMusicStreamState_Stopping:
break;
case eMusicStreamState_Play:
break;
case eMusicStreamState_Playing:
#ifdef _WINDOWS64
{
static int frameCount = 0;
if (frameCount++ % 60 == 0)
{
if (m_musicStreamActive)
{
bool isPlaying = ma_sound_is_playing(&m_musicStream);
float vol = ma_sound_get_volume(&m_musicStream);
bool isAtEnd = ma_sound_at_end(&m_musicStream);
}
}
}
#endif
if(GetIsPlayingStreamingGameMusic())
{
//if(m_MusicInfo.pCue!=NULL)
@@ -1513,6 +2128,7 @@ void SoundEngine::playMusicUpdate()
}
// volume change required?
#ifndef _WINDOWS64
if(fMusicVol!=getMasterMusicVolume())
{
fMusicVol=getMasterMusicVolume();
@@ -1520,6 +2136,14 @@ void SoundEngine::playMusicUpdate()
//AIL_set_sample_3D_position( hSample, m_StreamingAudioInfo.x, m_StreamingAudioInfo.y, m_StreamingAudioInfo.z );
AIL_set_sample_volume_levels( hSample, fMusicVol, fMusicVol);
}
#else
if (m_musicStreamActive)
{
float finalVolume = m_StreamingAudioInfo.volume * fMusicVol;
ma_sound_set_volume(&m_musicStream, finalVolume);
}
#endif
}
}
else
@@ -1527,6 +2151,7 @@ void SoundEngine::playMusicUpdate()
// Music disc playing - if it's a 3D stream, then set the position - we don't have any streaming audio in the world that moves, so this isn't
// required unless we have more than one listener, and are setting the listening position to the origin and setting a fake position
// for the sound down the z axis
#ifndef _WINDOWS64
if(m_StreamingAudioInfo.bIs3D)
{
if(m_validListenerCount>1)
@@ -1565,6 +2190,40 @@ void SoundEngine::playMusicUpdate()
}
}
}
#else
if (m_StreamingAudioInfo.bIs3D && m_validListenerCount > 1)
{
int iClosestListener = 0;
float fClosestDist = 1e6f;
for (size_t i = 0; i < MAX_LOCAL_PLAYERS; i++)
{
if (m_ListenerA[i].bValid)
{
float dx = m_StreamingAudioInfo.x - m_ListenerA[i].vPosition.x;
float dy = m_StreamingAudioInfo.y - m_ListenerA[i].vPosition.y;
float dz = m_StreamingAudioInfo.z - m_ListenerA[i].vPosition.z;
float dist = sqrtf(dx*dx + dy*dy + dz*dz);
if (dist < fClosestDist)
{
fClosestDist = dist;
iClosestListener = i;
}
}
}
float relX = m_StreamingAudioInfo.x - m_ListenerA[iClosestListener].vPosition.x;
float relY = m_StreamingAudioInfo.y - m_ListenerA[iClosestListener].vPosition.y;
float relZ = m_StreamingAudioInfo.z - m_ListenerA[iClosestListener].vPosition.z;
if (m_musicStreamActive)
{
ma_sound_set_position(&m_musicStream, relX, relY, relZ);
}
}
}
#endif
break;
@@ -1616,7 +2275,7 @@ void SoundEngine::playMusicUpdate()
}
// check the status of the stream - this is for when a track completes rather than is stopped by the user action
#ifndef _WINDOWS64
if(m_hStream!=0)
{
if(AIL_stream_status(m_hStream)==SMP_DONE ) // SMP_DONE
@@ -1629,6 +2288,7 @@ void SoundEngine::playMusicUpdate()
m_StreamState=eMusicStreamState_Completed;
}
}
#endif
}
@@ -1657,7 +2317,7 @@ char *SoundEngine::ConvertSoundPathToName(const wstring& name, bool bConvertSpac
#endif
#ifndef _WINDOWS64
F32 AILCALLBACK custom_falloff_function (HSAMPLE S,
F32 distance,
F32 rolloff_factor,
@@ -1680,3 +2340,4 @@ F32 AILCALLBACK custom_falloff_function (HSAMPLE S,
return result;
}
#endif