sfxSound.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /// Create a new voice for this source.
  70. bool _allocVoice( SFXDevice* device );
  71. /// Release the voice if the source has one.
  72. bool _releaseVoice();
  73. ///
  74. void _setBuffer( SFXBuffer* buffer );
  75. /// Reload the sound buffer. Temporarily goes to virtualized playback when necessary.
  76. void _reloadBuffer();
  77. ///
  78. void _onProfileChanged( SFXProfile* profile )
  79. {
  80. if( profile == mTrack )
  81. _reloadBuffer();
  82. }
  83. // SFXSource.
  84. virtual void _play();
  85. virtual void _pause();
  86. virtual void _stop();
  87. virtual void _updateStatus();
  88. virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event );
  89. virtual void _updateVolume( const MatrixF& listener );
  90. virtual void _updatePitch();
  91. virtual void _updatePriority();
  92. virtual void _setMinMaxDistance( F32 min, F32 max );
  93. virtual void _setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume );
  94. public:
  95. DECLARE_CONOBJECT( SFXSound );
  96. /// The default constructor is *only* here to satisfy the
  97. /// construction needs of IMPLEMENT_CONOBJECT. It does not
  98. /// create a valid source!
  99. explicit SFXSound();
  100. /// This is normally called from the system to
  101. /// detect if this source has been assigned a
  102. /// voice for playback.
  103. bool hasVoice() const { return mVoice != NULL; }
  104. /// Return the current playback position in milliseconds.
  105. /// @note For looping sources, this returns the position in the current cycle.
  106. U32 getPosition() const;
  107. /// Set the current playback position in milliseconds.
  108. void setPosition( U32 ms );
  109. /// Returns the source's total playback time in milliseconds.
  110. U32 getDuration() const { return mDuration; }
  111. /// Return true if the sound stream tied to the source is currently in a buffer underrun situation.
  112. bool isBlocked() const { return ( mVoice && mVoice->getStatus() == SFXStatusBlocked ); }
  113. /// Returns true if this is a continuously streaming source.
  114. bool isStreaming() const { return mDescription->mIsStreaming; }
  115. /// Returns true if the source's associated data is ready for playback.
  116. bool isReady() const;
  117. /// Return the SFXProfile datablock attached to this sound.
  118. SFXProfile* getProfile() const;
  119. /// Used to sort sources by attenuated volume and channel priority.
  120. static S32 QSORT_CALLBACK qsortCompare( const void* item1, const void* item2 );
  121. // SFXSource.
  122. virtual void setTransform( const MatrixF& transform );
  123. virtual void setVelocity( const VectorF& velocity );
  124. virtual bool isVirtualized() const;
  125. virtual F32 getElapsedPlayTimeCurrentCycle() const;
  126. virtual F32 getTotalPlayTime() const;
  127. // SimObject.
  128. virtual void onRemove();
  129. virtual void onDeleteNotify( SimObject* object );
  130. };
  131. #endif // !_SFXSOUND_H_