sfxMemoryStream.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/sfxMemoryStream.h"
  23. #include "platform/typetraits.h"
  24. #include "console/console.h"
  25. SFXMemoryStream::SFXMemoryStream( const SFXFormat& format,
  26. SourceStreamType* stream,
  27. U32 numSamples )
  28. : IInputStreamFilter< U8, SourceStreamType* >( stream ),
  29. mFormat( format ),
  30. mNumSamplesTotal( numSamples ),
  31. mNumSamplesLeft( numSamples ),
  32. mCurrentPacket( NULL ),
  33. mCurrentPacketOffset( 0 )
  34. {
  35. }
  36. void SFXMemoryStream::reset()
  37. {
  38. if( dynamic_cast< IResettable* >( getSourceStream() ) )
  39. {
  40. reinterpret_cast< IResettable* >( getSourceStream() )->reset();
  41. if( mCurrentPacket )
  42. destructSingle( mCurrentPacket );
  43. mCurrentPacket = NULL;
  44. mCurrentPacketOffset = 0;
  45. mNumSamplesLeft = mNumSamplesTotal;
  46. }
  47. else
  48. Con::errorf( "SFXMemoryStream - cannot reset source stream" );
  49. }
  50. U32 SFXMemoryStream::read( U8* buffer, U32 length )
  51. {
  52. U32 bufferOffset = 0;
  53. // Determine how much we're supposed to read.
  54. U32 numBytesToCopy = length;
  55. if( mNumSamplesLeft != U32_MAX )
  56. numBytesToCopy = getMin( length, mNumSamplesLeft * mFormat.getBytesPerSample() );
  57. numBytesToCopy -= numBytesToCopy % mFormat.getBytesPerSample();
  58. // Copy the data.
  59. U32 numBytesLeftToCopy = numBytesToCopy;
  60. while( numBytesLeftToCopy )
  61. {
  62. // If we have a current packet, use its data.
  63. if( mCurrentPacket )
  64. {
  65. U32 numBytesLeftInCurrentPacket = mCurrentPacket->size - mCurrentPacketOffset;
  66. // Copy data.
  67. if( numBytesLeftInCurrentPacket )
  68. {
  69. const U32 remainingNumBytesToCopy = getMin( numBytesLeftInCurrentPacket, numBytesLeftToCopy );
  70. dMemcpy( &buffer[ bufferOffset ], &mCurrentPacket->data[ mCurrentPacketOffset ], remainingNumBytesToCopy);
  71. bufferOffset += remainingNumBytesToCopy;
  72. mCurrentPacketOffset += remainingNumBytesToCopy;
  73. numBytesLeftInCurrentPacket -= remainingNumBytesToCopy;
  74. numBytesLeftToCopy -= remainingNumBytesToCopy;
  75. }
  76. // Discard the packet if there's no data left.
  77. if( !numBytesLeftInCurrentPacket )
  78. {
  79. destructSingle( mCurrentPacket );
  80. mCurrentPacket = NULL;
  81. mCurrentPacketOffset = 0;
  82. }
  83. }
  84. else
  85. {
  86. // Read a new packet.
  87. if( !getSourceStream()->read( &mCurrentPacket, 1 ) )
  88. break;
  89. }
  90. }
  91. // Update count of remaining samples.
  92. U32 numBytesCopied = numBytesToCopy - numBytesLeftToCopy;
  93. if( mNumSamplesLeft != U32_MAX )
  94. mNumSamplesLeft -= ( numBytesCopied / mFormat.getBytesPerSample() );
  95. return numBytesCopied;
  96. }