sfxProfile.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. char buf[1024];
  217. FileName fullFilename = String(Platform::makeFullPathName(mFilename, buf, sizeof(buf)));
  218. if (!mResource && SFXResource::exists(fullFilename))
  219. mResource = SFXResource::load(mFilename);
  220. else
  221. mResource = NULL;
  222. return mResource;
  223. }
  224. //-----------------------------------------------------------------------------
  225. SFXBuffer* SFXProfile::getBuffer()
  226. {
  227. if ( mDescription->mIsStreaming )
  228. {
  229. // Streaming requires unique buffers per
  230. // source, so this creates a new buffer.
  231. if ( SFX )
  232. return _createBuffer();
  233. return NULL;
  234. }
  235. if ( mBuffer.isNull() )
  236. _preloadBuffer();
  237. return mBuffer;
  238. }
  239. //-----------------------------------------------------------------------------
  240. SFXBuffer* SFXProfile::_createBuffer()
  241. {
  242. SFXBuffer* buffer = 0;
  243. // Try to create through SFXDevie.
  244. if( mFilename != StringTable->EmptyString() && SFX )
  245. {
  246. buffer = SFX->_createBuffer( mFilename, mDescription );
  247. if( buffer )
  248. {
  249. #ifdef TORQUE_DEBUG
  250. const SFXFormat& format = buffer->getFormat();
  251. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  252. mDescription->mIsStreaming ? "Streaming" : "Loaded", mFilename,
  253. format.getChannels(),
  254. format.getSamplesPerSecond() / 1000,
  255. F32( buffer->getDuration() ) / 1000.0f,
  256. format.getDataLength( buffer->getDuration() ) / 1024 );
  257. #endif
  258. }
  259. }
  260. // If that failed, load through SFXResource.
  261. if( !buffer )
  262. {
  263. Resource< SFXResource >& resource = getResource();
  264. if( resource != NULL && SFX )
  265. {
  266. #ifdef TORQUE_DEBUG
  267. const SFXFormat& format = resource->getFormat();
  268. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  269. mDescription->mIsStreaming ? "Streaming" : "Loading", resource->getFileName().c_str(),
  270. format.getChannels(),
  271. format.getSamplesPerSecond() / 1000,
  272. F32( resource->getDuration() ) / 1000.0f,
  273. format.getDataLength( resource->getDuration() ) / 1024 );
  274. #endif
  275. ThreadSafeRef< SFXStream > sfxStream = resource->openStream();
  276. buffer = SFX->_createBuffer( sfxStream, mDescription );
  277. }
  278. }
  279. return buffer;
  280. }
  281. //-----------------------------------------------------------------------------
  282. U32 SFXProfile::getSoundDuration()
  283. {
  284. Resource< SFXResource >& resource = getResource();
  285. if( resource != NULL )
  286. return mResource->getDuration();
  287. else
  288. return 0;
  289. }
  290. //-----------------------------------------------------------------------------
  291. DefineEngineMethod( SFXProfile, getSoundDuration, F32, (),,
  292. "Return the length of the sound data in seconds.\n\n"
  293. "@return The length of the sound data in seconds or 0 if the sound referenced by the profile could not be found." )
  294. {
  295. return ( F32 ) object->getSoundDuration() * 0.001f;
  296. }
  297. // enable this to help verify that temp-clones of AudioProfile are being deleted
  298. //#define TRACK_AUDIO_PROFILE_CLONES
  299. #ifdef TRACK_AUDIO_PROFILE_CLONES
  300. static int audio_prof_clones = 0;
  301. #endif
  302. SFXProfile::SFXProfile(const SFXProfile& other, bool temp_clone) : SFXTrack(other, temp_clone)
  303. {
  304. #ifdef TRACK_AUDIO_PROFILE_CLONES
  305. audio_prof_clones++;
  306. if (audio_prof_clones == 1)
  307. Con::errorf("SFXProfile -- Clones are on the loose!");
  308. #endif
  309. mResource = other.mResource;
  310. mFilename = other.mFilename;
  311. mPreload = other.mPreload;
  312. mBuffer = other.mBuffer; // -- AudioBuffer loaded using mFilename
  313. mChangedSignal = other.mChangedSignal;
  314. }
  315. SFXProfile::~SFXProfile()
  316. {
  317. if (!isTempClone())
  318. return;
  319. // cleanup after a temp-clone
  320. if (mDescription && mDescription->isTempClone())
  321. {
  322. delete mDescription;
  323. mDescription = 0;
  324. }
  325. #ifdef TRACK_AUDIO_PROFILE_CLONES
  326. if (audio_prof_clones > 0)
  327. {
  328. audio_prof_clones--;
  329. if (audio_prof_clones == 0)
  330. Con::errorf("SFXProfile -- Clones eliminated!");
  331. }
  332. else
  333. Con::errorf("SFXProfile -- Too many clones deleted!");
  334. #endif
  335. }
  336. // Clone and perform substitutions on the SFXProfile and on any SFXDescription
  337. // it references.
  338. SFXProfile* SFXProfile::cloneAndPerformSubstitutions(const SimObject* owner, S32 index)
  339. {
  340. if (!owner)
  341. return this;
  342. SFXProfile* sub_profile_db = this;
  343. // look for mDescriptionObject subs
  344. SFXDescription* desc_db;
  345. if (mDescription && mDescription->getSubstitutionCount() > 0)
  346. {
  347. SFXDescription* orig_db = mDescription;
  348. desc_db = new SFXDescription(*orig_db, true);
  349. orig_db->performSubstitutions(desc_db, owner, index);
  350. }
  351. else
  352. desc_db = 0;
  353. if (this->getSubstitutionCount() > 0 || desc_db)
  354. {
  355. sub_profile_db = new SFXProfile(*this, true);
  356. performSubstitutions(sub_profile_db, owner, index);
  357. if (desc_db)
  358. sub_profile_db->mDescription = desc_db;
  359. }
  360. return sub_profile_db;
  361. }
  362. void SFXProfile::onPerformSubstitutions()
  363. {
  364. if ( SFX )
  365. {
  366. // If preload is enabled we load the resource
  367. // and device buffer now to avoid a delay on
  368. // first playback.
  369. if ( mPreload && !_preloadBuffer() )
  370. Con::errorf( "SFXProfile(%s)::onPerformSubstitutions: The preload failed!", getName() );
  371. // We need to get device change notifications.
  372. SFX->getEventSignal().notify( this, &SFXProfile::_onDeviceEvent );
  373. }
  374. }
  375. // This allows legacy AudioProfile datablocks to be recognized as an alias
  376. // for SFXProfile. It is intended to ease the transition from older scripts
  377. // especially those that still need to support pre-1.7 applications.
  378. // (This maybe removed in future releases so treat as deprecated.)
  379. class AudioProfile : public SFXProfile
  380. {
  381. typedef SFXProfile Parent;
  382. public:
  383. DECLARE_CONOBJECT(AudioProfile);
  384. };
  385. IMPLEMENT_CO_DATABLOCK_V1(AudioProfile);
  386. ConsoleDocClass( AudioProfile,
  387. "@brief Allows legacy AudioProfile datablocks to be treated as SFXProfile datablocks.\n\n"
  388. "@ingroup afxMisc\n"
  389. "@ingroup AFX\n"
  390. "@ingroup Datablocks\n"
  391. );