00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_AUDIO_MIXER_USER_H
00027 #define NL_AUDIO_MIXER_USER_H
00028
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/sound/u_audio_mixer.h"
00031 #include "nel/misc/time_nl.h"
00032 #include "driver/source.h"
00033 #include "listener_user.h"
00034 #include "background_sound_manager.h"
00035 #include "mixing_track.h"
00036 #include "sound.h"
00037 #include <vector>
00038 #include <hash_set>
00039 #include <list>
00040
00041
00042 namespace NLSOUND {
00043
00044
00045 class CSimpleSource;
00046 class CEnvSoundUser;
00047 class CEnvEffect;
00048 class CSoundBank;
00049 class CSourceCommon;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 const uint MAX_TRACKS = 32;
00062
00064 template <class Pointer>
00065 struct THashPtr : public std::unary_function<const Pointer &, size_t>
00066 {
00067 size_t operator () (const Pointer &ptr) const
00068 {
00069 std::hash_set<uint>::hasher h;
00070
00071 return h.operator()(uint(ptr));
00072 }
00073 };
00074
00097 class CAudioMixerUser : public UAudioMixer
00098 {
00099 public:
00100
00102 CAudioMixerUser();
00104 static CAudioMixerUser *instance() { return _Instance; }
00106 virtual ~CAudioMixerUser();
00107
00108
00117 virtual void init();
00130 virtual void setPriorityReserve(TSoundPriority priorityChannel, uint reserve);
00143 virtual void setLowWaterMark(uint value);
00144
00145
00147 virtual void reset();
00149 virtual void enable( bool b );
00151
00156 virtual uint32 loadSampleBank(bool async, const std::string &filename, std::vector<std::string> *notfoundfiles=NULL );
00159 virtual bool unloadSampleBank( const std::string &filename);
00160 virtual void reloadSampleBanks(bool async);
00161 virtual uint32 getLoadedSampleSize();
00162
00163
00165
00166
00167
00168
00169
00171
00172
00173
00180 virtual USource *createSource( const std::string &name, bool spawn=false, TSpawnEndCallback cb=NULL, void *cbUserParam = NULL, CSoundContext *context = 0 );
00182 virtual USource *createSource( TSoundId id, bool spawn=false, TSpawnEndCallback cb=NULL, void *cbUserParam = NULL, CSoundContext *context = 0 );
00184 void addSource( CSourceCommon *source );
00188 virtual void removeSource( CSourceCommon *source );
00189
00191
00193
00194
00198 virtual void setListenerPos (const NLMISC::CVector &pos);
00199
00201 virtual UListener *getListener() { return &_Listener; }
00202
00203
00205 virtual void selectEnvEffects( const std::string &tag );
00207 virtual void update();
00208
00209
00211 virtual void getSoundNames( std::vector<std::string>& names ) const;
00213 virtual uint getPolyphony() const { return _NbTracks; }
00215 virtual uint getSourcesNumber() const { return _Sources.size(); }
00217 virtual uint getPlayingSourcesNumber() const;
00219 virtual uint getNumberAvailableTracks() const;
00220
00222 virtual std::string getSourcesStats() const;
00223
00224
00226 void applyListenerMove( const NLMISC::CVector& listenerpos );
00228
00230
00234
00236
00237
00238
00239
00241 virtual void setSamplePath(const std::string& path) { _SamplePath = path; }
00242
00243
00244 CBackgroundSoundManager *getBackgroundSoundManager() { return _BackgroundSoundManager; }
00246 virtual void writeProfile(std::ostream& out);
00247
00248 virtual void loadBackgroundSoundFromRegion (const NLLIGO::CPrimRegion ®ion);
00249 virtual void loadBackgroundEffectsFromRegion (const NLLIGO::CPrimRegion ®ion);
00250 virtual void loadBackgroundSamplesFromRegion (const NLLIGO::CPrimRegion ®ion);
00251 virtual void loadBackgroundSound (const std::string &continent);
00252 virtual void playBackgroundSound ();
00253 virtual void stopBackgroundSound ();
00254
00255
00256
00258 ISoundDriver* getSoundDriver();
00259
00260 void registerBufferAssoc(CSound *sound, IBuffer *buffer);
00261 void unregisterBufferAssoc(CSound *sound, IBuffer *buffer);
00262
00263 void bufferUnloaded(IBuffer *buffer);
00264
00265 void setBackgroundFlags(const TBackgroundFlags &backgroundFlags);
00266
00267
00268
00269
00270 CTrack *getFreeTrack(CSimpleSource *source);
00271 void freeTrack(CTrack *track);
00272
00273 void incPlayingSource() { ++_PlayingSources; };
00274 void decPlayingSource() { --_PlayingSources; };
00275
00276
00277 public:
00279 class IMixerUpdate
00280 {
00281 public:
00282 virtual void onUpdate() =0;
00283 };
00284
00286 void registerUpdate(IMixerUpdate *pmixerUpdate);
00288 void unregisterUpdate(IMixerUpdate *pmixerUpdate);
00289
00291 class IMixerEvent
00292 {
00293 public:
00294 virtual void onEvent() =0;
00295 };
00296
00298 void addEvent(IMixerEvent *pmixerEvent, const NLMISC::TTime &date);
00300 void removeEvents(IMixerEvent *pmixerEvent);
00301
00303 void addSourceWaitingForPlay(CSimpleSource *source);
00304
00305 private:
00306
00307 typedef std::hash_set<CSourceCommon*, THashPtr<CSourceCommon*> > TSourceContainer;
00308 typedef std::hash_set<IMixerUpdate*, THashPtr<IMixerUpdate*> > TMixerUpdateContainer;
00309 typedef std::hash_map<IBuffer*, std::vector<class CSound*>, THashPtr<IBuffer*> > TBufferToSourceContainer;
00310 typedef std::multimap<NLMISC::TTime, IMixerEvent*> TTimedEventContainer;
00311 typedef std::multimap<IMixerEvent*, TTimedEventContainer::iterator> TEventContainer;
00312
00313 protected:
00315 TMixerUpdateContainer _UpdateList;
00317 std::vector<std::pair<IMixerUpdate*, bool> > _UpdateEventList;
00319
00321
00323
00325
00327
00328
00330
00332
00334
00336
00338
00340
00341
00342 virtual void getPlayingSoundsPos(std::vector<std::pair<bool, NLMISC::CVector> > &pos);
00343
00344
00345 private:
00347 std::vector<CTrack*> _FreeTracks;
00348
00350 std::list<CSimpleSource*> _SourceWaitingForPlay;
00351
00353 uint32 _PriorityReserve[NbSoundPriorities];
00355 uint32 _ReserveUsage[NbSoundPriorities];
00357 uint32 _LowWaterMark;
00358
00360 static CAudioMixerUser *_Instance;
00361
00363 ISoundDriver *_SoundDriver;
00364
00366 CBackgroundSoundManager *_BackgroundSoundManager;
00367
00369
00370
00372
00373
00375 CListenerUser _Listener;
00376
00378 NLMISC::CVector _ListenPosition;
00379
00383
00384
00386
00387
00389 std::string _SamplePath;
00390
00392 TBufferToSourceContainer _BufferToSources;
00393
00395
00396
00397 public:
00398
00400 TSourceContainer _Sources;
00402
00403
00404 uint32 _PlayingSources;
00405
00406
00408
00409
00410 private:
00411
00413 CEnvEffect *_CurEnvEffect;
00414
00415 public:
00416
00418 CTrack *_Tracks [MAX_TRACKS];
00419
00421 uint _NbTracks;
00422
00424 uint _MaxNbTracks;
00425
00427 bool _Leaving;
00428
00429 NLMISC::TTicks _StartTime;
00430
00431 uint32 curTime() { return (uint32) (NLMISC::CTime::getLocalTime() - _StartTime); }
00432
00433
00434 #define NL_PROFILE_MIXER 1
00435 #if NL_PROFILE_MIXER
00436 public:
00437
00438 double _UpdateTime;
00439 double _CreateTime;
00440 uint32 _UpdateCount;
00441 uint32 _CreateCount;
00442 #endif
00443
00444 friend struct displaySoundInfoClass;
00445
00446 };
00447
00448
00450 const char *getPriorityStr( TSoundPriority p );
00451
00452
00453 }
00454
00455
00456 #endif // NL_AUDIO_MIXER_USER_H
00457
00458