sfxXAudioBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "sfx/xaudio/sfxXAudioBuffer.h"
  23. #include "sfx/xaudio/sfxXAudioVoice.h"
  24. //#define DEBUG_SPEW
  25. SFXXAudioBuffer* SFXXAudioBuffer::create( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description )
  26. {
  27. SFXXAudioBuffer *buffer = new SFXXAudioBuffer( stream, description );
  28. return buffer;
  29. }
  30. SFXXAudioBuffer::SFXXAudioBuffer( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description )
  31. : Parent( stream, description )
  32. {
  33. VECTOR_SET_ASSOCIATION( mBufferQueue );
  34. }
  35. SFXXAudioBuffer::~SFXXAudioBuffer()
  36. {
  37. _flush();
  38. }
  39. void SFXXAudioBuffer::write( SFXInternal::SFXStreamPacket* const* packets, U32 num )
  40. {
  41. AssertFatal( SFXInternal::isSFXThread(), "SFXXAudioBuffer::write() - not on SFX thread" );
  42. using namespace SFXInternal;
  43. // Unqueue processed packets.
  44. if( isStreaming() )
  45. {
  46. EnterCriticalSection( &_getUniqueVoice()->mLock );
  47. XAUDIO2_VOICE_STATE state;
  48. _getUniqueVoice()->mXAudioVoice->GetState( &state );
  49. U32 numProcessed = mBufferQueue.size() - state.BuffersQueued;
  50. for( U32 i = 0; i < numProcessed; ++ i )
  51. {
  52. destructSingle< SFXStreamPacket* >( mBufferQueue.first().mPacket );
  53. mBufferQueue.pop_front();
  54. #ifdef DEBUG_SPEW
  55. Platform::outputDebugString( "[SFXXAudioBuffer] Unqueued packet" );
  56. #endif
  57. }
  58. LeaveCriticalSection( &_getUniqueVoice()->mLock );
  59. }
  60. // Queue new packets.
  61. for( U32 i = 0; i < num; ++ i )
  62. {
  63. SFXStreamPacket* packet = packets[ i ];
  64. Buffer buffer;
  65. if( packet->mIsLast )
  66. buffer.mData.Flags = XAUDIO2_END_OF_STREAM;
  67. buffer.mPacket = packet;
  68. buffer.mData.AudioBytes = packet->mSizeActual;
  69. buffer.mData.pAudioData = packet->data;
  70. mBufferQueue.push_back( buffer );
  71. #ifdef DEBUG_SPEW
  72. Platform::outputDebugString( "[SFXXAudioBuffer] Queued packet" );
  73. #endif
  74. // If this is a streaming buffer, submit the packet to the
  75. // voice queue right away.
  76. if( isStreaming() )
  77. {
  78. EnterCriticalSection( &_getUniqueVoice()->mLock );
  79. IXAudio2SourceVoice* voice = _getUniqueVoice()->mXAudioVoice;
  80. voice->SubmitSourceBuffer( &buffer.mData );
  81. LeaveCriticalSection( &_getUniqueVoice()->mLock );
  82. }
  83. }
  84. }
  85. void SFXXAudioBuffer::_flush()
  86. {
  87. #ifdef DEBUG_SPEW
  88. Platform::outputDebugString( "[SFXXAudioBuffer] Flushing buffer" );
  89. #endif
  90. if( _getUniqueVoice() )
  91. _getUniqueVoice()->_stop();
  92. while( !mBufferQueue.empty() )
  93. {
  94. destructSingle( mBufferQueue.last().mPacket );
  95. mBufferQueue.pop_back();
  96. }
  97. }