sfxController.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 _SFXCONTROLLER_H_
  23. #define _SFXCONTROLLER_H_
  24. #ifndef _SFXSOURCE_H_
  25. #include "sfx/sfxSource.h"
  26. #endif
  27. #ifndef _SFXCOMMON_H_
  28. #include "sfx/sfxCommon.h"
  29. #endif
  30. #ifndef _SFXSOURCE_H_
  31. #include "sfx/sfxSource.h"
  32. #endif
  33. #ifndef _SFXPLAYLIST_H_
  34. #include "sfx/sfxPlayList.h"
  35. #endif
  36. #ifndef _TVECTOR_H_
  37. #include "core/util/tVector.h"
  38. #endif
  39. class SFXTrack;
  40. class SFXProfile;
  41. class SFXState;
  42. /// SFXSource that drives multi-source playback.
  43. ///
  44. /// Basically, this class is an interpreter for the instruction slots in
  45. /// SFXPlayLists.
  46. ///
  47. /// Controllers can be switched between states. When no state is set, all
  48. /// tracks from playlists that do not have a state set will be played. When
  49. /// setting a state, only tracks with the given state will be played. If
  50. /// currently tracks with a different state are playing, the respective
  51. /// controllers will transition out of their respective slots.
  52. ///
  53. class SFXController : public SFXSource
  54. {
  55. public:
  56. typedef SFXSource Parent;
  57. friend class SFXSystem; // _create
  58. protected:
  59. typedef SFXVariantFloat< 1 > VariantFloat;
  60. enum EOp
  61. {
  62. OP_Delay,
  63. OP_WaitSingle,
  64. OP_WaitAll,
  65. OP_StopSingle,
  66. OP_StopAll,
  67. OP_Play,
  68. OP_Jump,
  69. OP_LoopBegin,
  70. OP_LoopEnd,
  71. };
  72. struct Insn
  73. {
  74. EOp mOpcode;
  75. U32 mSlotIndex;
  76. SFXState* mState;
  77. union
  78. {
  79. VariantFloat mDelayTime;
  80. U32 mJumpIp;
  81. U32 mLoopCount;
  82. } mArg;
  83. Insn()
  84. : mOpcode(SFXController::OP_Delay), mSlotIndex(0), mState(NULL) {mArg.mLoopCount=0;}
  85. Insn( EOp opcode )
  86. : mOpcode( opcode ), mSlotIndex( U32_MAX ), mState( NULL ) {mArg.mLoopCount=0;}
  87. Insn( U32 slotIndex, SFXState* state )
  88. : mOpcode(SFXController::OP_Delay), mSlotIndex( slotIndex ), mState( state ){mArg.mLoopCount=0;}
  89. Insn( EOp opcode, U32 slotIndex, SFXState* state )
  90. : mOpcode( opcode ), mSlotIndex( slotIndex ), mState( state ) {mArg.mLoopCount=0;}
  91. };
  92. ///
  93. struct Source
  94. {
  95. /// The play-once source.
  96. SimObjectPtr< SFXSource > mPtr;
  97. /// The state to which the source is tied. Only taken over from
  98. /// the instruction if the state mode is not set to ignored.
  99. SFXState* mState;
  100. /// Index of slot in playlist that this source was spawned on.
  101. U32 mSlotIndex;
  102. /// Volume scale factor to apply to the source. Saved as it may have been
  103. /// randomly generated.
  104. F32 mVolumeScale;
  105. /// Pitch scale factor to apply to the source. Saved as it may have been
  106. /// randomly generated.
  107. F32 mPitchScale;
  108. ///
  109. F32 mFadeInTime;
  110. ///
  111. F32 mFadeOutTime;
  112. Source()
  113. : mState( 0 ), mSlotIndex(0), mVolumeScale(1.0f), mPitchScale(1.0f), mFadeInTime(0), mFadeOutTime(0) {}
  114. };
  115. /// The current instruction in "mInsns".
  116. U32 mIp;
  117. /// The instruction list. This is compiled from the playlist and then executed
  118. /// in the controller's update.
  119. Vector< Insn > mInsns;
  120. /// The stack of currently playing sources.
  121. ///
  122. /// All sources on this list are play-once sources so we can leave their lifetime
  123. /// management to the SFX system. This is especially convenient in combination
  124. /// with fade-outs where a source cannot be immediately deleted.
  125. Vector< Source > mSources;
  126. ///
  127. bool mTrace;
  128. ///
  129. U32 mDelayEndTime;
  130. ///
  131. U32 mLoopCounter;
  132. ///
  133. SFXController( SFXPlayList* playList );
  134. ///
  135. void _printInsn( Insn& insn );
  136. ///
  137. void _compileList( SFXPlayList* playList );
  138. ///
  139. void _genTransition( Insn& insn, SFXPlayList::ETransitionMode transition );
  140. ///
  141. void _dumpInsns() {};
  142. ///
  143. void _initInsn();
  144. ///
  145. bool _execInsn();
  146. ///
  147. void _advanceIp();
  148. ///
  149. static SFXController* _create( SFXPlayList* playList );
  150. // SFXSource.
  151. virtual void _play();
  152. virtual void _pause();
  153. virtual void _stop();
  154. virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event );
  155. virtual void _updateVolume( const MatrixF& listener );
  156. virtual void _updatePitch();
  157. virtual void _updatePriority();
  158. virtual void _update();
  159. public:
  160. ~SFXController();
  161. /// Constructor for the sake of ConsoleObject.
  162. explicit SFXController(): mIp(0), mTrace(false), mDelayEndTime(0), mLoopCounter(0) {}
  163. /// Return the playlist being played back by the controller.
  164. SFXPlayList* getPlayList() const;
  165. /// Return the index of the playlist slot being processed by the controller.
  166. U32 getCurrentSlot() const;
  167. /// Set the index of the playlist slot to process.
  168. void setCurrentSlot( U32 index );
  169. // SFXSource.
  170. static void initPersistFields();
  171. DECLARE_CONOBJECT( SFXController );
  172. DECLARE_DESCRIPTION( "Controls the playback of an SFXPlayList." );
  173. };
  174. #endif // !_SFXCONTROLLER_H_