NLSOUND::CEnvSoundUser Class Reference

#include <env_sound_user.h>

Inheritance diagram for NLSOUND::CEnvSoundUser:

NLSOUND::UEnvSound

Detailed Description

Implementation of UEnvSound

An envsound object is a node of an envsound tree, the root of which is returned by CAudioMixerUser::loadEnvSounds() and getEnvSounds(). The root is the world envsound: it has no bounds. The areas of children envsounds have bounds and they must be totally included in the area of their parent envsound.

Here is an example of an envsound tree:

* Root (world envsound) * / \ * Transition Transition * | | * Cave1 City * / \ * Transition Transition * | | * Building1 Building2 *

In a transition, the _Source object is the center source played when the listener not in the child area (CSimpleSource). In a non-transition envsound, the _Source object is the ambiant source (CAmbiantSource) which plays a stereo mix.

Author:
Olivier Cado

Nevrax France

Date:
2001

Definition at line 82 of file env_sound_user.h.

Public Member Functions

void addChild (CEnvSoundUser *child)
 Add a child (EDIT).

void addEnvTag (IPlayable *source, const std::string &tag="")
 CEnvSoundUser ()
 Constructor.

IBoundingShape * getBoundingShape ()
 Return the bounding shape (EDIT).

virtual std::vector< UEnvSound * > & getChildren ()
 Return the children envsounds.

CEnvSoundUsergetParent ()
 Return the parent (EDIT).

virtual void getPos (NLMISC::CVector &pos) const=0
 Return the position.

virtual const NLMISC::CVectorgetPos () const
 Return the position.

IPlayable * getSource ()
 Return the source (EDIT).

bool mustPlay () const
 Return the play flag.

virtual void play (bool children_too=false)
 Play this node, and all descendants if children_too is true.

void recompute ()
 Recompute the EnvSound. Call this method after having moved the 3D source (call only on the root env).

virtual void selectEnv (const char *tag, bool children_too=false)
 Select the current env for this node, and for all descendants if children_too is true.

virtual void setPos (const NLMISC::CVector &pos)
 Moves the envsound (and its transition envsound if it has one).

void setProperties (bool transition, IBoundingShape *bshape)
 Set properties (EDIT) (set a NULL shape for all world).

virtual void stop (bool children_too=false)
 Stop playing this node, and all descendants if children_too is true.

void update ()
 Update the stereo mixes (call evenly on the root) (recursive).

virtual ~CEnvSoundUser ()
 Destructor.


Protected Member Functions

void applySourcesMarks ()
CEnvSoundUserfindCurrentEnv (const NLMISC::CVector &listenerpos)
 Find the area where the listener is located (recursive).

uint32 getCount () const
 Count the envs in the tree (call on the root).

bool isRoot () const
 Return true if the envsound is the root of the hierarchy tree.

void markSources (const NLMISC::CVector &listenerpos, float gain)
void playSub (bool children_too=false)
 Play this node, and all descendants if children_too is true, but do not recompute.

void stopSub (bool children_too=false)
 Stop playing this node, and all descendants if children_too is true, but do not recompute.


Private Attributes

IBoundingShape * _BoundingShape
std::vector< CEnvSoundUser * > _Children
float _Gain
bool _Mark
CEnvSoundUser_Parent
bool _Play
IPlayable * _Source
std::vector< IPlayable * > _SrcBank
std::vector< std::string > _Tags
bool _Transition


Constructor & Destructor Documentation

NLSOUND::CEnvSoundUser::CEnvSoundUser  ) 
 

Constructor.

Definition at line 43 of file env_sound_user.cpp.

00043                              : _Play(false), _Source(NULL), _BoundingShape(NULL), _Transition(false), _Parent(NULL), _Mark(false), _Gain(1.0f)
00044 {
00045 }

NLSOUND::CEnvSoundUser::~CEnvSoundUser  )  [virtual]
 

Destructor.

Definition at line 51 of file env_sound_user.cpp.

References _SrcBank.

00052 {
00053         vector<CEnvSoundUser*>::iterator ipe;
00054         for ( ipe=_Children.begin(); ipe!=_Children.end(); ++ipe )
00055         {
00056                 delete (*ipe);
00057         }
00058 
00059         vector<IPlayable*>::iterator ipp;
00060         for ( ipp=_SrcBank.begin(); ipp!=_SrcBank.end(); ++ipp )
00061         {
00062                 // An IPlayable object can be a CAmbiantSource or a CSimpleSource.
00063                 // A CAmbiantSource removes its source (channels) in destructor, but not a CSimpleSource
00064                 if ( dynamic_cast<CSimpleSource*>(*ipp) )
00065                 {
00066                         CAudioMixerUser::instance()->removeSource( static_cast<CSimpleSource*>(*ipp) );
00067                 }
00068                 else
00069                 {
00070                         if ( (*ipp) != NULL )
00071                         {
00072                                 delete (*ipp);
00073                         }
00074                 }
00075         }
00076 
00077         if ( _BoundingShape != NULL )
00078         {
00079                 delete _BoundingShape;
00080         }
00081 }


Member Function Documentation

void NLSOUND::CEnvSoundUser::addChild CEnvSoundUser child  ) 
 

Add a child (EDIT).

Definition at line 588 of file env_sound_user.cpp.

References _Parent.

00589 {
00590         child->_Parent = this;
00591         _Children.push_back( child );
00592 }

void NLSOUND::CEnvSoundUser::addEnvTag IPlayable *  source,
const std::string &  tag = ""
 

Add an environment source/tag (EDIT) (set a NULL source for no source at all). The current source always becomes the first one.

Definition at line 487 of file env_sound_user.cpp.

References _Source, and _SrcBank.

00488 {
00489         _SrcBank.push_back( source );
00490         _Tags.push_back( tag );
00491 
00492         if ( _Source == NULL ) // becomes the first one and stays there
00493         {
00494                 _Source = source;
00495         }
00496 }

void NLSOUND::CEnvSoundUser::applySourcesMarks  )  [protected]
 

Definition at line 452 of file env_sound_user.cpp.

References _Mark, and _Source.

Referenced by recompute().

00453 {
00454         if ( ! _Play )
00455         {
00456                 _Mark = false;
00457         }
00458         if ( _Source != NULL )
00459         {
00460                 _Source->enable( _Mark, _Gain );
00461         }
00462         _Mark = false;
00463 
00464         // Apply on children
00465         vector<CEnvSoundUser*>::iterator ipe;
00466         for ( ipe=_Children.begin(); ipe!=_Children.end(); ++ipe )
00467         {
00468                 (*ipe)->applySourcesMarks();
00469         }
00470 }

CEnvSoundUser * NLSOUND::CEnvSoundUser::findCurrentEnv const NLMISC::CVector listenerpos  )  [protected]
 

Find the area where the listener is located (recursive).

Definition at line 303 of file env_sound_user.cpp.

Referenced by recompute().

00304 {
00305         // Find in children first (check from leaves to root)
00306         vector<CEnvSoundUser*>::iterator ipe = _Children.begin();
00307         CEnvSoundUser *found = NULL;
00308         while ( ! ( (found) || (ipe==_Children.end()) ) )
00309         {
00310                 found = (*ipe)->findCurrentEnv( listenerpos );
00311                 ipe++;
00312         }
00313         if ( found )
00314                 return found;
00315         else if ( (_BoundingShape == NULL) || (_BoundingShape->include( listenerpos )) )
00316                 return this;
00317         else
00318                 return NULL;
00319 }

IBoundingShape* NLSOUND::CEnvSoundUser::getBoundingShape  )  [inline]
 

Return the bounding shape (EDIT).

Definition at line 136 of file env_sound_user.h.

00136 { return _BoundingShape; }

std::vector< UEnvSound * > & NLSOUND::CEnvSoundUser::getChildren  )  [virtual]
 

Return the children envsounds.

Implements NLSOUND::UEnvSound.

Definition at line 376 of file env_sound_user.cpp.

00377 {
00378         return (vector<UEnvSound*>&)(_Children);
00379 }

uint32 NLSOUND::CEnvSoundUser::getCount  )  const [protected]
 

Count the envs in the tree (call on the root).

Definition at line 248 of file env_sound_user.cpp.

References uint32.

00249 {
00250         uint32 cnt=1;
00251         vector<CEnvSoundUser*>::const_iterator ipe;
00252         for ( ipe=_Children.begin(); ipe!=_Children.end(); ++ipe )
00253         {
00254                 cnt += (*ipe)->getCount();
00255         }
00256         return cnt;
00257 }

CEnvSoundUser* NLSOUND::CEnvSoundUser::getParent  )  [inline]
 

Return the parent (EDIT).

Definition at line 140 of file env_sound_user.h.

References _Parent.

00140 { return _Parent; }

virtual void NLSOUND::UEnvSound::getPos NLMISC::CVector pos  )  const [pure virtual, inherited]
 

Return the position.

virtual const NLMISC::CVector& NLSOUND::CEnvSoundUser::getPos  )  const [virtual]
 

Return the position.

IPlayable* NLSOUND::CEnvSoundUser::getSource  )  [inline]
 

Return the source (EDIT).

Definition at line 138 of file env_sound_user.h.

References _Source.

00138 { return _Source; }

bool NLSOUND::CEnvSoundUser::isRoot  )  const [inline, protected]
 

Return true if the envsound is the root of the hierarchy tree.

Definition at line 148 of file env_sound_user.h.

References _Parent.

Referenced by recompute().

00148 { return _Parent==NULL; }

void NLSOUND::CEnvSoundUser::markSources const NLMISC::CVector listenerpos,
float  gain
[protected]
 

Prepare the related sources to play (recursive). In each children branch, there must be an env which is not a transition, for the recursion to stop

Definition at line 408 of file env_sound_user.cpp.

References _Mark, _Parent, _Transition, and nlassert.

Referenced by recompute().

00409 {
00410         // Is the listener in a transition area ?
00411         if ( _Transition )
00412         {
00413                 //nldebug( "AM: EnvSound: Marking sources for transition between child and parent" );
00414 
00415                 // Compute the listener position to find the ratio between up and down envs
00416                 nlassert( (_Children.size() == 1) && (_Children[0] != NULL) && (_Parent != NULL) );
00417                 nlassert( _BoundingShape && _Children[0]->_BoundingShape );
00418                 float ratio = _BoundingShape->getRatio( listenerpos, _Children[0]->_BoundingShape );
00419                 nlassert( ratio >= 0.0f && ratio <= 1.0f );
00420 
00421                 // The child env plays at gain*ratio
00422                 // The recursion stops because the child env is not a transition area
00423                 _Children[0]->markSources( listenerpos, gain * ratio );
00424 
00425                 // The parent env (therefore the 3d source of the current env as well) plays at gain*(1-ratio)
00426                 // The recursion stops because the parent env is not a transition area
00427                 _Parent->markSources( listenerpos, gain * (1.0f-ratio) );
00428         }
00429         else
00430         {
00431                 //nldebug( "AM: EnvSound: Marking sources for environnement" );
00432 
00433                 // The listener in an environment, containing other environments (e.g. a town) or not (e.g. a room).
00434                 // The current env plays
00435                 _Mark = true;
00436                 _Gain = gain;
00437 
00438                 // The children env (next level only) play
00439                 vector<CEnvSoundUser*>::iterator ipe;
00440                 for( ipe=_Children.begin(); ipe!=_Children.end(); ++ipe )
00441                 {
00442                         (*ipe)->_Mark = true;
00443                         (*ipe)->_Gain = gain;
00444                 }
00445         }
00446 }

bool NLSOUND::CEnvSoundUser::mustPlay  )  const [inline]
 

Return the play flag.

Definition at line 122 of file env_sound_user.h.

00122 { return _Play; }

void NLSOUND::CEnvSoundUser::play bool  children_too = false  )  [virtual]
 

Play this node, and all descendants if children_too is true.

Implements NLSOUND::UEnvSound.

Definition at line 516 of file env_sound_user.cpp.

References playSub().

00517 {
00518         playSub( children_too );
00519 
00520         CAudioMixerUser::instance()->getEnvSounds()->recompute();
00521 }

void NLSOUND::CEnvSoundUser::playSub bool  children_too = false  )  [protected]
 

Play this node, and all descendants if children_too is true, but do not recompute.

Definition at line 538 of file env_sound_user.cpp.

References _Parent, _Play, and _Transition.

Referenced by play().

00539 {
00540         _Play = true;
00541 
00542         // Start transition as well
00543         if ( (_Parent != NULL) && ( _Parent->_Transition ) )
00544         {
00545                 _Parent->_Play = true;
00546         }
00547 
00548         // Apply to descendants
00549         if ( children_too )
00550         {
00551                 vector<CEnvSoundUser*>::iterator ipc;
00552                 for ( ipc=_Children.begin(); ipc!=_Children.end(); ++ipc )
00553                 {
00554                         (*ipc)->playSub( children_too );
00555                 }
00556         }
00557 }

void NLSOUND::CEnvSoundUser::recompute  ) 
 

Recompute the EnvSound. Call this method after having moved the 3D source (call only on the root env).

Definition at line 385 of file env_sound_user.cpp.

References applySourcesMarks(), findCurrentEnv(), isRoot(), markSources(), and nlassert.

00386 {
00387         nlassert( isRoot() );
00388 
00389         // Find the area of the listener
00390         CVector listenerpos;
00391         CAudioMixerUser::instance()->getListener()->getPos( listenerpos );
00392         CEnvSoundUser *current = findCurrentEnv( listenerpos );
00393 
00394         // Mark the envs that have to play their source
00395         if ( current != NULL )
00396         {
00397                 current->markSources( listenerpos, 1.0f );
00398         }
00399         
00400         // Enable/disable the sources in the hierarchy, and reset the marks
00401         applySourcesMarks();
00402 }

void NLSOUND::CEnvSoundUser::selectEnv const char *  tag,
bool  children_too = false
[virtual]
 

Select the current env for this node, and for all descendants if children_too is true.

Implements NLSOUND::UEnvSound.

Definition at line 190 of file env_sound_user.cpp.

References _Source, _SrcBank, nldebug, and uint.

00191 {
00192         uint i;
00193         for ( i=0; i!= _Tags.size(); i++ )
00194         {
00195                 if ( _Tags[i] == string(tag) )
00196                 {
00197                         if ( _Source != NULL )
00198                         {
00199                                 _Source->enable( false, 1.0f );
00200                         }
00201                         _Source = _SrcBank[i];
00202                         nldebug( "AM: EnvSound: Environment changed to %s", tag );
00203                         CAudioMixerUser::instance()->getEnvSounds()->recompute();
00204                         break;
00205                 }
00206         }
00207         //nldebug( "AM: EnvSound: Environment %s not found", tag );
00208         // Don't change _Source if not found
00209 
00210         // Apply to descendants
00211         if ( children_too )
00212         {
00213                 vector<CEnvSoundUser*>::iterator ipc;
00214                 for ( ipc=_Children.begin(); ipc!=_Children.end(); ++ipc )
00215                 {
00216                         (*ipc)->selectEnv( tag, children_too );
00217                 }
00218         }
00219 }

void NLSOUND::CEnvSoundUser::setPos const NLMISC::CVector pos  )  [virtual]
 

Moves the envsound (and its transition envsound if it has one).

Implements NLSOUND::UEnvSound.

Definition at line 341 of file env_sound_user.cpp.

References _BoundingShape, _Parent, _Source, and _Transition.

00342 {
00343         if ( _BoundingShape != NULL )
00344         {
00345                 // Get the vector between the pos of this envsound and the pos of its transition envsound
00346                 CVector newpos;
00347                 if ( (_Parent != NULL) && ( _Parent->_Transition ) )
00348                 {
00349                         newpos = pos + _Parent->_BoundingShape->getCenter() - _BoundingShape->getCenter();
00350                 }
00351                 else
00352                 {
00353                         newpos = pos;
00354                 }
00355 
00356                 // Set the new pos
00357                 _BoundingShape->setCenter( pos );
00358                 if ( (_Parent != NULL) && ( _Parent->_Transition ) )
00359                 {
00360                         _Parent->_BoundingShape->setCenter( newpos );
00361                         if ( _Parent->_Source != NULL )
00362                         {
00363                                 _Parent->_Source->moveTo( newpos );
00364                         }
00365                 }
00366 
00367                 // Recompute the entire tree
00368                 CAudioMixerUser::instance()->getEnvSounds()->recompute();
00369         }
00370 }

void NLSOUND::CEnvSoundUser::setProperties bool  transition,
IBoundingShape *  bshape
 

Set properties (EDIT) (set a NULL shape for all world).

Definition at line 476 of file env_sound_user.cpp.

References _Transition.

00477 {
00478         _Transition = transition;
00479         _BoundingShape = bshape;
00480 }

void NLSOUND::CEnvSoundUser::stop bool  children_too = false  )  [virtual]
 

Stop playing this node, and all descendants if children_too is true.

Implements NLSOUND::UEnvSound.

Definition at line 527 of file env_sound_user.cpp.

References stopSub().

00528 {
00529         stopSub( children_too );
00530         
00531         CAudioMixerUser::instance()->getEnvSounds()->recompute();
00532 }

void NLSOUND::CEnvSoundUser::stopSub bool  children_too = false  )  [protected]
 

Stop playing this node, and all descendants if children_too is true, but do not recompute.

Definition at line 563 of file env_sound_user.cpp.

References _Parent, _Play, and _Transition.

Referenced by stop().

00564 {
00565         _Play = false;
00566 
00567         // Stop transition as well
00568         if ( (_Parent != NULL) && ( _Parent->_Transition ) )
00569         {
00570                 _Parent->_Play = false;
00571         }
00572 
00573         // Apply to descendants
00574         if ( children_too )
00575         {
00576                 vector<CEnvSoundUser*>::iterator ipc;
00577                 for ( ipc=_Children.begin(); ipc!=_Children.end(); ++ipc )
00578                 {
00579                         (*ipc)->stopSub( children_too );
00580                 }
00581         }
00582 }

void NLSOUND::CEnvSoundUser::update  ) 
 

Update the stereo mixes (call evenly on the root) (recursive).

Definition at line 286 of file env_sound_user.cpp.

References _Source.

00287 {
00288         if ( _Source != NULL )
00289         {
00290                 _Source->update();
00291         }
00292         vector<CEnvSoundUser*>::iterator ipe;
00293         for ( ipe=_Children.begin(); ipe!=_Children.end(); ++ipe )
00294         {
00295                 (*ipe)->update();
00296         }
00297 }


Field Documentation

IBoundingShape* NLSOUND::CEnvSoundUser::_BoundingShape [private]
 

Definition at line 182 of file env_sound_user.h.

Referenced by setPos().

std::vector<CEnvSoundUser*> NLSOUND::CEnvSoundUser::_Children [private]
 

Definition at line 189 of file env_sound_user.h.

float NLSOUND::CEnvSoundUser::_Gain [private]
 

Definition at line 193 of file env_sound_user.h.

bool NLSOUND::CEnvSoundUser::_Mark [private]
 

Definition at line 192 of file env_sound_user.h.

Referenced by applySourcesMarks(), and markSources().

CEnvSoundUser* NLSOUND::CEnvSoundUser::_Parent [private]
 

Definition at line 188 of file env_sound_user.h.

Referenced by addChild(), getParent(), isRoot(), markSources(), playSub(), setPos(), and stopSub().

bool NLSOUND::CEnvSoundUser::_Play [private]
 

Definition at line 174 of file env_sound_user.h.

Referenced by playSub(), and stopSub().

IPlayable* NLSOUND::CEnvSoundUser::_Source [private]
 

Definition at line 177 of file env_sound_user.h.

Referenced by addEnvTag(), applySourcesMarks(), getSource(), selectEnv(), setPos(), and update().

std::vector<IPlayable*> NLSOUND::CEnvSoundUser::_SrcBank [private]
 

Definition at line 178 of file env_sound_user.h.

Referenced by addEnvTag(), selectEnv(), and ~CEnvSoundUser().

std::vector<std::string> NLSOUND::CEnvSoundUser::_Tags [private]
 

Definition at line 179 of file env_sound_user.h.

bool NLSOUND::CEnvSoundUser::_Transition [private]
 

Definition at line 185 of file env_sound_user.h.

Referenced by markSources(), playSub(), setPos(), setProperties(), and stopSub().


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