sfxDevice.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 _SFXDEVICE_H_
  23. #define _SFXDEVICE_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _SFXCOMMON_H_
  31. #include "sfx/sfxCommon.h"
  32. #endif
  33. #ifndef _THREADSAFEREF_H_
  34. #include "platform/threads/threadSafeRefCount.h"
  35. #endif
  36. class SFXProvider;
  37. class SFXListener;
  38. class SFXBuffer;
  39. class SFXVoice;
  40. class SFXProfile;
  41. class SFXDevice;
  42. class SFXStream;
  43. class SFXDescription;
  44. /// Abstract base class for back-end sound API implementations.
  45. class SFXDevice
  46. {
  47. public:
  48. typedef void Parent;
  49. /// Device capability flags.
  50. enum ECaps
  51. {
  52. CAPS_Reverb = BIT( 0 ), ///< Device supports reverb environments.
  53. CAPS_VoiceManagement = BIT( 1 ), ///< Device manages voices on its own; deactivates virtualization code in SFX system.
  54. CAPS_Occlusion = BIT( 2 ), ///< Device has its own sound occlusion handling (SFXOcclusionManager).
  55. CAPS_DSPEffects = BIT( 3 ), ///< Device implements DSP effects (SFXDSPManager).
  56. CAPS_MultiListener = BIT( 4 ), ///< Device supports multiple listeners.
  57. CAPS_FMODDesigner = BIT( 5 ), ///< FMOD Designer support.
  58. };
  59. protected:
  60. typedef Vector< SFXBuffer* > BufferVector;
  61. typedef Vector< SFXVoice* > VoiceVector;
  62. typedef BufferVector::iterator BufferIterator;
  63. typedef VoiceVector::iterator VoiceIterator;
  64. SFXDevice( const String& name, SFXProvider* provider, bool useHardware, S32 maxBuffers );
  65. /// The name of this device.
  66. String mName;
  67. /// The provider which created this device.
  68. SFXProvider* mProvider;
  69. /// Should the device try to use hardware processing.
  70. bool mUseHardware;
  71. /// The maximum playback buffers this device will use.
  72. S32 mMaxBuffers;
  73. /// Current set of sound buffers.
  74. BufferVector mBuffers;
  75. /// Current set of voices.
  76. VoiceVector mVoices;
  77. /// Device capabilities.
  78. U32 mCaps;
  79. /// Current number of buffers. Reflected in $SFX::Device::numBuffers.
  80. U32 mStatNumBuffers;
  81. /// Current number of voices. Reflected in $SFX::Device::numVoices.
  82. U32 mStatNumVoices;
  83. /// Current total memory size of sound buffers. Reflected in $SFX::Device::numBufferBytes.
  84. U32 mStatNumBufferBytes;
  85. /// Register a buffer with the device.
  86. /// This also triggers the buffer's stream packet request chain.
  87. void _addBuffer( SFXBuffer* buffer );
  88. /// Unregister the given buffer.
  89. void _removeBuffer( SFXBuffer* buffer );
  90. /// Register a voice with the device.
  91. void _addVoice( SFXVoice* voice );
  92. /// Unregister the given voice.
  93. virtual void _removeVoice( SFXVoice* buffer );
  94. /// Release all resources tied to the device. Can be called repeatedly
  95. /// without harm. It is meant for device destructors that will severe
  96. /// the connection to the sound API and thus need all resources freed
  97. /// before the base destructor is called.
  98. void _releaseAllResources();
  99. public:
  100. virtual ~SFXDevice();
  101. /// Returns the provider which created this device.
  102. SFXProvider* getProvider() const { return mProvider; }
  103. /// Is the device set to use hardware processing.
  104. bool getUseHardware() const { return mUseHardware; }
  105. /// The maximum number of playback buffers this device will use.
  106. S32 getMaxBuffers() const { return mMaxBuffers; }
  107. /// Returns the name of this device.
  108. const String& getName() const { return mName; }
  109. /// Return the device capability flags.
  110. U32 getCaps() const { return mCaps; }
  111. /// Tries to create a new sound buffer. If creation fails
  112. /// freeing another buffer will usually allow a new one to
  113. /// be created.
  114. ///
  115. /// @param stream The sound data stream.
  116. /// @param description The playback configuration.
  117. ///
  118. /// @return Returns a new buffer or NULL if one cannot be created.
  119. ///
  120. virtual SFXBuffer* createBuffer( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description ) = 0;
  121. /// Create a sound buffer directly for a file. This is for
  122. /// devices that implemented their own custom file loading.
  123. ///
  124. /// @note Only implemented on specific SFXDevices.
  125. /// @return Return a new buffer or NULL.
  126. virtual SFXBuffer* createBuffer( const String& fileName, SFXDescription* description ) { return NULL; }
  127. /// Tries to create a new voice.
  128. ///
  129. /// @param is3d True if the voice should have 3D sound enabled.
  130. /// @param buffer The sound data to play by the voice.
  131. ///
  132. /// @return Returns a new voice or NULL if one cannot be created.
  133. virtual SFXVoice* createVoice( bool is3D, SFXBuffer* buffer ) = 0;
  134. /// Set the rolloff curve to be used by distance attenuation of 3D sounds.
  135. virtual void setDistanceModel( SFXDistanceModel model ) {}
  136. /// Set the scale factor to use for doppler effects on 3D sounds.
  137. virtual void setDopplerFactor( F32 factor ) {}
  138. /// Set the rolloff scale factor for distance attenuation of 3D sounds.
  139. virtual void setRolloffFactor( F32 factor ) {}
  140. /// Set the global reverb environment.
  141. virtual void setReverb( const SFXReverbProperties& reverb ) {}
  142. /// Reset the global reverb environment to its default.
  143. virtual void resetReverb() {}
  144. /// Set the number of concurrent listeners on the device.
  145. ///
  146. /// @note On devices that do not support multiple listeners, any value
  147. /// other than 1 will be ignored.
  148. virtual void setNumListeners( U32 num ) {}
  149. /// Set the properties of the given listener.
  150. ///
  151. /// @note On devices that do not support multiple listeners, only setting
  152. /// the properties on index=0 will have a effect.
  153. virtual void setListener( U32 index, const SFXListenerProperties& listener ) {}
  154. /// Return the current total number of sound buffers.
  155. U32 getBufferCount() const { return mBuffers.size(); }
  156. /// Return the current total number of voices.
  157. U32 getVoiceCount() const { return mVoices.size(); }
  158. /// Called from SFXSystem to do any updates the device may need to make.
  159. virtual void update();
  160. };
  161. #endif // _SFXDEVICE_H_