afxEA_AudioBank.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "sfx/sfxSystem.h"
  27. #include "sfx/sfxSource.h"
  28. #include "sfx/sfxProfile.h"
  29. #include "afx/afxEffectDefs.h"
  30. #include "afx/afxEffectWrapper.h"
  31. #include "afx/afxChoreographer.h"
  32. #include "afx/ce/afxAudioBank.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. // afxEA_AudioBank
  35. class afxEA_AudioBank : public afxEffectWrapper
  36. {
  37. typedef afxEffectWrapper Parent;
  38. SFXSource* sound_handle;
  39. afxAudioBank* sound_bank;
  40. S32 play_index;
  41. void do_runtime_substitutions();
  42. public:
  43. /*C*/ afxEA_AudioBank();
  44. /*D*/ ~afxEA_AudioBank();
  45. virtual void ea_set_datablock(SimDataBlock*);
  46. virtual bool ea_start();
  47. virtual bool ea_update(F32 dt);
  48. virtual void ea_finish(bool was_stopped);
  49. };
  50. //~~~~~~~~~~~~~~~~~~~~//
  51. afxEA_AudioBank::afxEA_AudioBank()
  52. {
  53. sound_handle = 0;
  54. sound_bank = 0;
  55. play_index = -1;
  56. }
  57. afxEA_AudioBank::~afxEA_AudioBank()
  58. {
  59. if (sound_bank && sound_bank->isTempClone())
  60. delete sound_bank;
  61. sound_bank = 0;
  62. sound_handle = 0;
  63. }
  64. void afxEA_AudioBank::ea_set_datablock(SimDataBlock* db)
  65. {
  66. sound_bank = dynamic_cast<afxAudioBank*>(db);
  67. }
  68. bool afxEA_AudioBank::ea_start()
  69. {
  70. if (!sound_bank)
  71. {
  72. Con::errorf("afxEA_AudioBank::ea_start() -- missing or incompatible afxAudioBank.");
  73. return false;
  74. }
  75. do_runtime_substitutions();
  76. if (sound_bank->mPath == ST_NULLSTRING)
  77. return false;
  78. play_index = sound_bank->play_index;
  79. return true;
  80. }
  81. bool afxEA_AudioBank::ea_update(F32 dt)
  82. {
  83. if (!sound_handle)
  84. {
  85. if (play_index >= 0 && play_index < 32 && sound_bank->mFilenames[play_index] != ST_NULLSTRING)
  86. {
  87. SFXProfile* temp_prof = new SFXProfile(sound_bank->mDescriptionObject,
  88. avar("%s/%s", sound_bank->mPath, sound_bank->mFilenames[play_index]),
  89. true);
  90. if (!temp_prof->registerObject())
  91. {
  92. Con::errorf( "afxEA_AudioBank::ea_update() -- unable to create profile");
  93. delete temp_prof;
  94. }
  95. else
  96. {
  97. sound_handle = SFX->createSource(temp_prof);
  98. if (sound_handle)
  99. sound_handle->play();
  100. temp_prof->setAutoDelete(true);
  101. }
  102. }
  103. }
  104. if (sound_handle)
  105. {
  106. sound_handle->setTransform(mUpdated_xfm);
  107. sound_handle->setVolume(mUpdated_scale.x*mFade_value);
  108. }
  109. return true;
  110. }
  111. void afxEA_AudioBank::ea_finish(bool was_stopped)
  112. {
  113. if (sound_handle)
  114. {
  115. sound_handle->stop();
  116. SFX_DELETE(sound_handle);
  117. }
  118. }
  119. void afxEA_AudioBank::do_runtime_substitutions()
  120. {
  121. sound_bank = sound_bank->cloneAndPerformSubstitutions(mChoreographer, mGroup_index);
  122. }
  123. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  124. class afxEA_SoundBankDesc : public afxEffectAdapterDesc, public afxEffectDefs
  125. {
  126. static afxEA_SoundBankDesc mDesc;
  127. public:
  128. virtual bool testEffectType(const SimDataBlock*) const;
  129. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  130. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  131. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  132. //virtual void prepEffect(afxEffectWrapperData*) const;
  133. virtual afxEffectWrapper* create() const { return new afxEA_AudioBank; }
  134. };
  135. afxEA_SoundBankDesc afxEA_SoundBankDesc::mDesc;
  136. bool afxEA_SoundBankDesc::testEffectType(const SimDataBlock* db) const
  137. {
  138. return (typeid(afxAudioBank) == typeid(*db));
  139. }
  140. bool afxEA_SoundBankDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  141. {
  142. SFXDescription* desc = ((SFXProfile*)ew->effect_data)->getDescription();
  143. return (desc && desc->mIsLooping) ? (timing.lifetime < 0) : false;
  144. }
  145. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//