sfxSound.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 _SFXSOUND_H_
  23. #define _SFXSOUND_H_
  24. #ifndef _SFXSOURCE_H_
  25. #include "sfx/sfxSource.h"
  26. #endif
  27. #ifndef _SFXVOICE_H_
  28. #include "sfx/sfxVoice.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "console/simBase.h"
  32. #endif
  33. #ifndef _MPOINT3_H_
  34. #include "math/mPoint3.h"
  35. #endif
  36. #ifndef _MMATRIX_H_
  37. #include "math/mMatrix.h"
  38. #endif
  39. #ifndef _TSTREAM_H_
  40. #include "core/stream/tStream.h"
  41. #endif
  42. #ifndef _SFXPROFILE_H_
  43. #include "sfx/sfxProfile.h"
  44. #endif
  45. class SFXBuffer;
  46. class SFXDevice;
  47. /// A scriptable controller playing a specific single sound file.
  48. class SFXSound : public SFXSource,
  49. public IPositionable< U32 >
  50. {
  51. friend class SFXSystem;
  52. typedef SFXSource Parent;
  53. protected:
  54. /// Used by SFXSystem to create sources.
  55. static SFXSound* _create( SFXDevice* device, SFXProfile* profile );
  56. static SFXSound* _create( SFXDevice* device, const ThreadSafeRef< SFXStream >& stream, SFXDescription* description );
  57. /// Internal constructor used for sources.
  58. SFXSound( SFXProfile* profile, SFXDescription* description );
  59. /// The device specific voice which is used during
  60. /// playback. By making it a SafePtr it will NULL
  61. /// automatically when the device is deleted.
  62. StrongWeakRefPtr< SFXVoice > mVoice;
  63. /// The reference counted device specific buffer used by
  64. /// the voice for playback.
  65. StrongWeakRefPtr< SFXBuffer > mBuffer;
  66. /// The duration of the sound cached from the buffer in
  67. /// _initBuffer() used for managing virtual sources.
  68. U32 mDuration;
  69. ///Used for setPosition (time in miliseconds)
  70. U32 mSetPositionValue;
  71. /// Create a new voice for this source.
  72. bool _allocVoice( SFXDevice* device );
  73. /// Release the voice if the source has one.
  74. bool _releaseVoice();
  75. ///
  76. void _setBuffer( SFXBuffer* buffer );
  77. /// Reload the sound buffer. Temporarily goes to virtualized playback when necessary.
  78. void _reloadBuffer();
  79. ///
  80. void _onProfileChanged( SFXProfile* profile )
  81. {
  82. if( profile == mTrack )
  83. _reloadBuffer();
  84. }
  85. // SFXSource.
  86. virtual void _play();
  87. virtual void _pause();
  88. virtual void _stop();
  89. virtual void _updateStatus();
  90. virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event );
  91. virtual void _updateVolume( const MatrixF& listener );
  92. virtual void _updatePitch();
  93. virtual void _updatePriority();
  94. virtual void _setMinMaxDistance( F32 min, F32 max );
  95. virtual void _setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume );
  96. public:
  97. DECLARE_CONOBJECT( SFXSound );
  98. /// The default constructor is *only* here to satisfy the
  99. /// construction needs of IMPLEMENT_CONOBJECT. It does not
  100. /// create a valid source!
  101. explicit SFXSound();
  102. /// This is normally called from the system to
  103. /// detect if this source has been assigned a
  104. /// voice for playback.
  105. bool hasVoice() const { return mVoice != NULL; }
  106. /// Return the current playback position in milliseconds.
  107. /// @note For looping sources, this returns the position in the current cycle.
  108. U32 getPosition() const;
  109. /// Set the current playback position in milliseconds.
  110. void setPosition( U32 ms );
  111. /// Returns the source's total playback time in milliseconds.
  112. U32 getDuration() const { return mDuration; }
  113. /// Return true if the sound stream tied to the source is currently in a buffer underrun situation.
  114. bool isBlocked() const { return ( mVoice && mVoice->getStatus() == SFXStatusBlocked ); }
  115. /// Returns true if this is a continuously streaming source.
  116. bool isStreaming() const { return mDescription->mIsStreaming; }
  117. /// Returns true if the source's associated data is ready for playback.
  118. bool isReady() const;
  119. /// Return the SFXProfile datablock attached to this sound.
  120. SFXProfile* getProfile() const;
  121. /// Used to sort sources by attenuated volume and channel priority.
  122. static S32 QSORT_CALLBACK qsortCompare( const void* item1, const void* item2 );
  123. // SFXSource.
  124. virtual void setTransform( const MatrixF& transform );
  125. virtual void setVelocity( const VectorF& velocity );
  126. virtual bool isVirtualized() const;
  127. virtual F32 getElapsedPlayTimeCurrentCycle() const;
  128. virtual F32 getTotalPlayTime() const;
  129. // SimObject.
  130. virtual void onRemove();
  131. virtual void onDeleteNotify( SimObject* object );
  132. };
  133. #endif // !_SFXSOUND_H_