SoundAsset.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. #ifndef _SFXPROFILE_H_
  41. #include "sfx/sfxProfile.h"
  42. #endif // !_SFXPROFILE_H_
  43. // Debug Profiling.
  44. #include "platform/profiler.h"
  45. #include "sfx/sfxTypes.h"
  46. #include "SoundAssetInspectors.h"
  47. #include "console/typeValidators.h"
  48. //-----------------------------------------------------------------------------
  49. IMPLEMENT_CONOBJECT(SoundAsset);
  50. ConsoleType(SoundAssetPtr, TypeSoundAssetPtr, const char*, ASSET_ID_FIELD_PREFIX)
  51. //-----------------------------------------------------------------------------
  52. ConsoleGetType(TypeSoundAssetPtr)
  53. {
  54. // Fetch asset Id.
  55. return *((const char**)(dptr));
  56. }
  57. //-----------------------------------------------------------------------------
  58. ConsoleSetType(TypeSoundAssetPtr)
  59. {
  60. // Was a single argument specified?
  61. if (argc == 1)
  62. {
  63. // Yes, so fetch field value.
  64. *((const char**)dptr) = StringTable->insert(argv[0]);
  65. return;
  66. }
  67. // Warn.
  68. Con::warnf("(TypeSoundAssetPtr) - Cannot set multiple args to a single asset.");
  69. }
  70. //-----------------------------------------------------------------------------
  71. ConsoleType(assetIdString, TypeSoundAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  72. ConsoleGetType(TypeSoundAssetId)
  73. {
  74. // Fetch asset Id.
  75. return *((const char**)(dptr));
  76. }
  77. ConsoleSetType(TypeSoundAssetId)
  78. {
  79. // Was a single argument specified?
  80. if (argc == 1)
  81. {
  82. // Yes, so fetch field value.
  83. *((const char**)dptr) = StringTable->insert(argv[0]);
  84. return;
  85. }
  86. // Warn.
  87. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  88. }
  89. const String SoundAsset::mErrCodeStrings[] =
  90. {
  91. "BadProfile",
  92. "BadDescription",
  93. "BadBufferData",
  94. "UnKnown"
  95. };
  96. //-----------------------------------------------------------------------------
  97. SoundAsset::SoundAsset()
  98. : AssetBase()
  99. {
  100. dMemset(mPlaylist.mSlots.mReplayMode, 0, sizeof(mPlaylist.mSlots.mReplayMode));
  101. dMemset(mPlaylist.mSlots.mTransitionIn, 0, sizeof(mPlaylist.mSlots.mTransitionIn));
  102. dMemset(mPlaylist.mSlots.mRepeatCount, 0, sizeof(mPlaylist.mSlots.mRepeatCount));
  103. dMemset(mPlaylist.mSlots.mState, 0, sizeof(mPlaylist.mSlots.mState));
  104. dMemset(mPlaylist.mSlots.mTrack, 0, sizeof(mPlaylist.mSlots.mTrack));
  105. dMemset(mPlaylist.mSlots.mStateMode, 0, sizeof(mPlaylist.mSlots.mStateMode));
  106. for (U32 i = 0; i < SFXPlayList::NUM_SLOTS; i++)
  107. {
  108. mSoundFile[i] = StringTable->EmptyString();
  109. mSoundPath[i] = StringTable->EmptyString();
  110. mPlaylist.mSlots.mTransitionOut[i] = SFXPlayList::TRANSITION_Wait;
  111. mPlaylist.mSlots.mVolumeScale.mValue[i] = 1.f;
  112. mPlaylist.mSlots.mPitchScale.mValue[i] = 1.f;
  113. mPlaylist.mSlots.mFadeTimeIn.mValue[i] = -1.f; // Don't touch by default.
  114. mPlaylist.mSlots.mFadeTimeOut.mValue[i] = -1.f; // Don't touch by default.
  115. mPlaylist.mSlots.mMinDistance.mValue[i] = -1.f; // Don't touch by default.
  116. mPlaylist.mSlots.mMaxDistance.mValue[i] = -1.f; // Don't touch by default.
  117. }
  118. mSubtitleString = StringTable->EmptyString();
  119. mLoadedState = AssetErrCode::NotLoaded;
  120. mPreload = false;
  121. // SFX description inits
  122. // reverb is useless here, reverb is inacted on listener.
  123. mProfileDesc.mPitch = 1;
  124. mProfileDesc.mVolume = 1;
  125. mProfileDesc.mIs3D = false;
  126. mProfileDesc.mIsLooping = false;
  127. mProfileDesc.mIsStreaming = false;
  128. mProfileDesc.mUseHardware = false;
  129. mProfileDesc.mMinDistance = 1;
  130. mProfileDesc.mMaxDistance = 100;
  131. mProfileDesc.mConeInsideAngle = 360;
  132. mProfileDesc.mConeOutsideAngle = 360;
  133. mProfileDesc.mConeOutsideVolume = 1;
  134. mProfileDesc.mRolloffFactor = -1.0f;
  135. mProfileDesc.mScatterDistance = Point3F(0.f, 0.f, 0.f);
  136. mProfileDesc.mPriority = 1.0f;
  137. mProfileDesc.mSourceGroup = NULL;
  138. mProfileDesc.mFadeInEase = EaseF();
  139. mIsPlaylist = false;
  140. mPlaylist.mNumSlotsToPlay = SFXPlayList::SFXPlaylistSettings::NUM_SLOTS;
  141. mPlaylist.mRandomMode = SFXPlayList::RANDOM_NotRandom;
  142. mPlaylist.mTrace = false;
  143. mPlaylist.mLoopMode = SFXPlayList::LOOP_All;
  144. mPlaylist.mActiveSlots = 1;
  145. }
  146. //-----------------------------------------------------------------------------
  147. SoundAsset::~SoundAsset()
  148. {
  149. }
  150. //-----------------------------------------------------------------------------
  151. void SoundAsset::initPersistFields()
  152. {
  153. docsURL;
  154. // Call parent.
  155. Parent::initPersistFields();
  156. addGroup("SoundSlots");
  157. addArray("slots", SFXPlayList::SFXPlaylistSettings::NUM_SLOTS);
  158. addProtectedField("soundFile", TypeAssetLooseFilePath, Offset(mSoundFile, SoundAsset),
  159. &_setSoundFile, &defaultProtectedGetFn, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS, "Path to the sound file.");
  160. addField("replay", TYPEID< SFXPlayList::EReplayMode >(), Offset(mPlaylist.mSlots.mReplayMode, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  161. "Behavior when an already playing sound is encountered on this slot from a previous cycle.\n"
  162. "Each slot can have an arbitrary number of sounds playing on it from previous cycles. This field determines "
  163. "how SFXController will handle these sources.");
  164. addField("transitionIn", TYPEID< SFXPlayList::ETransitionMode >(), Offset(mPlaylist.mSlots.mTransitionIn, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  165. "Behavior when moving into this slot.\n"
  166. "After the delayIn time has expired (if any), this slot determines what the controller "
  167. "will do before actually playing the slot.");
  168. addField("transitionOut", TYPEID< SFXPlayList::ETransitionMode >(), Offset(mPlaylist.mSlots.mTransitionOut, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  169. "Behavior when moving out of this slot.\n"
  170. "After the #detailTimeOut has expired (if any), this slot determines what the controller "
  171. "will do before moving on to the next slot.");
  172. addFieldV("delayTimeIn", TypeRangedF32, Offset(mPlaylist.mSlots.mDelayTimeIn.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  173. "Seconds to wait after moving into slot before #transitionIn.");
  174. addField("delayTimeInVariance", TypePoint2F, Offset(mPlaylist.mSlots.mDelayTimeIn.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  175. "Bounds on randomization of #delayTimeIn.\n\n"
  176. "@ref SFXPlayList_randomization\n");
  177. addFieldV("delayTimeOut", TypeRangedF32, Offset(mPlaylist.mSlots.mDelayTimeOut.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  178. "Seconds to wait before moving out of slot after #transitionOut.");
  179. addField("delayTimeOutVariance", TypePoint2F, Offset(mPlaylist.mSlots.mDelayTimeOut.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  180. "Bounds on randomization of #delayTimeOut.\n\n"
  181. "@ref SFXPlayList_randomization\n");
  182. addFieldV("fadeTimeIn", TypeRangedF32, Offset(mPlaylist.mSlots.mFadeTimeIn.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  183. "Seconds to fade sound in (-1 to use the track's own fadeInTime.)\n"
  184. "@see SFXDescription::fadeTimeIn");
  185. addField("fadeTimeInVariance", TypePoint2F, Offset(mPlaylist.mSlots.mFadeTimeIn.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  186. "Bounds on randomization of #fadeInTime.\n\n"
  187. "@ref SFXPlayList_randomization\n");
  188. addFieldV("fadeTimeOut", TypeRangedF32, Offset(mPlaylist.mSlots.mFadeTimeOut.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  189. "Seconds to fade sound out (-1 to use the track's own fadeOutTime.)\n"
  190. "@see SFXDescription::fadeTimeOut");
  191. addField("fadeTimeOutVariance", TypePoint2F, Offset(mPlaylist.mSlots.mFadeTimeOut.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  192. "Bounds on randomization of #fadeOutTime\n\n"
  193. "@ref SFXPlayList_randomization\n");
  194. addFieldV("referenceDistance", TypeRangedF32, Offset(mPlaylist.mSlots.mMinDistance.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  195. "@c referenceDistance to set for 3D sounds in this slot (<1 to use @c referenceDistance of track's own description).\n"
  196. "@see SFXDescription::referenceDistance");
  197. addField("referenceDistanceVariance", TypePoint2F, Offset(mPlaylist.mSlots.mMinDistance.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  198. "Bounds on randomization of #referenceDistance.\n\n"
  199. "@ref SFXPlayList_randomization\n");
  200. addFieldV("maxSlotDistance", TypeRangedF32, Offset(mPlaylist.mSlots.mMaxDistance.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  201. "@c maxDistance to apply to 3D sounds in this slot (<1 to use @c maxDistance of track's own description).\n"
  202. "@see SFXDescription::maxDistance");
  203. addField("maxSlotDistanceVariance", TypePoint2F, Offset(mPlaylist.mSlots.mMaxDistance.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  204. "Bounds on randomization of #maxDistance.\n\n"
  205. "@ref SFXPlayList_randomization\n");
  206. addFieldV("volumeScale", TypeRangedF32, Offset(mPlaylist.mSlots.mVolumeScale.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  207. "Scale factor to apply to volume of sounds played on this list slot.\n"
  208. "This value will scale the actual volume level set on the track assigned to the slot, i.e. a value of 0.5 will "
  209. "cause the track to play at half-volume.");
  210. addField("volumeScaleVariance", TypePoint2F, Offset(mPlaylist.mSlots.mVolumeScale.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  211. "Bounds on randomization of #volumeScale.\n\n"
  212. "@ref SFXPlayList_randomization\n");
  213. addFieldV("pitchScale", TypeRangedF32, Offset(mPlaylist.mSlots.mPitchScale.mValue, SoundAsset), &CommonValidators::PositiveFloat, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  214. "Scale factor to apply to pitch of sounds played on this list slot.\n"
  215. "This value will scale the actual pitch set on the track assigned to the slot, i.e. a value of 0.5 will "
  216. "cause the track to play at half its assigned speed.");
  217. addField("pitchScaleVariance", TypePoint2F, Offset(mPlaylist.mSlots.mPitchScale.mVariance, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  218. "Bounds on randomization of #pitchScale.\n\n"
  219. "@ref SFXPlayList_randomization\n");
  220. addFieldV("repeatCount", TypeRangedS32, Offset(mPlaylist.mSlots.mRepeatCount, SoundAsset), &CommonValidators::PositiveInt, SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  221. "Number of times to loop this slot.");
  222. addField("state", TypeSFXStateName, Offset(mPlaylist.mSlots.mState, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  223. "State that must be active for this slot to play.\n\n"
  224. "@ref SFXPlayList_states");
  225. addField("stateMode", TYPEID< SFXPlayList::EStateMode >(), Offset(mPlaylist.mSlots.mStateMode, SoundAsset), SFXPlayList::SFXPlaylistSettings::NUM_SLOTS,
  226. "Behavior when assigned state is deactivated while slot is playing.\n\n"
  227. "@ref SFXPlayList_states");
  228. endArray("slots");
  229. endGroup("SoundSlots");
  230. addGroup("General Profile");
  231. addFieldV("pitchAdjust", TypeRangedF32, Offset(mProfileDesc.mPitch, SoundAsset), &CommonValidators::PositiveFloat, "Adjustment of the pitch value 1 is default.");
  232. addFieldV("volumeAdjust", TypeRangedF32, Offset(mProfileDesc.mVolume, SoundAsset), &CommonValidators::PositiveFloat, "Adjustment to the volume.");
  233. addField("is3D", TypeBool, Offset(mProfileDesc.mIs3D, SoundAsset), "Set this sound to 3D.");
  234. addField("isLooping", TypeBool, Offset(mProfileDesc.mIsLooping, SoundAsset), "Does this sound loop.");
  235. // if streaming, a default packet size should be chosen for all sounds.
  236. addField("isStreaming", TypeBool, Offset(mProfileDesc.mIsStreaming, SoundAsset), "Use streaming.");
  237. //....why?
  238. addField("useHardware", TypeBool, Offset(mProfileDesc.mUseHardware, SoundAsset), "Use hardware mixing for this sound.");
  239. addField("sourceGroup", TypeSFXSourceName, Offset(mProfileDesc.mSourceGroup, SoundAsset), "Group that sources playing with this description should be put into.");
  240. addField("preload", TypeBool, Offset(mPreload, SoundAsset), "Whether to preload sound data when the profile is added to system.");
  241. endGroup("General Profile");
  242. addGroup("Fading");
  243. addFieldV("fadeInTime", TypeRangedF32, Offset(mProfileDesc.mFadeInTime, SoundAsset), &CommonValidators::PositiveFloat, "Number of seconds to gradually fade in volume from zero when playback starts.");
  244. addFieldV("fadeOutTime", TypeRangedF32, Offset(mProfileDesc.mFadeOutTime, SoundAsset), &CommonValidators::PositiveFloat, "Number of seconds to gradually fade out volume down to zero when playback is stopped or paused.");
  245. addField("fadeInEase", TypeEaseF, Offset(mProfileDesc.mFadeInEase, SoundAsset), "Easing curve for fade-in transition.");
  246. addField("fadeOutEase", TypeEaseF, Offset(mProfileDesc.mFadeOutEase, SoundAsset), "Easing curve for fade-out transition.");
  247. addField("fadeLoops", TypeBool, Offset(mProfileDesc.mFadeLoops, SoundAsset), "Fade each cycle of a loop in and/or out; otherwise only fade-in first cycle.");
  248. endGroup("Fading");
  249. addGroup("3D");
  250. addFieldV("minDistance", TypeRangedF32, Offset(mProfileDesc.mMinDistance, SoundAsset), &CommonValidators::PositiveFloat, "Minimum distance for sound.");
  251. addFieldV("maxDistance", TypeRangedF32, Offset(mProfileDesc.mMaxDistance, SoundAsset), &CommonValidators::PositiveFloat, "Max distance for sound.");
  252. addFieldV("coneInsideAngle", TypeRangedS32, Offset(mProfileDesc.mConeInsideAngle, SoundAsset), &CommonValidators::S32_PosDegreeRange, "Cone inside angle.");
  253. addFieldV("coneOutsideAngle", TypeRangedS32, Offset(mProfileDesc.mConeOutsideAngle, SoundAsset), &CommonValidators::S32_PosDegreeRange, "Cone outside angle.");
  254. addFieldV("coneOutsideVolume", TypeRangedF32, Offset(mProfileDesc.mConeOutsideVolume, SoundAsset), &CommonValidators::NormalizedFloat, "Cone outside volume.");
  255. addFieldV("rolloffFactor", TypeRangedF32, Offset(mProfileDesc.mRolloffFactor, SoundAsset), &CommonValidators::NegDefaultF32, "Rolloff factor.");
  256. addField("scatterDistance", TypePoint3F, Offset(mProfileDesc.mScatterDistance, SoundAsset), "Randomization to the spacial position of the sound.");
  257. endGroup("3D");
  258. addGroup("Playlist settings");
  259. addField("random", TYPEID< SFXPlayList::ERandomMode >(), Offset(mPlaylist.mRandomMode, SoundAsset), "Slot playback order randomization pattern.");
  260. addField("loopMode", TYPEID< SFXPlayList::ELoopMode >(), Offset(mPlaylist.mLoopMode, SoundAsset), "Behavior when description has looping enabled.");
  261. addFieldV("numSlotsToPlay", TypeRangedS32, Offset(mPlaylist.mNumSlotsToPlay, SoundAsset), &playlistSlotRange, "Number of slots to play.");
  262. addField("trace", TypeBool, Offset(mPlaylist.mTrace, SoundAsset), "Enable/disable execution tracing for this playlist (local only).");
  263. endGroup("Playlist settings");
  264. }
  265. //------------------------------------------------------------------------------
  266. void SoundAsset::copyTo(SimObject* object)
  267. {
  268. // Call to parent.
  269. Parent::copyTo(object);
  270. }
  271. void SoundAsset::initializeAsset(void)
  272. {
  273. Parent::initializeAsset();
  274. for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
  275. {
  276. if (i == 0 && mSoundFile[i] == StringTable->EmptyString())
  277. return;
  278. if (mSoundFile[i] == StringTable->EmptyString())
  279. break;
  280. mSoundPath[i] = getOwned() ? expandAssetFilePath(mSoundFile[i]) : mSoundPath[i];
  281. if (!Torque::FS::IsFile(mSoundPath[i]))
  282. Con::errorf("SoundAsset::initializeAsset (%s)[%d] could not find %s!", getAssetName(), i, mSoundPath[i]);
  283. }
  284. }
  285. void SoundAsset::_onResourceChanged(const Torque::Path &path)
  286. {
  287. for (U32 i = 0; i < SFXPlayList::NUM_SLOTS; i++)
  288. {
  289. if (path != Torque::Path(mSoundPath[i]))
  290. return;
  291. }
  292. refreshAsset();
  293. }
  294. void SoundAsset::onAssetRefresh(void)
  295. {
  296. for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
  297. {
  298. if (i == 0 && mSoundFile[i] == StringTable->EmptyString())
  299. return;
  300. if (mSoundFile[i] == StringTable->EmptyString())
  301. break;
  302. mSoundPath[i] = getOwned() ? expandAssetFilePath(mSoundFile[i]) : mSoundPath[i];
  303. }
  304. }
  305. U32 SoundAsset::load()
  306. {
  307. if (mLoadedState == AssetErrCode::Ok) return mLoadedState;
  308. // find out how many active slots we have.
  309. U32 numSlots = 0;
  310. for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
  311. {
  312. if (i == 0 && mSoundPath[i] == StringTable->EmptyString())
  313. return false;
  314. if (mSoundPath[i] == StringTable->EmptyString())
  315. break;
  316. numSlots++;
  317. }
  318. if (numSlots > 1)
  319. {
  320. mIsPlaylist = true;
  321. for (U32 i = 0; i < numSlots; i++)
  322. {
  323. if (mSoundPath[i])
  324. {
  325. if (!Torque::FS::IsFile(mSoundPath[i]))
  326. {
  327. Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[i]);
  328. mLoadedState = BadFileReference;
  329. mSFXProfile[i].setDescription(NULL);
  330. mSFXProfile[i].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
  331. mSFXProfile[i].setPreload(false);
  332. return mLoadedState;
  333. }
  334. else
  335. {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
  336. if (mProfileDesc.mSourceGroup == NULL)
  337. mProfileDesc.mSourceGroup = dynamic_cast<SFXSource*>(Sim::findObject("AudioChannelMaster"));
  338. SFXProfile* trackProfile = new SFXProfile();
  339. trackProfile->setDescription(&mProfileDesc);
  340. trackProfile->setSoundFileName(mSoundPath[i]);
  341. trackProfile->setPreload(mPreload);
  342. trackProfile->getBuffer();
  343. mSFXProfile[i] = *trackProfile;
  344. mPlaylist.mSlots.mTrack[i] = trackProfile;
  345. }
  346. }
  347. }
  348. mPlaylist.setDescription(&mProfileDesc);
  349. }
  350. else
  351. {
  352. if (mSoundPath[0])
  353. {
  354. if (!Torque::FS::IsFile(mSoundPath[0]))
  355. {
  356. Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[0]);
  357. mLoadedState = BadFileReference;
  358. mSFXProfile[0].setDescription(NULL);
  359. mSFXProfile[0].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
  360. mSFXProfile[0].setPreload(false);
  361. return mLoadedState;
  362. }
  363. else
  364. {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
  365. if (mProfileDesc.mSourceGroup == NULL)
  366. mProfileDesc.mSourceGroup = dynamic_cast<SFXSource*>(Sim::findObject("AudioChannelMaster"));
  367. mSFXProfile[0].setDescription(&mProfileDesc);
  368. mSFXProfile[0].setSoundFileName(mSoundPath[0]);
  369. mSFXProfile[0].setPreload(mPreload);
  370. //give it a nudge to preload if required
  371. mSFXProfile[0].getBuffer();
  372. }
  373. }
  374. }
  375. mChangeSignal.trigger();
  376. mLoadedState = Ok;
  377. return mLoadedState;
  378. }
  379. bool SoundAsset::_setSoundFile(void* object, const char* index, const char* data)
  380. {
  381. SoundAsset* pData = static_cast<SoundAsset*>(object);
  382. U32 id = 0;
  383. if (index)
  384. id = dAtoui(index);
  385. // Update.
  386. pData->mSoundFile[id] = StringTable->insert(data, true);
  387. if (pData->mSoundFile[id] == StringTable->EmptyString())
  388. pData->mSoundPath[id] = StringTable->EmptyString();
  389. // Refresh the asset.
  390. pData->refreshAsset();
  391. return true;
  392. }
  393. StringTableEntry SoundAsset::getAssetIdByFileName(StringTableEntry fileName)
  394. {
  395. if (fileName == StringTable->EmptyString())
  396. return StringTable->EmptyString();
  397. StringTableEntry soundAssetId = StringTable->EmptyString();
  398. AssetQuery query;
  399. U32 foundCount = AssetDatabase.findAssetType(&query, "SoundAsset");
  400. if (foundCount != 0)
  401. {
  402. for (U32 i = 0; i < foundCount && soundAssetId == StringTable->EmptyString(); i++)
  403. {
  404. SoundAsset* soundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  405. if (soundAsset)
  406. {
  407. if (soundAsset->getSoundPath() == fileName)
  408. soundAssetId = soundAsset->getAssetId();
  409. AssetDatabase.releaseAsset(query.mAssetList[i]);
  410. }
  411. }
  412. }
  413. return soundAssetId;
  414. }
  415. U32 SoundAsset::getAssetById(StringTableEntry assetId, AssetPtr<SoundAsset>* soundAsset)
  416. {
  417. (*soundAsset) = assetId;
  418. if (soundAsset->notNull())
  419. {
  420. return (*soundAsset)->mLoadedState;
  421. }
  422. else
  423. {
  424. //Well that's bad, loading the fallback failed.
  425. Con::warnf("SoundAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  426. return AssetErrCode::Failed;
  427. }
  428. }
  429. U32 SoundAsset::getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsset>* soundAsset)
  430. {
  431. AssetQuery query;
  432. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "SoundAsset");
  433. if (foundAssetcount == 0)
  434. {
  435. //Well that's bad, loading the fallback failed.
  436. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with filename %s failed with no fallback asset", fileName);
  437. return AssetErrCode::Failed;
  438. }
  439. else
  440. {
  441. for (U32 i = 0; i < foundAssetcount; i++)
  442. {
  443. SoundAsset* tSoundAsset = AssetDatabase.acquireAsset<SoundAsset>(query.mAssetList[i]);
  444. if (tSoundAsset && tSoundAsset->getSoundPath() == fileName)
  445. {
  446. soundAsset->setAssetId(query.mAssetList[i]);
  447. AssetDatabase.releaseAsset(query.mAssetList[i]);
  448. return (*soundAsset)->mLoadedState;
  449. }
  450. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  451. }
  452. }
  453. //No good match
  454. return AssetErrCode::Failed;
  455. }
  456. DefineEngineMethod(SoundAsset, getSoundPath, const char*, (), , "")
  457. {
  458. return object->getSoundPath();
  459. }
  460. DefineEngineMethod(SoundAsset, playSound, S32, (Point3F position), (Point3F::Zero),
  461. "Plays the sound for this asset.\n"
  462. "@return (sound plays).\n")
  463. {
  464. if (object->getSFXTrack())
  465. {
  466. MatrixF transform;
  467. transform.setPosition(position);
  468. SFXSource* source;
  469. if (position == Point3F::Zero || !object->is3D())
  470. source = SFX->playOnce(object->getSFXTrack());
  471. else
  472. source = SFX->playOnce(object->getSFXTrack(), &transform, NULL, -1);
  473. if(source)
  474. return source->getId();
  475. else
  476. return 0;
  477. }
  478. else
  479. return 0;
  480. }
  481. #ifdef TORQUE_TOOLS
  482. DefineEngineStaticMethod(SoundAsset, getAssetIdByFilename, const char*, (const char* filePath), (""),
  483. "Queries the Asset Database to see if any asset exists that is associated with the provided file path.\n"
  484. "@return The AssetId of the associated asset, if any.")
  485. {
  486. return SoundAsset::getAssetIdByFileName(StringTable->insert(filePath));
  487. }
  488. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetPtr);
  489. ConsoleDocClass(GuiInspectorTypeSoundAssetPtr,
  490. "@brief Inspector field type for Sounds\n\n"
  491. "Editor use only.\n\n"
  492. "@internal"
  493. );
  494. void GuiInspectorTypeSoundAssetPtr::consoleInit()
  495. {
  496. Parent::consoleInit();
  497. ConsoleBaseType::getType(TypeSoundAssetPtr)->setInspectorFieldType("GuiInspectorTypeSoundAssetPtr");
  498. }
  499. GuiControl * GuiInspectorTypeSoundAssetPtr::constructEditControl()
  500. {
  501. // Create base filename edit controls
  502. GuiControl* retCtrl = Parent::constructEditControl();
  503. if (retCtrl == NULL)
  504. return retCtrl;
  505. // Change filespec
  506. char szBuffer[512];
  507. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"SoundAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  508. getIdString());
  509. mBrowseButton->setField("Command", szBuffer);
  510. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  511. // Create "Open in Editor" button
  512. mEditButton = new GuiBitmapButtonCtrl();
  513. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  514. mEditButton->setField("Command", szBuffer);
  515. char bitmapName[512] = "ToolsModule:SFXEmitter_image";
  516. mEditButton->setBitmap(StringTable->insert(bitmapName));
  517. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  518. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  519. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  520. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Test play this sound");
  521. mEditButton->registerObject();
  522. addObject(mEditButton);
  523. return retCtrl;
  524. }
  525. bool GuiInspectorTypeSoundAssetPtr::updateRects()
  526. {
  527. S32 dividerPos, dividerMargin;
  528. mInspector->getDivider(dividerPos, dividerMargin);
  529. Point2I fieldExtent = getExtent();
  530. Point2I fieldPos = getPosition();
  531. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  532. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  533. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  534. if (mBrowseButton != NULL)
  535. {
  536. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  537. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  538. }
  539. if (mEditButton != NULL)
  540. {
  541. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  542. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  543. }
  544. return resized;
  545. }
  546. IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetId);
  547. ConsoleDocClass(GuiInspectorTypeSoundAssetId,
  548. "@brief Inspector field type for Sounds\n\n"
  549. "Editor use only.\n\n"
  550. "@internal"
  551. );
  552. void GuiInspectorTypeSoundAssetId::consoleInit()
  553. {
  554. Parent::consoleInit();
  555. ConsoleBaseType::getType(TypeSoundAssetId)->setInspectorFieldType("GuiInspectorTypeSoundAssetId");
  556. }
  557. #endif