sfxProfile.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "sfx/sfxProfile.h"
  28. #include "sfx/sfxDescription.h"
  29. #include "sfx/sfxSystem.h"
  30. #include "sfx/sfxStream.h"
  31. #include "sim/netConnection.h"
  32. #include "core/stream/bitStream.h"
  33. #include "core/resourceManager.h"
  34. #include "console/engineAPI.h"
  35. using namespace Torque;
  36. IMPLEMENT_CO_DATABLOCK_V1( SFXProfile );
  37. ConsoleDocClass( SFXProfile,
  38. "@brief Encapsulates a single sound file for playback by the sound system.\n\n"
  39. "SFXProfile combines a sound description (SFXDescription) with a sound file such that it can be played "
  40. "by the sound system. To be able to play a sound file, the sound system will always require a profile "
  41. "for it to be created. However, several of the SFX functions (sfxPlayOnce(), sfxCreateSource()) perform "
  42. "this creation internally for convenience using temporary profile objects.\n\n"
  43. "Sound files can be in either OGG or WAV format. However, extended format support is available when using FMOD. "
  44. "See @ref SFX_formats.\n\n"
  45. "@section SFXProfile_loading Profile Loading\n\n"
  46. "By default, the sound data referenced by a profile will be loaded when the profile is first played and the "
  47. "data then kept until either the profile is deleted or until the sound device on which the sound data is held "
  48. "is deleted.\n\n"
  49. "This initial loading my incur a small delay when the sound is first played. To avoid this, a profile may be "
  50. "expicitly set to load its sound data immediately when the profile is added to the system. This is done by "
  51. "setting the #preload property to true.\n\n"
  52. "@note Sounds using streamed playback (SFXDescription::isStreaming) cannot be preloaded and will thus "
  53. "ignore the #preload flag.\n\n"
  54. "@tsexample\n"
  55. "datablock SFXProfile( Shore01Snd )\n"
  56. "{\n"
  57. " fileName = \"art/sound/Lakeshore_mono_01\";\n"
  58. " description = Shore01Looping3d;\n"
  59. " preload = true;\n"
  60. "};\n"
  61. "@endtsexample\n\n"
  62. "@ingroup SFX\n"
  63. "@ingroup Datablocks\n"
  64. );
  65. //-----------------------------------------------------------------------------
  66. SFXProfile::SFXProfile()
  67. : mPreload( false )
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. SFXProfile::SFXProfile( SFXDescription* desc, const String& filename, bool preload )
  72. : Parent( desc ),
  73. mFilename( filename ),
  74. mPreload( preload )
  75. {
  76. }
  77. //-----------------------------------------------------------------------------
  78. //-----------------------------------------------------------------------------
  79. void SFXProfile::initPersistFields()
  80. {
  81. addGroup( "Sound" );
  82. addField( "filename", TypeStringFilename, Offset( mFilename, SFXProfile ),
  83. "%Path to the sound file.\n"
  84. "If the extension is left out, it will be inferred by the sound system. This allows to "
  85. "easily switch the sound format without having to go through the profiles and change the "
  86. "filenames there, too.\n" );
  87. addField( "preload", TypeBool, Offset( mPreload, SFXProfile ),
  88. "Whether to preload sound data when the profile is added to system.\n"
  89. "@note This flag is ignored by streamed sounds.\n\n"
  90. "@ref SFXProfile_loading" );
  91. endGroup( "Sound" );
  92. // disallow some field substitutions
  93. disableFieldSubstitutions("description");
  94. Parent::initPersistFields();
  95. }
  96. //-----------------------------------------------------------------------------
  97. bool SFXProfile::onAdd()
  98. {
  99. if( !Parent::onAdd() )
  100. return false;
  101. // If we're a streaming profile we don't preload
  102. // or need device events.
  103. if( SFX && !mDescription->mIsStreaming )
  104. {
  105. // If preload is enabled we load the resource
  106. // and device buffer now to avoid a delay on
  107. // first playback.
  108. if( mPreload && !_preloadBuffer() )
  109. Con::errorf( "SFXProfile(%s)::onAdd: The preload failed!", getName() );
  110. }
  111. _registerSignals();
  112. return true;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void SFXProfile::onRemove()
  116. {
  117. _unregisterSignals();
  118. Parent::onRemove();
  119. }
  120. //-----------------------------------------------------------------------------
  121. bool SFXProfile::preload( bool server, String &errorStr )
  122. {
  123. if ( !Parent::preload( server, errorStr ) )
  124. return false;
  125. // TODO: Investigate how NetConnection::filesWereDownloaded()
  126. // effects the system.
  127. // Validate the datablock... has nothing to do with mPreload.
  128. if( !server &&
  129. NetConnection::filesWereDownloaded() &&
  130. ( mFilename.isEmpty() || !SFXResource::exists( mFilename ) ) )
  131. return false;
  132. return true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. void SFXProfile::packData(BitStream* stream)
  136. {
  137. Parent::packData( stream );
  138. char buffer[256];
  139. if ( mFilename.isEmpty() )
  140. buffer[0] = 0;
  141. else
  142. dStrncpy( buffer, mFilename.c_str(), 256 );
  143. stream->writeString( buffer );
  144. stream->writeFlag( mPreload );
  145. }
  146. //-----------------------------------------------------------------------------
  147. void SFXProfile::unpackData(BitStream* stream)
  148. {
  149. Parent::unpackData( stream );
  150. char buffer[256];
  151. stream->readString( buffer );
  152. mFilename = buffer;
  153. mPreload = stream->readFlag();
  154. }
  155. //-----------------------------------------------------------------------------
  156. bool SFXProfile::isLooping() const
  157. {
  158. return getDescription()->mIsLooping;
  159. }
  160. //-----------------------------------------------------------------------------
  161. void SFXProfile::_registerSignals()
  162. {
  163. SFX->getEventSignal().notify( this, &SFXProfile::_onDeviceEvent );
  164. ResourceManager::get().getChangedSignal().notify( this, &SFXProfile::_onResourceChanged );
  165. }
  166. //-----------------------------------------------------------------------------
  167. void SFXProfile::_unregisterSignals()
  168. {
  169. ResourceManager::get().getChangedSignal().remove( this, &SFXProfile::_onResourceChanged );
  170. if( SFX )
  171. SFX->getEventSignal().remove( this, &SFXProfile::_onDeviceEvent );
  172. }
  173. //-----------------------------------------------------------------------------
  174. void SFXProfile::_onDeviceEvent( SFXSystemEventType evt )
  175. {
  176. switch( evt )
  177. {
  178. case SFXSystemEvent_CreateDevice:
  179. {
  180. if( mPreload && !mDescription->mIsStreaming && !_preloadBuffer() )
  181. Con::errorf( "SFXProfile::_onDeviceEvent: The preload failed! %s", getName() );
  182. break;
  183. }
  184. default:
  185. break;
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. void SFXProfile::_onResourceChanged( const Torque::Path& path )
  190. {
  191. if( path != Path( mFilename ) )
  192. return;
  193. // Let go of the old resource and buffer.
  194. mResource = NULL;
  195. mBuffer = NULL;
  196. // Load the new resource.
  197. getResource();
  198. if( mPreload && !mDescription->mIsStreaming )
  199. {
  200. if( !_preloadBuffer() )
  201. Con::errorf( "SFXProfile::_onResourceChanged() - failed to preload '%s'", mFilename.c_str() );
  202. }
  203. mChangedSignal.trigger( this );
  204. }
  205. //-----------------------------------------------------------------------------
  206. bool SFXProfile::_preloadBuffer()
  207. {
  208. AssertFatal( !mDescription->mIsStreaming, "SFXProfile::_preloadBuffer() - must not be called for streaming profiles" );
  209. mBuffer = _createBuffer();
  210. return ( !mBuffer.isNull() );
  211. }
  212. //-----------------------------------------------------------------------------
  213. Resource<SFXResource>& SFXProfile::getResource()
  214. {
  215. if( !mResource && !mFilename.isEmpty() )
  216. mResource = SFXResource::load( mFilename );
  217. return mResource;
  218. }
  219. //-----------------------------------------------------------------------------
  220. SFXBuffer* SFXProfile::getBuffer()
  221. {
  222. if ( mDescription->mIsStreaming )
  223. {
  224. // Streaming requires unique buffers per
  225. // source, so this creates a new buffer.
  226. if ( SFX )
  227. return _createBuffer();
  228. return NULL;
  229. }
  230. if ( mBuffer.isNull() )
  231. _preloadBuffer();
  232. return mBuffer;
  233. }
  234. //-----------------------------------------------------------------------------
  235. SFXBuffer* SFXProfile::_createBuffer()
  236. {
  237. SFXBuffer* buffer = 0;
  238. // Try to create through SFXDevie.
  239. if( !mFilename.isEmpty() && SFX )
  240. {
  241. buffer = SFX->_createBuffer( mFilename, mDescription );
  242. if( buffer )
  243. {
  244. #ifdef TORQUE_DEBUG
  245. const SFXFormat& format = buffer->getFormat();
  246. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  247. mDescription->mIsStreaming ? "Streaming" : "Loaded", mFilename.c_str(),
  248. format.getChannels(),
  249. format.getSamplesPerSecond() / 1000,
  250. F32( buffer->getDuration() ) / 1000.0f,
  251. format.getDataLength( buffer->getDuration() ) / 1024 );
  252. #endif
  253. }
  254. }
  255. // If that failed, load through SFXResource.
  256. if( !buffer )
  257. {
  258. Resource< SFXResource >& resource = getResource();
  259. if( resource != NULL && SFX )
  260. {
  261. #ifdef TORQUE_DEBUG
  262. const SFXFormat& format = resource->getFormat();
  263. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  264. mDescription->mIsStreaming ? "Streaming" : "Loading", resource->getFileName().c_str(),
  265. format.getChannels(),
  266. format.getSamplesPerSecond() / 1000,
  267. F32( resource->getDuration() ) / 1000.0f,
  268. format.getDataLength( resource->getDuration() ) / 1024 );
  269. #endif
  270. ThreadSafeRef< SFXStream > sfxStream = resource->openStream();
  271. buffer = SFX->_createBuffer( sfxStream, mDescription );
  272. }
  273. }
  274. return buffer;
  275. }
  276. //-----------------------------------------------------------------------------
  277. U32 SFXProfile::getSoundDuration()
  278. {
  279. Resource< SFXResource >& resource = getResource();
  280. if( resource != NULL )
  281. return mResource->getDuration();
  282. else
  283. return 0;
  284. }
  285. //-----------------------------------------------------------------------------
  286. DefineEngineMethod( SFXProfile, getSoundDuration, F32, (),,
  287. "Return the length of the sound data in seconds.\n\n"
  288. "@return The length of the sound data in seconds or 0 if the sound referenced by the profile could not be found." )
  289. {
  290. return ( F32 ) object->getSoundDuration() * 0.001f;
  291. }
  292. // enable this to help verify that temp-clones of AudioProfile are being deleted
  293. //#define TRACK_AUDIO_PROFILE_CLONES
  294. #ifdef TRACK_AUDIO_PROFILE_CLONES
  295. static int audio_prof_clones = 0;
  296. #endif
  297. SFXProfile::SFXProfile(const SFXProfile& other, bool temp_clone) : SFXTrack(other, temp_clone)
  298. {
  299. #ifdef TRACK_AUDIO_PROFILE_CLONES
  300. audio_prof_clones++;
  301. if (audio_prof_clones == 1)
  302. Con::errorf("SFXProfile -- Clones are on the loose!");
  303. #endif
  304. mResource = other.mResource;
  305. mFilename = other.mFilename;
  306. mPreload = other.mPreload;
  307. mBuffer = other.mBuffer; // -- AudioBuffer loaded using mFilename
  308. mChangedSignal = other.mChangedSignal;
  309. }
  310. SFXProfile::~SFXProfile()
  311. {
  312. if (!isTempClone())
  313. return;
  314. // cleanup after a temp-clone
  315. if (mDescription && mDescription->isTempClone())
  316. {
  317. delete mDescription;
  318. mDescription = 0;
  319. }
  320. #ifdef TRACK_AUDIO_PROFILE_CLONES
  321. if (audio_prof_clones > 0)
  322. {
  323. audio_prof_clones--;
  324. if (audio_prof_clones == 0)
  325. Con::errorf("SFXProfile -- Clones eliminated!");
  326. }
  327. else
  328. Con::errorf("SFXProfile -- Too many clones deleted!");
  329. #endif
  330. }
  331. // Clone and perform substitutions on the SFXProfile and on any SFXDescription
  332. // it references.
  333. SFXProfile* SFXProfile::cloneAndPerformSubstitutions(const SimObject* owner, S32 index)
  334. {
  335. if (!owner)
  336. return this;
  337. SFXProfile* sub_profile_db = this;
  338. // look for mDescriptionObject subs
  339. SFXDescription* desc_db;
  340. if (mDescription && mDescription->getSubstitutionCount() > 0)
  341. {
  342. SFXDescription* orig_db = mDescription;
  343. desc_db = new SFXDescription(*orig_db, true);
  344. orig_db->performSubstitutions(desc_db, owner, index);
  345. }
  346. else
  347. desc_db = 0;
  348. if (this->getSubstitutionCount() > 0 || desc_db)
  349. {
  350. sub_profile_db = new SFXProfile(*this, true);
  351. performSubstitutions(sub_profile_db, owner, index);
  352. if (desc_db)
  353. sub_profile_db->mDescription = desc_db;
  354. }
  355. return sub_profile_db;
  356. }
  357. void SFXProfile::onPerformSubstitutions()
  358. {
  359. if ( SFX )
  360. {
  361. // If preload is enabled we load the resource
  362. // and device buffer now to avoid a delay on
  363. // first playback.
  364. if ( mPreload && !_preloadBuffer() )
  365. Con::errorf( "SFXProfile(%s)::onPerformSubstitutions: The preload failed!", getName() );
  366. // We need to get device change notifications.
  367. SFX->getEventSignal().notify( this, &SFXProfile::_onDeviceEvent );
  368. }
  369. }
  370. // This allows legacy AudioProfile datablocks to be recognized as an alias
  371. // for SFXProfile. It is intended to ease the transition from older scripts
  372. // especially those that still need to support pre-1.7 applications.
  373. // (This maybe removed in future releases so treat as deprecated.)
  374. class AudioProfile : public SFXProfile
  375. {
  376. typedef SFXProfile Parent;
  377. public:
  378. DECLARE_CONOBJECT(AudioProfile);
  379. };
  380. IMPLEMENT_CO_DATABLOCK_V1(AudioProfile);
  381. ConsoleDocClass( AudioProfile,
  382. "@brief Allows legacy AudioProfile datablocks to be treated as SFXProfile datablocks.\n\n"
  383. "@ingroup afxMisc\n"
  384. "@ingroup AFX\n"
  385. "@ingroup Datablocks\n"
  386. );