SoundAsset.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. mSFXProfile.setDescription(NULL);
  178. mSFXProfile.setSoundFileName(StringTable->insert(StringTable->EmptyString()));
  179. mSFXProfile.setPreload(false);
  180. return false;
  181. }
  182. else
  183. {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
  184. mSFXProfile.setDescription(&mProfileDesc);
  185. mSFXProfile.setSoundFileName(mSoundPath);
  186. mSFXProfile.setPreload(mPreload);
  187. //give it a nudge to preload if required
  188. mSFXProfile.getBuffer();
  189. }
  190. }
  191. mChangeSignal.trigger();
  192. mLoadedState = Ok;
  193. return true;
  194. }
  195. void SoundAsset::setSoundFile(const char* pSoundFile)
  196. {
  197. // Sanity!
  198. AssertFatal(pSoundFile != NULL, "Cannot use a NULL sound file.");
  199. // Fetch sound file.
  200. pSoundFile = StringTable->insert(pSoundFile, true);
  201. // Ignore no change,
  202. if (pSoundFile == mSoundFile)
  203. return;
  204. // Update.
  205. mSoundFile = getOwned() ? expandAssetFilePath(pSoundFile) : pSoundFile;
  206. // Refresh the asset.
  207. refreshAsset();
  208. }
  209. StringTableEntry SoundAsset::getAssetIdByFileName(StringTableEntry fileName)
  210. {
  211. if (fileName == StringTable->EmptyString())
  212. return StringTable->EmptyString();
  213. StringTableEntry soundAssetId = StringTable->EmptyString();
  214. AssetQuery query;
  215. U32 foundCount = AssetDatabase.findAssetType(&query, "SoundAsset");
  216. if (foundCount != 0)
  217. {
  218. for (U32 i = 0; i < foundCount; i++)
  219. {
  220. SoundAsset* soundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  221. if (soundAsset && soundAsset->getSoundPath() == fileName)
  222. {
  223. soundAssetId = soundAsset->getAssetId();
  224. AssetDatabase.releaseAsset(query.mAssetList[i]);
  225. break;
  226. }
  227. AssetDatabase.releaseAsset(query.mAssetList[i]);
  228. }
  229. }
  230. return soundAssetId;
  231. }
  232. U32 SoundAsset::getAssetById(StringTableEntry assetId, AssetPtr<SoundAsset>* soundAsset)
  233. {
  234. (*soundAsset) = assetId;
  235. if (soundAsset->notNull())
  236. {
  237. return (*soundAsset)->mLoadedState;
  238. }
  239. else
  240. {
  241. //Well that's bad, loading the fallback failed.
  242. Con::warnf("SoundAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  243. return AssetErrCode::Failed;
  244. }
  245. }
  246. U32 SoundAsset::getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsset>* soundAsset)
  247. {
  248. AssetQuery query;
  249. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "SoundAsset");
  250. if (foundAssetcount == 0)
  251. {
  252. //Well that's bad, loading the fallback failed.
  253. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with filename %s failed with no fallback asset", fileName);
  254. return AssetErrCode::Failed;
  255. }
  256. else
  257. {
  258. for (U32 i = 0; i < foundAssetcount; i++)
  259. {
  260. SoundAsset* tSoundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  261. if (tSoundAsset && tSoundAsset->getSoundPath() == fileName)
  262. {
  263. soundAsset->setAssetId(query.mAssetList[i]);
  264. AssetDatabase.releaseAsset(query.mAssetList[i]);
  265. return (*soundAsset)->mLoadedState;
  266. }
  267. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  268. }
  269. }
  270. //No good match
  271. return AssetErrCode::Failed;
  272. }
  273. DefineEngineMethod(SoundAsset, getSoundPath, const char*, (), , "")
  274. {
  275. return object->getSoundPath();
  276. }
  277. DefineEngineMethod(SoundAsset, playSound, S32, (Point3F position), (Point3F::Zero),
  278. "Plays the sound for this asset.\n"
  279. "@return (sound plays).\n")
  280. {
  281. if (object->getSfxProfile())
  282. {
  283. MatrixF transform;
  284. transform.setPosition(position);
  285. SFXSource* source = SFX->playOnce(object->getSfxProfile(), &transform, NULL, -1);
  286. if(source)
  287. return source->getId();
  288. else
  289. return 0;
  290. }
  291. else
  292. return 0;
  293. }
  294. #ifdef TORQUE_TOOLS
  295. DefineEngineStaticMethod(SoundAsset, getAssetIdByFilename, const char*, (const char* filePath), (""),
  296. "Queries the Asset Database to see if any asset exists that is associated with the provided file path.\n"
  297. "@return The AssetId of the associated asset, if any.")
  298. {
  299. return SoundAsset::getAssetIdByFileName(StringTable->insert(filePath));
  300. }
  301. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetPtr);
  302. ConsoleDocClass(GuiInspectorTypeSoundAssetPtr,
  303. "@brief Inspector field type for Sounds\n\n"
  304. "Editor use only.\n\n"
  305. "@internal"
  306. );
  307. void GuiInspectorTypeSoundAssetPtr::consoleInit()
  308. {
  309. Parent::consoleInit();
  310. ConsoleBaseType::getType(TypeSoundAssetPtr)->setInspectorFieldType("GuiInspectorTypeSoundAssetPtr");
  311. }
  312. GuiControl * GuiInspectorTypeSoundAssetPtr::constructEditControl()
  313. {
  314. // Create base filename edit controls
  315. GuiControl* retCtrl = Parent::constructEditControl();
  316. if (retCtrl == NULL)
  317. return retCtrl;
  318. // Change filespec
  319. char szBuffer[512];
  320. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"SoundAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  321. getIdString());
  322. mBrowseButton->setField("Command", szBuffer);
  323. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  324. // Create "Open in Editor" button
  325. mEditButton = new GuiBitmapButtonCtrl();
  326. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  327. mEditButton->setField("Command", szBuffer);
  328. char bitmapName[512] = "ToolsModule:SFXEmitter_image";
  329. mEditButton->setBitmap(StringTable->insert(bitmapName));
  330. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  331. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  332. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  333. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Test play this sound");
  334. mEditButton->registerObject();
  335. addObject(mEditButton);
  336. return retCtrl;
  337. }
  338. bool GuiInspectorTypeSoundAssetPtr::updateRects()
  339. {
  340. S32 dividerPos, dividerMargin;
  341. mInspector->getDivider(dividerPos, dividerMargin);
  342. Point2I fieldExtent = getExtent();
  343. Point2I fieldPos = getPosition();
  344. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  345. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  346. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  347. if (mBrowseButton != NULL)
  348. {
  349. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  350. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  351. }
  352. if (mEditButton != NULL)
  353. {
  354. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  355. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  356. }
  357. return resized;
  358. }
  359. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetId);
  360. ConsoleDocClass(GuiInspectorTypeSoundAssetId,
  361. "@brief Inspector field type for Sounds\n\n"
  362. "Editor use only.\n\n"
  363. "@internal"
  364. );
  365. void GuiInspectorTypeSoundAssetId::consoleInit()
  366. {
  367. Parent::consoleInit();
  368. ConsoleBaseType::getType(TypeSoundAssetId)->setInspectorFieldType("GuiInspectorTypeSoundAssetId");
  369. }
  370. #endif