SoundAsset.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef SOUND_ASSET_H
  23. #include "SoundAsset.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assets/assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. #ifndef _ASSET_PTR_H_
  35. #include "assets/assetPtr.h"
  36. #endif
  37. // Debug Profiling.
  38. #include "platform/profiler.h"
  39. #include "sfx/sfxTypes.h"
  40. //-----------------------------------------------------------------------------
  41. IMPLEMENT_CONOBJECT(SoundAsset);
  42. ConsoleType(SoundAssetPtr, TypeSoundAssetPtr, const char*, ASSET_ID_FIELD_PREFIX)
  43. //-----------------------------------------------------------------------------
  44. ConsoleGetType(TypeSoundAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. return *((const char**)(dptr));
  48. }
  49. //-----------------------------------------------------------------------------
  50. ConsoleSetType(TypeSoundAssetPtr)
  51. {
  52. // Was a single argument specified?
  53. if (argc == 1)
  54. {
  55. // Yes, so fetch field value.
  56. *((const char**)dptr) = StringTable->insert(argv[0]);
  57. return;
  58. }
  59. // Warn.
  60. Con::warnf("(TypeSoundAssetPtr) - Cannot set multiple args to a single asset.");
  61. }
  62. //-----------------------------------------------------------------------------
  63. ConsoleType(assetIdString, TypeSoundAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  64. ConsoleGetType(TypeSoundAssetId)
  65. {
  66. // Fetch asset Id.
  67. return *((const char**)(dptr));
  68. }
  69. ConsoleSetType(TypeSoundAssetId)
  70. {
  71. // Was a single argument specified?
  72. if (argc == 1)
  73. {
  74. // Yes, so fetch field value.
  75. *((const char**)dptr) = StringTable->insert(argv[0]);
  76. return;
  77. }
  78. // Warn.
  79. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  80. }
  81. //-----------------------------------------------------------------------------
  82. SoundAsset::SoundAsset()
  83. {
  84. mSoundFile = StringTable->EmptyString();
  85. mSoundPath = StringTable->EmptyString();
  86. mSubtitleString = StringTable->EmptyString();
  87. mLoadedState = AssetErrCode::NotLoaded;
  88. mPreload = false;
  89. // SFX description inits
  90. // reverb is useless here, reverb is inacted on listener.
  91. mProfileDesc.mPitch = 1;
  92. mProfileDesc.mVolume = 1;
  93. mProfileDesc.mIs3D = false;
  94. mProfileDesc.mIsLooping = false;
  95. mProfileDesc.mIsStreaming = false;
  96. mProfileDesc.mUseHardware = false;
  97. mProfileDesc.mMinDistance = 1;
  98. mProfileDesc.mMaxDistance = 100;
  99. mProfileDesc.mConeInsideAngle = 360;
  100. mProfileDesc.mConeOutsideAngle = 360;
  101. mProfileDesc.mConeOutsideVolume = 1;
  102. mProfileDesc.mRolloffFactor = -1.0f;
  103. mProfileDesc.mScatterDistance = Point3F(0.f, 0.f, 0.f);
  104. mProfileDesc.mPriority = 1.0f;
  105. mProfileDesc.mSourceGroup = NULL;
  106. }
  107. //-----------------------------------------------------------------------------
  108. SoundAsset::~SoundAsset()
  109. {
  110. }
  111. //-----------------------------------------------------------------------------
  112. void SoundAsset::initPersistFields()
  113. {
  114. // Call parent.
  115. Parent::initPersistFields();
  116. addProtectedField("soundFile", TypeAssetLooseFilePath, Offset(mSoundFile, SoundAsset),
  117. &setSoundFile, &getSoundFile, "Path to the sound file.");
  118. addField("pitchAdjust", TypeF32, Offset(mProfileDesc.mPitch, SoundAsset), "Adjustment of the pitch value 1 is default.");
  119. addField("volumeAdjust", TypeF32, Offset(mProfileDesc.mVolume, SoundAsset), "Adjustment to the volume.");
  120. addField("is3D", TypeBool, Offset(mProfileDesc.mIs3D, SoundAsset), "Set this sound to 3D.");
  121. addField("isLooping", TypeBool, Offset(mProfileDesc.mIsLooping, SoundAsset), "Does this sound loop.");
  122. // if streaming, a default packet size should be chosen for all sounds.
  123. addField("isStreaming", TypeBool, Offset(mProfileDesc.mIsStreaming, SoundAsset), "Use streaming.");
  124. //....why?
  125. addField("useHardware", TypeBool, Offset(mProfileDesc.mUseHardware, SoundAsset), "Use hardware mixing for this sound.");
  126. addField("minDistance", TypeF32, Offset(mProfileDesc.mMinDistance, SoundAsset), "Minimum distance for sound.");
  127. // more like it.
  128. addField("maxDistance", TypeF32, Offset(mProfileDesc.mMaxDistance, SoundAsset), "Max distance for sound.");
  129. addField("coneInsideAngle", TypeS32, Offset(mProfileDesc.mConeInsideAngle, SoundAsset), "Cone inside angle.");
  130. addField("coneOutsideAngle", TypeS32, Offset(mProfileDesc.mConeOutsideAngle, SoundAsset), "Cone outside angle.");
  131. addField("coneOutsideVolume", TypeS32, Offset(mProfileDesc.mConeOutsideVolume, SoundAsset), "Cone outside volume.");
  132. addField("rolloffFactor", TypeF32, Offset(mProfileDesc.mRolloffFactor, SoundAsset), "Rolloff factor.");
  133. addField("scatterDistance", TypePoint3F, Offset(mProfileDesc.mScatterDistance, SoundAsset), "Randomization to the spacial position of the sound.");
  134. addField("sourceGroup", TypeSFXSourceName, Offset(mProfileDesc.mSourceGroup, SoundAsset), "Group that sources playing with this description should be put into.");
  135. }
  136. //------------------------------------------------------------------------------
  137. void SoundAsset::copyTo(SimObject* object)
  138. {
  139. // Call to parent.
  140. Parent::copyTo(object);
  141. }
  142. void SoundAsset::initializeAsset(void)
  143. {
  144. Parent::initializeAsset();
  145. if (mSoundFile == StringTable->EmptyString())
  146. return;
  147. //ResourceManager::get().getChangedSignal.notify(this, &SoundAsset::_onResourceChanged);
  148. //Ensure our path is expando'd if it isn't already
  149. if (!Platform::isFullPath(mSoundPath))
  150. mSoundPath = getOwned() ? expandAssetFilePath(mSoundFile) : mSoundPath;
  151. mSoundPath = expandAssetFilePath(mSoundPath);
  152. loadSound();
  153. }
  154. void SoundAsset::_onResourceChanged(const Torque::Path &path)
  155. {
  156. if (path != Torque::Path(mSoundPath))
  157. return;
  158. refreshAsset();
  159. loadSound();
  160. }
  161. void SoundAsset::onAssetRefresh(void)
  162. {
  163. if (mSoundFile == StringTable->EmptyString())
  164. return;
  165. //Update
  166. if (!Platform::isFullPath(mSoundFile))
  167. mSoundPath = getOwned() ? expandAssetFilePath(mSoundFile) : mSoundPath;
  168. loadSound();
  169. }
  170. bool SoundAsset::loadSound()
  171. {
  172. if (mSoundPath)
  173. {
  174. if (!Torque::FS::IsFile(mSoundPath))
  175. {
  176. Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile);
  177. mLoadedState = BadFileReference;
  178. return false;
  179. }
  180. else
  181. {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
  182. mSFXProfile.setDescription(&mProfileDesc);
  183. mSFXProfile.setSoundFileName(mSoundFile);
  184. mSFXProfile.setPreload(mPreload);
  185. }
  186. }
  187. mChangeSignal.trigger();
  188. mLoadedState = Ok;
  189. return true;
  190. }
  191. void SoundAsset::setSoundFile(const char* pSoundFile)
  192. {
  193. // Sanity!
  194. AssertFatal(pSoundFile != NULL, "Cannot use a NULL sound file.");
  195. // Fetch sound file.
  196. pSoundFile = StringTable->insert(pSoundFile);
  197. // Ignore no change,
  198. if (pSoundFile == mSoundFile)
  199. return;
  200. // Update.
  201. mSoundFile = pSoundFile;
  202. // Refresh the asset.
  203. refreshAsset();
  204. }
  205. DefineEngineMethod(SoundAsset, getSoundPath, const char*, (), , "")
  206. {
  207. return object->getSoundPath();
  208. }
  209. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetPtr);
  210. ConsoleDocClass(GuiInspectorTypeSoundAssetPtr,
  211. "@brief Inspector field type for Sounds\n\n"
  212. "Editor use only.\n\n"
  213. "@internal"
  214. );
  215. void GuiInspectorTypeSoundAssetPtr::consoleInit()
  216. {
  217. Parent::consoleInit();
  218. ConsoleBaseType::getType(TypeSoundAssetPtr)->setInspectorFieldType("GuiInspectorTypeSoundAssetPtr");
  219. }
  220. GuiControl * GuiInspectorTypeSoundAssetPtr::constructEditControl()
  221. {
  222. return nullptr;
  223. }
  224. bool GuiInspectorTypeSoundAssetPtr::updateRects()
  225. {
  226. return false;
  227. }
  228. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetId);
  229. ConsoleDocClass(GuiInspectorTypeSoundAssetId,
  230. "@brief Inspector field type for Sounds\n\n"
  231. "Editor use only.\n\n"
  232. "@internal"
  233. );
  234. void GuiInspectorTypeSoundAssetId::consoleInit()
  235. {
  236. Parent::consoleInit();
  237. ConsoleBaseType::getType(TypeSoundAssetId)->setInspectorFieldType("GuiInspectorTypeSoundAssetId");
  238. }