sfxVoice.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 _SFXVOICE_H_
  23. #define _SFXVOICE_H_
  24. #ifndef _REFBASE_H_
  25. #include "core/util/refBase.h"
  26. #endif
  27. #ifndef _TSTREAM_H_
  28. #include "core/stream/tStream.h"
  29. #endif
  30. #ifndef _MPOINT3_H_
  31. #include "math/mPoint3.h"
  32. #endif
  33. #ifndef _MMATRIX_H_
  34. #include "math/mMatrix.h"
  35. #endif
  36. #ifndef _SFXBUFFER_H_
  37. #include "sfx/sfxBuffer.h"
  38. #endif
  39. namespace SFXInternal {
  40. class SFXVoiceTimeSource;
  41. class SFXAynscQueue;
  42. }
  43. /// The voice interface provides for playback of sound buffers and positioning
  44. /// of 3D sounds.
  45. ///
  46. /// This abstract class is derived from in the different device layers to implement
  47. /// device-specific playback control.
  48. ///
  49. /// The primary responsibility of this class is to mediate between the user requests
  50. /// (play(), stop(), pause(), setPosition()), the buffer (which may change state
  51. /// asynchronously), and the underlying device playback control (_play(), _stop(),
  52. /// _pause(), _seek()).
  53. class SFXVoice : public StrongRefBase,
  54. public IPositionable< U32 >
  55. {
  56. public:
  57. typedef void Parent;
  58. friend class SFXDevice; // _attachToBuffer
  59. friend class SFXInternal::SFXVoiceTimeSource; // _tell
  60. friend class SFXInternal::SFXAsyncQueue; // mOffset
  61. protected:
  62. /// Current playback status.
  63. /// @note This is maintained on both the sound update thread as well
  64. /// as the main thread.
  65. mutable volatile SFXStatus mStatus;
  66. /// Sound data played back by the voice.
  67. WeakRefPtr< SFXBuffer > mBuffer;
  68. /// For streaming voices, this keeps track of play start offset
  69. /// after seeking. Expressed in number of samples.
  70. U32 mOffset;
  71. explicit SFXVoice( SFXBuffer* buffer );
  72. /// @name Device Control Methods
  73. /// @{
  74. /// Return the current playback status (playing, paused, or stopped). Default
  75. /// status is stopped.
  76. virtual SFXStatus _status() const = 0;
  77. /// Stop playback on the device.
  78. /// @note Called from both the SFX update thread and the main thread.
  79. virtual void _stop() = 0;
  80. /// Start playback on the device.
  81. /// @note Called from both the SFX update thread and the main thread.
  82. virtual void _play() = 0;
  83. /// Pause playback on the device.
  84. /// @note Called from both the SFX update thread and the main thread.
  85. virtual void _pause() = 0;
  86. /// Set the playback cursor on the device.
  87. /// @note Only used for non-streaming voices.
  88. virtual void _seek( U32 sample ) = 0;
  89. /// Get the playback cursor on the device.
  90. ///
  91. /// When the voice is playing or paused, this method must return a valid sample position.
  92. /// When the voice is stopped, the result of this method is undefined.
  93. ///
  94. /// For streaming voices that are looping, the sample position must be a total count of the
  95. /// number of samples played so far which thus includes the count of all cycles before the
  96. /// current one. For non-looping voices, this behavior is optional.
  97. ///
  98. /// @note This is called for both streaming and non-streaming voices.
  99. virtual U32 _tell() const = 0;
  100. /// @}
  101. /// Hooked up to SFXBuffer::mOnStatusChange of #mBuffer.
  102. /// @note Called on the SFX update thread.
  103. virtual void _onBufferStatusChange( SFXBuffer* buffer, SFXBuffer::Status newStatus );
  104. ///
  105. void _attachToBuffer();
  106. /// @name Streaming
  107. /// The following methods are for streaming voices only.
  108. /// @{
  109. /// Reset streaming of the voice by cloning the current streaming source and
  110. /// letting the resulting stream start from @a sampleStartPos.
  111. void _resetStream( U32 sampleStartPos, bool triggerUpdate = true );
  112. /// @}
  113. public:
  114. static Signal< void( SFXVoice* ) > smVoiceCreatedSignal;
  115. static Signal< void( SFXVoice* ) > smVoiceDestroyedSignal;
  116. /// The destructor.
  117. virtual ~SFXVoice();
  118. ///
  119. const SFXFormat& getFormat() const { return mBuffer->getFormat(); }
  120. /// Return the current playback position (in number of samples).
  121. ///
  122. /// @note For looping sounds, this will return the position in the
  123. /// current cycle and not the total number of samples played so far.
  124. virtual U32 getPosition() const;
  125. /// Sets the playback position to the given sample count.
  126. ///
  127. /// @param sample Offset in number of samples. This is allowed to use an offset
  128. /// accumulated from multiple cycles. Each cycle will wrap around back to the
  129. /// beginning of the buffer.
  130. virtual void setPosition( U32 sample );
  131. /// @return the current playback status.
  132. /// @note For streaming voices, the reaction to for the voice to update its status
  133. /// to SFXStatusStopped after the voice has stopped playing depends on the synchronization
  134. /// of the underlying device. If, for example, the underlying device uses periodic updates
  135. /// and doesn't have notifications, then a delay up to the total length of the period time
  136. /// may occur before the status changes. Note that in-between the actual playback stopping
  137. /// and the voice updating its status, the result of getPosition() is undefined.
  138. virtual SFXStatus getStatus() const;
  139. /// Starts playback from the current position.
  140. virtual void play( bool looping );
  141. /// Stops playback and moves the position to the start.
  142. virtual void stop();
  143. /// Pauses playback.
  144. virtual void pause();
  145. /// Sets the position and orientation for a 3d voice.
  146. virtual void setTransform( const MatrixF &transform ) = 0;
  147. /// Sets the velocity for a 3d voice.
  148. virtual void setVelocity( const VectorF &velocity ) = 0;
  149. /// Sets the minimum and maximum distances for 3d falloff.
  150. virtual void setMinMaxDistance( F32 min, F32 max ) = 0;
  151. /// Set the distance attenuation rolloff factor. Support by device optional.
  152. virtual void setRolloffFactor( F32 factor ) {}
  153. /// Sets the volume.
  154. virtual void setVolume( F32 volume ) = 0;
  155. /// Sets the pitch scale.
  156. virtual void setPitch( F32 pitch ) = 0;
  157. /// Set sound cone of a 3D sound.
  158. ///
  159. /// @param innerAngle Inner cone angle in degrees.
  160. /// @param outerAngle Outer cone angle in degrees.
  161. /// @param outerVolume Outer volume factor.
  162. virtual void setCone( F32 innerAngle,
  163. F32 outerAngle,
  164. F32 outerVolume ) = 0;
  165. /// Set the reverb properties for playback of this sound.
  166. /// @note Has no effect on devices that do not support reverb.
  167. virtual void setReverb( const SFXSoundReverbProperties& reverb ) {}
  168. /// Set the priority of this voice. Default 1.0.
  169. /// @note Has no effect on devices that do not support voice management.
  170. virtual void setPriority( F32 priority ) {}
  171. /// Returns true if the voice is virtualized on the device.
  172. /// @note Always false on devices that do not support voice management.
  173. virtual bool isVirtual() const { return false; }
  174. };
  175. #endif // _SFXVOICE_H_