sfxXAudioVoice.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 _SFXXAUDIOVOICE_H_
  23. #define _SFXXAUDIOVOICE_H_
  24. #include <xaudio2.h>
  25. #include <x3daudio.h>
  26. #include "sfx/sfxVoice.h"
  27. class SFXXAudioBuffer;
  28. class SFXXAudioVoice : public SFXVoice,
  29. public IXAudio2VoiceCallback
  30. {
  31. public:
  32. typedef SFXVoice Parent;
  33. friend class SFXXAudioDevice;
  34. friend class SFXXAudioBuffer;
  35. protected:
  36. /// This constructor does not create a valid voice.
  37. /// @see SFXXAudioVoice::create
  38. SFXXAudioVoice( SFXXAudioBuffer* buffer );
  39. /// The device that created us.
  40. SFXXAudioDevice *mXAudioDevice;
  41. /// The XAudio voice.
  42. IXAudio2SourceVoice *mXAudioVoice;
  43. ///
  44. CRITICAL_SECTION mLock;
  45. /// Used to know what sounds need positional updates.
  46. bool mIs3D;
  47. /// Whether the voice has stopped playing.
  48. mutable bool mHasStopped;
  49. /// Whether we have started playback.
  50. bool mHasStarted;
  51. /// Whether playback is currently running.
  52. bool mIsPlaying;
  53. /// Whether playback is looping.
  54. bool mIsLooping;
  55. /// Since 3D sounds are pitch shifted for doppler
  56. /// effect we need to track the users base pitch.
  57. F32 mPitch;
  58. /// The cached X3DAudio emitter data.
  59. X3DAUDIO_EMITTER mEmitter;
  60. /// XAudio does not reset the SamplesPlayed count as is stated in the docs. To work around
  61. /// that, we offset the values reported by XAudio through this field.
  62. S32 mSamplesPlayedOffset;
  63. /// @name Data for Non-Streaming Voices
  64. /// @{
  65. /// Whether we have loaded our non-streaming data. We use an explicit
  66. /// flag here as we really can't rely on XAUDIO2_VOICE_STATE.
  67. bool mNonStreamBufferLoaded;
  68. /// Audio buffer for non-streaming voice.
  69. XAUDIO2_BUFFER mNonStreamBuffer;
  70. /// Sample to start playing at when seting up #mNonStreamBuffer.
  71. U32 mNonStreamSampleStartPos;
  72. /// @}
  73. // IXAudio2VoiceCallback
  74. void STDMETHODCALLTYPE OnStreamEnd();
  75. void STDMETHODCALLTYPE OnVoiceProcessingPassStart( UINT32 BytesRequired ) {}
  76. void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
  77. void STDMETHODCALLTYPE OnBufferEnd( void *bufferContext );
  78. void STDMETHODCALLTYPE OnBufferStart( void *bufferContext ) {}
  79. void STDMETHODCALLTYPE OnLoopEnd( void *bufferContext ) {}
  80. void STDMETHODCALLTYPE OnVoiceError( void * bufferContext, HRESULT error ) {}
  81. /// @deprecated This is only here for compatibility with
  82. /// the March 2008 SDK version of IXAudio2VoiceCallback.
  83. void STDMETHODCALLTYPE OnVoiceProcessingPassStart() {}
  84. void _flush();
  85. void _loadNonStreamed();
  86. // SFXVoice.
  87. virtual SFXStatus _status() const;
  88. virtual void _play();
  89. virtual void _pause();
  90. virtual void _stop();
  91. virtual void _seek( U32 sample );
  92. virtual U32 _tell() const;
  93. SFXXAudioBuffer* _getBuffer() const { return ( SFXXAudioBuffer* ) mBuffer.getPointer(); }
  94. public:
  95. ///
  96. static SFXXAudioVoice* create( IXAudio2 *xaudio,
  97. bool is3D,
  98. SFXXAudioBuffer *buffer,
  99. SFXXAudioVoice* inVoice = NULL );
  100. ///
  101. virtual ~SFXXAudioVoice();
  102. // SFXVoice
  103. void setMinMaxDistance( F32 min, F32 max );
  104. void play( bool looping );
  105. void setVelocity( const VectorF& velocity );
  106. void setTransform( const MatrixF& transform );
  107. void setVolume( F32 volume );
  108. void setPitch( F32 pitch );
  109. void setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume );
  110. /// Is this a 3D positional voice.
  111. bool is3D() const { return mIs3D; }
  112. ///
  113. const X3DAUDIO_EMITTER& getEmitter() const { return mEmitter; }
  114. };
  115. #endif // _SFXXAUDIOVOICE_H_