afxAudioBank.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. docsURL;
  102. addField("path", TypeFilename, Offset(mPath, afxAudioBank),
  103. "A filesystem path to the folder containing the sound files specified by the "
  104. "filenames[] field. All sound files used in a single AudioBank must be located in "
  105. "the same folder.");
  106. addField("filenames", TypeString, Offset(mFilenames, afxAudioBank), 32,
  107. "Up to 32 names of sound files found in the path folder. The sound that is actually "
  108. "played by an Audio Bank effect is determined by the playIndex field.");
  109. addField("description", TYPEID<SFXDescription>(), Offset(mDescriptionObject, afxAudioBank),
  110. "SFXDescription datablock to use with this set of sounds.");
  111. addField("preload", TypeBool, Offset(mPreload, afxAudioBank),
  112. "If set to true, file is pre-loaded, otherwise it is loaded on-demand.");
  113. addField("playIndex", TypeS32, Offset(play_index, afxAudioBank),
  114. "An array index that selects a sound to play from the filenames[] field. Values "
  115. "outside of the range of assigned filename[] entries will not play any sound.");
  116. Parent::initPersistFields();
  117. }
  118. bool afxAudioBank::preload(bool server, String &errorStr)
  119. {
  120. if(!Parent::preload(server, errorStr))
  121. return false;
  122. return true;
  123. }
  124. bool afxAudioBank::onAdd()
  125. {
  126. if (!Parent::onAdd())
  127. return false;
  128. if (!mDescriptionObject && mDescriptionObjectID)
  129. Sim::findObject(mDescriptionObjectID , mDescriptionObject);
  130. // if this is client side, make sure that description is as well
  131. if(mDescriptionObject)
  132. { // client side dataBlock id's are not in the dataBlock id range
  133. if (getId() >= DataBlockObjectIdFirst && getId() <= DataBlockObjectIdLast)
  134. {
  135. SimObjectId pid = mDescriptionObject->getId();
  136. if (pid < DataBlockObjectIdFirst || pid > DataBlockObjectIdLast)
  137. {
  138. Con::errorf(ConsoleLogEntry::General,"afxAudioBank: data dataBlock not networkable (use datablock to create).");
  139. return false;
  140. }
  141. }
  142. }
  143. return(true);
  144. }
  145. void afxAudioBank::packData(BitStream* stream)
  146. {
  147. Parent::packData(stream);
  148. if (stream->writeFlag(mDescriptionObject))
  149. stream->writeRangedU32(mDescriptionObject->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  150. /*
  151. char buffer[256];
  152. if(!mFilename)
  153. buffer[0] = 0;
  154. else
  155. dStrcpy(buffer, mFilename, 256);
  156. stream->writeString(buffer);
  157. */
  158. stream->writeString(mPath);
  159. for (S32 i = 0; i < 32; i++)
  160. {
  161. stream->writeString(mFilenames[i]);
  162. if (mFilenames[i] == ST_NULLSTRING)
  163. break;
  164. }
  165. stream->writeFlag(mPreload);
  166. if (stream->writeFlag(play_index >= 0 && play_index < 32))
  167. stream->writeInt(play_index, 5);
  168. }
  169. void afxAudioBank::unpackData(BitStream* stream)
  170. {
  171. Parent::unpackData(stream);
  172. if (stream->readFlag()) // AudioDescription
  173. {
  174. SimObjectId id = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  175. mDescriptionObjectID = id;
  176. Sim::findObject(id, mDescriptionObject);
  177. }
  178. // Filename
  179. /*
  180. char buffer[256];
  181. stream->readString(buffer);
  182. mFilename = StringTable->insert(buffer);
  183. */
  184. char buffer[256];
  185. stream->readString(buffer);
  186. mPath = StringTable->insert(buffer);
  187. for (S32 i = 0; i < 32; i++)
  188. {
  189. stream->readString(buffer);
  190. mFilenames[i] = StringTable->insert(buffer);
  191. if (mFilenames[i] == ST_NULLSTRING)
  192. break;
  193. }
  194. mPreload = stream->readFlag(); // Preload
  195. if (stream->readFlag())
  196. play_index = stream->readInt(5);
  197. else
  198. play_index = -1;
  199. }
  200. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//