Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_MEMORYINPUTSTREAM_H_INCLUDED
  21. #define WATER_MEMORYINPUTSTREAM_H_INCLUDED
  22. #include "InputStream.h"
  23. #include "../memory/MemoryBlock.h"
  24. #include "../memory/HeapBlock.h"
  25. namespace water {
  26. //==============================================================================
  27. /**
  28. Allows a block of data to be accessed as a stream.
  29. This can either be used to refer to a shared block of memory, or can make its
  30. own internal copy of the data when the MemoryInputStream is created.
  31. */
  32. class MemoryInputStream : public InputStream
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a MemoryInputStream.
  37. @param sourceData the block of data to use as the stream's source
  38. @param sourceDataSize the number of bytes in the source data block
  39. @param keepInternalCopyOfData if false, the stream will just keep a pointer to
  40. the source data, so this data shouldn't be changed
  41. for the lifetime of the stream; if this parameter is
  42. true, the stream will make its own copy of the
  43. data and use that.
  44. */
  45. MemoryInputStream (const void* sourceData,
  46. size_t sourceDataSize,
  47. bool keepInternalCopyOfData);
  48. /** Creates a MemoryInputStream.
  49. @param data a block of data to use as the stream's source
  50. @param keepInternalCopyOfData if false, the stream will just keep a reference to
  51. the source data, so this data shouldn't be changed
  52. for the lifetime of the stream; if this parameter is
  53. true, the stream will make its own copy of the
  54. data and use that.
  55. */
  56. MemoryInputStream (const MemoryBlock& data,
  57. bool keepInternalCopyOfData);
  58. /** Destructor. */
  59. ~MemoryInputStream();
  60. /** Returns a pointer to the source data block from which this stream is reading. */
  61. const void* getData() const noexcept { return data; }
  62. /** Returns the number of bytes of source data in the block from which this stream is reading. */
  63. size_t getDataSize() const noexcept { return dataSize; }
  64. //==============================================================================
  65. int64 getPosition() override;
  66. bool setPosition (int64 pos) override;
  67. int64 getTotalLength() override;
  68. bool isExhausted() override;
  69. int read (void* destBuffer, int maxBytesToRead) override;
  70. private:
  71. //==============================================================================
  72. const void* data;
  73. size_t dataSize, position;
  74. HeapBlock<char> internalCopy;
  75. void createInternalCopy();
  76. CARLA_DECLARE_NON_COPY_CLASS (MemoryInputStream)
  77. };
  78. }
  79. #endif // WATER_MEMORYINPUTSTREAM_H_INCLUDED