2026-06-04 10:46:01 -07:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
#include "SoundGroup.h"
|
2025-08-10 10:59:02 -07:00
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
#include "graphics/Camera.h"
|
|
|
|
|
#include "graphics/GameView.h"
|
2010-07-04 03:15:53 -07:00
|
|
|
#include "lib/rand.h"
|
2025-07-16 11:25:46 -07:00
|
|
|
#include "maths/Vector2D.h"
|
2009-04-11 10:00:39 -07:00
|
|
|
#include "ps/CLogger.h"
|
Tweak sound distance attenuation & add configurability
Following D3108 / 876f6d5e50, sounds are attenuated by their actual
distance. However, as noted by players on A24, the dropoff is stark and,
when zoomed out, can easily result in not hearing things that are
happening in the middle of the screen.
The new default settings almost double the max-range, change the minimum
range to have greater dynamic range, and reduce the stereo depth
slightly to better match the default camera FOV.
These are stored per sound-grounp, possibly allowing future tweaks on a
per-soundgroup basis, and are configurable.
Tested by: Imarok
Discussed over mail with: Samulis, Porru
Differential Revision: https://code.wildfiregames.com/D3612
This was SVN commit r25547.
2021-05-24 23:19:25 -07:00
|
|
|
#include "ps/ConfigDB.h"
|
2012-09-01 11:23:13 -07:00
|
|
|
#include "ps/CStr.h"
|
2010-07-04 03:15:53 -07:00
|
|
|
#include "ps/Filesystem.h"
|
2019-04-25 15:37:15 -07:00
|
|
|
#include "ps/Game.h"
|
2011-04-09 17:54:15 -07:00
|
|
|
#include "ps/Util.h"
|
2012-08-14 17:10:44 -07:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2019-04-25 15:37:15 -07:00
|
|
|
#include "simulation2/components/ICmpVisual.h"
|
|
|
|
|
#include "simulation2/system/Component.h"
|
2012-08-14 17:10:44 -07:00
|
|
|
#include "soundmanager/items/ISoundItem.h"
|
|
|
|
|
#include "soundmanager/SoundManager.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
extern CGame *g_Game;
|
|
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
#if CONFIG2_AUDIO
|
2020-12-12 16:04:17 -08:00
|
|
|
|
2025-08-10 10:59:02 -07:00
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "lib/status.h"
|
|
|
|
|
#include "lib/utf8.h"
|
|
|
|
|
#include "maths/Matrix3D.h"
|
|
|
|
|
#include "maths/Vector3D.h"
|
|
|
|
|
#include "ps/Errors.h"
|
|
|
|
|
#include "ps/XMB/XMBData.h"
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
#include "soundmanager/ISoundManager.h"
|
2025-10-10 11:49:37 -07:00
|
|
|
#include "soundmanager/data/SoundData.h"
|
2025-08-10 10:59:02 -07:00
|
|
|
|
|
|
|
|
#include <AL/al.h>
|
|
|
|
|
#include <cmath>
|
2026-06-04 10:46:01 -07:00
|
|
|
#include <numbers>
|
2025-08-10 10:59:02 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2021-01-19 14:40:56 -08:00
|
|
|
constexpr ALfloat DEFAULT_ROLLOFF = 0.5f;
|
|
|
|
|
constexpr ALfloat MAX_ROLLOFF = 0.7f;
|
2020-12-12 16:04:17 -08:00
|
|
|
|
2020-11-30 07:52:16 -08:00
|
|
|
/**
|
|
|
|
|
* Low randomness, quite-a-lot-faster-than-std::mt19937 random number generator.
|
|
|
|
|
* It matches the interface of UniformRandomBitGenerator for use in std::shuffle.
|
|
|
|
|
*/
|
|
|
|
|
class CFastRand
|
2019-01-02 14:31:46 -08:00
|
|
|
{
|
2020-11-30 07:52:16 -08:00
|
|
|
public:
|
|
|
|
|
using result_type = u32;
|
2019-04-25 15:37:15 -07:00
|
|
|
|
2020-11-30 07:52:16 -08:00
|
|
|
constexpr static result_type min() { return 0; }
|
|
|
|
|
constexpr static result_type max() { return 0xFFFF; }
|
|
|
|
|
|
|
|
|
|
static result_type Rand(result_type& seed)
|
|
|
|
|
{
|
|
|
|
|
// This is a mixed linear congruential random number generator.
|
|
|
|
|
// The magic numbers are chosen so that they generate pseudo random numbers over a big enough period (0xFFFF).
|
|
|
|
|
seed = 214013 * seed + 2531011;
|
|
|
|
|
return (seed >> 16) & max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static float RandFloat(result_type& seed, float min, float max)
|
|
|
|
|
{
|
|
|
|
|
return (static_cast<float>(Rand(seed)) / (0xFFFF)) * (max - min) + min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CFastRand() {};
|
|
|
|
|
CFastRand(result_type init) : m_Seed(init) {};
|
|
|
|
|
|
|
|
|
|
result_type operator()()
|
|
|
|
|
{
|
|
|
|
|
return Rand(m_Seed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result_type m_Seed;
|
|
|
|
|
};
|
2019-01-02 14:31:46 -08:00
|
|
|
#endif
|
2009-04-11 10:00:39 -07:00
|
|
|
|
|
|
|
|
void CSoundGroup::SetGain(float gain)
|
|
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
m_Gain = std::min(gain, 1.0f);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSoundGroup::SetDefaultValues()
|
|
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
m_CurrentSoundIndex = 0;
|
2009-04-11 10:00:39 -07:00
|
|
|
m_Flags = 0;
|
2019-01-02 14:31:46 -08:00
|
|
|
m_CurTime = 0.f;
|
|
|
|
|
m_Gain = 0.7f;
|
|
|
|
|
m_Pitch = 1.f;
|
|
|
|
|
m_Priority = 60.f;
|
2009-04-11 10:00:39 -07:00
|
|
|
m_PitchUpper = 1.1f;
|
|
|
|
|
m_PitchLower = 0.9f;
|
2019-01-02 14:31:46 -08:00
|
|
|
m_GainUpper = 1.f;
|
2009-04-11 10:00:39 -07:00
|
|
|
m_GainLower = 0.8f;
|
2019-01-02 14:31:46 -08:00
|
|
|
m_ConeOuterGain = 0.f;
|
|
|
|
|
m_ConeInnerAngle = 360.f;
|
|
|
|
|
m_ConeOuterAngle = 360.f;
|
|
|
|
|
m_Decay = 3.f;
|
2019-04-25 15:37:15 -07:00
|
|
|
m_Seed = 0;
|
2019-01-02 14:31:46 -08:00
|
|
|
m_IntensityThreshold = 3.f;
|
Tweak sound distance attenuation & add configurability
Following D3108 / 876f6d5e50, sounds are attenuated by their actual
distance. However, as noted by players on A24, the dropoff is stark and,
when zoomed out, can easily result in not hearing things that are
happening in the middle of the screen.
The new default settings almost double the max-range, change the minimum
range to have greater dynamic range, and reduce the stereo depth
slightly to better match the default camera FOV.
These are stored per sound-grounp, possibly allowing future tweaks on a
per-soundgroup basis, and are configurable.
Tested by: Imarok
Discussed over mail with: Samulis, Porru
Differential Revision: https://code.wildfiregames.com/D3612
This was SVN commit r25547.
2021-05-24 23:19:25 -07:00
|
|
|
|
2025-01-29 07:38:52 -08:00
|
|
|
m_MinDist = CConfigDB::GetIfInitialised("sound.mindistance", 1.f);
|
|
|
|
|
m_MaxDist = CConfigDB::GetIfInitialised("sound.maxdistance", 350.f);
|
Tweak sound distance attenuation & add configurability
Following D3108 / 876f6d5e50, sounds are attenuated by their actual
distance. However, as noted by players on A24, the dropoff is stark and,
when zoomed out, can easily result in not hearing things that are
happening in the middle of the screen.
The new default settings almost double the max-range, change the minimum
range to have greater dynamic range, and reduce the stereo depth
slightly to better match the default camera FOV.
These are stored per sound-grounp, possibly allowing future tweaks on a
per-soundgroup basis, and are configurable.
Tested by: Imarok
Discussed over mail with: Samulis, Porru
Differential Revision: https://code.wildfiregames.com/D3612
This was SVN commit r25547.
2021-05-24 23:19:25 -07:00
|
|
|
// This is more than the default camera FOV: for now, our soundscape is not realistic anyways.
|
2026-06-04 10:46:01 -07:00
|
|
|
m_MaxStereoAngle = CConfigDB::GetIfInitialised("sound.maxstereoangle", std::numbers::pi_v<float> / 6.f);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSoundGroup::CSoundGroup()
|
|
|
|
|
{
|
|
|
|
|
SetDefaultValues();
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
CSoundGroup::CSoundGroup(const VfsPath& pathnameXML)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
SetDefaultValues();
|
2009-11-03 13:46:35 -08:00
|
|
|
LoadSoundGroup(pathnameXML);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-10 11:49:37 -07:00
|
|
|
CSoundGroup::~CSoundGroup()
|
|
|
|
|
{
|
|
|
|
|
// clean up all the handles from this group.
|
|
|
|
|
ReleaseGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 03:29:47 -07:00
|
|
|
float CSoundGroup::RadiansOffCenter([[maybe_unused]] const CVector3D& position,
|
|
|
|
|
[[maybe_unused]] bool& onScreen, [[maybe_unused]] float& itemRollOff)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2021-03-04 09:36:24 -08:00
|
|
|
#if !CONFIG2_AUDIO
|
|
|
|
|
return 0.f;
|
|
|
|
|
#else
|
2019-01-02 14:31:46 -08:00
|
|
|
const int screenWidth = g_Game->GetView()->GetCamera()->GetViewPort().m_Width;
|
|
|
|
|
const int screenHeight = g_Game->GetView()->GetCamera()->GetViewPort().m_Height;
|
|
|
|
|
const float xBufferSize = screenWidth * 0.1f;
|
|
|
|
|
const float yBufferSize = 15.f;
|
Tweak sound distance attenuation & add configurability
Following D3108 / 876f6d5e50, sounds are attenuated by their actual
distance. However, as noted by players on A24, the dropoff is stark and,
when zoomed out, can easily result in not hearing things that are
happening in the middle of the screen.
The new default settings almost double the max-range, change the minimum
range to have greater dynamic range, and reduce the stereo depth
slightly to better match the default camera FOV.
These are stored per sound-grounp, possibly allowing future tweaks on a
per-soundgroup basis, and are configurable.
Tested by: Imarok
Discussed over mail with: Samulis, Porru
Differential Revision: https://code.wildfiregames.com/D3612
This was SVN commit r25547.
2021-05-24 23:19:25 -07:00
|
|
|
const float radianCap = m_MaxStereoAngle;
|
2015-12-31 03:45:13 -08:00
|
|
|
|
2024-11-17 01:17:06 -08:00
|
|
|
const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)};
|
2015-12-31 03:45:13 -08:00
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
onScreen = true;
|
2019-01-02 14:31:46 -08:00
|
|
|
float answer = 0.f;
|
2024-11-17 01:17:06 -08:00
|
|
|
if (screenPos.X < -xBufferSize)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2012-08-14 17:10:44 -07:00
|
|
|
onScreen = false;
|
|
|
|
|
answer = -radianCap;
|
|
|
|
|
}
|
2024-11-17 01:17:06 -08:00
|
|
|
else if (screenPos.X > screenWidth + xBufferSize)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
|
|
|
|
onScreen = false;
|
|
|
|
|
answer = radianCap;
|
|
|
|
|
}
|
2013-05-30 18:03:07 -07:00
|
|
|
else
|
|
|
|
|
{
|
2024-11-17 01:17:06 -08:00
|
|
|
if (screenPos.X < 0 || screenPos.X > screenWidth)
|
2020-12-12 16:04:17 -08:00
|
|
|
itemRollOff = MAX_ROLLOFF;
|
2019-01-02 14:31:46 -08:00
|
|
|
|
2024-11-17 01:17:06 -08:00
|
|
|
answer = radianCap * (screenPos.X * 2 / screenWidth - 1);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-01 09:58:35 -08:00
|
|
|
if (screenPos.Y < -yBufferSize)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
|
|
|
|
onScreen = false;
|
|
|
|
|
}
|
2024-11-17 01:17:06 -08:00
|
|
|
else if (screenPos.Y > screenHeight + yBufferSize)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
|
|
|
|
onScreen = false;
|
|
|
|
|
}
|
2024-11-17 01:17:06 -08:00
|
|
|
else if (screenPos.Y < 0 || screenPos.Y > screenHeight)
|
2013-05-30 18:03:07 -07:00
|
|
|
{
|
2020-12-12 16:04:17 -08:00
|
|
|
itemRollOff = MAX_ROLLOFF;
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
return answer;
|
2021-03-04 09:36:24 -08:00
|
|
|
#endif // !CONFIG2_AUDIO
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2025-05-21 03:29:47 -07:00
|
|
|
void CSoundGroup::UploadPropertiesAndPlay([[maybe_unused]] size_t index,
|
|
|
|
|
[[maybe_unused]] const CVector3D& position, [[maybe_unused]] entity_id_t source)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2025-05-21 03:29:47 -07:00
|
|
|
#if CONFIG2_AUDIO
|
2018-02-24 10:13:03 -08:00
|
|
|
if (!g_SoundManager)
|
2013-06-16 19:30:40 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool isOnscreen = false;
|
2020-12-12 16:04:17 -08:00
|
|
|
ALfloat itemRollOff = DEFAULT_ROLLOFF;
|
2018-02-24 10:13:03 -08:00
|
|
|
float offset = RadiansOffCenter(position, isOnscreen, itemRollOff);
|
2019-01-02 14:31:46 -08:00
|
|
|
|
2018-02-24 10:13:03 -08:00
|
|
|
if (!isOnscreen && !TestFlag(eDistanceless) && !TestFlag(eOmnipresent))
|
2013-06-16 19:30:40 -07:00
|
|
|
return;
|
|
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
if (m_SoundGroups.empty())
|
2013-06-16 19:30:40 -07:00
|
|
|
Reload();
|
|
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
if (m_SoundGroups.size() <= index)
|
2013-06-16 19:30:40 -07:00
|
|
|
return;
|
|
|
|
|
|
2025-10-10 11:49:37 -07:00
|
|
|
CSoundData* sndData = m_SoundGroups[index];
|
2019-01-02 14:31:46 -08:00
|
|
|
if (!sndData)
|
2013-06-16 19:30:40 -07:00
|
|
|
return;
|
|
|
|
|
|
2018-02-24 10:13:03 -08:00
|
|
|
ISoundItem* hSound = static_cast<CSoundManager*>(g_SoundManager)->ItemForEntity(source, sndData);
|
2019-01-02 14:31:46 -08:00
|
|
|
if (!hSound)
|
2013-06-16 19:30:40 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!TestFlag(eOmnipresent))
|
2012-11-21 19:20:51 -08:00
|
|
|
{
|
2013-06-16 19:30:40 -07:00
|
|
|
CVector3D origin = g_Game->GetView()->GetCamera()->GetOrientation().GetTranslation();
|
2018-02-24 10:13:03 -08:00
|
|
|
float itemDist = (position - origin).Length();
|
2012-08-14 17:10:44 -07:00
|
|
|
|
2013-06-16 19:30:40 -07:00
|
|
|
if (TestFlag(eDistanceless))
|
|
|
|
|
itemRollOff = 0;
|
2015-12-31 03:45:13 -08:00
|
|
|
|
2018-02-24 10:13:03 -08:00
|
|
|
if (sndData->IsStereo())
|
2025-10-10 11:49:37 -07:00
|
|
|
LOGWARNING("OpenAL: stereo sounds can't be positioned: %s", sndData->GetFileName().string8());
|
2013-06-16 19:30:40 -07:00
|
|
|
|
2020-12-12 16:04:17 -08:00
|
|
|
hSound->SetLocation(CVector3D(itemDist * sin(offset), 0, -itemDist * cos(offset)));
|
Tweak sound distance attenuation & add configurability
Following D3108 / 876f6d5e50, sounds are attenuated by their actual
distance. However, as noted by players on A24, the dropoff is stark and,
when zoomed out, can easily result in not hearing things that are
happening in the middle of the screen.
The new default settings almost double the max-range, change the minimum
range to have greater dynamic range, and reduce the stereo depth
slightly to better match the default camera FOV.
These are stored per sound-grounp, possibly allowing future tweaks on a
per-soundgroup basis, and are configurable.
Tested by: Imarok
Discussed over mail with: Samulis, Porru
Differential Revision: https://code.wildfiregames.com/D3612
This was SVN commit r25547.
2021-05-24 23:19:25 -07:00
|
|
|
hSound->SetRollOff(itemRollOff, m_MinDist, m_MaxDist);
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
2013-06-16 19:30:40 -07:00
|
|
|
|
2019-04-25 15:37:15 -07:00
|
|
|
CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), source);
|
|
|
|
|
if (cmpVisual)
|
|
|
|
|
m_Seed = cmpVisual->GetActorSeed();
|
|
|
|
|
|
2020-11-30 07:52:16 -08:00
|
|
|
hSound->SetPitch(TestFlag(eRandPitch) ? CFastRand::RandFloat(m_Seed, m_PitchLower, m_PitchUpper) : m_Pitch);
|
2013-06-16 19:30:40 -07:00
|
|
|
|
|
|
|
|
if (TestFlag(eRandGain))
|
2020-11-30 07:52:16 -08:00
|
|
|
m_Gain = CFastRand::RandFloat(m_Seed, m_GainLower, m_GainUpper);
|
2013-06-16 19:30:40 -07:00
|
|
|
|
|
|
|
|
hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain);
|
2018-02-24 10:13:03 -08:00
|
|
|
static_cast<CSoundManager*>(g_SoundManager)->PlayGroupItem(hSound, m_Gain);
|
2012-08-31 12:08:41 -07:00
|
|
|
#endif // !CONFIG2_AUDIO
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
static void HandleError(const std::wstring& message, const VfsPath& pathname, Status err)
|
2010-05-01 11:36:24 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
// Open failed because sound is disabled (don't log this)
|
2012-08-14 17:10:44 -07:00
|
|
|
if (err == ERR::AGAIN)
|
2019-01-02 14:31:46 -08:00
|
|
|
return;
|
|
|
|
|
|
2022-04-12 10:39:05 -07:00
|
|
|
LOGERROR("%s: pathname=%s, error=%s", utf8_from_wstring(message), pathname.string8(), GetStatusAsString(err).c_str());
|
2010-05-01 11:36:24 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-01 06:22:28 -08:00
|
|
|
void CSoundGroup::PlayNext(const CVector3D& position, entity_id_t source)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (m_Filenames.empty())
|
2012-08-14 17:10:44 -07:00
|
|
|
return;
|
2015-12-31 03:45:13 -08:00
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
m_CurrentSoundIndex = rand(0, m_Filenames.size());
|
|
|
|
|
UploadPropertiesAndPlay(m_CurrentSoundIndex, position, source);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSoundGroup::Reload()
|
|
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
m_CurrentSoundIndex = 0;
|
2012-08-31 12:08:41 -07:00
|
|
|
#if CONFIG2_AUDIO
|
2025-10-10 11:49:37 -07:00
|
|
|
ReleaseGroup();
|
2012-08-14 17:10:44 -07:00
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
if (!g_SoundManager)
|
|
|
|
|
return;
|
2012-08-14 17:10:44 -07:00
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
for (const std::wstring& filename : m_Filenames)
|
|
|
|
|
{
|
|
|
|
|
VfsPath absolutePath = m_Filepath / filename;
|
2025-10-10 11:49:37 -07:00
|
|
|
CSoundData* itemData = CSoundData::SoundDataFromFile(absolutePath);
|
2019-01-02 14:31:46 -08:00
|
|
|
if (!itemData)
|
|
|
|
|
HandleError(L"error loading sound", absolutePath, ERR::FAIL);
|
|
|
|
|
else
|
2025-10-10 11:49:37 -07:00
|
|
|
m_SoundGroups.push_back(itemData->IncrementCount());
|
2012-08-31 12:08:41 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
|
|
|
|
|
if (TestFlag(eRandOrder))
|
2020-11-30 07:52:16 -08:00
|
|
|
std::shuffle(m_SoundGroups.begin(), m_SoundGroups.end(), CFastRand(m_Seed));
|
2019-01-02 14:31:46 -08:00
|
|
|
#endif
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-10 11:49:37 -07:00
|
|
|
void CSoundGroup::ReleaseGroup()
|
|
|
|
|
{
|
|
|
|
|
#if CONFIG2_AUDIO
|
|
|
|
|
for (CSoundData* soundGroup : m_SoundGroups)
|
|
|
|
|
CSoundData::ReleaseSoundData(soundGroup);
|
|
|
|
|
|
|
|
|
|
m_SoundGroups.clear();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
void CSoundGroup::Update(float /*TimeSinceLastFrame*/)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
bool CSoundGroup::LoadSoundGroup(const VfsPath& pathnameXML)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
CXeromyces XeroFile;
|
2015-06-07 14:56:52 -07:00
|
|
|
if (XeroFile.Load(g_VFS, pathnameXML, "sound_group") != PSRETURN_OK)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2012-08-21 09:44:54 -07:00
|
|
|
HandleError(L"error loading file", pathnameXML, ERR::FAIL);
|
2009-04-11 10:00:39 -07:00
|
|
|
return false;
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
|
|
|
|
|
#define EL(x) int el_##x = XeroFile.GetElementID(#x)
|
2009-04-11 10:00:39 -07:00
|
|
|
EL(soundgroup);
|
|
|
|
|
EL(gain);
|
|
|
|
|
EL(looping);
|
|
|
|
|
EL(omnipresent);
|
2013-06-17 08:46:28 -07:00
|
|
|
EL(heardby);
|
2012-08-14 17:10:44 -07:00
|
|
|
EL(distanceless);
|
2009-04-11 10:00:39 -07:00
|
|
|
EL(pitch);
|
|
|
|
|
EL(priority);
|
|
|
|
|
EL(randorder);
|
|
|
|
|
EL(randgain);
|
|
|
|
|
EL(randpitch);
|
|
|
|
|
EL(conegain);
|
|
|
|
|
EL(coneinner);
|
|
|
|
|
EL(coneouter);
|
|
|
|
|
EL(sound);
|
|
|
|
|
EL(gainupper);
|
|
|
|
|
EL(gainlower);
|
|
|
|
|
EL(pitchupper);
|
|
|
|
|
EL(pitchlower);
|
|
|
|
|
EL(path);
|
|
|
|
|
EL(threshold);
|
|
|
|
|
EL(decay);
|
2019-01-02 14:31:46 -08:00
|
|
|
#undef EL
|
2009-04-11 10:00:39 -07:00
|
|
|
|
|
|
|
|
XMBElement root = XeroFile.GetRoot();
|
|
|
|
|
|
|
|
|
|
if (root.GetNodeName() != el_soundgroup)
|
|
|
|
|
{
|
2025-04-10 21:24:32 -07:00
|
|
|
LOGERROR("Invalid SoundGroup format (unrecognized root element '%s')", XeroFile.GetElementString(root.GetNodeName()));
|
2009-04-11 10:00:39 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-31 03:45:13 -08:00
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
XERO_ITER_EL(root, child)
|
|
|
|
|
{
|
2015-12-31 03:45:13 -08:00
|
|
|
int child_name = child.GetNodeName();
|
|
|
|
|
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child_name == el_gain)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
SetGain(child.GetText().ToFloat());
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_looping)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2009-04-11 10:00:39 -07:00
|
|
|
SetFlag(eLoop);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_omnipresent)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2009-04-11 10:00:39 -07:00
|
|
|
SetFlag(eOmnipresent);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_heardby)
|
2013-06-16 19:30:40 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().FindInsensitive("owner") == 0)
|
2013-06-16 19:30:40 -07:00
|
|
|
SetFlag(eOwnerOnly);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_distanceless)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2012-08-14 17:10:44 -07:00
|
|
|
SetFlag(eDistanceless);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_pitch)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_Pitch = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_priority)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_Priority = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_randorder)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2009-04-11 10:00:39 -07:00
|
|
|
SetFlag(eRandOrder);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_randgain)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2009-04-11 10:00:39 -07:00
|
|
|
SetFlag(eRandGain);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_gainupper)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_GainUpper = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_gainlower)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_GainLower = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_randpitch)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
if (child.GetText().ToInt() == 1)
|
2009-04-11 10:00:39 -07:00
|
|
|
SetFlag(eRandPitch);
|
|
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_pitchupper)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_PitchUpper = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_pitchlower)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_PitchLower = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_conegain)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_ConeOuterGain = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_coneinner)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_ConeInnerAngle = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_coneouter)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
this->m_ConeOuterAngle = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_sound)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
this->m_Filenames.push_back(child.GetText().FromUTF8());
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_path)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2019-01-02 14:31:46 -08:00
|
|
|
m_Filepath = child.GetText().FromUTF8();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_threshold)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
m_IntensityThreshold = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
2019-01-02 14:31:46 -08:00
|
|
|
else if (child_name == el_decay)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2011-02-17 12:08:20 -08:00
|
|
|
m_Decay = child.GetText().ToFloat();
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|