NLSOUND::CSoundBank Class Reference

#include <sound_bank.h>


Detailed Description

A set of sounds.
Author:
Peter Hanappe Boris Boucher

Nevrax France

Date:
2001

Definition at line 50 of file sound_bank.h.

Public Member Functions

void addSound (CSound *sound)
void bufferLoaded (const NLMISC::TStringId &bufferName, IBuffer *buffer)
void bufferUnloaded (const NLMISC::TStringId &bufferName)
uint countSounds ()
 Return the number of sounds in this bank.

void getNames (std::vector< NLMISC::TStringId > &names)
 Return the names of the sounds.

CSoundgetSound (const NLMISC::TStringId &name)
 Return a sound corresponding to a name.

bool isLoaded ()
 Returns true if the sounds in this bank have been loaded.

void load ()
 Load all the sounds.

void registerBufferAssoc (CSimpleSound *sound, IBuffer *buffer)
void removeSound (const NLMISC::TStringId &name)
void unload ()
 Remove all the sounds in this bank.

void unregisterBufferAssoc (CSimpleSound *sound, IBuffer *buffer)
virtual ~CSoundBank ()
 Destructor.


Static Public Member Functions

CSoundBankinstance ()
 Release all the loaded sound banks.

void release ()

Private Types

typedef std::hash_map< NLMISC::TStringId,
TSimpleSoundContainer
TBufferAssocContainer
typedef std::hash_set< class
CSimpleSound *, THashPtr<
CSimpleSound * > > 
TSimpleSoundContainer
typedef std::hash_map< NLMISC::TStringId,
CSound * > 
TSoundTable
 Sound names hash map.


Private Member Functions

 CSoundBank ()
 Constructor.


Private Attributes

TBufferAssocContainer _BufferAssoc
 Assoc from buffer to sound. Used for sound unloading.

bool _Loaded
TSoundTable _Sounds

Static Private Attributes

CSoundBank_Instance
 CSoundBank singleton instance.


Member Typedef Documentation

typedef std::hash_map<NLMISC::TStringId, TSimpleSoundContainer > NLSOUND::CSoundBank::TBufferAssocContainer [private]
 

Definition at line 107 of file sound_bank.h.

typedef std::hash_set<class CSimpleSound*, THashPtr<CSimpleSound*> > NLSOUND::CSoundBank::TSimpleSoundContainer [private]
 

Definition at line 105 of file sound_bank.h.

typedef std::hash_map<NLMISC::TStringId, CSound*> NLSOUND::CSoundBank::TSoundTable [private]
 

Sound names hash map.

Definition at line 110 of file sound_bank.h.


Constructor & Destructor Documentation

NLSOUND::CSoundBank::~CSoundBank  )  [virtual]
 

Destructor.

Definition at line 146 of file sound_bank.cpp.

References unload().

00147 {
00148         unload();
00149 }

NLSOUND::CSoundBank::CSoundBank  )  [inline, private]
 

Constructor.

Definition at line 99 of file sound_bank.h.

Referenced by instance().

00099 : _Loaded(false) {};


Member Function Documentation

void NLSOUND::CSoundBank::addSound CSound sound  ) 
 

Definition at line 151 of file sound_bank.cpp.

References NLSOUND::CSound::getName(), and nlassert.

Referenced by load().

00152 {
00153         std::pair<TSoundTable::iterator, bool> ret;
00154         ret = _Sounds.insert(make_pair(sound->getName(), sound));
00155         nlassert(ret.second);
00156 }

void NLSOUND::CSoundBank::bufferLoaded const NLMISC::TStringId bufferName,
IBuffer buffer
 

Called by CSampleBank when a sample(buffer) is loaded. Regenerate link between CSound and IBuffer.

Definition at line 89 of file sound_bank.cpp.

References _BufferAssoc, buffer, and NLSOUND::CSimpleSound::setBuffer().

00090 {
00091 //      std::map<std::string, std::vector<TBufferAssoc> >::iterator it(_BufferAssoc.find(buffer->getName()));
00092         TBufferAssocContainer::iterator it(_BufferAssoc.find(buffer->getName()));
00093 
00094         if (it != _BufferAssoc.end())
00095         {
00096                 // ok, found some sound associated with this buffer.
00097                 // update all sounds.
00098                 TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
00099                 for (; first != last; ++first)
00100                 {
00101                         CSimpleSound *ss = const_cast<CSimpleSound*>(*(it->second.begin()));
00102                         // restore the associated buffer.
00103                         ss->setBuffer(buffer);
00104                 }
00105         }
00106 
00107 }

void NLSOUND::CSoundBank::bufferUnloaded const NLMISC::TStringId bufferName  ) 
 

Called by CSampleBank when a sample(buffer) is unloaded. Remove link from CSound to unloaded IBuffer.

Definition at line 70 of file sound_bank.cpp.

References _BufferAssoc, and NLSOUND::CSimpleSound::setBuffer().

00071 {
00072         TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
00073 
00074         if (it != _BufferAssoc.end())
00075         {
00076                 // ok, found some sound associated with this buffer.
00077                 // update all sounds.
00078                 TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
00079                 for (; first != last; ++first)
00080                 {
00081                         // remove the associated buffer.
00082                         CSimpleSound *ss = const_cast<CSimpleSound*>(*(first));
00083                         ss->setBuffer(NULL);
00084                 }
00085         }
00086 }

uint NLSOUND::CSoundBank::countSounds  ) 
 

Return the number of sounds in this bank.

Definition at line 380 of file sound_bank.cpp.

References uint.

00381 {
00382         return _Sounds.size();
00383 }

void NLSOUND::CSoundBank::getNames std::vector< NLMISC::TStringId > &  names  ) 
 

Return the names of the sounds.

Return the names of the sounds

Definition at line 367 of file sound_bank.cpp.

00368 {
00369         TSoundTable::const_iterator iter;
00370         for (iter = _Sounds.begin(); iter != _Sounds.end(); ++iter)
00371         {
00372                 names.push_back((*iter).first);
00373                 //nlwarning("getting sound %s", (*iter).first);
00374         }
00375 }

CSound * NLSOUND::CSoundBank::getSound const NLMISC::TStringId name  ) 
 

Return a sound corresponding to a name.

Definition at line 350 of file sound_bank.cpp.

00351 {
00352         // Find sound
00353         TSoundTable::iterator iter = _Sounds.find(name);
00354         if ( iter == _Sounds.end() )
00355         {
00356                 return 0;
00357         }
00358         else
00359         {
00360                 return (*iter).second;
00361         }
00362 }

CSoundBank * NLSOUND::CSoundBank::instance  )  [static]
 

Release all the loaded sound banks.

Definition at line 54 of file sound_bank.cpp.

References CSoundBank().

00055 {
00056         NL_ALLOC_CONTEXT(NLSOUND_CSoundBank);
00057         if (_Instance == 0)
00058                 _Instance = new CSoundBank();
00059         return _Instance;
00060 }

bool NLSOUND::CSoundBank::isLoaded  ) 
 

Returns true if the sounds in this bank have been loaded.

Definition at line 342 of file sound_bank.cpp.

00343 {
00344         return _Loaded;
00345 }

void NLSOUND::CSoundBank::load  ) 
 

Load all the sounds.

Load all the sound samples.

Can throw EPathNotFound or ESoundFileNotFound (check Exception)

Definition at line 277 of file sound_bank.cpp.

References addSound(), NLSOUND::Container, and nlassert.

00278 {
00279         // this structure is fill by the loadForm() function and will contain all you need
00280         std::map<std::string, CSoundSerializer> Container;
00281         nlassert(!_Loaded);
00282         // Just call the GEORGE::loadFrom method to read all available sounds
00283         ::loadForm("sound", CAudioMixerUser::instance()->getPackedSheetPath()+"sounds.packed_sheets", Container, CAudioMixerUser::instance()->getPackedSheetUpdate(), false);
00284         _Loaded = true;
00285 
00286         // add all the loaded sound in the sound banks
00287         std::map<std::string, CSoundSerializer>::iterator first(Container.begin()), last(Container.end());
00288         for (; first != last; ++first)
00289         {
00290                 if (first->second.Sound != 0)
00291                         addSound(first->second.Sound);
00292         }
00293 
00294         Container.clear();
00295 }

void NLSOUND::CSoundBank::registerBufferAssoc CSimpleSound sound,
IBuffer buffer
 

Definition at line 109 of file sound_bank.cpp.

References _BufferAssoc, and buffer.

00110 {
00111         if (buffer != NULL)
00112         {
00113                 const NLMISC::TStringId &bufferName = buffer->getName();
00114                 _BufferAssoc[bufferName].insert(sound);
00115         }
00116 }

void NLSOUND::CSoundBank::release  )  [static]
 

Definition at line 62 of file sound_bank.cpp.

00063 {
00064         if (_Instance != 0)
00065                 delete _Instance;
00066         _Instance = 0;
00067 }

void NLSOUND::CSoundBank::removeSound const NLMISC::TStringId name  ) 
 

Definition at line 158 of file sound_bank.cpp.

00159 {
00160         _Sounds.erase(name);
00161 }

void NLSOUND::CSoundBank::unload  ) 
 

Remove all the sounds in this bank.

Definition at line 301 of file sound_bank.cpp.

References nlassert.

Referenced by ~CSoundBank().

00302 {
00303         nlassert(_Loaded);
00304 
00305         TSoundTable::iterator first(_Sounds.begin()), last(_Sounds.end());
00306         for (; first != last; ++first)
00307         {
00308                 delete first->second;
00309         }
00310 
00311         _Sounds.clear();
00312         _Loaded = false;
00313 
00314 /*      vector<CSound*> vec;
00315 
00316 
00317         TSoundTable::iterator map_iter;
00318 
00319         for (map_iter = _Sounds.begin(); map_iter != _Sounds.end(); ++map_iter)
00320         {
00321                 // We can't delete directly second because the map is based on second->getName()
00322                 vec.push_back( (*map_iter).second );
00323         }
00324         
00325         _Sounds.clear();
00326 
00327         vector<CSound*>::iterator vec_iter;
00328 
00329         for (vec_iter = vec.begin(); vec_iter != vec.end(); ++vec_iter)
00330         {
00331                 CSound *sound = *vec_iter;
00332                 delete sound;
00333         }
00334 
00335         _Loaded = false;
00336 */
00337 }

void NLSOUND::CSoundBank::unregisterBufferAssoc CSimpleSound sound,
IBuffer buffer
 

Definition at line 118 of file sound_bank.cpp.

References _BufferAssoc, buffer, and nlassert.

00119 {
00120         if (buffer != NULL)
00121         {
00122                 const TStringId &bufferName = buffer->getName();
00123                 TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
00124 
00125                 if (it != _BufferAssoc.end())
00126                 {
00127                         TSimpleSoundContainer::iterator it2(it->second.find(sound));
00128 
00129                         nlassert(it2 != it->second.end());
00130                         it->second.erase(it2);
00131 
00132                         if (it->second.empty())
00133                         {
00134                                 // last sound refenrecing this buffer
00135                                 _BufferAssoc.erase(it);
00136                         }
00137 
00138                 }
00139         }
00140 }


Field Documentation

TBufferAssocContainer NLSOUND::CSoundBank::_BufferAssoc [private]
 

Assoc from buffer to sound. Used for sound unloading.

Definition at line 113 of file sound_bank.h.

Referenced by bufferLoaded(), bufferUnloaded(), registerBufferAssoc(), and unregisterBufferAssoc().

CSoundBank * NLSOUND::CSoundBank::_Instance [static, private]
 

CSoundBank singleton instance.

Definition at line 50 of file sound_bank.cpp.

bool NLSOUND::CSoundBank::_Loaded [private]
 

Definition at line 119 of file sound_bank.h.

TSoundTable NLSOUND::CSoundBank::_Sounds [private]
 

Definition at line 116 of file sound_bank.h.


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 14:43:15 2004 for NeL by doxygen 1.3.6