SoundAsset.cpp 14 KB

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