sfxFileStream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef _SFXFILESTREAM_H_
  23. #define _SFXFILESTREAM_H_
  24. #ifndef _SFXSTREAM_H_
  25. # include "sfx/sfxStream.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. # include "core/util/tVector.h"
  29. #endif
  30. #ifndef _TORQUE_STRING_H_
  31. # include "core/util/str.h"
  32. #endif
  33. class Stream;
  34. class SFXFileStream;
  35. ///
  36. typedef SFXFileStream* ( *SFXFILESTREAM_CREATE_FN )( Stream *stream );
  37. /// An SFXStream that streams from a file.
  38. class SFXFileStream : public SFXStream
  39. {
  40. protected:
  41. typedef Vector< String > ExtensionsVector;
  42. typedef Vector< SFXFILESTREAM_CREATE_FN > CreateFnsVector;
  43. static ExtensionsVector smExtensions;
  44. static CreateFnsVector smCreateFns;
  45. /// The file stream we're reading from.
  46. Stream *mStream;
  47. /// If true then we're responsible for closing the stream.
  48. bool mOwnStream;
  49. /// The format of the data in the stream.
  50. SFXFormat mFormat;
  51. /// The number of samples in the data stream.
  52. U32 mSamples;
  53. /// Constructs the stream in an uninitilized state.
  54. SFXFileStream();
  55. ///
  56. SFXFileStream( const SFXFileStream& cloneFrom );
  57. /// Overloaded in the derived classes to read
  58. /// the file header. It should initialize
  59. /// mFormat and mSamples.
  60. virtual bool _readHeader() = 0;
  61. /// Overloaded for cleanup of file format
  62. /// specific structures.
  63. virtual void _close() = 0;
  64. public:
  65. ///
  66. static void registerExtension( String ext, SFXFILESTREAM_CREATE_FN create_fn );
  67. ///
  68. static void unregisterExtension( String ext );
  69. /// This is a helper function used to create an appropriate SFXStream
  70. /// for the requested sound file.
  71. ///
  72. /// @param filename The sound file path with or without extension.
  73. ///
  74. static SFXFileStream* create( String filename );
  75. ///
  76. static bool exists( String filename );
  77. /// Destructor.
  78. virtual ~SFXFileStream();
  79. /// Opens and optionally takes ownership of the stream.
  80. bool open( Stream *stream, bool ownStream = false );
  81. /// Closes the stream.
  82. void close();
  83. // SFXStream.
  84. const SFXFormat& getFormat() const { return mFormat; }
  85. U32 getSampleCount() const { return mSamples; }
  86. U32 getDataLength() const { return mSamples * mFormat.getBytesPerSample(); }
  87. U32 getDuration() const { return mFormat.getDuration( mSamples ); }
  88. bool isEOS() const;
  89. };
  90. #endif // _SFXFILESTREAM_H_