guiAudioCtrl.cpp 6.5 KB

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