sfxProfile.cpp 15 KB

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