Skip to content
Permalink
Browse files

Add ability to play sounds from raw data to playSound(3D) (#1234)

* Add raw data option to playSound(3D)

* Restore const to SString

* Get rid of regex for URL parsing

* Change Substr and memcmp
  • Loading branch information
Dezash committed Mar 23, 2020
1 parent b59d670 commit f215edb545212de7aba2a42acaa4389649b82c5a
@@ -123,14 +123,22 @@ void CClientSoundManager::SetDimension(unsigned short usDimension)
m_usDimension = usDimension;
}

CClientSound* CClientSoundManager::PlaySound2D(const SString& strSound, bool bIsURL, bool bLoop, bool bThrottle)
CClientSound* CClientSoundManager::PlaySound2D(const SString& strSound, bool bIsURL, bool bIsRawData, bool bLoop, bool bThrottle)
{
CClientSound* pSound = new CClientSound(m_pClientManager, INVALID_ELEMENT_ID);
if (bIsURL)
{
pSound->PlayStream(strSound, bLoop, bThrottle);
return pSound;
}
else if (bIsRawData)
{
size_t size = strSound.size();
void* pMemory = new char[size];
memcpy(pMemory, strSound.data(), size);
if (pSound->Play((void*)pMemory, size, bLoop))
return pSound;
}
else if (pSound->Play(strSound, bLoop))
return pSound;

@@ -149,7 +157,7 @@ CClientSound* CClientSoundManager::PlaySound2D(void* pMemory, unsigned int uiLen
return NULL;
}

CClientSound* CClientSoundManager::PlaySound3D(const SString& strSound, bool bIsURL, const CVector& vecPosition, bool bLoop, bool bThrottle)
CClientSound* CClientSoundManager::PlaySound3D(const SString& strSound, bool bIsURL, bool bIsRawData, const CVector& vecPosition, bool bLoop, bool bThrottle)
{
CClientSound* pSound = new CClientSound(m_pClientManager, INVALID_ELEMENT_ID);

@@ -159,6 +167,17 @@ CClientSound* CClientSoundManager::PlaySound3D(const SString& strSound, bool bIs
pSound->SetPosition(vecPosition);
return pSound;
}
else if (bIsRawData)
{
size_t size = strSound.size();
void* pMemory = new char[size];
memcpy(pMemory, strSound.data(), size);
if (pSound->Play((void*)pMemory, size, bLoop))
{
pSound->SetPosition(vecPosition);
return pSound;
}
}
else if (pSound->Play3D(strSound, bLoop))
{
pSound->SetPosition(vecPosition);
@@ -27,9 +27,9 @@ class CClientSoundManager

void SetDimension(unsigned short usDimension);

CClientSound* PlaySound2D(const SString& strSound, bool bIsURL, bool bLoop, bool bThrottle);
CClientSound* PlaySound2D(const SString& strSound, bool bIsURL, bool bIsRawData, bool bLoop, bool bThrottle);
CClientSound* PlaySound2D(void* pMemory, unsigned int uiLength, bool bLoop);
CClientSound* PlaySound3D(const SString& strSound, bool bIsURL, const CVector& vecPosition, bool bLoop, bool bThrottle);
CClientSound* PlaySound3D(const SString& strSound, bool bIsURL, bool bIsRawData, const CVector& vecPosition, bool bLoop, bool bThrottle);
CClientSound* PlaySound3D(void* pMemory, unsigned int uiLength, const CVector& vecPosition, bool bLoop);

CClientSound* PlayGTASFX(eAudioLookupIndex containerIndex, int iBankIndex, int iAudioIndex, bool bLoop = false);
@@ -7828,18 +7828,18 @@ CClientEffect* CStaticFunctionDefinitions::CreateEffect(CResource& Resource, con
return pFx;
}

CClientSound* CStaticFunctionDefinitions::PlaySound(CResource* pResource, const SString& strSound, bool bIsURL, bool bLoop, bool bThrottle)
CClientSound* CStaticFunctionDefinitions::PlaySound(CResource* pResource, const SString& strSound, bool bIsURL, bool bIsRawData, bool bLoop, bool bThrottle)
{
CClientSound* pSound = m_pSoundManager->PlaySound2D(strSound, bIsURL, bLoop, bThrottle);
CClientSound* pSound = m_pSoundManager->PlaySound2D(strSound, bIsURL, bIsRawData, bLoop, bThrottle);
if (pSound)
pSound->SetParent(pResource->GetResourceDynamicEntity());
return pSound;
}

CClientSound* CStaticFunctionDefinitions::PlaySound3D(CResource* pResource, const SString& strSound, bool bIsURL, const CVector& vecPosition, bool bLoop,
CClientSound* CStaticFunctionDefinitions::PlaySound3D(CResource* pResource, const SString& strSound, bool bIsURL, bool bIsRawData, const CVector& vecPosition, bool bLoop,
bool bThrottle)
{
CClientSound* pSound = m_pSoundManager->PlaySound3D(strSound, bIsURL, vecPosition, bLoop, bThrottle);
CClientSound* pSound = m_pSoundManager->PlaySound3D(strSound, bIsURL, bIsRawData, vecPosition, bLoop, bThrottle);
if (pSound)
pSound->SetParent(pResource->GetResourceDynamicEntity());
return pSound;
@@ -731,8 +731,8 @@ class CStaticFunctionDefinitions
static CClientEffect* CreateEffect(CResource& Resource, const SString& strFxName, const CVector& vecPosition, bool bSoundEnable);

// Sound funcs
static CClientSound* PlaySound(CResource* pResource, const SString& strSound, bool bIsURL, bool bLoop, bool bThrottle);
static CClientSound* PlaySound3D(CResource* pResource, const SString& strSound, bool bIsURL, const CVector& vecPosition, bool bLoop, bool bThrottle);
static CClientSound* PlaySound(CResource* pResource, const SString& strSound, bool bIsURL, bool bIsRawData, bool bLoop, bool bThrottle);
static CClientSound* PlaySound3D(CResource* pResource, const SString& strSound, bool bIsURL, bool bIsRawData, const CVector& vecPosition, bool bLoop, bool bThrottle);
static bool StopSound(CClientSound& Sound);
static bool SetSoundPosition(CClientSound& Sound, double dPosition);
static bool GetSoundPosition(CClientSound& Sound, double& dPosition);
@@ -153,16 +153,23 @@ int CLuaAudioDefs::PlaySound(lua_State* luaVM)
{
SString strFilename;
bool bIsURL = false;
bool bIsRawData = false;
if (CResourceManager::ParseResourcePathInput(strSound, pResource, &strFilename))
strSound = strFilename;
else
bIsURL = true;
{
if ((stricmp(strSound.Left(4), "http") == 0 || stricmp(strSound.Left(3), "ftp") == 0)
&& (strSound.length() <= 2048 || strSound.find('\n') == SString::npos))
bIsURL = true;
else
bIsRawData = true;
}

// ParseResourcePathInput changes pResource in some cases e.g. an invalid resource URL - crun playSound( ":myNotRunningResource/music/track.mp3"
// ) Fixes #6507 - Caz
if (pResource)
{
CClientSound* pSound = CStaticFunctionDefinitions::PlaySound(pResource, strSound, bIsURL, bLoop, bThrottle);
CClientSound* pSound = CStaticFunctionDefinitions::PlaySound(pResource, strSound, bIsURL, bIsRawData, bLoop, bThrottle);
if (pSound)
{
// call onClientSoundStarted
@@ -206,16 +213,23 @@ int CLuaAudioDefs::PlaySound3D(lua_State* luaVM)
{
SString strFilename;
bool bIsURL = false;
bool bIsRawData = false;
if (CResourceManager::ParseResourcePathInput(strSound, pResource, &strFilename))
strSound = strFilename;
else
bIsURL = true;
{
if ((stricmp(strSound.Left(4), "http") == 0 || stricmp(strSound.Left(3), "ftp") == 0)
&& (strSound.length() <= 2048 || strSound.find('\n') == SString::npos))
bIsURL = true;
else
bIsRawData = true;
}

// ParseResourcePathInput changes pResource in some cases e.g. an invalid resource URL - crun playSound( ":myNotRunningResource/music/track.mp3"
// ) Fixes #6507 - Caz
if (pResource)
{
CClientSound* pSound = CStaticFunctionDefinitions::PlaySound3D(pResource, strSound, bIsURL, vecPosition, bLoop, bThrottle);
CClientSound* pSound = CStaticFunctionDefinitions::PlaySound3D(pResource, strSound, bIsURL, bIsRawData, vecPosition, bLoop, bThrottle);
if (pSound)
{
// call onClientSoundStarted

0 comments on commit f215edb

Please sign in to comment.
You can’t perform that action at this time.