afxAudioBank.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "afx/arcaneFX.h"
  25. #include "console/consoleTypes.h"
  26. #include "sim/netConnection.h"
  27. #include "sfx/sfxDescription.h"
  28. #include "afx/ce/afxAudioBank.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. IMPLEMENT_CO_DATABLOCK_V1(afxAudioBank);
  31. ConsoleDocClass( afxAudioBank,
  32. "@brief A datablock that specifies an Audio Bank effect.\n\n"
  33. "afxAudioBank is very similar to the stock Torque SFXProfile datablock but it allows specification of up to 32 different sound "
  34. "files. The sound that actually plays is determined by the playIndex field."
  35. "\n\n"
  36. "afxAudioBank is most useful when used in combination with field substitutions, whereby a substitution statement "
  37. "assigned to playIndex selects a different sound (perhaps randomly) each time the effect is used."
  38. "\n\n"
  39. "@ingroup afxEffects\n"
  40. "@ingroup AFX\n"
  41. "@ingroup Datablocks\n"
  42. );
  43. afxAudioBank::afxAudioBank()
  44. {
  45. mPath = ST_NULLSTRING;
  46. mDescriptionObjectID = 0;
  47. mDescriptionObject = NULL;
  48. mPreload = false;
  49. play_index = -1;
  50. for (S32 i = 0; i < 32; i++)
  51. mFilenames[i] = ST_NULLSTRING;
  52. }
  53. afxAudioBank::afxAudioBank(const afxAudioBank& other, bool temp_clone) : SimDataBlock(other, temp_clone)
  54. {
  55. mPath = other.mPath;
  56. mDescriptionObject = other.mDescriptionObject;
  57. mDescriptionObjectID = other.mDescriptionObjectID; // -- for pack/unpack of mDescriptionObject ptr
  58. mPreload = other.mPreload;
  59. play_index = other.play_index;
  60. for (S32 i = 0; i < 32; i++)
  61. mFilenames[i] = other.mFilenames[i];
  62. }
  63. afxAudioBank::~afxAudioBank()
  64. {
  65. if (!isTempClone())
  66. return;
  67. if (mDescriptionObject && mDescriptionObject->isTempClone())
  68. {
  69. delete mDescriptionObject;
  70. mDescriptionObject = 0;
  71. }
  72. }
  73. afxAudioBank* afxAudioBank::cloneAndPerformSubstitutions(const SimObject* owner, S32 index)
  74. {
  75. if (!owner)
  76. return this;
  77. afxAudioBank* sub_profile_db = this;
  78. SFXDescription* desc_db;
  79. if (mDescriptionObject && mDescriptionObject->getSubstitutionCount() > 0)
  80. {
  81. SFXDescription* orig_db = mDescriptionObject;
  82. desc_db = new SFXDescription(*orig_db, true);
  83. orig_db->performSubstitutions(desc_db, owner, index);
  84. }
  85. else
  86. desc_db = 0;
  87. if (this->getSubstitutionCount() > 0 || desc_db)
  88. {
  89. sub_profile_db = new afxAudioBank(*this, true);
  90. performSubstitutions(sub_profile_db, owner, index);
  91. if (desc_db)
  92. sub_profile_db->mDescriptionObject = desc_db;
  93. }
  94. return sub_profile_db;
  95. }
  96. void afxAudioBank::onPerformSubstitutions()
  97. {
  98. }
  99. void afxAudioBank::initPersistFields()
  100. {
  101. addField("path", TypeFilename, Offset(mPath, afxAudioBank),
  102. "A filesystem path to the folder containing the sound files specified by the "
  103. "filenames[] field. All sound files used in a single AudioBank must be located in "
  104. "the same folder.");
  105. addField("filenames", TypeString, Offset(mFilenames, afxAudioBank), 32,
  106. "Up to 32 names of sound files found in the path folder. The sound that is actually "
  107. "played by an Audio Bank effect is determined by the playIndex field.");
  108. addField("description", TYPEID<SFXDescription>(), Offset(mDescriptionObject, afxAudioBank),
  109. "SFXDescription datablock to use with this set of sounds.");
  110. addField("preload", TypeBool, Offset(mPreload, afxAudioBank),
  111. "If set to true, file is pre-loaded, otherwise it is loaded on-demand.");
  112. addField("playIndex", TypeS32, Offset(play_index, afxAudioBank),
  113. "An array index that selects a sound to play from the filenames[] field. Values "
  114. "outside of the range of assigned filename[] entries will not play any sound.");
  115. Parent::initPersistFields();
  116. }
  117. bool afxAudioBank::preload(bool server, String &errorStr)
  118. {
  119. if(!Parent::preload(server, errorStr))
  120. return false;
  121. return true;
  122. }
  123. bool afxAudioBank::onAdd()
  124. {
  125. if (!Parent::onAdd())
  126. return false;
  127. if (!mDescriptionObject && mDescriptionObjectID)
  128. Sim::findObject(mDescriptionObjectID , mDescriptionObject);
  129. // if this is client side, make sure that description is as well
  130. if(mDescriptionObject)
  131. { // client side dataBlock id's are not in the dataBlock id range
  132. if (getId() >= DataBlockObjectIdFirst && getId() <= DataBlockObjectIdLast)
  133. {
  134. SimObjectId pid = mDescriptionObject->getId();
  135. if (pid < DataBlockObjectIdFirst || pid > DataBlockObjectIdLast)
  136. {
  137. Con::errorf(ConsoleLogEntry::General,"afxAudioBank: data dataBlock not networkable (use datablock to create).");
  138. return false;
  139. }
  140. }
  141. }
  142. return(true);
  143. }
  144. void afxAudioBank::packData(BitStream* stream)
  145. {
  146. Parent::packData(stream);
  147. if (stream->writeFlag(mDescriptionObject))
  148. stream->writeRangedU32(mDescriptionObject->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  149. /*
  150. char buffer[256];
  151. if(!mFilename)
  152. buffer[0] = 0;
  153. else
  154. dStrcpy(buffer, mFilename, 256);
  155. stream->writeString(buffer);
  156. */
  157. stream->writeString(mPath);
  158. for (S32 i = 0; i < 32; i++)
  159. {
  160. stream->writeString(mFilenames[i]);
  161. if (mFilenames[i] == ST_NULLSTRING)
  162. break;
  163. }
  164. stream->writeFlag(mPreload);
  165. if (stream->writeFlag(play_index >= 0 && play_index < 32))
  166. stream->writeInt(play_index, 5);
  167. }
  168. void afxAudioBank::unpackData(BitStream* stream)
  169. {
  170. Parent::unpackData(stream);
  171. if (stream->readFlag()) // AudioDescription
  172. {
  173. SimObjectId id = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  174. mDescriptionObjectID = id;
  175. Sim::findObject(id, mDescriptionObject);
  176. }
  177. // Filename
  178. /*
  179. char buffer[256];
  180. stream->readString(buffer);
  181. mFilename = StringTable->insert(buffer);
  182. */
  183. char buffer[256];
  184. stream->readString(buffer);
  185. mPath = StringTable->insert(buffer);
  186. for (S32 i = 0; i < 32; i++)
  187. {
  188. stream->readString(buffer);
  189. mFilenames[i] = StringTable->insert(buffer);
  190. if (mFilenames[i] == ST_NULLSTRING)
  191. break;
  192. }
  193. mPreload = stream->readFlag(); // Preload
  194. if (stream->readFlag())
  195. play_index = stream->readInt(5);
  196. else
  197. play_index = -1;
  198. }
  199. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//