sfxSndStream.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 _SFXSNDSTREAM_H_
  23. #define _SFXSNDSTREAM_H_
  24. #ifndef _SFXFILESTREAM_H_
  25. # include "sfx/sfxFileStream.h"
  26. #endif
  27. #include "core/util/safeDelete.h"
  28. #include <sndfile.h>
  29. /// An SFXFileStream that loads sample data from a Vorbis file.
  30. class SFXSndStream : public SFXFileStream,
  31. public IPositionable< U32 >
  32. {
  33. public:
  34. typedef SFXFileStream Parent;
  35. protected:
  36. // setup our vio_data struct.
  37. typedef struct
  38. {
  39. sf_count_t offset, length;
  40. Stream* data;
  41. } VIO_DATA;
  42. /// Total number of bytes read from the stream so far.
  43. U32 mBytesRead;
  44. /// The current bitstream index.
  45. S32 mCurPos;
  46. SF_VIRTUAL_IO vio;
  47. SNDFILE* sndFile;
  48. VIO_DATA vio_data;
  49. SF_INFO sfinfo;
  50. // vio callbacks
  51. static sf_count_t sndSeek(sf_count_t offset, int whence, void* user_data);
  52. static sf_count_t sndRead(void *ptr, sf_count_t count, void* user_data);
  53. static sf_count_t sndWrite(const void* ptr, sf_count_t count, void* user_data);
  54. static sf_count_t sndTell(void* user_data);
  55. static sf_count_t sndFileLen(void* user_data);
  56. // SFXStream
  57. bool _readHeader() override;
  58. void _close() override;
  59. public:
  60. ///
  61. static SFXSndStream* create(Stream* stream);
  62. // SFXStream
  63. void reset() override;
  64. U32 read(U8* buffer, U32 length) override;
  65. bool isEOS() const override;
  66. SFXStream* clone() const override
  67. {
  68. SFXSndStream* stream = new SFXSndStream(*this);
  69. if (!stream->sndFile)
  70. SAFE_DELETE(stream);
  71. return stream;
  72. }
  73. // IPositionable
  74. U32 getPosition() const override;
  75. void setPosition(U32 offset) override;
  76. };
  77. #endif