sfxInternal.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/sfxInternal.h"
  23. #include "sfx/sfxDescription.h"
  24. #include "core/util/safeDelete.h"
  25. #include "platform/threads/threadPoolAsyncIO.h"
  26. /// @file
  27. /// Implementation of async sound I/O.
  28. //#define DEBUG_SPEW
  29. namespace SFXInternal {
  30. ThreadSafeRef< SFXUpdateThread > gUpdateThread;
  31. ThreadSafeRef< SFXBufferProcessList > gBufferUpdateList = new SFXBufferProcessList;
  32. ThreadSafeDeque< SFXBuffer* > gDeadBufferList;
  33. //==========================================================================
  34. // SFXAsyncStream implementation.
  35. //==========================================================================
  36. //--------------------------------------------------------------------------
  37. SFXAsyncStream::SFXAsyncStream( const SFXStreamRef& stream,
  38. bool isIncremental,
  39. U32 streamPacketLength,
  40. U32 numReadAhead,
  41. bool isLooping )
  42. : Parent( stream,
  43. isIncremental
  44. ? streamPacketLength
  45. * stream->getFormat().getSamplesPerSecond()
  46. * stream->getFormat().getBytesPerSample() // Streamed buffer; read in incremental packets.
  47. : stream->getDataLength(), // Non-streamed buffer; read entire stream in one packet.
  48. stream->getDataLength() // Read all remaining data in stream.
  49. - ( dynamic_cast< IPositionable< U32 >* >( stream.ptr() )
  50. ? dynamic_cast< IPositionable< U32 >* >( stream.ptr() )->getPosition()
  51. : 0 ),
  52. numReadAhead,
  53. isLooping,
  54. &THREAD_POOL() ),
  55. mReadSilenceAtEnd( false )
  56. {
  57. }
  58. //--------------------------------------------------------------------------
  59. void SFXAsyncStream::_onArrival( SFXStreamPacket* const& packet )
  60. {
  61. #ifdef DEBUG_SPEW
  62. Platform::outputDebugString( "[SFXAsyncStream] Packet arrived" );
  63. #endif
  64. Parent::_onArrival( packet );
  65. // Some buffer may be waiting for this data so trigger
  66. // an update.
  67. if( !mIsStopped )
  68. TriggerUpdate();
  69. }
  70. //--------------------------------------------------------------------------
  71. void SFXAsyncStream::_requestNext()
  72. {
  73. #ifdef DEBUG_SPEW
  74. Platform::outputDebugString( "[SFXAsyncStream] Next packet requested" );
  75. #endif
  76. if( !mNumRemainingSourceElements && mReadSilenceAtEnd )
  77. {
  78. // Push an artificial packet of silence.
  79. SFXStreamPacket* packet = _newPacket( mPacketSize );
  80. packet->mIndex = mNextPacketIndex;
  81. mNextPacketIndex ++;
  82. mReadSilenceAtEnd = false;
  83. dMemset( packet->data, 0, packet->size );
  84. packet->mIsLast = true;
  85. _onArrival( packet );
  86. }
  87. else
  88. Parent::_requestNext();
  89. }
  90. //==========================================================================
  91. // SFXWrapAroundBuffer implementation.
  92. //==========================================================================
  93. //--------------------------------------------------------------------------
  94. SFXWrapAroundBuffer::SFXWrapAroundBuffer( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description )
  95. : Parent( stream, description ),
  96. mWriteOffset( 0 )
  97. {
  98. // Determine the device buffer metrics.
  99. const U32 maxQueuedPackets = isStreaming() ? SFXAsyncQueue::DEFAULT_STREAM_QUEUE_LENGTH : 1;
  100. const U32 packetSize = mAsyncState->mStream->getPacketSize();
  101. mBufferSize = maxQueuedPackets * packetSize;
  102. #ifdef DEBUG_SPEW
  103. Platform::outputDebugString( "[SFXWrapAroundBuffer] size=%i, packets=%i",
  104. mBufferSize, maxQueuedPackets );
  105. #endif
  106. // For streaming buffers that are not looping, add a packet of silence to the
  107. // source stream.
  108. if( isStreaming() && !description->mIsLooping )
  109. mAsyncState->mStream->setReadSilenceAtEnd( true );
  110. }
  111. //--------------------------------------------------------------------------
  112. void SFXWrapAroundBuffer::write( SFXStreamPacket* const* packets, U32 num )
  113. {
  114. AssertFatal( SFXInternal::isSFXThread(), "SFXWrapAroundBuffer::write() - not on SFX thread" );
  115. for( U32 i = 0; i < num; ++ i )
  116. {
  117. const SFXStreamPacket* packet = packets[ i ];
  118. // Determine where in the buffer to copy the data to. In case we are crossing over
  119. // the wrap-around point, we need to copy in two slices.
  120. U32 offset1 = 0;
  121. U32 offset2 = 0;
  122. U32 numBytes1 = 0;
  123. U32 numBytes2 = 0;
  124. offset1 = mWriteOffset % mBufferSize;
  125. numBytes1 = packet->size;
  126. if( offset1 + numBytes1 > mBufferSize )
  127. {
  128. // Crossing wrap-around point.
  129. numBytes1 = mBufferSize - offset1;
  130. numBytes2 = packet->size - numBytes1;
  131. }
  132. offset2 = offset1 + numBytes1;
  133. #ifdef DEBUG_SPEW
  134. Platform::outputDebugString( "[SFXWrapAroundBuffer] writing %i bytes from packet #%i at %i (stream offset: %i)",
  135. numBytes1, packet->mIndex, offset1, mWriteOffset );
  136. #endif
  137. // Copy the packet data.
  138. _copyData( offset1, packet->data, numBytes1 );
  139. if( numBytes2 > 0 )
  140. {
  141. #ifdef DEBUG_SPEW
  142. Platform::outputDebugString( "[SFXWrapAroundBuffer] writing %i more bytes at %i",
  143. numBytes2, offset2 );
  144. #endif
  145. _copyData( offset2, &packet->data[ numBytes1 ], numBytes2 );
  146. }
  147. dFetchAndAdd( mWriteOffset, packet->size );
  148. // Free the packet.
  149. destructSingle( packet );
  150. }
  151. }
  152. } // namespace SFXInternal