SoundAsset.cpp 15 KB

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