sfxVorbisStream.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 _SFXVORBISSTREAM_H_
  23. #define _SFXVORBISSTREAM_H_
  24. #ifdef TORQUE_OGGVORBIS
  25. #ifndef _SFXFILESTREAM_H_
  26. # include "sfx/sfxFileStream.h"
  27. #endif
  28. #include "core/util/safeDelete.h"
  29. #include "vorbis/vorbisfile.h"
  30. /// An SFXFileStream that loads sample data from a Vorbis file.
  31. class SFXVorbisStream : public SFXFileStream,
  32. public IPositionable< U32 >
  33. {
  34. public:
  35. typedef SFXFileStream Parent;
  36. protected:
  37. /// The vorbis file.
  38. OggVorbis_File *mVF;
  39. /// The current bitstream index.
  40. S32 mBitstream;
  41. /// Total number of bytes read from the Vorbis stream so far.
  42. U32 mBytesRead;
  43. ///
  44. bool _openVorbis();
  45. // The ov_callbacks.
  46. static size_t _read_func( void *ptr, size_t size, size_t nmemb, void *datasource );
  47. static S32 _seek_func( void *datasource, ogg_int64_t offset, S32 whence );
  48. static long _tell_func( void *datasource );
  49. // SFXStream
  50. virtual bool _readHeader();
  51. virtual void _close();
  52. public:
  53. ///
  54. static SFXVorbisStream* create( Stream *stream );
  55. ///
  56. SFXVorbisStream();
  57. ///
  58. SFXVorbisStream( const SFXVorbisStream& cloneFrom );
  59. virtual ~SFXVorbisStream();
  60. ///
  61. const vorbis_info* getInfo( S32 link = -1 );
  62. ///
  63. const vorbis_comment* getComment( S32 link = -1 );
  64. ///
  65. // TODO: Deal with error cases... like for unseekable streams!
  66. U64 getPcmTotal( S32 link = -1 );
  67. ///
  68. S32 read( U8 *buffer,
  69. U32 length,
  70. S32 *bitstream );
  71. // SFXStream
  72. virtual void reset();
  73. virtual U32 read( U8 *buffer, U32 length );
  74. virtual bool isEOS() const;
  75. virtual SFXStream* clone() const
  76. {
  77. SFXVorbisStream* stream = new SFXVorbisStream( *this );
  78. if( !stream->mVF )
  79. SAFE_DELETE( stream );
  80. return stream;
  81. }
  82. // IPositionable
  83. virtual U32 getPosition() const;
  84. virtual void setPosition( U32 offset );
  85. };
  86. #endif // TORQUE_OGGVORBIS
  87. #endif // _SFXVORBISSTREAM_H_