SoundAsset.cpp 14 KB

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