sfxProfile.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifndef _SFXPROFILE_H_
  23. #define _SFXPROFILE_H_
  24. #ifndef _SFXTRACK_H_
  25. #include "sfx/sfxTrack.h"
  26. #endif
  27. #ifndef _SFXRESOURCE_H_
  28. #include "sfx/sfxResource.h"
  29. #endif
  30. #ifndef _SFXBUFFER_H_
  31. #include "sfx/sfxBuffer.h"
  32. #endif
  33. #ifndef _SFXSYSTEM_H_
  34. #include "sfx/sfxSystem.h"
  35. #endif
  36. #ifndef _TSIGNAL_H_
  37. #include "core/util/tSignal.h"
  38. #endif
  39. class SFXDescription;
  40. /// The SFXProfile is used to define a sound for playback.
  41. ///
  42. /// An SFXProfile will first try to load its file directly through the SFXDevice.
  43. /// Only if this fails (which is the case for most SFXDevices as these do not
  44. /// implement their own custom sound format loading), the file is loaded through
  45. /// SFXResource.
  46. ///
  47. /// A few tips:
  48. ///
  49. /// Make sure each of the defined SFXProfile's fileName doesn't specify
  50. /// an extension. An extension does not need to be specified and by not
  51. /// explicitly saying .ogg or .wav it will allow you to change from one
  52. /// format to the other without having to change the scripts.
  53. ///
  54. /// Make sure that server SFXProfiles are defined with the datablock
  55. /// keyword, and that client SFXProfiles are defined with the 'new'
  56. /// keyword.
  57. ///
  58. /// Make sure SFXDescriptions exist for your SFXProfiles. Also make sure
  59. /// that SFXDescriptions are defined BEFORE SFXProfiles. This is especially
  60. /// important if your SFXProfiles are located in different files than your
  61. /// SFXDescriptions. In this case, make sure the files containing SFXDescriptions
  62. /// are exec'd before the files containing the SFXProfiles.
  63. ///
  64. /// @note Live asset update will not work with files loaded directly through
  65. /// the SFXDevice.
  66. class SFXProfile : public SFXTrack
  67. {
  68. public:
  69. friend class SFXEmitter; // For access to mFilename
  70. typedef SFXTrack Parent;
  71. typedef Signal< void( SFXProfile* ) > ChangedSignal;
  72. protected:
  73. /// The sound data.
  74. /// @note ATM only valid if loaded through SFX's loading system rather than
  75. /// through the SFXDevice's loading system.
  76. Resource< SFXResource > mResource;
  77. /// The sound filename. If no extension is specified
  78. /// the system will try .wav first then other formats.
  79. String mFilename;
  80. /// If true the sound data will be loaded from
  81. /// disk and possibly cached with the active
  82. /// device before the first call for playback.
  83. bool mPreload;
  84. /// The device specific data buffer.
  85. /// This is only used if for non-streaming sounds.
  86. StrongWeakRefPtr< SFXBuffer > mBuffer;
  87. ///
  88. ChangedSignal mChangedSignal;
  89. /// Called when the buffer needs to be preloaded.
  90. bool _preloadBuffer();
  91. /// Callback for device events.
  92. void _onDeviceEvent( SFXSystemEventType evt );
  93. ///
  94. SFXBuffer* _createBuffer();
  95. ///
  96. void _onResourceChanged( const Torque::Path& path );
  97. ///
  98. void _registerSignals();
  99. ///
  100. void _unregisterSignals();
  101. public:
  102. /// This is only here to allow DECLARE_CONOBJECT
  103. /// to create us from script. You shouldn't use
  104. /// this constructor from C++.
  105. explicit SFXProfile();
  106. /// The constructor.
  107. SFXProfile( SFXDescription* desc,
  108. const String& filename = String(),
  109. bool preload = false );
  110. /// The destructor.
  111. virtual ~SFXProfile();
  112. DECLARE_CONOBJECT( SFXProfile );
  113. static void initPersistFields();
  114. // SFXTrack.
  115. virtual bool isLooping() const;
  116. // SimObject
  117. bool onAdd();
  118. void onRemove();
  119. void packData( BitStream* stream );
  120. void unpackData( BitStream* stream );
  121. /// Returns the sound filename.
  122. const String& getSoundFileName() const { return mFilename; }
  123. /// @note This has nothing to do with mPreload.
  124. /// @see SimDataBlock::preload
  125. bool preload( bool server, String &errorStr );
  126. /// Returns the sound resource loading it from
  127. /// disk if it hasn't been preloaded.
  128. ///
  129. /// @note May be NULL if file is loaded directly through SFXDevice.
  130. Resource<SFXResource>& getResource();
  131. /// Returns the device specific buffer for this for this
  132. /// sound. If it hasn't been preloaded it will be loaded
  133. /// at this time.
  134. ///
  135. /// If this is a streaming profile then the buffer
  136. /// returned must be deleted by the caller.
  137. SFXBuffer* getBuffer();
  138. /// Gets the sound duration in milliseconds or
  139. /// returns 0 if the resource was not found.
  140. U32 getSoundDuration();
  141. ///
  142. ChangedSignal& getChangedSignal() { return mChangedSignal; }
  143. };
  144. #endif // _SFXPROFILE_H_