2006-09-30 13:13:11 -07:00
|
|
|
/**
|
|
|
|
|
* =========================================================================
|
|
|
|
|
* File : SoundGroup.cpp
|
|
|
|
|
* Project : 0 A.D.
|
|
|
|
|
* Description : Loads up a group of sound files with shared properties,
|
|
|
|
|
* and provides a simple interface for playing them.
|
|
|
|
|
* =========================================================================
|
|
|
|
|
*/
|
2007-01-13 08:00:52 -08:00
|
|
|
|
2007-05-08 08:11:53 -07:00
|
|
|
// license: GPL; see sound/license.txt
|
2007-01-13 08:00:52 -08:00
|
|
|
|
2006-09-30 13:13:11 -07:00
|
|
|
#include "precompiled.h"
|
2007-05-08 08:11:53 -07:00
|
|
|
#include "SoundGroup.h"
|
2006-09-30 13:13:11 -07:00
|
|
|
|
2006-09-30 14:36:19 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2006-09-30 13:13:11 -07:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
2007-05-17 17:14:26 -07:00
|
|
|
#include "lib/rand.h"
|
2006-09-30 13:13:11 -07:00
|
|
|
|
|
|
|
|
#define LOG_CATEGORY "audio"
|
|
|
|
|
|
2006-10-17 00:55:35 -07:00
|
|
|
|
2006-09-30 13:13:11 -07:00
|
|
|
CSoundGroup::CSoundGroup()
|
|
|
|
|
{
|
2006-10-17 00:55:35 -07:00
|
|
|
m_index = 0;
|
2006-09-30 13:13:11 -07:00
|
|
|
m_Flags = 0;
|
2007-01-13 08:00:52 -08:00
|
|
|
m_Intensity = 0;
|
|
|
|
|
m_CurTime = 0.0f;
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSoundGroup::CSoundGroup(const char *XMLfile)
|
|
|
|
|
{
|
2006-10-17 00:55:35 -07:00
|
|
|
m_index = 0;
|
2006-09-30 13:13:11 -07:00
|
|
|
m_Flags = 0;
|
2007-01-13 08:00:52 -08:00
|
|
|
m_Intensity = 0;
|
|
|
|
|
m_CurTime = 0.0f;
|
2006-09-30 13:13:11 -07:00
|
|
|
LoadSoundGroup(XMLfile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSoundGroup::~CSoundGroup()
|
|
|
|
|
{
|
|
|
|
|
// clean up all the handles from this group.
|
|
|
|
|
ReleaseGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-21 13:06:30 -07:00
|
|
|
void CSoundGroup::PlayNext(const CVector3D& position)
|
2006-09-30 13:13:11 -07:00
|
|
|
{
|
2008-06-21 13:06:30 -07:00
|
|
|
// interface/UI sounds should always be played at the listener's
|
|
|
|
|
// position, which is achieved by setting position to 0 and
|
|
|
|
|
// having that treated as relative to the listener.
|
|
|
|
|
float x = 0.0f, y = 0.0f, z = 0.0f;
|
|
|
|
|
bool relative = true;
|
|
|
|
|
if(!TestFlag(eOmnipresent))
|
|
|
|
|
{
|
|
|
|
|
x = position.X;
|
|
|
|
|
y = position.Y;
|
|
|
|
|
z = position.Z;
|
|
|
|
|
relative = false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
if(m_Intensity >= m_IntensityThreshold)
|
|
|
|
|
{
|
|
|
|
|
if(!is_playing(m_hReplacement))
|
|
|
|
|
{
|
|
|
|
|
// load up replacement file
|
|
|
|
|
m_hReplacement = snd_open(m_filepath + m_intensity_file);
|
2008-07-22 22:39:54 -07:00
|
|
|
if(m_hReplacement < 0) // one cause: sound is disabled
|
2008-06-16 11:33:47 -07:00
|
|
|
return;
|
2007-01-13 08:00:52 -08:00
|
|
|
snd_set_gain(m_hReplacement, m_Gain);
|
|
|
|
|
snd_set_pitch(m_hReplacement, m_Pitch);
|
2008-06-21 13:06:30 -07:00
|
|
|
snd_set_pos(m_hReplacement, x, y, z, relative);
|
2007-01-13 08:00:52 -08:00
|
|
|
|
|
|
|
|
//check for randomization of pitch and gain
|
|
|
|
|
if(TestFlag(eRandPitch))
|
2007-01-19 20:23:08 -08:00
|
|
|
snd_set_pitch(m_hReplacement, (float)((rand(m_PitchLower * 100.0f, m_PitchUpper * 100.0f) / 100.0f)));
|
2007-01-13 08:00:52 -08:00
|
|
|
if(TestFlag(eRandGain))
|
2007-01-19 20:23:08 -08:00
|
|
|
snd_set_gain(m_hReplacement, (float)((rand(m_GainLower * 100.0f, m_GainUpper * 100.0f) / 100.0f)));
|
2007-01-13 08:00:52 -08:00
|
|
|
|
|
|
|
|
snd_play(m_hReplacement, m_Priority);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-06-16 11:19:35 -07:00
|
|
|
// if no sounds, return
|
|
|
|
|
if (filenames.size() == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
// try loading on the fly only when we need the sound to see if that fixes release problems...
|
|
|
|
|
if(TestFlag(eRandOrder))
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
m_index = (size_t)rand(0, (size_t)filenames.size());
|
2008-06-21 13:06:30 -07:00
|
|
|
// (note: previously snd_group[m_index] was used in place of hs)
|
|
|
|
|
Handle hs = snd_open(m_filepath + filenames[m_index]);
|
2008-07-22 22:39:54 -07:00
|
|
|
if(hs < 0) // one cause: sound is disabled
|
2008-06-16 11:33:47 -07:00
|
|
|
return;
|
2008-06-21 13:06:30 -07:00
|
|
|
snd_set_gain(hs, m_Gain);
|
|
|
|
|
snd_set_pitch(hs, m_Pitch);
|
|
|
|
|
snd_set_pos(hs, x, y, z, relative);
|
2006-10-17 00:55:35 -07:00
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
//check for randomization of pitch and gain
|
|
|
|
|
if(TestFlag(eRandPitch))
|
2008-06-21 13:06:30 -07:00
|
|
|
snd_set_pitch(hs, (float)((rand(m_PitchLower * 100.0f, m_PitchUpper * 100.0f) / 100.0f)));
|
2007-01-13 08:00:52 -08:00
|
|
|
if(TestFlag(eRandGain))
|
2008-06-21 13:06:30 -07:00
|
|
|
snd_set_gain(hs, (float)((rand(m_GainLower * 100.0f, m_GainUpper * 100.0f) / 100.0f)));
|
|
|
|
|
|
|
|
|
|
snd_play(hs, m_Priority);
|
2007-01-13 08:00:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playtimes[m_index] = 0.0f;
|
2006-09-30 13:13:11 -07:00
|
|
|
m_index++;
|
2007-01-13 08:00:52 -08:00
|
|
|
m_Intensity++;
|
|
|
|
|
if(m_Intensity > m_IntensityThreshold)
|
|
|
|
|
m_Intensity = m_IntensityThreshold;
|
|
|
|
|
|
|
|
|
|
if(m_index >= filenames.size())
|
2006-09-30 13:13:11 -07:00
|
|
|
Reload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSoundGroup::Reload()
|
|
|
|
|
{
|
2008-06-21 13:06:30 -07:00
|
|
|
m_index = 0; // reset our index
|
2006-09-30 13:13:11 -07:00
|
|
|
// get rid of the used handles
|
|
|
|
|
snd_group.clear();
|
2007-01-13 08:00:52 -08:00
|
|
|
// clear out the old timers
|
|
|
|
|
playtimes.clear();
|
2006-09-30 13:13:11 -07:00
|
|
|
//Reload the sounds
|
2007-01-13 08:00:52 -08:00
|
|
|
/*for(size_t i = 0; i < filenames.size(); i++)
|
2006-09-30 13:13:11 -07:00
|
|
|
{
|
2006-10-17 00:55:35 -07:00
|
|
|
string szTemp = m_filepath + filenames[i];
|
2006-09-30 13:13:11 -07:00
|
|
|
Handle temp = snd_open(m_filepath + filenames[i]);
|
|
|
|
|
snd_set_gain(temp, m_Gain);
|
|
|
|
|
snd_set_pitch(temp, m_Pitch);
|
2006-10-17 00:55:35 -07:00
|
|
|
snd_set_cone(temp, m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain);
|
2007-01-13 08:00:52 -08:00
|
|
|
snd_group.push_back(temp);
|
2006-09-30 13:13:11 -07:00
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
}*/
|
|
|
|
|
while(playtimes.size() < filenames.size())
|
|
|
|
|
playtimes.push_back(-1.0f);
|
|
|
|
|
//if(TestFlag(eRandOrder))
|
|
|
|
|
//random_shuffle(snd_group.begin(), snd_group.end());
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSoundGroup::ReleaseGroup()
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = m_index; i<snd_group.size(); i++)
|
2007-01-13 08:00:52 -08:00
|
|
|
{
|
|
|
|
|
//if(!is_playing(snd_group[i]))
|
|
|
|
|
snd_free(snd_group[i]);
|
|
|
|
|
}
|
2006-09-30 13:13:11 -07:00
|
|
|
snd_group.clear();
|
2007-01-13 08:00:52 -08:00
|
|
|
playtimes.clear();
|
|
|
|
|
//if(is_playing(m_hReplacement))
|
|
|
|
|
// snd_free(m_hReplacement);
|
2006-10-17 00:55:35 -07:00
|
|
|
m_index = 0;
|
2006-09-30 13:13:11 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
void CSoundGroup::Update(float TimeSinceLastFrame)
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = 0; i < playtimes.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if(playtimes[i] >= 0.0f)
|
|
|
|
|
playtimes[i] += TimeSinceLastFrame;
|
2006-10-17 00:55:35 -07:00
|
|
|
|
2007-01-13 08:00:52 -08:00
|
|
|
if(playtimes[i] >= m_Decay)
|
|
|
|
|
{
|
|
|
|
|
playtimes[i] = -1.0f;
|
|
|
|
|
m_Intensity--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-17 00:55:35 -07:00
|
|
|
|
2006-09-30 13:13:11 -07:00
|
|
|
bool CSoundGroup::LoadSoundGroup(const char *XMLfile)
|
|
|
|
|
{
|
|
|
|
|
CXeromyces XeroFile;
|
|
|
|
|
if (XeroFile.Load(XMLfile) != PSRETURN_OK)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// adjust the path name for resources if necessary
|
|
|
|
|
//m_Name = XMLfile + directorypath;
|
|
|
|
|
|
|
|
|
|
//Define elements used in XML file
|
2007-05-02 05:07:08 -07:00
|
|
|
#define EL(x) int el_##x = XeroFile.GetElementID(#x)
|
|
|
|
|
#define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
|
2006-09-30 13:13:11 -07:00
|
|
|
EL(soundgroup);
|
|
|
|
|
EL(gain);
|
|
|
|
|
EL(looping);
|
2008-06-21 13:06:30 -07:00
|
|
|
EL(omnipresent);
|
2006-09-30 13:13:11 -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);
|
2006-10-17 00:55:35 -07:00
|
|
|
EL(threshold);
|
2007-01-13 08:00:52 -08:00
|
|
|
EL(decay);
|
2006-10-17 00:55:35 -07:00
|
|
|
EL(replacement);
|
2006-09-30 13:13:11 -07:00
|
|
|
#undef AT
|
|
|
|
|
#undef EL
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
XMBElement root = XeroFile.GetRoot();
|
2006-09-30 13:13:11 -07:00
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
if (root.GetNodeName() != el_soundgroup)
|
2006-09-30 13:13:11 -07:00
|
|
|
{
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, "Invalid SoundGroup format (unrecognised root element '%s')", XeroFile.GetElementString(root.GetNodeName()).c_str());
|
2006-09-30 13:13:11 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XERO_ITER_EL(root, child)
|
|
|
|
|
{
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
int child_name = child.GetNodeName();
|
2006-09-30 13:13:11 -07:00
|
|
|
|
|
|
|
|
if(child_name == el_gain)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_Gain = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_looping)
|
2008-06-21 13:06:30 -07:00
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
if(CStr(child.GetText()).ToInt() == 1)
|
2006-09-30 13:13:11 -07:00
|
|
|
SetFlag(eLoop);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-21 13:06:30 -07:00
|
|
|
if(child_name == el_omnipresent)
|
|
|
|
|
{
|
|
|
|
|
if(CStr(child.GetText()).ToInt() == 1)
|
|
|
|
|
SetFlag(eOmnipresent);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-30 13:13:11 -07:00
|
|
|
if(child_name == el_pitch)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_Pitch = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_priority)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_Priority = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_randorder)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
if(CStr(child.GetText()).ToInt() == 1)
|
2006-09-30 13:13:11 -07:00
|
|
|
SetFlag(eRandOrder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_randgain)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
if(CStr(child.GetText()).ToInt() == 1)
|
2006-09-30 13:13:11 -07:00
|
|
|
SetFlag(eRandGain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_gainupper)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_GainUpper = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_gainlower)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_GainLower = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_randpitch)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
if(CStr(child.GetText()).ToInt() == 1)
|
2006-09-30 13:13:11 -07:00
|
|
|
SetFlag(eRandPitch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_pitchupper)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_PitchUpper = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_pitchlower)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_PitchLower = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_conegain)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_ConeOuterGain = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_coneinner)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_ConeInnerAngle = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_coneouter)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
this->m_ConeOuterAngle = CStr(child.GetText()).ToFloat();
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(child_name == el_sound)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
CStr szTemp(child.GetText());
|
2006-09-30 13:13:11 -07:00
|
|
|
this->filenames.push_back(szTemp);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(child_name == el_path)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
m_filepath = child.GetText();
|
2006-09-30 13:13:11 -07:00
|
|
|
|
|
|
|
|
}
|
2006-10-17 00:55:35 -07:00
|
|
|
if(child_name == el_threshold)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
//m_intensity_threshold = CStr(child.GetText()).ToFloat();
|
|
|
|
|
m_IntensityThreshold = CStr(child.GetText()).ToFloat();
|
2006-10-17 00:55:35 -07:00
|
|
|
}
|
2007-01-13 08:00:52 -08:00
|
|
|
|
|
|
|
|
if(child_name == el_decay)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
//m_intensity_threshold = CStr(child.GetText()).ToFloat();
|
|
|
|
|
m_Decay = CStr(child.GetText()).ToFloat();
|
2007-01-13 08:00:52 -08:00
|
|
|
}
|
|
|
|
|
|
2006-10-17 00:55:35 -07:00
|
|
|
if(child_name == el_replacement)
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
m_intensity_file = child.GetText();
|
2006-10-17 00:55:35 -07:00
|
|
|
|
|
|
|
|
}
|
2006-09-30 13:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Reload();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|