sfxFileStream.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/sfxFileStream.h"
  23. #include "core/stream/fileStream.h"
  24. #include "console/console.h"
  25. #include "core/util/safeDelete.h"
  26. #include "sfx/media/sfxSndStream.h"
  27. SFXFileStream* SFXFileStream::create( String filename )
  28. {
  29. SFXFileStream *sfxStream = NULL;
  30. Stream *stream = FileStream::createAndOpen(filename, Torque::FS::File::Read );
  31. if ( !stream )
  32. return NULL;
  33. // Note that the creation function swallows up the
  34. // resource stream and will take care of deleting it.
  35. sfxStream = SFXSndStream::create( stream );
  36. if ( sfxStream )
  37. return sfxStream;
  38. return NULL;
  39. }
  40. bool SFXFileStream::exists( String filename )
  41. {
  42. if( Torque::FS::IsFile(filename) )
  43. return true;
  44. return false;
  45. }
  46. SFXFileStream::SFXFileStream()
  47. : mStream( NULL ),
  48. mOwnStream( false ),
  49. mFormat( 0, 0, 0 ),
  50. mSamples( 0 )
  51. {
  52. }
  53. SFXFileStream::SFXFileStream( const SFXFileStream& cloneFrom )
  54. {
  55. mStream = cloneFrom.mStream->clone();
  56. if( !mStream )
  57. {
  58. Con::errorf( "SFXFileStream::SFXFileStream() - Failed to clone source stream" );
  59. return;
  60. }
  61. mOwnStream = true;
  62. mFormat = cloneFrom.mFormat;
  63. mSamples = cloneFrom.mSamples;
  64. }
  65. SFXFileStream::~SFXFileStream()
  66. {
  67. // If the stream is still open, close it now. _close()
  68. // should usually be called by the destructor of derived classes,
  69. // but it their constructor fails, these won't even run.
  70. if( mStream && mOwnStream )
  71. SAFE_DELETE( mStream );
  72. }
  73. bool SFXFileStream::open( Stream *stream, bool ownStream )
  74. {
  75. AssertFatal( stream, "SFXFileStream::open() - Got null stream!" );
  76. close();
  77. mStream = stream;
  78. mOwnStream = ownStream;
  79. if( _readHeader() )
  80. {
  81. reset(); // Make sure we're set to read sample data.
  82. return true;
  83. }
  84. else
  85. return false;
  86. }
  87. void SFXFileStream::close()
  88. {
  89. if ( !mStream )
  90. return;
  91. // Let the overloaded class cleanup.
  92. _close();
  93. // We only close it if we own it.
  94. if ( mOwnStream )
  95. SAFE_DELETE( mStream );
  96. // Reset these to make it easier to detect bugs.
  97. mFormat.set( 0, 0, 0 );
  98. mSamples = 0;
  99. }
  100. bool SFXFileStream::isEOS() const
  101. {
  102. if ( !mStream )
  103. return true;
  104. return mStream->getStatus() != Stream::Ok;
  105. }