Fixed: Sound CSheetId implementation (part 1) (not tested)

This commit is contained in:
kaetemi 2012-06-02 14:54:39 +02:00
parent caf00a5b7c
commit ea6ede03ba
15 changed files with 99 additions and 122 deletions

View file

@ -21,7 +21,6 @@
#include "nel/misc/string_mapper.h"
#include "nel/misc/plane.h"
#include "nel/misc/aabbox.h"
#include "nel/misc/sheet_id.h"
#include "nel/3d/transform.h"
@ -126,9 +125,9 @@ public:
//\name Sound related.
//@{
void setSoundGroup(const std::string &soundGroup);
void setSoundGroup(const NLMISC::CSheetId &soundGroupId);
void setSoundGroup(const NLMISC::TStringId &soundGroupId);
const std::string &getSoundGroup();
NLMISC::CSheetId getSoundGroupId();
NLMISC::TStringId getSoundGroupId();
void setEnvironmentFx(const std::string &environmentFx);
void setEnvironmentFx(const NLMISC::TStringId &environmentFxId);
const std::string &getEnvironmentFx();
@ -189,7 +188,7 @@ private:
std::vector<NLMISC::CPlane> _Volume;
/// Sound group name id
NLMISC::CSheetId _SoundGroupId;
NLMISC::TStringId _SoundGroupId;
/// Environement Fx name Id (using CStringMapper)
NLMISC::TStringId _EnvironmentFxId;

View file

@ -19,6 +19,7 @@
#include "nel/misc/types_nl.h"
#include "nel/misc/string_mapper.h"
#include "nel/misc/sheet_id.h"
#include <vector>
#include <map>
@ -249,14 +250,13 @@ private:
/// The segment of all the audio path.
std::vector<std::pair<NLMISC::CVector, NLMISC::CVector> > _AudioPath;
typedef CHashMap<NLMISC::CSheetId, CClusterSound, NLMISC::CSheetIdHashMapTraits> TClusterSoundCont;
typedef CHashMap<NLMISC::TStringId, CClusterSound, NLMISC::CStringIdHashMapTraits> TClusterSoundCont;
/// The current cluster playing source indexed with sound group id
TClusterSoundCont _Sources;
typedef CHashMap<NLMISC::CSheetId, NLMISC::CSheetId, NLMISC::CSheetIdHashMapTraits> TStringStringMap;
//typedef CHashMap<NLMISC::TStringId, NLMISC::TStringId, NLMISC::CStringIdHashMapTraits> TStringStringMap;
typedef CHashMap<NLMISC::TStringId, NLMISC::CSheetId, NLMISC::CStringIdHashMapTraits> TStringSheetMap;
/// The sound_group to sound assoc
TStringStringMap _SoundGroupToSound;
TStringSheetMap _SoundGroupToSound;
};
} // NLSOUND

View file

@ -54,7 +54,7 @@ class CSound
friend class CAudioMixerUser;
public:
/// Factory for specialized sound.
static CSound *createSound(const std::string &filename, NLGEORGES::UFormElm& formRoot);
static CSound *createSound(const NLMISC::CSheetId &sheetId, NLGEORGES::UFormElm& formRoot);
enum TSOUND_TYPE
{

View file

@ -68,16 +68,16 @@ public:
bool isLoaded();
/// Return a sound corresponding to a name.
CSound *getSound(const NLMISC::CSheetId &name);
CSound *getSound(const NLMISC::CSheetId &sheetId);
/// Return the names of the sounds
void getNames( std::vector<NLMISC::CSheetId> &names );
void getNames( std::vector<NLMISC::CSheetId> &sheetIds );
/// Return the number of sounds in this bank.
uint countSounds();
void addSound(CSound *sound);
void removeSound(const NLMISC::CSheetId &name);
void removeSound(const NLMISC::CSheetId &sheetId);
private:
@ -89,7 +89,8 @@ private:
typedef CHashMap<NLMISC::TStringId, TSimpleSoundContainer, NLMISC::CStringIdHashMapTraits> TBufferAssocContainer;
/// Sound names hash map
// typedef std::hash_map<std::string, CSound*> TSoundTable;
typedef CHashMap<NLMISC::CSheetId, CSound*, NLMISC::CSheetIdHashMapTraits> TSoundTable;
// typedef CHashMap<NLMISC::CSheetId, CSound*, NLMISC::CSheetIdHashMapTraits> TSoundTable;
typedef std::vector<CSound *> TSoundTable; // list the sheets by shortId of the sheetId
/// Assoc from buffer to sound. Used for sound unloading.
TBufferAssocContainer _BufferAssoc;

View file

@ -100,7 +100,7 @@ USource *OnAddSource( const char *name, float x, float y, float z )
/*
* Create a source with sound 'name', and set some of its initial properties, if successful
*/
USource *source = AudioMixer->createSource( /*CStringMapper::map(name)*/ CSheetId(name) );
USource *source = AudioMixer->createSource(CSheetId(name));
if ( source != NULL )
{
source->setPos( CVector(x,y,z) );
@ -161,8 +161,8 @@ int main()
printf( "One is 20 meters ahead, on the right\n" );
printf( "The other is 5 meters ahead, on the left\n" );
getchar();
USource *src1 = OnAddSource( "beep", 1.0f, 20.0f, 0.0f ); // Beep on the right, 20 meters ahead
USource *src2 = OnAddSource( "tuut", -2.0f, 5.0f, 0.0f ); // Tuut on the left, 5 meters ahead
USource *src1 = OnAddSource( "beep.sound", 1.0f, 20.0f, 0.0f ); // Beep on the right, 20 meters ahead
USource *src2 = OnAddSource( "tuut.sound", -2.0f, 5.0f, 0.0f ); // Tuut on the left, 5 meters ahead
// Second step: we will move the listener ahead
printf( "Press ENTER again to start moving the listener\n" );

View file

@ -97,7 +97,7 @@ static void initSample()
//NLMISC::CHTimer::startBench();
s_Source = s_AudioMixer->createSource(/*CStringMapper::map("stream_file")*/ CSheetId("stream_file"));
s_Source = s_AudioMixer->createSource(CSheetId("stream_file.sound"));
nlassert(s_Source);
s_StreamFileSource = dynamic_cast<CStreamFileSource *>(s_Source);
nlassert(s_StreamFileSource);

View file

@ -87,7 +87,7 @@ static void initSample()
//NLMISC::CHTimer::startBench();
USource *source = s_AudioMixer->createSource(CSheetId("default_stream")/*CStringMapper::map("default_stream")*/);
USource *source = s_AudioMixer->createSource(CSheetId("default_stream.sound"));
nlassert(source);
s_StreamSource = dynamic_cast<UStreamSource *>(source);
nlassert(s_StreamSource);

View file

@ -51,9 +51,7 @@ CCluster::CCluster ()
// map a no fx string
_EnvironmentFxId = CStringMapper::map("no fx");
// map a no soundgroup string
_SoundGroupId = NLMISC::CSheetId::Unknown; /*CStringMapper::map("")*/;
nldebug("SOUNDSHEET: %s", _SoundGroupId.toString().c_str());
_SoundGroupId = CStringMapper::map("");
// I am a transform cluster
CTransform::setIsCluster(true);
@ -78,21 +76,18 @@ CCluster::~CCluster()
void CCluster::setSoundGroup(const std::string &soundGroup)
{
_SoundGroupId = NLMISC::CSheetId(soundGroup);/*CStringMapper::map(soundGroup);*/
nldebug("SOUNDSHEET: %s", _SoundGroupId.toString().c_str());
_SoundGroupId = CStringMapper::map(soundGroup);
}
void CCluster::setSoundGroup(const NLMISC::CSheetId &soundGroupId)
void CCluster::setSoundGroup(const NLMISC::TStringId &soundGroupId)
{
_SoundGroupId = soundGroupId;
nldebug("SOUNDSHEET: %s", _SoundGroupId.toString().c_str());
}
const std::string &CCluster::getSoundGroup()
{
return _SoundGroupId.toString();/*CStringMapper::unmap(_SoundGroupId)*/;
return CStringMapper::unmap(_SoundGroupId);
}
NLMISC::CSheetId CCluster::getSoundGroupId()
NLMISC::TStringId CCluster::getSoundGroupId()
{
return _SoundGroupId;
}
@ -309,8 +304,7 @@ void CCluster::serial (NLMISC::IStream&f)
std::string envFxName;
f.serial(soundGroup);
_SoundGroupId = NLMISC::CSheetId(soundGroup); /*CStringMapper::map(soundGroup)*/;
nldebug("SOUNDSHEET: %s", _SoundGroupId.toString().c_str());
_SoundGroupId = CStringMapper::map(soundGroup);
f.serial(envFxName);
if (envFxName == "")
@ -320,7 +314,7 @@ void CCluster::serial (NLMISC::IStream&f)
else
{
// write the sound group
std::string soundGroup = _SoundGroupId.toString();/*CStringMapper::unmap(_SoundGroupId)*/;
std::string soundGroup = CStringMapper::unmap(_SoundGroupId);
f.serial(soundGroup);
// write the env fx name
std::string envFxName = CStringMapper::unmap(_EnvironmentFxId);

View file

@ -261,17 +261,8 @@ void CPSSound::serial(NLMISC::IStream &f) throw(NLMISC::EStream)
CPSLocatedBindable::serial(f);
// version 3 : added option to keep original pitch from the .sound
sint ver = f.serialVersion(3);
if (f.isReading())
{
std::string soundName;
f.serial(soundName);
_SoundName = NLMISC::CSheetId(soundName)/*NLMISC::CStringMapper::map(soundName)*/;
}
else
{
std::string soundName = _SoundName.toString()/*NLMISC::CStringMapper::unmap(_SoundName)*/;
f.serial(soundName);
}
_SoundName.serialString(f, "sound");
sint32 nbSounds;
bool hasScheme;

View file

@ -1050,9 +1050,8 @@ public:
for (uint i=0; i<size; ++i)
{
items->getArrayValue(soundName, i);
soundName = soundName.substr(0, soundName.find(".sound"));
cs.SoundNames.push_back(CSheetId(soundName)/*CStringMapper::map(soundName)*/);
nlassert(soundName.find(".sound") != std::string::npos);
cs.SoundNames.push_back(CSheetId(soundName));
}
if (!cs.SoundNames.empty())
@ -1133,7 +1132,7 @@ void CAudioMixerUser::CControledSources::serial(NLMISC::IStream &s)
for (uint i=0; i<size; ++i)
{
s.serial(soundName);
SoundNames.push_back(CSheetId(soundName)/*CStringMapper::map(soundName)*/);
SoundNames.push_back(CSheetId(soundName.find(".sound") == std::string::npos ? (soundName + ".sound") : soundName));
}
}
else
@ -1147,7 +1146,7 @@ void CAudioMixerUser::CControledSources::serial(NLMISC::IStream &s)
for (uint i=0; i<size; ++i)
{
soundName = SoundNames[i].toString();/*CStringMapper::unmap(SoundNames[i])*/;
soundName = SoundNames[i].toString();;
s.serial(soundName);
}
}
@ -2313,7 +2312,6 @@ void CAudioMixerUser::getLoadedSampleBankInfo(std::vector<std::pair<std::strin
}
void CAudioMixerUser::setListenerPos (const NLMISC::CVector &pos)
{
_Listener.setPos(pos);

View file

@ -84,7 +84,8 @@ void CBackgroundSound::importForm(const std::string& filename, NLGEORGES::UFormE
// Read the sound name.
std::string soundName;
psoundItem->getValueByName(soundName, "Sound");
sound.SoundName = NLMISC::CSheetId(CFile::getFilenameWithoutExtension(soundName));/*CStringMapper::map(CFile::getFilenameWithoutExtension(soundName))*/;
nlassert(soundName.find(".sound") != std::string::npos);
sound.SoundName = NLMISC::CSheetId(soundName);
// Read the environnement flag.

View file

@ -86,7 +86,8 @@ void CBackgroundSoundManager::addSound(const std::string &soundName, uint layerI
CAudioMixerUser *mixer = CAudioMixerUser::instance();
TSoundData sd;
sd.SoundName = /*CStringMapper::map(soundName)*/ NLMISC::CSheetId(soundName);
nlassert(soundName.find(".sound") != std::string::npos);
sd.SoundName = NLMISC::CSheetId(soundName);
sd.Sound = mixer->getSoundId(sd.SoundName);
sd.Source = 0;
@ -1431,21 +1432,15 @@ void CBackgroundSoundManager::TBanksData::serial(NLMISC::IStream &s)
void CBackgroundSoundManager::TSoundData::serial(NLMISC::IStream &s)
{
std::string str;
//std::string str;
SoundName.serialString(s, "sound");
if (s.isReading())
{
CAudioMixerUser *mixer = CAudioMixerUser::instance();
s.serial(str);
SoundName = /*NLMISC::CStringMapper::map(str)*/ NLMISC::CSheetId(str);
Sound = mixer->getSoundId(SoundName);
Source = NULL;
Selected = false;
}
else
{
s.serial(const_cast<std::string&>(SoundName.toString()/*NLMISC::CStringMapper::unmap(SoundName)*/));
}
s.serial(MinBox);
s.serial(MaxBox);
s.serial(Surface);

View file

@ -70,7 +70,7 @@ float EAX_MATERIAL_PARAM[] =
class CSoundGroupSerializer
{
public:
std::vector<std::pair<NLMISC::CSheetId, NLMISC::CSheetId> > _SoundGroupAssoc;
std::vector<std::pair<NLMISC::TStringId, NLMISC::CSheetId> > _SoundGroupAssoc;
// load the values using the george sheet (called by GEORGE::loadForm)
void readGeorges (const NLMISC::CSmartPtr<NLGEORGES::UForm> &form, const std::string &/* name */)
@ -95,15 +95,9 @@ public:
item->getValueByName(soundGroup, ".SoundGroup");
item->getValueByName(sound, ".Sound");
string::size_type n = sound.rfind(".sound");
nlassert(sound.find(".sound") != std::string::npos);
if (n != string::npos)
{
// remove the tailing .sound
sound = sound.substr(0, n);
}
_SoundGroupAssoc.push_back(make_pair(CSheetId(soundGroup), CSheetId(sound) /*CStringMapper::map(soundGroup), CStringMapper::map(sound)*/));
_SoundGroupAssoc.push_back(make_pair(CStringMapper::map(soundGroup), CSheetId(sound)));
}
}
catch(...)
@ -125,24 +119,18 @@ public:
{
if (s.isReading())
{
std::string soundGroup;
std::string sound;
TStringId soundGroup;
CSheetId sound;
s.serial(soundGroup);
s.serial(sound);
CStringMapper::serialString(s, soundGroup);
sound.serialString(s, "sound");
_SoundGroupAssoc.push_back(make_pair(CSheetId(soundGroup), CSheetId(sound) /*CStringMapper::map(soundGroup), CStringMapper::map(sound)*/));
_SoundGroupAssoc.push_back(make_pair(soundGroup, sound));
}
else
{
std::string soundGroup;
std::string sound;
soundGroup = _SoundGroupAssoc[i].first.toString();// CStringMapper::unmap(_SoundGroupAssoc[i].first);
sound = _SoundGroupAssoc[i].second.toString(); //CStringMapper::unmap(_SoundGroupAssoc[i].second);
s.serial(soundGroup);
s.serial(sound);
CStringMapper::serialString(s, _SoundGroupAssoc[i].first);
_SoundGroupAssoc[i].second.serialString(s, "sound");
}
}
}
@ -254,10 +242,10 @@ void CClusteredSound::update(const CVector &listenerPos, const CVector &/* view
TClusterStatusMap::const_iterator first(_AudibleClusters.begin()), last(_AudibleClusters.end());
for (; first != last; ++first )
{
static NLMISC::CSheetId NO_SOUND_GROUP = /*CStringMapper::emptyId()*/NLMISC::CSheetId::Unknown;
static NLMISC::TStringId NO_SOUND_GROUP = CStringMapper::emptyId();
const CClusterSoundStatus &css = first->second;
CCluster *cluster = first->first;
NLMISC::CSheetId soundGroup;
NLMISC::TStringId soundGroup;
soundGroup = cluster->getSoundGroupId();
@ -289,7 +277,7 @@ void CClusteredSound::update(const CVector &listenerPos, const CVector &/* view
// nldebug("Searching sound assoc for group [%s]", CStringMapper::unmap(soundGroup).c_str());
TStringStringMap::iterator it2(_SoundGroupToSound.find(soundGroup));
TStringSheetMap::iterator it2(_SoundGroupToSound.find(soundGroup));
if (it2 != _SoundGroupToSound.end())
{
NLMISC::CSheetId soundName = it2->second;

View file

@ -37,8 +37,9 @@ using namespace NLMISC;
namespace NLSOUND {
CSound *CSound::createSound(const std::string &filename, NLGEORGES::UFormElm& formRoot)
CSound *CSound::createSound(const NLMISC::CSheetId &sheetId, NLGEORGES::UFormElm& formRoot)
{
std::string filename = sheetId.toString();
CSound *ret = NULL;
string soundType;
@ -170,7 +171,7 @@ void CSound::serial(NLMISC::IStream &s)
void CSound::importForm(const std::string& filename, NLGEORGES::UFormElm& root)
{
// Name
_Name = NLMISC::CSheetId(CFile::getFilenameWithoutExtension(filename));//CStringMapper::map(CFile::getFilenameWithoutExtension(filename));
_Name = NLMISC::CSheetId(filename);
// InternalConeAngle
uint32 inner;

View file

@ -123,14 +123,13 @@ CSoundBank::~CSoundBank()
void CSoundBank::addSound(CSound *sound)
{
std::pair<TSoundTable::iterator, bool> ret;
ret = _Sounds.insert(make_pair(sound->getName(), sound));
nlassert(ret.second);
nlassert(_Sounds.size() > sound->getName().getShortId());
_Sounds[sound->getName().getShortId()] = sound;
}
void CSoundBank::removeSound(const NLMISC::CSheetId &name)
void CSoundBank::removeSound(const NLMISC::CSheetId &sheetId)
{
_Sounds.erase(name);
_Sounds[sheetId.getShortId()] = NULL;
}
@ -155,7 +154,7 @@ public:
void readGeorges (const NLMISC::CSmartPtr<NLGEORGES::UForm> &form, const NLMISC::CSheetId &name)
{
// just call the sound creation method with the xml form.
Sound = CSound::createSound(name.toString(), form->getRootNode());
Sound = CSound::createSound(name, form->getRootNode());
// success ?
// if (_Sound != 0)
@ -259,36 +258,51 @@ void CSoundBank::load(const std::string &packedSheetDir, bool packedSheetUpdate)
{
// this structure is fill by the loadForm() function and will contain all you need
//std::map<std::string, CSoundSerializer> Container;
std::map<NLMISC::CSheetId, CSoundSerializer> Container;
std::map<NLMISC::CSheetId, CSoundSerializer> container;
nlassert(!_Loaded);
// Just call the GEORGE::loadFrom method to read all available sounds
::loadForm("sound", packedSheetDir + "sounds.packed_sheets", Container, packedSheetUpdate, false);
::loadForm("sound", packedSheetDir + "sounds.packed_sheets", container, packedSheetUpdate, false);
_Loaded = true;
// add all the loaded sound in the sound banks
//std::map<std::string, CSoundSerializer>::iterator first(Container.begin()), last(Container.end());
std::map<NLMISC::CSheetId, CSoundSerializer>::iterator first(Container.begin()), last(Container.end());
for (; first != last; ++first)
// get the largest sheet id needed and init the sound bank
uint32 maxShortId = 0;
{
if (first->second.Sound != 0)
addSound(first->second.Sound);
std::map<NLMISC::CSheetId, CSoundSerializer>::iterator first(container.begin()), last(container.end());
for (; first != last; ++first)
{
if (first->first.getShortId() > maxShortId)
maxShortId = first->first.getShortId();
}
++maxShortId; // inc for size = last idx + 1
nlassert(maxShortId < (container.size() * 8)); // ensure no ridiculous sheet id values
if (maxShortId > _Sounds.size())
_Sounds.resize(maxShortId);
}
Container.clear();
// add all the loaded sound in the sound banks
{
std::map<NLMISC::CSheetId, CSoundSerializer>::iterator first(container.begin()), last(container.end());
for (; first != last; ++first)
{
if (first->second.Sound != 0)
addSound(first->second.Sound);
}
}
container.clear();
}
/*
* Unload all the sound samples in this bank.
*/
void CSoundBank::unload()
void CSoundBank::unload()
{
nlassert(_Loaded);
TSoundTable::iterator first(_Sounds.begin()), last(_Sounds.end());
for (; first != last; ++first)
for (TSoundTable::size_type i = 0; i < _Sounds.size(); ++i)
{
delete first->second;
delete _Sounds[i];
}
_Sounds.clear();
@ -322,7 +336,7 @@ void CSoundBank::unload()
/*
* Returns true if the samples in this bank have been loaded.
*/
bool CSoundBank::isLoaded()
bool CSoundBank::isLoaded()
{
return _Loaded;
}
@ -330,37 +344,32 @@ bool CSoundBank::isLoaded()
/*
* Return a sound sample corresponding to a name.
*/
CSound* CSoundBank::getSound(const NLMISC::CSheetId &name)
CSound* CSoundBank::getSound(const NLMISC::CSheetId &sheetId)
{
// Find sound
TSoundTable::iterator iter = _Sounds.find(name);
if ( iter == _Sounds.end() )
{
return 0;
}
else
{
return (*iter).second;
}
if (sheetId == NLMISC::CSheetId::Unknown)
return NULL;
nlassert(sheetId.getShortId() < _Sounds.size());
return _Sounds[sheetId.getShortId()];
}
/**
* Return the names of the sounds
*/
void CSoundBank::getNames( std::vector<NLMISC::CSheetId> &names )
void CSoundBank::getNames( std::vector<NLMISC::CSheetId> &sheetIds )
{
TSoundTable::const_iterator iter;
for (iter = _Sounds.begin(); iter != _Sounds.end(); ++iter)
for (TSoundTable::size_type i = 0; i < _Sounds.size(); ++i)
{
names.push_back((*iter).first);
//nlwarning("getting sound %s", (*iter).first);
if (_Sounds[i])
sheetIds.push_back(_Sounds[i]->getName());
}
}
/*
* Return the number of buffers in this bank.
*/
uint CSoundBank::countSounds()
uint CSoundBank::countSounds()
{
return (uint)_Sounds.size();
}