sfxProfile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "platform/platform.h"
  23. #include "sfx/sfxProfile.h"
  24. #include "sfx/sfxDescription.h"
  25. #include "sfx/sfxSystem.h"
  26. #include "sfx/sfxStream.h"
  27. #include "sim/netConnection.h"
  28. #include "core/stream/bitStream.h"
  29. #include "core/resourceManager.h"
  30. #include "console/engineAPI.h"
  31. IMPLEMENT_CO_DATABLOCK_V1( SFXProfile );
  32. ConsoleDocClass( SFXProfile,
  33. "@brief Encapsulates a single sound file for playback by the sound system.\n\n"
  34. "SFXProfile combines a sound description (SFXDescription) with a sound file such that it can be played "
  35. "by the sound system. To be able to play a sound file, the sound system will always require a profile "
  36. "for it to be created. However, several of the SFX functions (sfxPlayOnce(), sfxCreateSource()) perform "
  37. "this creation internally for convenience using temporary profile objects.\n\n"
  38. "Sound files can be in either OGG or WAV format. However, extended format support is available when using FMOD. "
  39. "See @ref SFX_formats.\n\n"
  40. "@section SFXProfile_loading Profile Loading\n\n"
  41. "By default, the sound data referenced by a profile will be loaded when the profile is first played and the "
  42. "data then kept until either the profile is deleted or until the sound device on which the sound data is held "
  43. "is deleted.\n\n"
  44. "This initial loading my incur a small delay when the sound is first played. To avoid this, a profile may be "
  45. "expicitly set to load its sound data immediately when the profile is added to the system. This is done by "
  46. "setting the #preload property to true.\n\n"
  47. "@note Sounds using streamed playback (SFXDescription::isStreaming) cannot be preloaded and will thus "
  48. "ignore the #preload flag.\n\n"
  49. "@tsexample\n"
  50. "datablock SFXProfile( Shore01Snd )\n"
  51. "{\n"
  52. " fileName = \"art/sound/Lakeshore_mono_01\";\n"
  53. " description = Shore01Looping3d;\n"
  54. " preload = true;\n"
  55. "};\n"
  56. "@endtsexample\n\n"
  57. "@ingroup SFX\n"
  58. "@ingroup Datablocks\n"
  59. );
  60. //-----------------------------------------------------------------------------
  61. SFXProfile::SFXProfile()
  62. : mPreload( false )
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. SFXProfile::SFXProfile( SFXDescription* desc, const String& filename, bool preload )
  67. : Parent( desc ),
  68. mFilename( filename ),
  69. mPreload( preload )
  70. {
  71. }
  72. //-----------------------------------------------------------------------------
  73. SFXProfile::~SFXProfile()
  74. {
  75. }
  76. //-----------------------------------------------------------------------------
  77. void SFXProfile::initPersistFields()
  78. {
  79. addGroup( "Sound" );
  80. addField( "filename", TypeStringFilename, Offset( mFilename, SFXProfile ),
  81. "%Path to the sound file.\n"
  82. "If the extension is left out, it will be inferred by the sound system. This allows to "
  83. "easily switch the sound format without having to go through the profiles and change the "
  84. "filenames there, too.\n" );
  85. addField( "preload", TypeBool, Offset( mPreload, SFXProfile ),
  86. "Whether to preload sound data when the profile is added to system.\n"
  87. "@note This flag is ignored by streamed sounds.\n\n"
  88. "@ref SFXProfile_loading" );
  89. endGroup( "Sound" );
  90. Parent::initPersistFields();
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool SFXProfile::onAdd()
  94. {
  95. if( !Parent::onAdd() )
  96. return false;
  97. // If we're a streaming profile we don't preload
  98. // or need device events.
  99. if( SFX && !mDescription->mIsStreaming )
  100. {
  101. // If preload is enabled we load the resource
  102. // and device buffer now to avoid a delay on
  103. // first playback.
  104. if( mPreload && !_preloadBuffer() )
  105. Con::errorf( "SFXProfile(%s)::onAdd: The preload failed!", getName() );
  106. }
  107. _registerSignals();
  108. return true;
  109. }
  110. //-----------------------------------------------------------------------------
  111. void SFXProfile::onRemove()
  112. {
  113. _unregisterSignals();
  114. Parent::onRemove();
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool SFXProfile::preload( bool server, String &errorStr )
  118. {
  119. if ( !Parent::preload( server, errorStr ) )
  120. return false;
  121. // TODO: Investigate how NetConnection::filesWereDownloaded()
  122. // effects the system.
  123. // Validate the datablock... has nothing to do with mPreload.
  124. if( !server &&
  125. NetConnection::filesWereDownloaded() &&
  126. ( mFilename.isEmpty() || !SFXResource::exists( mFilename ) ) )
  127. return false;
  128. return true;
  129. }
  130. //-----------------------------------------------------------------------------
  131. void SFXProfile::packData(BitStream* stream)
  132. {
  133. Parent::packData( stream );
  134. char buffer[256];
  135. if ( mFilename.isEmpty() )
  136. buffer[0] = 0;
  137. else
  138. dStrncpy( buffer, mFilename.c_str(), 256 );
  139. stream->writeString( buffer );
  140. stream->writeFlag( mPreload );
  141. }
  142. //-----------------------------------------------------------------------------
  143. void SFXProfile::unpackData(BitStream* stream)
  144. {
  145. Parent::unpackData( stream );
  146. char buffer[256];
  147. stream->readString( buffer );
  148. mFilename = buffer;
  149. mPreload = stream->readFlag();
  150. }
  151. //-----------------------------------------------------------------------------
  152. bool SFXProfile::isLooping() const
  153. {
  154. return getDescription()->mIsLooping;
  155. }
  156. //-----------------------------------------------------------------------------
  157. void SFXProfile::_registerSignals()
  158. {
  159. SFX->getEventSignal().notify( this, &SFXProfile::_onDeviceEvent );
  160. ResourceManager::get().getChangedSignal().notify( this, &SFXProfile::_onResourceChanged );
  161. }
  162. //-----------------------------------------------------------------------------
  163. void SFXProfile::_unregisterSignals()
  164. {
  165. ResourceManager::get().getChangedSignal().remove( this, &SFXProfile::_onResourceChanged );
  166. if( SFX )
  167. SFX->getEventSignal().remove( this, &SFXProfile::_onDeviceEvent );
  168. }
  169. //-----------------------------------------------------------------------------
  170. void SFXProfile::_onDeviceEvent( SFXSystemEventType evt )
  171. {
  172. switch( evt )
  173. {
  174. case SFXSystemEvent_CreateDevice:
  175. {
  176. if( mPreload && !mDescription->mIsStreaming && !_preloadBuffer() )
  177. Con::errorf( "SFXProfile::_onDeviceEvent: The preload failed! %s", getName() );
  178. break;
  179. }
  180. default:
  181. break;
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. void SFXProfile::_onResourceChanged( const Torque::Path& path )
  186. {
  187. if( path != Path( mFilename ) )
  188. return;
  189. // Let go of the old resource and buffer.
  190. mResource = NULL;
  191. mBuffer = NULL;
  192. // Load the new resource.
  193. getResource();
  194. if( mPreload && !mDescription->mIsStreaming )
  195. {
  196. if( !_preloadBuffer() )
  197. Con::errorf( "SFXProfile::_onResourceChanged() - failed to preload '%s'", mFilename.c_str() );
  198. }
  199. mChangedSignal.trigger( this );
  200. }
  201. //-----------------------------------------------------------------------------
  202. bool SFXProfile::_preloadBuffer()
  203. {
  204. AssertFatal( !mDescription->mIsStreaming, "SFXProfile::_preloadBuffer() - must not be called for streaming profiles" );
  205. mBuffer = _createBuffer();
  206. return ( !mBuffer.isNull() );
  207. }
  208. //-----------------------------------------------------------------------------
  209. Resource<SFXResource>& SFXProfile::getResource()
  210. {
  211. if( !mResource && !mFilename.isEmpty() )
  212. mResource = SFXResource::load( mFilename );
  213. return mResource;
  214. }
  215. //-----------------------------------------------------------------------------
  216. SFXBuffer* SFXProfile::getBuffer()
  217. {
  218. if ( mDescription->mIsStreaming )
  219. {
  220. // Streaming requires unique buffers per
  221. // source, so this creates a new buffer.
  222. if ( SFX )
  223. return _createBuffer();
  224. return NULL;
  225. }
  226. if ( mBuffer.isNull() )
  227. _preloadBuffer();
  228. return mBuffer;
  229. }
  230. //-----------------------------------------------------------------------------
  231. SFXBuffer* SFXProfile::_createBuffer()
  232. {
  233. SFXBuffer* buffer = 0;
  234. // Try to create through SFXDevie.
  235. if( !mFilename.isEmpty() && SFX )
  236. {
  237. buffer = SFX->_createBuffer( mFilename, mDescription );
  238. if( buffer )
  239. {
  240. #ifdef TORQUE_DEBUG
  241. const SFXFormat& format = buffer->getFormat();
  242. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  243. mDescription->mIsStreaming ? "Streaming" : "Loaded", mFilename.c_str(),
  244. format.getChannels(),
  245. format.getSamplesPerSecond() / 1000,
  246. F32( buffer->getDuration() ) / 1000.0f,
  247. format.getDataLength( buffer->getDuration() ) / 1024 );
  248. #endif
  249. }
  250. }
  251. // If that failed, load through SFXResource.
  252. if( !buffer )
  253. {
  254. Resource< SFXResource >& resource = getResource();
  255. if( resource != NULL && SFX )
  256. {
  257. #ifdef TORQUE_DEBUG
  258. const SFXFormat& format = resource->getFormat();
  259. Con::printf( "%s SFX: %s (%i channels, %i kHz, %.02f sec, %i kb)",
  260. mDescription->mIsStreaming ? "Streaming" : "Loading", resource->getFileName().c_str(),
  261. format.getChannels(),
  262. format.getSamplesPerSecond() / 1000,
  263. F32( resource->getDuration() ) / 1000.0f,
  264. format.getDataLength( resource->getDuration() ) / 1024 );
  265. #endif
  266. ThreadSafeRef< SFXStream > sfxStream = resource->openStream();
  267. buffer = SFX->_createBuffer( sfxStream, mDescription );
  268. }
  269. }
  270. return buffer;
  271. }
  272. //-----------------------------------------------------------------------------
  273. U32 SFXProfile::getSoundDuration()
  274. {
  275. Resource< SFXResource >& resource = getResource();
  276. if( resource != NULL )
  277. return mResource->getDuration();
  278. else
  279. return 0;
  280. }
  281. //-----------------------------------------------------------------------------
  282. DefineEngineMethod( SFXProfile, getSoundDuration, F32, (),,
  283. "Return the length of the sound data in seconds.\n\n"
  284. "@return The length of the sound data in seconds or 0 if the sound referenced by the profile could not be found." )
  285. {
  286. return ( F32 ) object->getSoundDuration() * 0.001f;
  287. }