SoundAsset.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #ifndef _SFXSOURCE_H_
  38. #include "sfx/sfxSource.h"
  39. #endif
  40. // Debug Profiling.
  41. #include "platform/profiler.h"
  42. #include "sfx/sfxTypes.h"
  43. //-----------------------------------------------------------------------------
  44. IMPLEMENT_CONOBJECT(SoundAsset);
  45. ConsoleType(SoundAssetPtr, TypeSoundAssetPtr, const char*, ASSET_ID_FIELD_PREFIX)
  46. //-----------------------------------------------------------------------------
  47. ConsoleGetType(TypeSoundAssetPtr)
  48. {
  49. // Fetch asset Id.
  50. return *((const char**)(dptr));
  51. }
  52. //-----------------------------------------------------------------------------
  53. ConsoleSetType(TypeSoundAssetPtr)
  54. {
  55. // Was a single argument specified?
  56. if (argc == 1)
  57. {
  58. // Yes, so fetch field value.
  59. *((const char**)dptr) = StringTable->insert(argv[0]);
  60. return;
  61. }
  62. // Warn.
  63. Con::warnf("(TypeSoundAssetPtr) - Cannot set multiple args to a single asset.");
  64. }
  65. //-----------------------------------------------------------------------------
  66. ConsoleType(assetIdString, TypeSoundAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  67. ConsoleGetType(TypeSoundAssetId)
  68. {
  69. // Fetch asset Id.
  70. return *((const char**)(dptr));
  71. }
  72. ConsoleSetType(TypeSoundAssetId)
  73. {
  74. // Was a single argument specified?
  75. if (argc == 1)
  76. {
  77. // Yes, so fetch field value.
  78. *((const char**)dptr) = StringTable->insert(argv[0]);
  79. return;
  80. }
  81. // Warn.
  82. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  83. }
  84. //-----------------------------------------------------------------------------
  85. SoundAsset::SoundAsset()
  86. {
  87. mSoundFile = StringTable->EmptyString();
  88. mSoundPath = StringTable->EmptyString();
  89. mSubtitleString = StringTable->EmptyString();
  90. mLoadedState = AssetErrCode::NotLoaded;
  91. mPreload = false;
  92. // SFX description inits
  93. // reverb is useless here, reverb is inacted on listener.
  94. mProfileDesc.mPitch = 1;
  95. mProfileDesc.mVolume = 1;
  96. mProfileDesc.mIs3D = false;
  97. mProfileDesc.mIsLooping = false;
  98. mProfileDesc.mIsStreaming = false;
  99. mProfileDesc.mUseHardware = false;
  100. mProfileDesc.mMinDistance = 1;
  101. mProfileDesc.mMaxDistance = 100;
  102. mProfileDesc.mConeInsideAngle = 360;
  103. mProfileDesc.mConeOutsideAngle = 360;
  104. mProfileDesc.mConeOutsideVolume = 1;
  105. mProfileDesc.mRolloffFactor = -1.0f;
  106. mProfileDesc.mScatterDistance = Point3F(0.f, 0.f, 0.f);
  107. mProfileDesc.mPriority = 1.0f;
  108. mProfileDesc.mSourceGroup = NULL;
  109. }
  110. //-----------------------------------------------------------------------------
  111. SoundAsset::~SoundAsset()
  112. {
  113. }
  114. //-----------------------------------------------------------------------------
  115. void SoundAsset::initPersistFields()
  116. {
  117. // Call parent.
  118. Parent::initPersistFields();
  119. addProtectedField("soundFile", TypeAssetLooseFilePath, Offset(mSoundFile, SoundAsset),
  120. &setSoundFile, &getSoundFile, "Path to the sound file.");
  121. addField("pitchAdjust", TypeF32, Offset(mProfileDesc.mPitch, SoundAsset), "Adjustment of the pitch value 1 is default.");
  122. addField("volumeAdjust", TypeF32, Offset(mProfileDesc.mVolume, SoundAsset), "Adjustment to the volume.");
  123. addField("is3D", TypeBool, Offset(mProfileDesc.mIs3D, SoundAsset), "Set this sound to 3D.");
  124. addField("isLooping", TypeBool, Offset(mProfileDesc.mIsLooping, SoundAsset), "Does this sound loop.");
  125. // if streaming, a default packet size should be chosen for all sounds.
  126. addField("isStreaming", TypeBool, Offset(mProfileDesc.mIsStreaming, SoundAsset), "Use streaming.");
  127. //....why?
  128. addField("useHardware", TypeBool, Offset(mProfileDesc.mUseHardware, SoundAsset), "Use hardware mixing for this sound.");
  129. addField("minDistance", TypeF32, Offset(mProfileDesc.mMinDistance, SoundAsset), "Minimum distance for sound.");
  130. // more like it.
  131. addField("maxDistance", TypeF32, Offset(mProfileDesc.mMaxDistance, SoundAsset), "Max distance for sound.");
  132. addField("coneInsideAngle", TypeS32, Offset(mProfileDesc.mConeInsideAngle, SoundAsset), "Cone inside angle.");
  133. addField("coneOutsideAngle", TypeS32, Offset(mProfileDesc.mConeOutsideAngle, SoundAsset), "Cone outside angle.");
  134. addField("coneOutsideVolume", TypeF32, Offset(mProfileDesc.mConeOutsideVolume, SoundAsset), "Cone outside volume.");
  135. addField("rolloffFactor", TypeF32, Offset(mProfileDesc.mRolloffFactor, SoundAsset), "Rolloff factor.");
  136. addField("scatterDistance", TypePoint3F, Offset(mProfileDesc.mScatterDistance, SoundAsset), "Randomization to the spacial position of the sound.");
  137. addField("sourceGroup", TypeSFXSourceName, Offset(mProfileDesc.mSourceGroup, SoundAsset), "Group that sources playing with this description should be put into.");
  138. }
  139. //------------------------------------------------------------------------------
  140. void SoundAsset::copyTo(SimObject* object)
  141. {
  142. // Call to parent.
  143. Parent::copyTo(object);
  144. }
  145. void SoundAsset::initializeAsset(void)
  146. {
  147. Parent::initializeAsset();
  148. if (mSoundFile == StringTable->EmptyString())
  149. return;
  150. mSoundPath = getOwned() ? expandAssetFilePath(mSoundFile) : mSoundPath;
  151. loadSound();
  152. }
  153. void SoundAsset::_onResourceChanged(const Torque::Path &path)
  154. {
  155. if (path != Torque::Path(mSoundPath))
  156. return;
  157. refreshAsset();
  158. loadSound();
  159. }
  160. void SoundAsset::onAssetRefresh(void)
  161. {
  162. if (mSoundFile == StringTable->EmptyString())
  163. return;
  164. //Update
  165. mSoundPath = getOwned() ? expandAssetFilePath(mSoundFile) : mSoundPath;
  166. loadSound();
  167. }
  168. bool SoundAsset::loadSound()
  169. {
  170. if (mSoundPath)
  171. {
  172. if (!Torque::FS::IsFile(mSoundPath))
  173. {
  174. Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile);
  175. mLoadedState = BadFileReference;
  176. return false;
  177. }
  178. else
  179. {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
  180. mSFXProfile.setDescription(&mProfileDesc);
  181. mSFXProfile.setSoundFileName(mSoundPath);
  182. mSFXProfile.setPreload(mPreload);
  183. }
  184. }
  185. mChangeSignal.trigger();
  186. mLoadedState = Ok;
  187. return true;
  188. }
  189. void SoundAsset::setSoundFile(const char* pSoundFile)
  190. {
  191. // Sanity!
  192. AssertFatal(pSoundFile != NULL, "Cannot use a NULL sound file.");
  193. // Fetch sound file.
  194. pSoundFile = StringTable->insert(pSoundFile, true);
  195. // Ignore no change,
  196. if (pSoundFile == mSoundFile)
  197. return;
  198. // Update.
  199. mSoundFile = getOwned() ? expandAssetFilePath(pSoundFile) : pSoundFile;
  200. // Refresh the asset.
  201. refreshAsset();
  202. }
  203. StringTableEntry SoundAsset::getAssetIdByFileName(StringTableEntry fileName)
  204. {
  205. if (fileName == StringTable->EmptyString())
  206. return StringTable->EmptyString();
  207. StringTableEntry materialAssetId = "";
  208. AssetQuery query;
  209. U32 foundCount = AssetDatabase.findAssetType(&query, "SoundAsset");
  210. if (foundCount != 0)
  211. {
  212. for (U32 i = 0; i < foundCount; i++)
  213. {
  214. SoundAsset* soundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  215. if (soundAsset && soundAsset->getSoundPath() == fileName)
  216. {
  217. materialAssetId = soundAsset->getAssetId();
  218. AssetDatabase.releaseAsset(query.mAssetList[i]);
  219. break;
  220. }
  221. AssetDatabase.releaseAsset(query.mAssetList[i]);
  222. }
  223. }
  224. return materialAssetId;
  225. }
  226. U32 SoundAsset::getAssetById(StringTableEntry assetId, AssetPtr<SoundAsset>* materialAsset)
  227. {
  228. (*materialAsset) = assetId;
  229. if (materialAsset->notNull())
  230. {
  231. return (*materialAsset)->mLoadedState;
  232. }
  233. else
  234. {
  235. //Well that's bad, loading the fallback failed.
  236. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  237. return AssetErrCode::Failed;
  238. }
  239. }
  240. U32 SoundAsset::getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsset>* soundAsset)
  241. {
  242. AssetQuery query;
  243. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "SoundAsset");
  244. if (foundAssetcount == 0)
  245. {
  246. //Well that's bad, loading the fallback failed.
  247. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with filename %s failed with no fallback asset", fileName);
  248. return AssetErrCode::Failed;
  249. }
  250. else
  251. {
  252. for (U32 i = 0; i < foundAssetcount; i++)
  253. {
  254. SoundAsset* tSoundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  255. if (tSoundAsset && tSoundAsset->getSoundPath() == fileName)
  256. {
  257. soundAsset->setAssetId(query.mAssetList[i]);
  258. AssetDatabase.releaseAsset(query.mAssetList[i]);
  259. return (*soundAsset)->mLoadedState;
  260. }
  261. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  262. }
  263. }
  264. //No good match
  265. return AssetErrCode::Failed;
  266. }
  267. DefineEngineMethod(SoundAsset, getSoundPath, const char*, (), , "")
  268. {
  269. return object->getSoundPath();
  270. }
  271. DefineEngineMethod(SoundAsset, playSound, S32, (Point3F position), (Point3F::Zero),
  272. "Gets the number of materials for this shape asset.\n"
  273. "@return Material count.\n")
  274. {
  275. if (object->getSfxProfile())
  276. {
  277. MatrixF transform;
  278. transform.setPosition(position);
  279. SFXSource* source = SFX->playOnce(object->getSfxProfile(), &transform, NULL, -1);
  280. return source->getId();
  281. }
  282. else
  283. return 0;
  284. }
  285. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetPtr);
  286. ConsoleDocClass(GuiInspectorTypeSoundAssetPtr,
  287. "@brief Inspector field type for Sounds\n\n"
  288. "Editor use only.\n\n"
  289. "@internal"
  290. );
  291. void GuiInspectorTypeSoundAssetPtr::consoleInit()
  292. {
  293. Parent::consoleInit();
  294. ConsoleBaseType::getType(TypeSoundAssetPtr)->setInspectorFieldType("GuiInspectorTypeSoundAssetPtr");
  295. }
  296. GuiControl * GuiInspectorTypeSoundAssetPtr::constructEditControl()
  297. {
  298. // Create base filename edit controls
  299. GuiControl* retCtrl = Parent::constructEditControl();
  300. if (retCtrl == NULL)
  301. return retCtrl;
  302. // Change filespec
  303. char szBuffer[512];
  304. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"SoundAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  305. getIdString());
  306. mBrowseButton->setField("Command", szBuffer);
  307. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  308. // Create "Open in Editor" button
  309. mEditButton = new GuiBitmapButtonCtrl();
  310. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  311. mEditButton->setField("Command", szBuffer);
  312. char bitmapName[512] = "ToolsModule:SFXEmitter_image";
  313. mEditButton->setBitmap(StringTable->insert(bitmapName));
  314. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  315. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  316. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  317. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Test play this sound");
  318. mEditButton->registerObject();
  319. addObject(mEditButton);
  320. return retCtrl;
  321. }
  322. bool GuiInspectorTypeSoundAssetPtr::updateRects()
  323. {
  324. S32 dividerPos, dividerMargin;
  325. mInspector->getDivider(dividerPos, dividerMargin);
  326. Point2I fieldExtent = getExtent();
  327. Point2I fieldPos = getPosition();
  328. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  329. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  330. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  331. if (mBrowseButton != NULL)
  332. {
  333. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  334. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  335. }
  336. if (mEditButton != NULL)
  337. {
  338. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  339. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  340. }
  341. return resized;
  342. }
  343. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetId);
  344. ConsoleDocClass(GuiInspectorTypeSoundAssetId,
  345. "@brief Inspector field type for Sounds\n\n"
  346. "Editor use only.\n\n"
  347. "@internal"
  348. );
  349. void GuiInspectorTypeSoundAssetId::consoleInit()
  350. {
  351. Parent::consoleInit();
  352. ConsoleBaseType::getType(TypeSoundAssetId)->setInspectorFieldType("GuiInspectorTypeSoundAssetId");
  353. }