afxAudioBank.cpp 7.5 KB

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