sound_bank.cpp
Go to the documentation of this file.00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "stdsound.h"
00025
00026 #include "sound_bank.h"
00027 #include "simple_sound.h"
00028 #include "complex_sound.h"
00029 #include "context_sound.h"
00030 #include "background_sound.h"
00031 #include "music_sound.h"
00032
00033 #include "nel/georges/u_form_loader.h"
00034 #include "nel/georges/u_form_elm.h"
00035 #include "nel/georges/u_form.h"
00036 #include "nel/misc/path.h"
00037 #include "driver/buffer.h"
00038
00039 #include "nel/georges/load_form.h"
00040
00041 #include <limits>
00042
00043 using namespace std;
00044 using namespace NLMISC;
00045 using namespace NLGEORGES;
00046
00047
00048 namespace NLSOUND {
00049
00050 CSoundBank *CSoundBank::_Instance;
00051
00052
00053
00054 CSoundBank *CSoundBank::instance()
00055 {
00056 if (_Instance == 0)
00057 _Instance = new CSoundBank();
00058 return _Instance;
00059 }
00060
00061 void CSoundBank::release()
00062 {
00063 if (_Instance != 0)
00064 delete _Instance;
00065 _Instance = 0;
00066 }
00067
00068
00069 void CSoundBank::bufferUnloaded(const NLMISC::TStringId &bufferName)
00070 {
00071 TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
00072
00073 if (it != _BufferAssoc.end())
00074 {
00075
00076
00077 TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
00078 for (; first != last; ++first)
00079 {
00080
00081 CSimpleSound *ss = const_cast<CSimpleSound*>(*(first));
00082 ss->setBuffer(NULL);
00083 }
00084 }
00085 }
00086
00087
00088 void CSoundBank::bufferLoaded(const NLMISC::TStringId &, IBuffer *buffer)
00089 {
00090
00091 TBufferAssocContainer::iterator it(_BufferAssoc.find(buffer->getName()));
00092
00093 if (it != _BufferAssoc.end())
00094 {
00095
00096
00097 TSimpleSoundContainer::iterator first(it->second.begin()), last(it->second.end());
00098 for (; first != last; ++first)
00099 {
00100 CSimpleSound *ss = const_cast<CSimpleSound*>(*(it->second.begin()));
00101
00102 ss->setBuffer(buffer);
00103 }
00104 }
00105
00106 }
00107
00108 void CSoundBank::registerBufferAssoc(CSimpleSound *sound, IBuffer *buffer)
00109 {
00110 if (buffer != NULL)
00111 {
00112 const NLMISC::TStringId &bufferName = buffer->getName();
00113 _BufferAssoc[bufferName].insert(sound);
00114 }
00115 }
00116
00117 void CSoundBank::unregisterBufferAssoc(CSimpleSound *sound, IBuffer * buffer)
00118 {
00119 if (buffer != NULL)
00120 {
00121 const TStringId &bufferName = buffer->getName();
00122 TBufferAssocContainer::iterator it(_BufferAssoc.find(bufferName));
00123
00124 if (it != _BufferAssoc.end())
00125 {
00126 TSimpleSoundContainer::iterator it2(it->second.find(sound));
00127
00128 nlassert(it2 != it->second.end());
00129 it->second.erase(it2);
00130
00131 if (it->second.empty())
00132 {
00133
00134 _BufferAssoc.erase(it);
00135 }
00136
00137 }
00138 }
00139 }
00140
00141
00142
00143
00144
00145 CSoundBank::~CSoundBank()
00146 {
00147 unload();
00148 }
00149
00150 void CSoundBank::addSound(CSound *sound)
00151 {
00152 std::pair<TSoundTable::iterator, bool> ret;
00153 ret = _Sounds.insert(make_pair(sound->getName(), sound));
00154 nlassert(ret.second);
00155 }
00156
00157 void CSoundBank::removeSound(const NLMISC::TStringId &name)
00158 {
00159 _Sounds.erase(name);
00160 }
00161
00162
00169 class CSoundSerializer
00170 {
00171 public:
00173 CSound *Sound;
00174
00176 CSoundSerializer()
00177 : Sound(0)
00178 {}
00179
00180
00181 void readGeorges (const NLMISC::CSmartPtr<NLGEORGES::UForm> &form, const std::string &name)
00182 {
00183
00184 Sound = CSound::createSound(name, form->getRootNode());
00185
00186
00187
00188
00189 }
00190
00191
00192 void serial (NLMISC::IStream &s)
00193 {
00194 if (s.isReading())
00195 {
00196
00197 CSound::TSOUND_TYPE type = CSound::SOUND_SIMPLE;
00198 s.serialEnum(type);
00199
00200
00201
00202
00203
00204 switch(CSound::TSOUND_TYPE(type))
00205 {
00206 case CSound::SOUND_SIMPLE:
00207 Sound = new CSimpleSound();
00208 break;
00209 case CSound::SOUND_COMPLEX:
00210 Sound = new CComplexSound();
00211 break;
00212 case CSound::SOUND_CONTEXT:
00213 Sound = new CContextSound();
00214 break;
00215 case CSound::SOUND_BACKGROUND:
00216 Sound = new CBackgroundSound();
00217 break;
00218 case CSound::SOUND_MUSIC:
00219 Sound = new CMusicSound();
00220 break;
00221 default:
00222 Sound = 0;
00223 }
00224
00225
00226 if (Sound)
00227 {
00228
00229 Sound->serial(s);
00230
00231 }
00232 }
00233 else
00234 {
00235 if (Sound == 0)
00236 {
00237
00238 uint32 i = std::numeric_limits<uint32>::max();
00239 s.serialEnum(i);
00240
00241 }
00242 else
00243 {
00244
00245 CSound::TSOUND_TYPE type = Sound->getSoundType();
00246 s.serialEnum(type);
00247
00248
00249
00250
00251 Sound->serial(s);
00252 }
00253 }
00254 }
00255
00259 void removed()
00260 {
00261 if (Sound != 0)
00262 {
00263
00264
00265 delete Sound;
00266 }
00267 }
00268
00269
00270 static uint getVersion () { return 3; }
00271 };
00272
00273
00278 void CSoundBank::load()
00279 {
00280
00281 std::map<std::string, CSoundSerializer> Container;
00282 nlassert(!_Loaded);
00283
00284 ::loadForm("sound", CAudioMixerUser::instance()->getPackedSheetPath()+"sounds.packed_sheets", Container, CAudioMixerUser::instance()->getPackedSheetUpdate(), false);
00285 _Loaded = true;
00286
00287
00288 std::map<std::string, CSoundSerializer>::iterator first(Container.begin()), last(Container.end());
00289 for (; first != last; ++first)
00290 {
00291 if (first->second.Sound != 0)
00292 addSound(first->second.Sound);
00293 }
00294
00295 Container.clear();
00296 }
00297
00298
00299
00300
00301
00302 void CSoundBank::unload()
00303 {
00304 nlassert(_Loaded);
00305
00306 TSoundTable::iterator first(_Sounds.begin()), last(_Sounds.end());
00307 for (; first != last; ++first)
00308 {
00309 delete first->second;
00310 }
00311
00312 _Sounds.clear();
00313 _Loaded = false;
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 }
00339
00340
00341
00342
00343 bool CSoundBank::isLoaded()
00344 {
00345 return _Loaded;
00346 }
00347
00348
00349
00350
00351 CSound* CSoundBank::getSound(const NLMISC::TStringId &name)
00352 {
00353
00354 TSoundTable::iterator iter = _Sounds.find(name);
00355 if ( iter == _Sounds.end() )
00356 {
00357 return 0;
00358 }
00359 else
00360 {
00361 return (*iter).second;
00362 }
00363 }
00364
00368 void CSoundBank::getNames( std::vector<NLMISC::TStringId> &names )
00369 {
00370 TSoundTable::const_iterator iter;
00371 for (iter = _Sounds.begin(); iter != _Sounds.end(); ++iter)
00372 {
00373 names.push_back((*iter).first);
00374
00375 }
00376 }
00377
00378
00379
00380
00381 uint CSoundBank::countSounds()
00382 {
00383 return _Sounds.size();
00384 }
00385
00386
00387 }
00388