guiAudioCtrl.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "gui/shiny/guiAudioCtrl.h"
  23. #include "console/engineAPI.h"
  24. #include "console/script.h"
  25. #include "sfx/sfxSystem.h"
  26. #include "sfx/sfxTrack.h"
  27. #include "sfx/sfxSource.h"
  28. #include "sfx/sfxTypes.h"
  29. #define TickMs 32
  30. IMPLEMENT_CONOBJECT( GuiAudioCtrl );
  31. ConsoleDocClass( GuiAudioCtrl,
  32. "@brief Brief Description.\n\n"
  33. "Audio PLayback.\n\n"
  34. "@ingroup GuiUtil\n");
  35. GuiAudioCtrl::GuiAudioCtrl()
  36. {
  37. INIT_ASSET(Sound);
  38. mTickPeriodMS = 100;
  39. mLastThink = 0;
  40. mCurrTick = 0;
  41. mPlayIf = "";
  42. mSoundPlaying = NULL;
  43. mUseTrackDescriptionOnly = false;
  44. mDescription.mIs3D = false;
  45. mDescription.mIsLooping = true;
  46. mDescription.mIsStreaming = false;
  47. mDescription.mFadeInTime = -1.f;
  48. mDescription.mFadeOutTime = -1.f;
  49. mVolume = 1;
  50. mPitch = 1;
  51. mFadeInTime = -1;
  52. mFadeOutTime = -1;
  53. mSourceGroup = NULL;
  54. setProcessTicks();
  55. }
  56. GuiAudioCtrl::~GuiAudioCtrl()
  57. {
  58. if (mSoundPlaying)
  59. mSoundPlaying->stop();
  60. SFX_DELETE(mSoundPlaying);
  61. }
  62. bool GuiAudioCtrl::onWake()
  63. {
  64. return Parent::onWake();
  65. }
  66. void GuiAudioCtrl::onSleep()
  67. {
  68. if (mSoundPlaying)
  69. mSoundPlaying->stop();
  70. SFX_DELETE(mSoundPlaying);
  71. Parent::onSleep();
  72. }
  73. void GuiAudioCtrl::processTick()
  74. {
  75. if (mLastThink + mTickPeriodMS < mCurrTick)
  76. {
  77. mCurrTick = 0;
  78. mLastThink = 0;
  79. if (isSoundValid())
  80. {
  81. _update();
  82. }
  83. }
  84. else
  85. {
  86. mCurrTick += TickMs;
  87. }
  88. }
  89. bool GuiAudioCtrl::testCondition()
  90. {
  91. if (mPlayIf.isEmpty())
  92. return true; //we've got no tests to run so just do it
  93. //test the mapper plugged in condition line
  94. String resVar = getIdString() + String(".result");
  95. Con::setBoolVariable(resVar.c_str(), false);
  96. String command = resVar + "=" + mPlayIf + ";";
  97. Con::evaluatef(command.c_str());
  98. if (Con::getBoolVariable(resVar.c_str()) == 1)
  99. {
  100. return true;
  101. }
  102. return false;
  103. }
  104. void GuiAudioCtrl::initPersistFields()
  105. {
  106. addGroup("Sounds");
  107. INITPERSISTFIELD_SOUNDASSET(Sound, GuiAudioCtrl, "Looping SoundAsset to play while GuiAudioCtrl is active.");
  108. addField("tickPeriodMS", TypeS32, Offset(mTickPeriodMS, GuiAudioCtrl),
  109. "@brief Time in milliseconds between calls to onTick().\n\n"
  110. "@see onTickTrigger()\n");
  111. addField("playIf", TypeCommand, Offset(mPlayIf, GuiAudioCtrl), "evaluation condition to trip playback (true/false)");
  112. addField("useTrackDescriptionOnly", TypeBool, Offset(mUseTrackDescriptionOnly, GuiAudioCtrl),
  113. "If this is true, all fields except for #playOnAdd and #track are ignored on the emitter object.\n"
  114. "This is useful to prevent fields in the #track's description from being overridden by emitter fields.");
  115. addField("sourceGroup", TypeSFXSourceName, Offset(mSourceGroup, GuiAudioCtrl),
  116. "The SFXSource to which to assign the sound of this emitter as a child.\n"
  117. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  118. "@see SFXDescription::sourceGroup");
  119. addField("volume", TypeF32, Offset(mVolume, GuiAudioCtrl),
  120. "Volume level to apply to the sound.\n"
  121. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  122. "@see SFXDescription::volume");
  123. addField("pitch", TypeF32, Offset(mPitch, GuiAudioCtrl),
  124. "Pitch shift to apply to the sound. Default is 1 = play at normal speed.\n"
  125. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  126. "@see SFXDescription::pitch");
  127. addField("fadeInTime", TypeF32, Offset(mFadeInTime, GuiAudioCtrl),
  128. "Number of seconds to gradually fade in volume from zero when playback starts.\n"
  129. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  130. "@see SFXDescription::fadeInTime");
  131. addField("fadeOutTime", TypeF32, Offset(mFadeOutTime, GuiAudioCtrl),
  132. "Number of seconds to gradually fade out volume down to zero when playback is stopped or paused.\n"
  133. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  134. "@see SFXDescription::fadeOutTime");
  135. endGroup("Sounds");
  136. Parent::initPersistFields();
  137. }
  138. void GuiAudioCtrl::_update()
  139. {
  140. if (isSoundValid())
  141. {
  142. //mLocalProfile = *mSoundAsset->getSfxProfile();
  143. mDescription = *mSoundAsset->getSfxDescription();
  144. }
  145. // Make sure all the settings are valid.
  146. mDescription.validate();
  147. bool useTrackDescriptionOnly = (mUseTrackDescriptionOnly && getSoundProfile());
  148. if (getSoundProfile())
  149. {
  150. if (mSoundPlaying == NULL)
  151. {
  152. mSoundPlaying = SFX->createSource(getSoundProfile());
  153. }
  154. }
  155. // The rest only applies if we have a source.
  156. if (mSoundPlaying && !useTrackDescriptionOnly)
  157. {
  158. // Set the volume irrespective of the profile.
  159. if (mSourceGroup)
  160. {
  161. mSourceGroup->addObject(mSoundPlaying);
  162. mSoundPlaying->setVolume(mSourceGroup->getVolume() * mVolume);
  163. }
  164. else
  165. {
  166. mSoundPlaying->setVolume(mVolume);
  167. }
  168. mSoundPlaying->setPitch(mPitch);
  169. mSoundPlaying->setFadeTimes(mFadeInTime, mFadeOutTime);
  170. }
  171. if (testCondition() && isActive() && isAwake())
  172. {
  173. if (mSoundPlaying && !mSoundPlaying->isPlaying())
  174. {
  175. mSoundPlaying->play();
  176. }
  177. }
  178. else
  179. {
  180. if (mSoundPlaying != NULL)
  181. {
  182. mSoundPlaying->stop();
  183. }
  184. }
  185. }