Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Audio::SceneManager Class Reference

#include <SceneManager.h>

Inheritance diagram for Audio::SceneManager:
Singleton< SceneManager >

Public Types

typedef VirtualIterator
< SharedPtr< Scene > > 
SceneIterator
 

Public Member Functions

 SceneManager () throw ()
 
virtual ~SceneManager ()
 
virtual SharedPtr< SourcecreateSource (SharedPtr< Sound > sound, bool looping=false) throw (Exception)
 
SharedPtr< SourcecreateSource (SharedPtr< SourceTemplate > tpl) throw (Exception)
 
SharedPtr< SourcecreateSource (SharedPtr< SourceTemplate > tpl, const std::string &name) throw (Exception)
 
virtual void destroySource (SharedPtr< Source > source) throw ()
 
void playSource (SharedPtr< SourceTemplate > tpl, const std::string &sceneName, LVector3 position, Vector3 direction, Vector3 velocity, Scalar radius) throw (Exception)
 
void playSource (SharedPtr< SourceTemplate > tpl, const std::string &soundName, const std::string &sceneName, LVector3 position, Vector3 direction, Vector3 velocity, Scalar radius) throw (Exception)
 
virtual SharedPtr< ScenecreateScene (const std::string &name) throw (DuplicateObjectException)
 
virtual SharedPtr< ScenegetScene (const std::string &name) const throw (NotFoundException)
 
virtual void destroyScene (const std::string &name) throw (NotFoundException)
 
virtual void setSceneActive (const std::string &name, bool active) throw (NotFoundException)
 
virtual bool getSceneActive (const std::string &name) throw (NotFoundException)
 
virtual SharedPtr< ListenergetRootListener () const throw ()
 
virtual SharedPtr< SceneIteratorgetSceneIterator () const throw ()
 
virtual SharedPtr< SceneIteratorgetActiveSceneIterator () const throw ()
 
virtual void setRenderer (SharedPtr< Renderer > renderer) throw (Exception)
 
SharedPtr< RenderergetRenderer () const throw ()
 
virtual void commit () throw (Exception)
 
Duration getPositionUpdateFrequency () const throw ()
 
Duration getListenerUpdateFrequency () const throw ()
 
Duration getAttributeUpdateFrequency () const throw ()
 
Duration getActivationFrequency () const throw ()
 
virtual void setPositionUpdateFrequency (Duration interval) const throw ()
 
virtual void setListenerUpdateFrequency (Duration interval) const throw ()
 
virtual void setAttributeUpdateFrequency (Duration interval) const throw ()
 
virtual void setActivationFrequency (Duration interval) const throw ()
 
virtual unsigned int getMaxSources () const throw ()
 
virtual void setMaxSources (unsigned int n) throw (Exception)
 
virtual float getMinGain () const throw ()
 
virtual void setMinGain (float gain) throw (Exception)
 
virtual double getMaxDistance () const throw ()
 
virtual void setMaxDistance (double distance) throw (Exception)
 
virtual void notifySourcePlaying (SharedPtr< Source > source, SharedPtr< Scene > scene, bool playing) throw (Exception)
 

Protected Member Functions

const SharedPtr< Renderer > & internalRenderer () const
 
void addScene (SharedPtr< Scene > scene) throw (DuplicateObjectException)
 
virtual void activationPhaseImpl () throw (Exception)
 
virtual void updateSourcesImpl (bool withAttributes) throw (Exception)
 
virtual void updateListenerImpl (bool withAttributes) throw (Exception)
 
- Protected Member Functions inherited from Singleton< SceneManager >
 ~Singleton ()
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< SceneManager >
static SceneManager * getSingleton ()
 
- Static Protected Member Functions inherited from Singleton< SceneManager >
static void initializeSingleton ()
 
static void deinitializeSingleton ()
 
- Static Protected Attributes inherited from Singleton< SceneManager >
static SceneManager * _singletonInstance
 

Detailed Description

Audio Scene Manager.

Remarks
Use it to create and manage sources, listeners, instantiate source templates, scenes, and other various scene management tasks.
SceneManager s will handle the complex task of prioritizing sources, managing
source channels, volume levels, and doing all kinds of culling.
Another responsability of scene managers is the manipulation and analysis of
environments to feed environmental information to capable renderers.
Inactive sources without external references are automatically destroyed when
they become permanently inactive (notice this requirement is merely approximate - implementations are free to make reasonable approximations for the determination of such condition as "permanently inactive"). This allows for "fire & forget" sources, where the source is activated (by calling startPlaying), and then immediately unreferenced. The SceneManager will keep track of that source, as it would any other, until it stops playing (make sure though it's not a looping source!), and will remove it when that happens, freeing any associated resources.
In fact this is so common as to make it part of the API: playSource does exactly
that, it creates a source, plays it, and discards the reference.
Thus, SceneManager s are the hub of all activity relating sound. If you're doing
anything bypassing the SceneManager, you're doing it wrong.
This class is NOT abstract, it provides a very basic implementation that does
nothing special.
Note
You must set a renderer or you won't hear anything ;)

Definition at line 59 of file SceneManager.h.

Member Typedef Documentation

Constructor & Destructor Documentation

Audio::SceneManager::SceneManager ( )
throw (
)

Construct a new instance of the manager

Remarks
End-users of the class shouldn't be using this. Singletons need it. Instead, end-users should use the Root class to find manager plugins they like.

Definition at line 116 of file SceneManager.cpp.

116  :
117  data(new SceneManagerData)
118  {
119  }
Audio::SceneManager::~SceneManager ( )
virtual

Definition at line 121 of file SceneManager.cpp.

122  {
123  }

Member Function Documentation

void Audio::SceneManager::activationPhaseImpl ( )
throw (Exception
)
protectedvirtual

Synchronize activation state with the scenes

Definition at line 454 of file SceneManager.cpp.

References Audio::TVector3< T >::distanceSquared(), Audio::SourcePriorityRef::gain, Audio::SimpleScene::getActiveSources(), Audio::SimpleScene::getActiveSourcesEnd(), Audio::SimpleScene::getListener(), Audio::Listener::getPosition(), and Audio::RenderableSource::UPDATE_LOCATION.

456  {
457  // Just clear the active source set and recreate it from scratch.
458  // Use a "source ref heap" to find the most relevant sources (using the approximated
459  // intensity as priority). Since the heap will copy things all over, use cheap
460  // "SourceIterator"s as entries. These are SimpleScene-specific, so any subclass of
461  // SceneManager will probably want to override the activation phase.
462 
463  const SharedPtr<Renderer> &renderer = internalRenderer();
464 
465  LScalar maxDistanceSq = data->maxDistance * data->maxDistance;
466 
467  std::vector<SourcePriorityRef> selection;
468  bool heapified = false;
469  selection.reserve(data->maxSources+1);
470 
471  for (SceneManagerData::SceneMap::iterator it = data->activeScenes.begin();
472  it != data->activeScenes.end();
473  ++it)
474  {
475  SimpleScene *scene = dynamic_cast<SimpleScene*>(it->second.get());
476  Listener &listener = scene->getListener();
477 
478  for (SimpleScene::SourceIterator sit = scene->getActiveSources(),
479  send = scene->getActiveSourcesEnd();
480  sit != send;
481  ++sit)
482  {
483  if ( (*sit)->getSourceListener().get() ) {
484  // Must invoke the listener to get updated positions
485  (*sit)->getSourceListener()->onUpdate(**sit, RenderableSource::UPDATE_LOCATION);
486  }
487 
488  if (listener.getPosition().distanceSquared((*sit)->getPosition()) < maxDistanceSq) {
489  SourcePriorityRef ref(sit, listener, scene);
490  if (ref.gain > data->minGain) {
491  selection.push_back(ref);
492  if (selection.size() > data->maxSources) {
493  if (!heapified) {
494  make_heap(selection.begin(), selection.end());
495  heapified = true;
496  } else {
497  push_heap(selection.begin(), selection.end());
498  }
499  while (selection.size() > data->maxSources) {
500  pop_heap(selection.begin(), selection.end());
501  selection.resize( selection.size()-1 );
502  }
503  }
504  }
505  }
506  }
507  }
508 
510  for (std::vector<SourcePriorityRef>::const_iterator it = selection.begin(); it != selection.end(); ++it) {
511  newSources.insert(
512  SceneManagerData::SourceRef(
513  *(it->iter),
514  dynamic_cast<SimpleScene*>(it->scene)->shared_from_this()
515  ) );
516  }
517 
518  // Detach deactivated sources
519  for (SceneManagerData::SourceRefSet::iterator sit = data->activeSources.begin(); sit != data->activeSources.end(); ++sit) {
520  if (newSources.find(*sit) == newSources.end())
521  renderer->detach(sit->source);
522  }
523 
524  // Attach newly activated sources, detach and remove finished ones
525  for (SceneManagerData::SourceRefSet::iterator nit = newSources.begin(); nit != newSources.end(); ) {
526  bool erase = false;
527  if (data->activeSources.find(*nit) == data->activeSources.end()) {
528  // Newly activated source
529  renderer->attach(nit->source);
530  nit->needsActivation = true;
531  } else {
532  // Pre-existing source - check if it's finished
533  if (!nit->source->getRenderable()->isPlaying()) {
534  // Give the renderable an opportunity to restart itself
535  // (by calling update without any update flag set)
536  nit->source->getRenderable()->update(0, nit->scene->getListener());
537 
538  if (!nit->source->getRenderable()->isPlaying()) {
539  // Finished - detach stop and remove
540  renderer->detach(nit->source);
541  nit->source->stopPlaying();
542  erase = true;
543 
544  // Check if it has a listener, notify in that case
545  SharedPtr<SourceListener> listener = nit->source->getSourceListener();
546  if (listener.get() != NULL && listener->wantPlayEvents()) {
547  listener->onEndOfStream(*nit->source);
548  }
549  }
550  }
551  }
552  if (erase)
553  newSources.erase(nit++);
554  else
555  ++nit;
556  }
557 
558  // Swap sets
559  data->activeSources.swap(newSources);
560  }
void Audio::SceneManager::addScene ( SharedPtr< Scene scene)
throw (DuplicateObjectException
)
protected

Add a new scene

See Also
createScene

Definition at line 186 of file SceneManager.cpp.

188  {
189  if ( data->activeScenes.count(scene->getName())
190  || data->inactiveScenes.count(scene->getName()) )
191  throw(DuplicateObjectException(scene->getName()));
192 
193  data->inactiveScenes[scene->getName()] = scene;
194  }
void Audio::SceneManager::commit ( )
throw (Exception
)
virtual

Commit changes done between frames

Remarks
Depending on the underlying implementation, changes applied to sources may or may not be immediately available to the renderer. Calling commit() ensures that they are. Furthermore, some scene managers may use commit boundaries to ensure frame coherence (the renderer receives information coherent to the state as it was at the commit), though this is certainly not a requirement.
The process may be lengthy and throw various exceptions.

Definition at line 392 of file SceneManager.cpp.

References Audio::getRealTime(), and realTime().

393  {
395  bool needActivation = ((realTime - getActivationFrequency()) >= data->lastActivationTime);
396  bool needPosUpdates = ((realTime - getPositionUpdateFrequency()) >= data->lastPositionUpdateTime);
397  bool needAttUpdates = ((realTime - getAttributeUpdateFrequency()) >= data->lastAttributeUpdateTime);
398  bool needListenerUpdate = ((realTime - getListenerUpdateFrequency()) >= data->lastListenerUpdateTime);
399  bool needListenerAttUpdate = ((realTime - getAttributeUpdateFrequency()) >= data->lastListenerAttUpdateTime);
400 
401  // If we have an activation phase, in order for it to be effective
402  // we'll need an update phase as well.
403  needPosUpdates = needPosUpdates || needActivation;
404 
405  internalRenderer()->beginTransaction();
406 
407  if (needActivation) {
409 
410  data->lastActivationTime = realTime;
411  }
412 
413  if (needListenerUpdate) {
414  updateListenerImpl(needListenerAttUpdate);
415 
416  data->lastListenerUpdateTime = realTime;
417  if (needListenerAttUpdate)
418  data->lastListenerAttUpdateTime = realTime;
419  }
420 
421  if (needPosUpdates || needAttUpdates) {
422  updateSourcesImpl(needAttUpdates);
423 
424  data->lastPositionUpdateTime = realTime;
425  if (needAttUpdates)
426  data->lastAttributeUpdateTime = realTime;
427  }
428 
429  internalRenderer()->commitTransaction();
430  }
SharedPtr< Scene > Audio::SceneManager::createScene ( const std::string &  name)
throw (DuplicateObjectException
)
virtual

Create a new named scene

Definition at line 196 of file SceneManager.cpp.

Referenced by initScenes(), Audio::Test::testComplexScene(), Audio::Test::testMultiStreaming(), Audio::Test::testMultiStreaming2(), Audio::Test::testRendererless(), Audio::Test::testSimpleScene(), Audio::Test::testSimpleSceneWTemplates(), and Audio::Test::testStreaming().

198  {
199  SharedPtr<Scene> scenePtr(new SimpleScene(name));
200  addScene(scenePtr);
201  return scenePtr;
202  }
SharedPtr< Source > Audio::SceneManager::createSource ( SharedPtr< Sound sound,
bool  looping = false 
)
throw (Exception
)
virtual

Create a new source based on the speicified sound

Remarks
All the attributes will hold unspecified values, so you have to fill them in.}
Note
The sound must be associated to the correct renderer, or bad things will happen.
See Also
Renderer, which creates sounds.

Definition at line 132 of file SceneManager.cpp.

Referenced by Audio::Test::testComplexScene(), Audio::Test::testMultiStreaming(), Audio::Test::testMultiStreaming2(), Audio::Test::testSimpleScene(), Audio::Test::testSimpleSceneWTemplates(), and Audio::Test::testStreaming().

134  {
135  if (!internalRenderer()->owns(sound))
136  throw Exception("Invalid sound: incompatible renderers used");
137 
138  return SharedPtr<Source>( new SimpleSource(sound, looping) );
139  }
SharedPtr< Source > Audio::SceneManager::createSource ( SharedPtr< SourceTemplate tpl)
throw (Exception
)

Create a new source based on the specified template

Remarks
All location information will hold unspecified values, so you have to fill them in.

Definition at line 141 of file SceneManager.cpp.

143  {
144  return createSource(tpl, tpl->getSoundName());
145  }
SharedPtr< Source > Audio::SceneManager::createSource ( SharedPtr< SourceTemplate tpl,
const std::string &  name 
)
throw (Exception
)

Create a new source based on the specified template, but overriding its sound resource.

Remarks
All location information will hold unspecified values, so you have to fill them in.
It's useful to have "mode" templates that can be used to spawn many sources based
on many different streams. Eg: a "music" template for spawning music tracks, a "radio" template for spawning radio voiceovers, etc...

Definition at line 147 of file SceneManager.cpp.

149  {
150  SharedPtr<Source> source = createSource(
151  internalRenderer()->getSound(
152  name,
153  tpl->getSoundType(),
154  tpl->isStreaming() ),
155  tpl->isLooping() );
156 
157  source->setCosAngleRange( tpl->getCosAngleRange() );
158  source->setPerFrequencyRadiusRatios( tpl->getPerFrequencyRadiusRatios() );
159  source->setReferenceFreqs( tpl->getReferenceFreqs() );
160  source->setGain( tpl->getGain() );
161  source->setAttenuated( tpl->isAttenuated() );
162  source->setRelative( tpl->isRelative() );
163 
164  return source;
165  }
void Audio::SceneManager::destroyScene ( const std::string &  name)
throw (NotFoundException
)
virtual

Destroy an existing scene by its name

Definition at line 220 of file SceneManager.cpp.

Referenced by Audio::Test::clearScene().

222  {
223  // By simply unreferencing, it should get destroyed when all references are released.
224  // Which is good for multithreading - never destroy something that is being referenced.
225  // Any active sources will get deactivated in the next update since there aren't any active scenes
226  // containing them.
227  data->activeScenes.erase(name);
228  data->inactiveScenes.erase(name);
229  }
void Audio::SceneManager::destroySource ( SharedPtr< Source source)
throw (
)
virtual

Destroy a source created with this manager

Definition at line 167 of file SceneManager.cpp.

169  {
170  // By simply unreferencing, it should get destroyed when all references are released.
171  // Which is good for multithreading - never destroy something that is being referenced.
172 
173  // We cannot have playing sources without references within the manager
174  // Since it's stopped, it will eventually be removed from the active list if there.
175  if (source->isPlaying())
176  source->stopPlaying();
177 
178  // Remove all references to it within every scene
179  SceneManagerData::SceneMap::iterator it;
180  for (it = data->activeScenes.begin(); it != data->activeScenes.end(); ++it)
181  it->second->remove(source);
182  for (it = data->inactiveScenes.begin(); it != data->inactiveScenes.end(); ++it)
183  it->second->remove(source);
184  }
Duration Audio::SceneManager::getActivationFrequency ( ) const
throw (
)

Return source activation frequency

Remarks
Sources may become active or inactive over time, and depending on the impementation the effects may not be immediate. More so, since there's a "maximum renderable sources" limit, activation may depend on a lot of factors, and not just a call to startPlaying(). So, activation updates are the costlier of all updates since it involves evaluating and prioritizing all sources in all scenes, not just active ones (sources). Frequent activation passes are however necessary (but perhaps not that often) since otherwise sources that didn't start off as active may never become so.

Definition at line 621 of file SceneManager.cpp.

623  {
624  return data->activationFrequency;
625  }
SharedPtr< SceneManager::SceneIterator > Audio::SceneManager::getActiveSceneIterator ( ) const
throw (
)
virtual

Get an iterator over all active scenes

Definition at line 383 of file SceneManager.cpp.

385  {
386  return SharedPtr<SceneIterator>(
387  new VirtualValuesIterator<SceneManagerData::SceneMap::iterator>(
388  data->activeScenes.begin(),
389  data->activeScenes.end() ) );
390  }
Duration Audio::SceneManager::getAttributeUpdateFrequency ( ) const
throw (
)

Return attribute update frequency

Remarks
Source attribute updates are rare but necessary. They are very costly since all active sources must be updated (and each update is far more costly than simple position updates). This value specifies how often they're updated, however the actual interval may vary depending on the implementation (it's a mere guideline).

Definition at line 615 of file SceneManager.cpp.

617  {
618  return data->attributeUpdateFrequency;
619  }
Duration Audio::SceneManager::getListenerUpdateFrequency ( ) const
throw (
)

Return listener update frequency

Remarks
Position updates are a very important kind of update that needs to be performed regularly and quite often - even more so for listeners. Since there are very few listeners (one per scene), they're perhaps not so costly, or perhaps so? (depending on the underlying implementation). This value specifies how often they're updated, however the actual interval may vary depending on the implementation (it's a mere guideline).

Definition at line 609 of file SceneManager.cpp.

611  {
612  return data->listenerUpdateFrequency;
613  }
double Audio::SceneManager::getMaxDistance ( ) const
throw (
)
virtual

Get the maximum distance of active sources

Remarks
This value specifies the maximum distance of active sources. If a source is at a greater distance from the listener, it is ignored and deactivated, to conserve resources.
See Also
For more culling options: get/setMinGain

Definition at line 355 of file SceneManager.cpp.

357  {
358  return data->maxDistance;
359  }
unsigned int Audio::SceneManager::getMaxSources ( ) const
throw (
)
virtual

Get the maximum number of simultaneous sources that can be playing at a time

Remarks
This value may be approximate, and it refers to the maximum number of Source class instances that can effectively be in playing state. Some sources, when environmental effects are being applied, can count more than once towards the effective limit, and the manager will try to compensate this when setting the "MaxSources" attribute. However, this compensation may not be perfect.

Definition at line 283 of file SceneManager.cpp.

285  {
286  return data->maxSources;
287  }
float Audio::SceneManager::getMinGain ( ) const
throw (
)
virtual

Get the minimum gain that would be culled off

Remarks
This value specifies the minimum gain of active sources. If a source ends having a lesser gain, it is ignored and deactivated, to conserve resources.
The manager is free to approximate such determination in order to optimize
culling: for instance, it could, given attenuation factors and the like, compute a maximum distance that would result in gains potentially greater than this value to avoid processing of any source beyond that distance. However, such a computation is required to be conservative (never cull sources that would fall outside of the culling rule based on actual gain), and as such may be difficult without aid.
See Also
For more culling options: get/setMaxDistance

Definition at line 342 of file SceneManager.cpp.

344  {
345  return data->minGain;
346  }
Duration Audio::SceneManager::getPositionUpdateFrequency ( ) const
throw (
)

Return position update frequency

Remarks
Source position updates are a very important kind of update that needs to be performed regularly and quite often. They are costly (all active sources have to be updated). This value specifies how often they're updated, however the actual interval may vary depending on the implementation (it's a mere guideline).

Definition at line 603 of file SceneManager.cpp.

605  {
606  return data->positionUpdateFrequency;
607  }
SharedPtr< Renderer > Audio::SceneManager::getRenderer ( ) const
throw (
)

Get the current renderer

Definition at line 277 of file SceneManager.cpp.

Referenced by Audio::Test::testComplexScene(), Audio::Test::testMultiStreaming(), Audio::Test::testMultiStreaming2(), Audio::Test::testSimpleScene(), and Audio::Test::testStreaming().

279  {
280  return data->renderer;
281  }
SharedPtr< Listener > Audio::SceneManager::getRootListener ( ) const
throw (
)
virtual

Get the root listener

Remarks
Renderers can only have one listener. Sources attached to scenes require translation into the reference listener. It is usually convenient to apply some listener changes directly into the root listener rather than into the scenes. For instance, if one scene represents the cockpit and another the ship's exterior, the cockpit listener will be fixed and the exterior listener will move with the ship. But if the user wants to "look to the right", the best way to achieve this would be to rotate the renderer's listener to the right rather than rotate all sources to the left (what would be required if the listeners of each scene were rotated, since they're "artificial" listeners).

Definition at line 651 of file SceneManager.cpp.

653  {
654  return data->rootListener;
655  }
SharedPtr< Scene > Audio::SceneManager::getScene ( const std::string &  name) const
throw (NotFoundException
)
virtual

Get an existing scene by its name

Definition at line 204 of file SceneManager.cpp.

Referenced by Audio::Test::testComplexScene(), Audio::Test::testMultiStreaming(), Audio::Test::testMultiStreaming2(), Audio::Test::testRendererless(), Audio::Test::testSimpleScene(), Audio::Test::testSimpleSceneWTemplates(), and Audio::Test::testStreaming().

206  {
207  SceneManagerData::SceneMap::const_iterator it;
208 
209  it = data->activeScenes.find(name);
210  if (it != data->activeScenes.end())
211  return it->second;
212 
213  it = data->inactiveScenes.find(name);
214  if (it != data->inactiveScenes.end())
215  return it->second;
216 
217  throw(NotFoundException(name));
218  }
bool Audio::SceneManager::getSceneActive ( const std::string &  name)
throw (NotFoundException
)
virtual

Get the active state of a scene

Definition at line 246 of file SceneManager.cpp.

248  {
249  return data->activeScenes.count(name) > 0;
250  }
SharedPtr< SceneManager::SceneIterator > Audio::SceneManager::getSceneIterator ( ) const
throw (
)
virtual

Get an iterator over all scenes

Definition at line 368 of file SceneManager.cpp.

Referenced by Audio::Test::clearScene().

370  {
371  return SharedPtr<SceneIterator>(
372  new ChainingIterator<VirtualValuesIterator<SceneManagerData::SceneMap::iterator> >(
373  VirtualValuesIterator<SceneManagerData::SceneMap::iterator>(
374  data->activeScenes.begin(),
375  data->activeScenes.end() ),
376  VirtualValuesIterator<SceneManagerData::SceneMap::iterator>(
377  data->inactiveScenes.begin(),
378  data->inactiveScenes.end() )
379  )
380  );
381  }
const SharedPtr< Renderer > & Audio::SceneManager::internalRenderer ( ) const
protected

Returns the renderer

Remarks
Throws an exception if no renderer has been set

Definition at line 125 of file SceneManager.cpp.

126  {
127  if (!data->renderer.get())
128  throw Exception("No renderer");
129  return data->renderer;
130  }
void Audio::SceneManager::notifySourcePlaying ( SharedPtr< Source source,
SharedPtr< Scene scene,
bool  playing 
)
throw (Exception
)
virtual

Notify the scene manager of a source that starts or stops playing.

Definition at line 657 of file SceneManager.cpp.

Referenced by Audio::SimpleScene::notifySourcePlaying().

659  {
660  // If the source is within maxDistance from its scene's listener,
661  // schedule an immediate activation phase
662  double maxDistanceSq = getMaxDistance();
663  maxDistanceSq *= maxDistanceSq;
664 
665  if (scene->getListener().getPosition().distanceSquared(source->getPosition()) <= maxDistanceSq)
666  data->lastActivationTime = -std::numeric_limits<Timestamp>::infinity();
667  }
void Audio::SceneManager::playSource ( SharedPtr< SourceTemplate tpl,
const std::string &  sceneName,
LVector3  position,
Vector3  direction,
Vector3  velocity,
Scalar  radius 
)
throw (Exception
)

Convenience API to play a source once and forget.

Parameters
tplThe source template from which a source should be instanced
sceneNameThe name of the scene to which the source should be attached
positionThe initial position of the source
directionThe direction of the (if directional) source
velocityThe movement velocity of the source
radiusThe base radius of the source
Remarks
The source should not be looping, and an exception will be thrown if it is.

Definition at line 295 of file SceneManager.cpp.

Referenced by Audio::Test::testSimpleSceneWTemplates().

302  {
303  if (tpl->isLooping())
304  throw(Exception("Cannot fire a looping source and forget!"));
305 
306  SharedPtr<Source> src = createSource(tpl);
307 
308  src->setPosition(position);
309  src->setDirection(direction);
310  src->setVelocity(velocity);
311  src->setRadius(radius);
312 
313  getScene(sceneName)->add(src);
314 
315  src->startPlaying();
316  }
void Audio::SceneManager::playSource ( SharedPtr< SourceTemplate tpl,
const std::string &  soundName,
const std::string &  sceneName,
LVector3  position,
Vector3  direction,
Vector3  velocity,
Scalar  radius 
)
throw (Exception
)

Convenience API to play a source once and forget.

Parameters
tplThe source template from which a source should be instanced
soundNameThe name of the sound stream from which the source will be created, overriding the template's
sceneNameThe name of the scene to which the source should be attached
positionThe initial position of the source
directionThe direction of the (if directional) source
velocityThe movement velocity of the source
radiusThe base radius of the source
Remarks
The source should not be looping, and an exception will be thrown if it is.

Definition at line 318 of file SceneManager.cpp.

326  {
327  if (tpl->isLooping())
328  throw(Exception("Cannot fire a looping source and forget!"));
329 
330  SharedPtr<Source> src = createSource(tpl, soundName);
331 
332  src->setPosition(position);
333  src->setDirection(direction);
334  src->setVelocity(velocity);
335  src->setRadius(radius);
336 
337  getScene(sceneName)->add(src);
338 
339  src->startPlaying();
340  }
void Audio::SceneManager::setActivationFrequency ( Duration  interval) const
throw (
)
virtual
See Also
getActivationFrequency

Definition at line 645 of file SceneManager.cpp.

647  {
648  data->activationFrequency = interval;
649  }
void Audio::SceneManager::setAttributeUpdateFrequency ( Duration  interval) const
throw (
)
virtual
See Also
getAttributeUpdateFrequency

Definition at line 639 of file SceneManager.cpp.

641  {
642  data->attributeUpdateFrequency = interval;
643  }
void Audio::SceneManager::setListenerUpdateFrequency ( Duration  interval) const
throw (
)
virtual
See Also
getListenerUpdateFrequency

Definition at line 633 of file SceneManager.cpp.

635  {
636  data->listenerUpdateFrequency = interval;
637  }
void Audio::SceneManager::setMaxDistance ( double  distance)
throw (Exception
)
virtual

Set the maximum distance of active sources

Parameters
distanceThe new limit.
See Also
getMaxDistance

Definition at line 361 of file SceneManager.cpp.

References f.

Referenced by Audio::Test::testComplexScene().

363  {
364  assert(distance >= 0.f);
365  data->maxDistance = distance;
366  }
void Audio::SceneManager::setMaxSources ( unsigned int  n)
throw (Exception
)
virtual

Set the maximum number of simultaneous sources that can be playing at a time

Parameters
nThe maximum number of simultaneous playing sources desired.
Remarks
This is not guaranteed to success. If failure arises, either no change or a seemingly approximate change will be made (ie: if the specified number is too high, the closest possible one will be set instead).
See Also
getMaxSources

Definition at line 289 of file SceneManager.cpp.

Referenced by initSceneManager(), and Audio::Test::testComplexScene().

291  {
292  data->maxSources = n;
293  }
void Audio::SceneManager::setMinGain ( float  gain)
throw (Exception
)
virtual

Set the minimum gain that would be culled off

Parameters
gainThe new minimum gain.
See Also
getMinGain

Definition at line 348 of file SceneManager.cpp.

References f.

350  {
351  assert(gain >= 0.f);
352  data->minGain = gain;
353  }
void Audio::SceneManager::setPositionUpdateFrequency ( Duration  interval) const
throw (
)
virtual
See Also
getPositionUpdateFrequency

Definition at line 627 of file SceneManager.cpp.

629  {
630  data->positionUpdateFrequency = interval;
631  }
void Audio::SceneManager::setRenderer ( SharedPtr< Renderer renderer)
throw (Exception
)
virtual

Set a new renderer

Parameters
rendererA new renderer to be used.
Remarks
Setting the renderer should be done at the very beginning. It is possible to switch renderers at any point, but the operation is rather costly: all sounds must be unloaded and recreated, all active sources must be detached and reattached.
Note
Overriding implementations must call the base implementation, since getRenderer is not overridable.

Definition at line 252 of file SceneManager.cpp.

Referenced by initALRenderer(), and Audio::Test::initALRenderer().

254  {
255  if (data->renderer.get()) {
256  // Detach all active sources
257  for (SceneManagerData::SourceRefSet::const_iterator it = data->activeSources.begin(); it != data->activeSources.end(); ++it)
258  data->renderer->detach(it->source);
259 
260  // Detach the root listener
261  data->renderer->detach(data->rootListener);
262  }
263 
264  // Swap renderers
265  data->renderer.swap(renderer);
266 
267  if (data->renderer.get()) {
268  // Attach the root listener
269  data->renderer->attach(data->rootListener);
270 
271  // Attach all active sources
272  for (SceneManagerData::SourceRefSet::const_iterator it = data->activeSources.begin(); it != data->activeSources.end(); ++it)
273  data->renderer->attach(it->source);
274  }
275  }
void Audio::SceneManager::setSceneActive ( const std::string &  name,
bool  active 
)
throw (NotFoundException
)
virtual

Sets the active state of a scene

Definition at line 231 of file SceneManager.cpp.

Referenced by initScenes(), Audio::Test::testComplexScene(), Audio::Test::testMultiStreaming(), Audio::Test::testMultiStreaming2(), Audio::Test::testRendererless(), Audio::Test::testSimpleScene(), Audio::Test::testSimpleSceneWTemplates(), and Audio::Test::testStreaming().

233  {
234  // Simply move the pointer from one map to the other.
235  // The next update will take care of activating sources as necessary.
236  SharedPtr<Scene> scene = getScene(name);
237  if (active) {
238  data->inactiveScenes.erase(name);
239  data->activeScenes[name] = scene;
240  } else {
241  data->activeScenes.erase(name);
242  data->inactiveScenes[name] = scene;
243  }
244  }
void Audio::SceneManager::updateListenerImpl ( bool  withAttributes)
throw (Exception
)
protectedvirtual

Synchronize listeners

Remarks
Since renderer implementations require one listener, this only updates the root listener. Scene listeners fall under the category of position updates.

Definition at line 587 of file SceneManager.cpp.

References i, Audio::RenderableListener::UPDATE_ALL, and Audio::RenderableListener::UPDATE_LOCATION.

589  {
590  // Update root listener
591  RenderableListener::UpdateFlags updateFlags =
592  withAttributes ?
595  if (data->rootListener.get())
596  data->rootListener->update(updateFlags);
597 
598  // And all scene listeners
599  for (SceneManagerData::SceneMap::const_iterator i = data->activeScenes.begin(); i != data->activeScenes.end(); ++i)
600  i->second->getListener().update(updateFlags);
601  }
void Audio::SceneManager::updateSourcesImpl ( bool  withAttributes)
throw (Exception
)
protectedvirtual

Synchronize source positions/attributes with the renderer

Definition at line 562 of file SceneManager.cpp.

References Audio::RenderableSource::UPDATE_ALL, and Audio::RenderableSource::UPDATE_LOCATION.

564  {
565  // Two-pass stuff.
566 
567  // First, update attributes (mostly location)
568  RenderableSource::UpdateFlags updateFlags =
569  withAttributes ?
572  for (SceneManagerData::SourceRefSet::const_iterator it = data->activeSources.begin(); it != data->activeSources.end(); ++it) {
573  // Update the renderable (attributes)
574  it->source->updateRenderable(
575  updateFlags | (it->needsActivation ? RenderableSource::UPDATE_ALL : 0),
576  it->scene->getListener());
577 
578  // Then, start playing if not playing and should be playing
579  if (it->needsActivation) {
580  it->source->getRenderable()->startPlaying(
581  it->source->getWouldbePlayingTime() );
582  it->needsActivation = false;
583  }
584  }
585  }

The documentation for this class was generated from the following files: