2
0

sfxProfile.cpp 15 KB

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