sfxModifier.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 _SFXMODIFIER_H_
  23. #define _SFXMODIFIER_H_
  24. #ifndef _TSTREAM_H_
  25. #include "core/stream/tStream.h"
  26. #endif
  27. class SFXSource;
  28. /// An SFXModifier modifies the properties of an SFXSource while its playback
  29. /// is running.
  30. class SFXModifier : public IPolled
  31. {
  32. protected:
  33. /// The source that this effect works on.
  34. SFXSource* mSource;
  35. /// If true, the effect is removed from the effects stack
  36. bool mRemoveWhenDone;
  37. public:
  38. /// Create an effect that operates on "source".
  39. SFXModifier( SFXSource* source, bool removeWhenDone = false )
  40. : mSource( source ), mRemoveWhenDone(removeWhenDone) {}
  41. virtual ~SFXModifier() {}
  42. };
  43. /// An SFXModifier that is triggered once after passing a certain playback position.
  44. class SFXOneShotModifier : public SFXModifier
  45. {
  46. public:
  47. typedef SFXModifier Parent;
  48. protected:
  49. /// Playback position that triggers the effect.
  50. F32 mTriggerPos;
  51. ///
  52. virtual void _onTrigger() = 0;
  53. public:
  54. /// Create an effect that triggers when playback of "source" passes "triggerPos".
  55. SFXOneShotModifier( SFXSource* source, F32 triggerPos, bool removeWhenDone = false );
  56. // IPolled.
  57. virtual bool update();
  58. };
  59. /// An SFXModifier that is spans a certain range of playback time.
  60. class SFXRangeModifier : public SFXModifier
  61. {
  62. public:
  63. typedef SFXModifier Parent;
  64. protected:
  65. /// If true, the effect is currently being applied to the source.
  66. bool mIsActive;
  67. /// Playback position in milliseconds when this effect becomes active.
  68. F32 mStartTime;
  69. /// Playback position in milliseconds when this effect becomes inactive.
  70. F32 mEndTime;
  71. /// Called when the play cursor passes mStartTime.
  72. /// @note There may be latency between the cursor actually passing mStartTime
  73. /// and this method being called.
  74. virtual void _onStart() {}
  75. /// Called on each update() while the play cursor is in range.
  76. virtual void _onUpdate() {}
  77. /// Called when the play cursor passes mEndTime.
  78. /// @note There may be latency between the cursor actually passing mEndTime
  79. /// and this method being called.
  80. virtual void _onEnd() {}
  81. public:
  82. /// Create an effect that operates on "source" between "startTime" seconds
  83. /// (inclusive) and "endTime" seconds (exclusive).
  84. SFXRangeModifier( SFXSource* source, F32 startTime, F32 endTime, bool removeWhenDone = false );
  85. ///
  86. bool isActive() const { return mIsActive; }
  87. // IPolled.
  88. virtual bool update();
  89. };
  90. /// A volume fade effect (fade-in or fade-out).
  91. class SFXFadeModifier : public SFXRangeModifier
  92. {
  93. public:
  94. typedef SFXRangeModifier Parent;
  95. enum EOnEnd
  96. {
  97. ON_END_Nop, ///< Do nothing with source when fade is complete.
  98. ON_END_Stop, ///< Stop source when fade is complete.
  99. ON_END_Pause, ///< Pause source when fade is complete.
  100. };
  101. protected:
  102. /// Volume when beginning fade. Set when effect is activated.
  103. F32 mStartVolume;
  104. /// Volume when ending fade.
  105. F32 mEndVolume;
  106. /// Current volume level.
  107. F32 mCurrentVolume;
  108. /// Action to perform when the fade has been completed. Defaults to no action.
  109. EOnEnd mOnEnd;
  110. // SFXModifier.
  111. virtual void _onStart();
  112. virtual void _onUpdate();
  113. virtual void _onEnd();
  114. public:
  115. /// Create an effect that fades the volume of "source" to "endVolume" over the
  116. /// period of "time" seconds. The fade will start at "referenceTime" using the
  117. /// source's current volume at the time as the start.
  118. SFXFadeModifier( SFXSource* source, F32 time, F32 endVolume, F32 startTime, EOnEnd onEndDo = ON_END_Nop, bool removeWhenDone = false );
  119. virtual ~SFXFadeModifier();
  120. };
  121. /// A modifer that calls a method on the SFXSource when a particular playback position
  122. /// is passed.
  123. ///
  124. /// @note At the moment, doing a setPosition() on a source will not cause markers that have
  125. /// been jumped over in the operation to be ignored. Instead they will trigger on the
  126. /// next update.
  127. class SFXMarkerModifier : public SFXOneShotModifier
  128. {
  129. public:
  130. typedef SFXOneShotModifier Parent;
  131. protected:
  132. /// Symbolic marker name that is passed to the "onMarkerPassed" callback.
  133. String mMarkerName;
  134. // SFXOneShotModifier
  135. virtual void _onTrigger();
  136. public:
  137. SFXMarkerModifier( SFXSource* source, const String& name, F32 pos, bool removeWhenDone = false );
  138. };
  139. #endif // !_SFXMODIFIER_H_