sfxMemoryStream.cpp 4.1 KB

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