sfxProfile.h 6.6 KB

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