sfxDevice.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. };
  58. protected:
  59. typedef Vector< SFXBuffer* > BufferVector;
  60. typedef Vector< SFXVoice* > VoiceVector;
  61. typedef BufferVector::iterator BufferIterator;
  62. typedef VoiceVector::iterator VoiceIterator;
  63. SFXDevice( const String& name, SFXProvider* provider, bool useHardware, S32 maxBuffers );
  64. /// The name of this device.
  65. String mName;
  66. /// The provider which created this device.
  67. SFXProvider* mProvider;
  68. /// Should the device try to use hardware processing.
  69. bool mUseHardware;
  70. /// The maximum playback buffers this device will use.
  71. S32 mMaxBuffers;
  72. /// Current set of sound buffers.
  73. BufferVector mBuffers;
  74. /// Current set of voices.
  75. VoiceVector mVoices;
  76. /// Device capabilities.
  77. U32 mCaps;
  78. /// Current number of buffers. Reflected in $SFX::Device::numBuffers.
  79. U32 mStatNumBuffers;
  80. /// Current number of voices. Reflected in $SFX::Device::numVoices.
  81. U32 mStatNumVoices;
  82. /// Current total memory size of sound buffers. Reflected in $SFX::Device::numBufferBytes.
  83. U32 mStatNumBufferBytes;
  84. /// Register a buffer with the device.
  85. /// This also triggers the buffer's stream packet request chain.
  86. void _addBuffer( SFXBuffer* buffer );
  87. /// Unregister the given buffer.
  88. void _removeBuffer( SFXBuffer* buffer );
  89. /// Register a voice with the device.
  90. void _addVoice( SFXVoice* voice );
  91. /// Unregister the given voice.
  92. virtual void _removeVoice( SFXVoice* buffer );
  93. /// Release all resources tied to the device. Can be called repeatedly
  94. /// without harm. It is meant for device destructors that will severe
  95. /// the connection to the sound API and thus need all resources freed
  96. /// before the base destructor is called.
  97. void _releaseAllResources();
  98. public:
  99. virtual ~SFXDevice();
  100. /// Returns the provider which created this device.
  101. SFXProvider* getProvider() const { return mProvider; }
  102. /// Is the device set to use hardware processing.
  103. bool getUseHardware() const { return mUseHardware; }
  104. /// The maximum number of playback buffers this device will use.
  105. S32 getMaxBuffers() const { return mMaxBuffers; }
  106. /// Returns the name of this device.
  107. const String& getName() const { return mName; }
  108. /// Return the device capability flags.
  109. U32 getCaps() const { return mCaps; }
  110. /// Tries to create a new sound buffer. If creation fails
  111. /// freeing another buffer will usually allow a new one to
  112. /// be created.
  113. ///
  114. /// @param stream The sound data stream.
  115. /// @param description The playback configuration.
  116. ///
  117. /// @return Returns a new buffer or NULL if one cannot be created.
  118. ///
  119. virtual SFXBuffer* createBuffer( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description ) = 0;
  120. /// Create a sound buffer directly for a file. This is for
  121. /// devices that implemented their own custom file loading.
  122. ///
  123. /// @note Only implemented on specific SFXDevices.
  124. /// @return Return a new buffer or NULL.
  125. virtual SFXBuffer* createBuffer( const String& fileName, SFXDescription* description ) { return NULL; }
  126. /// Tries to create a new voice.
  127. ///
  128. /// @param is3d True if the voice should have 3D sound enabled.
  129. /// @param buffer The sound data to play by the voice.
  130. ///
  131. /// @return Returns a new voice or NULL if one cannot be created.
  132. virtual SFXVoice* createVoice( bool is3D, SFXBuffer* buffer ) = 0;
  133. /// Set the rolloff curve to be used by distance attenuation of 3D sounds.
  134. virtual void setDistanceModel( SFXDistanceModel model ) {}
  135. /// Set the scale factor to use for doppler effects on 3D sounds.
  136. virtual void setDopplerFactor( F32 factor ) {}
  137. /// Set the rolloff scale factor for distance attenuation of 3D sounds.
  138. virtual void setRolloffFactor( F32 factor ) {}
  139. /// send empty function to all sfxdevices
  140. virtual void openSlots() {}
  141. /// Set the global reverb environment.
  142. virtual void setReverb( const SFXReverbProperties& reverb ) {}
  143. /// Reset the global reverb environment to its default.
  144. virtual void resetReverb() {}
  145. /// Set the number of concurrent listeners on the device.
  146. ///
  147. /// @note On devices that do not support multiple listeners, any value
  148. /// other than 1 will be ignored.
  149. virtual void setNumListeners( U32 num ) {}
  150. /// Set the properties of the given listener.
  151. ///
  152. /// @note On devices that do not support multiple listeners, only setting
  153. /// the properties on index=0 will have a effect.
  154. virtual void setListener( U32 index, const SFXListenerProperties& listener ) {}
  155. /// Return the current total number of sound buffers.
  156. U32 getBufferCount() const { return mBuffers.size(); }
  157. /// Return the current total number of voices.
  158. U32 getVoiceCount() const { return mVoices.size(); }
  159. /// Called from SFXSystem to do any updates the device may need to make.
  160. virtual void update();
  161. };
  162. #endif // _SFXDEVICE_H_