From aa7598efe23e2474c68f6bcee4328b09fb263204 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sat, 2 Jun 2012 14:54:39 +0200 Subject: [PATCH] Fixed: Sound CSheetId implementation (part 1) (not tested) --HG-- branch : sound_dev --- code/nel/include/nel/3d/cluster.h | 7 +- code/nel/include/nel/sound/clustered_sound.h | 8 +- code/nel/include/nel/sound/sound.h | 2 +- code/nel/include/nel/sound/sound_bank.h | 9 +- code/nel/samples/sound/sound_sources/main.cpp | 6 +- .../samples/sound/stream_file/stream_file.cpp | 2 +- .../stream_ogg_vorbis/stream_ogg_vorbis.cpp | 2 +- code/nel/src/3d/cluster.cpp | 20 ++--- code/nel/src/3d/ps_sound.cpp | 13 +-- code/nel/src/sound/audio_mixer_user.cpp | 10 +-- code/nel/src/sound/background_sound.cpp | 3 +- .../src/sound/background_sound_manager.cpp | 13 +-- code/nel/src/sound/clustered_sound.cpp | 38 +++------ code/nel/src/sound/sound.cpp | 5 +- code/nel/src/sound/sound_bank.cpp | 83 ++++++++++--------- 15 files changed, 99 insertions(+), 122 deletions(-) diff --git a/code/nel/include/nel/3d/cluster.h b/code/nel/include/nel/3d/cluster.h index 14b3fb13c..552d7474a 100644 --- a/code/nel/include/nel/3d/cluster.h +++ b/code/nel/include/nel/3d/cluster.h @@ -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 _Volume; /// Sound group name id - NLMISC::CSheetId _SoundGroupId; + NLMISC::TStringId _SoundGroupId; /// Environement Fx name Id (using CStringMapper) NLMISC::TStringId _EnvironmentFxId; diff --git a/code/nel/include/nel/sound/clustered_sound.h b/code/nel/include/nel/sound/clustered_sound.h index 0736c5a5c..ed674f879 100644 --- a/code/nel/include/nel/sound/clustered_sound.h +++ b/code/nel/include/nel/sound/clustered_sound.h @@ -19,6 +19,7 @@ #include "nel/misc/types_nl.h" #include "nel/misc/string_mapper.h" +#include "nel/misc/sheet_id.h" #include #include @@ -249,14 +250,13 @@ private: /// The segment of all the audio path. std::vector > _AudioPath; - typedef CHashMap TClusterSoundCont; + typedef CHashMap TClusterSoundCont; /// The current cluster playing source indexed with sound group id TClusterSoundCont _Sources; - typedef CHashMap TStringStringMap; - //typedef CHashMap TStringStringMap; + typedef CHashMap TStringSheetMap; /// The sound_group to sound assoc - TStringStringMap _SoundGroupToSound; + TStringSheetMap _SoundGroupToSound; }; } // NLSOUND diff --git a/code/nel/include/nel/sound/sound.h b/code/nel/include/nel/sound/sound.h index c4c494912..a84393944 100644 --- a/code/nel/include/nel/sound/sound.h +++ b/code/nel/include/nel/sound/sound.h @@ -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 { diff --git a/code/nel/include/nel/sound/sound_bank.h b/code/nel/include/nel/sound/sound_bank.h index 8a4573849..05993bb07 100644 --- a/code/nel/include/nel/sound/sound_bank.h +++ b/code/nel/include/nel/sound/sound_bank.h @@ -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 &names ); + void getNames( std::vector &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 TBufferAssocContainer; /// Sound names hash map // typedef std::hash_map TSoundTable; - typedef CHashMap TSoundTable; +// typedef CHashMap TSoundTable; + typedef std::vector TSoundTable; // list the sheets by shortId of the sheetId /// Assoc from buffer to sound. Used for sound unloading. TBufferAssocContainer _BufferAssoc; diff --git a/code/nel/samples/sound/sound_sources/main.cpp b/code/nel/samples/sound/sound_sources/main.cpp index 3ece14f73..c4efa7a4e 100644 --- a/code/nel/samples/sound/sound_sources/main.cpp +++ b/code/nel/samples/sound/sound_sources/main.cpp @@ -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" ); diff --git a/code/nel/samples/sound/stream_file/stream_file.cpp b/code/nel/samples/sound/stream_file/stream_file.cpp index 060c4b433..b07da8ba4 100644 --- a/code/nel/samples/sound/stream_file/stream_file.cpp +++ b/code/nel/samples/sound/stream_file/stream_file.cpp @@ -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(s_Source); nlassert(s_StreamFileSource); diff --git a/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp b/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp index 490ae8dd5..e4a2e507c 100644 --- a/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp +++ b/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp @@ -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(source); nlassert(s_StreamSource); diff --git a/code/nel/src/3d/cluster.cpp b/code/nel/src/3d/cluster.cpp index cb34b7f10..2faebcec7 100644 --- a/code/nel/src/3d/cluster.cpp +++ b/code/nel/src/3d/cluster.cpp @@ -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); diff --git a/code/nel/src/3d/ps_sound.cpp b/code/nel/src/3d/ps_sound.cpp index 322bd2729..5cee12c3e 100644 --- a/code/nel/src/3d/ps_sound.cpp +++ b/code/nel/src/3d/ps_sound.cpp @@ -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; diff --git a/code/nel/src/sound/audio_mixer_user.cpp b/code/nel/src/sound/audio_mixer_user.cpp index 016f3d13d..61dca9f9e 100644 --- a/code/nel/src/sound/audio_mixer_user.cpp +++ b/code/nel/src/sound/audio_mixer_user.cpp @@ -1050,9 +1050,8 @@ public: for (uint i=0; igetArrayValue(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; igetValueByName(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. diff --git a/code/nel/src/sound/background_sound_manager.cpp b/code/nel/src/sound/background_sound_manager.cpp index 4eb5b042d..2008b37db 100644 --- a/code/nel/src/sound/background_sound_manager.cpp +++ b/code/nel/src/sound/background_sound_manager.cpp @@ -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(SoundName.toString()/*NLMISC::CStringMapper::unmap(SoundName)*/)); - } s.serial(MinBox); s.serial(MaxBox); s.serial(Surface); diff --git a/code/nel/src/sound/clustered_sound.cpp b/code/nel/src/sound/clustered_sound.cpp index c2a8db8ef..25f0e5b64 100644 --- a/code/nel/src/sound/clustered_sound.cpp +++ b/code/nel/src/sound/clustered_sound.cpp @@ -70,7 +70,7 @@ float EAX_MATERIAL_PARAM[] = class CSoundGroupSerializer { public: - std::vector > _SoundGroupAssoc; + std::vector > _SoundGroupAssoc; // load the values using the george sheet (called by GEORGE::loadForm) void readGeorges (const NLMISC::CSmartPtr &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; diff --git a/code/nel/src/sound/sound.cpp b/code/nel/src/sound/sound.cpp index bb9fd9372..f1d06bd5f 100644 --- a/code/nel/src/sound/sound.cpp +++ b/code/nel/src/sound/sound.cpp @@ -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; diff --git a/code/nel/src/sound/sound_bank.cpp b/code/nel/src/sound/sound_bank.cpp index 93cf09de4..1124eeaa5 100644 --- a/code/nel/src/sound/sound_bank.cpp +++ b/code/nel/src/sound/sound_bank.cpp @@ -123,14 +123,13 @@ CSoundBank::~CSoundBank() void CSoundBank::addSound(CSound *sound) { - std::pair 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 &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 Container; - std::map Container; + std::map 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::iterator first(Container.begin()), last(Container.end()); - std::map::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::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::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 &names ) +void CSoundBank::getNames( std::vector &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(); }