SoundAsset.cpp 14 KB

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