sfxSoundscape.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SFXSOUNDSCAPE_H_
  23. #define _SFXSOUNDSCAPE_H_
  24. #ifndef _SFXCOMMON_H_
  25. #include "sfx/sfxCommon.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _DATACHUNKER_H_
  31. #include "core/dataChunker.h"
  32. #endif
  33. #ifndef _BITSET_H_
  34. #include "core/bitSet.h"
  35. #endif
  36. #ifndef _TRESPONSECURVE_H_
  37. #include "math/util/tResponseCurve.h"
  38. #endif
  39. #ifndef _SFXAMBIENCE_H_
  40. #include "sfx/sfxAmbience.h"
  41. #endif
  42. /// @file
  43. /// The soundscape system is responsible for controlling ambient audio.
  44. /// It is largely driven by SFXWorld's listener tracking.
  45. class SFXSource;
  46. /// An instance of an SFXAmbience on the soundscape mixer stack.
  47. ///
  48. class SFXSoundscape
  49. {
  50. public:
  51. typedef void Parent;
  52. friend class SFXSoundscapeManager;
  53. enum Flags
  54. {
  55. FlagOverridden = BIT( 0 ), ///< Same ambience is pushed lower down onto the stack.
  56. FlagUnique = BIT( 1 ), ///< No other instance of this ambience on stack.
  57. };
  58. enum DirtyBits : U32
  59. {
  60. AmbienceDirty = BIT( 0 ), ///< Associated ambience has changed.
  61. AllDirty = 0xFFFFFFFF
  62. };
  63. protected:
  64. ///
  65. BitSet32 mFlags;
  66. ///
  67. BitSet32 mDirtyBits;
  68. /// The current soundtrack playing in this soundscape. This is either the
  69. /// ambient track or the surround sound track depending on whether
  70. SimObjectPtr< SFXSource > mSource;
  71. /// The ambient space assigned to this soundscape.
  72. SFXAmbience* mAmbience;
  73. /// States activated by this soundscape.
  74. SFXState* mStates[ SFXAmbience::MaxStates ];
  75. /// Return true if another soundscape lower down the stack is using the same
  76. /// ambient space as this soundscape.
  77. bool _isOverridden() const { return mFlags.test( FlagOverridden ); }
  78. /// Return true if this soundscape is the only soundscape using the assigned
  79. /// ambient space.
  80. bool _isUnique() const { return mFlags.test( FlagUnique ); }
  81. public:
  82. /// Create a soundscape associated with the given ambient space.
  83. SFXSoundscape( SFXAmbience* ambience );
  84. /// Return the ambient space associated with this soundscape.
  85. SFXAmbience* getAmbience() const { return mAmbience; }
  86. /// Set the ambient space associated with this soundscape. Triggers corresponding
  87. /// recomputations on the next soundscape manager update.
  88. void setAmbience( SFXAmbience* ambience );
  89. };
  90. /// The soundscape manager produces a dynamic mix between multiple active soundscapes.
  91. ///
  92. /// @note The more layered soundscapes there are, the costlier ambient sound updates will get.
  93. ///
  94. class SFXSoundscapeManager
  95. {
  96. public:
  97. typedef void Parent;
  98. protected:
  99. /// Linear stack of soundscapes. Evaluated bottom to top. Scapes
  100. /// being faded out are always last on the stack.
  101. Vector< SFXSoundscape* > mStack;
  102. /// Stack of soundscape that are currently being faded out. These are
  103. /// kept around so that when their ambient spaces are activated again,
  104. /// they can be brought back with their fades simply being reversed.
  105. ///
  106. /// All soundscapes on this stack are unique.
  107. Vector< SFXSoundscape* > mFadeStack;
  108. /// Index into #mStack for the soundscape that defines the current
  109. /// reverb settings. -1 if no current ambience has reverb settings.
  110. S32 mCurrentReverbIndex;
  111. /// Memory manager of soundscape instances.
  112. FreeListChunker< SFXSoundscape > mChunker;
  113. /// Default global SFXAmbience. Not a registered object.
  114. SFXAmbience* mDefaultGlobalAmbience;
  115. /// Found the topmost instance on the given stack associated with the given
  116. /// ambient space.
  117. S32 _findOnStack( SFXAmbience* ambience, const Vector< SFXSoundscape* >& stack );
  118. /// Find the topmost soundscape on the given stack that has a reverb environment
  119. /// defined on its ambient space.
  120. S32 _findTopmostReverbOnStack( const Vector< SFXSoundscape* >& stack );
  121. /// Method hooked up to the SFXAmbience change signal to automatically
  122. /// make soundscapes using the given ambience as dirty and trigger
  123. /// a recomputation of their properties.
  124. void _notifyAmbienceChanged( SFXAmbience* ambience );
  125. public:
  126. ///
  127. SFXSoundscapeManager();
  128. ///
  129. ~SFXSoundscapeManager();
  130. /// Update the current soundscape mix.
  131. void update();
  132. /// Return the total number of soundscape instances currently on the stack.
  133. /// Always >=1.
  134. U32 getNumTotalSoundscapes() const { return mStack.size(); }
  135. /// Insert a new soundscape instance associated with the given ambient space
  136. /// at the given stack index.
  137. SFXSoundscape* insertSoundscape( U32 index, SFXAmbience* ambience );
  138. /// Remove the given soundscape from the stack.
  139. void removeSoundscape( SFXSoundscape* soundscape );
  140. /// Return the topmost soundscape. This soundscape is always defined and cannot
  141. /// be removed.
  142. SFXSoundscape* getGlobalSoundscape() const { return mStack[ 0 ]; }
  143. };
  144. #endif // !_SFXSOUNDSCAPE_H_