# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

audio_mixer_user.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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  * Max number of tracks (physical sources)
00054  *
00055  * Note: In the Windows implementation of OpenAL, AL_SOURCES_MAX is currently defined to 32
00056  * but seems unused. In alc.c (AL Context implementation), the function alcOpenDevice() contains:
00057  *     // Check how many Hardware 3D Streaming buffers can be created
00058  *     device->MaxNoOfSources = dsCaps.dwMaxHw3DStreamingBuffers;
00059  * and this value seems to be no more than 32 in DirectX8.
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                 // transtype the pointer into int then hash it
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(/* uint32 balance_period=AUTOBALANCE_DEFAULT_PERIOD */);
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 //      virtual void                            loadEnvEffects( const char *filename );
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 //      virtual void                            loadSoundBank( const std::string &path );
00166 
00167         
00168         // Load environment sounds ; treeRoot can be null if you don't want an access to the envsounds
00169 //      virtual void                            loadEnvSounds( const char *filename, UEnvSound **treeRoot=NULL );
00171         virtual TSoundId                        getSoundId( const std::string &name );
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 //      void                                            giveTrack( CSimpleSource *source );
00193 //      void                                            releaseTrack( CSimpleSource *source );
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 //      CEnvSoundUser                           *getEnvSounds()                                                 { return _EnvSounds; }
00230         const NLMISC::CVector&          getListenPosVector() const                              { return _ListenPosition; }
00234 //      void                                            removeMySource( USource *source );
00236 //      void                                            addAmbiantSound( CSound *sound )                { _AmbSounds.insert( sound ); }
00237         // Allow to load sound files (nss) when the corresponding wave file is missing (see CSound)
00238         //static void                                   allowMissingWave( bool b )                              { CSound::allowMissingWave( b ); }      
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 &region);
00249         virtual void                            loadBackgroundEffectsFromRegion (const NLLIGO::CPrimRegion &region);
00250         virtual void                            loadBackgroundSamplesFromRegion (const NLLIGO::CPrimRegion &region);
00251         virtual void                            loadBackgroundSound (const std::string &continent);
00252         virtual void                            playBackgroundSound ();
00253         virtual void                            stopBackgroundSound ();
00254 
00255 //      virtual void                            setBackgroundSoundDayNightRatio (float ratio) { CBackgroundSoundManager::setDayNightRatio(ratio); }
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 //      bool                                            setPlaying(CSimpleSource *source);
00268 //      void                                            unsetPlaying(CSimpleSource *source);
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 //      std::vector<IMixerUpdate*>                                              _UpdateRemoveList;
00321         TTimedEventContainer                                                    _EventList;
00323         TEventContainer                                                                 _Events;
00325         std::vector<std::pair<NLMISC::TTime, IMixerEvent*> >    _EventListUpdate;
00327 //      std::vector<IMixerEvent*>                                               _EventRemoveList;
00328 
00330 //      void                                            balanceSources()                                                { if ( moreSourcesThanTracks() ) redispatchSourcesToTrack(); }
00332         void                                            getFreeTracks( uint nb, CTrack **tracks );
00334 //      void                                            computeEnvEffect( const NLMISC::CVector& listenerpos, bool force=false );
00336 //      bool                                            moreSourcesThanTracks() const                   { return _NbTracks < _Sources.size(); }
00338 //      void                                            redispatchSourcesToTrack();
00340 //      void                                            removeSource( TSourceContainer::iterator ips);
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         //TSoundMap                                     _Sounds;
00370 
00372 //      TSoundSet                                       _AmbSounds;
00373 
00375         CListenerUser                           _Listener;
00376 
00378         NLMISC::CVector                         _ListenPosition;
00379 
00383 //      CEnvSoundUser                           *_EnvSounds;
00384 
00386 //      uint32                                          _BalancePeriod;
00387 
00389         std::string                                     _SamplePath;
00390 
00392         TBufferToSourceContainer        _BufferToSources;
00393 
00395 //      CSoundBank                                      *_SoundBank;
00396 
00397 public: 
00398 
00400         TSourceContainer                _Sources;
00402 //      TSourceContainer                _PlayingSources;
00403 
00404         uint32                                  _PlayingSources;
00405 
00406 
00408 //      std::vector<CEnvEffect*>        _EnvEffects;
00409         
00410 private:
00411 
00413         CEnvEffect                                      *_CurEnvEffect;
00414 
00415 public: // Temp (EDIT)
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 } // NLSOUND
00454 
00455 
00456 #endif // NL_AUDIO_MIXER_USER_H
00457 
00458 /* End of audio_mixer_user.h */