The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

223 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_ABSTRACTFIFO_H_INCLUDED
  24. #define JUCE_ABSTRACTFIFO_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Encapsulates the logic required to implement a lock-free FIFO.
  28. This class handles the logic needed when building a single-reader, single-writer FIFO.
  29. It doesn't actually hold any data itself, but your FIFO class can use one of these to manage
  30. its position and status when reading or writing to it.
  31. To use it, you can call prepareToWrite() to determine the position within your own buffer that
  32. an incoming block of data should be stored, and prepareToRead() to find out when the next
  33. outgoing block should be read from.
  34. e.g.
  35. @code
  36. class MyFifo
  37. {
  38. public:
  39. MyFifo() : abstractFifo (1024)
  40. {
  41. }
  42. void addToFifo (const int* someData, int numItems)
  43. {
  44. int start1, size1, start2, size2;
  45. abstractFifo.prepareToWrite (numItems, start1, size1, start2, size2);
  46. if (size1 > 0)
  47. copySomeData (myBuffer + start1, someData, size1);
  48. if (size2 > 0)
  49. copySomeData (myBuffer + start2, someData + size1, size2);
  50. abstractFifo.finishedWrite (size1 + size2);
  51. }
  52. void readFromFifo (int* someData, int numItems)
  53. {
  54. int start1, size1, start2, size2;
  55. abstractFifo.prepareToRead (numItems, start1, size1, start2, size2);
  56. if (size1 > 0)
  57. copySomeData (someData, myBuffer + start1, size1);
  58. if (size2 > 0)
  59. copySomeData (someData + size1, myBuffer + start2, size2);
  60. abstractFifo.finishedRead (size1 + size2);
  61. }
  62. private:
  63. AbstractFifo abstractFifo;
  64. int myBuffer [1024];
  65. };
  66. @endcode
  67. */
  68. class JUCE_API AbstractFifo
  69. {
  70. public:
  71. //==============================================================================
  72. /** Creates a FIFO to manage a buffer with the specified capacity. */
  73. AbstractFifo (int capacity) noexcept;
  74. /** Destructor */
  75. ~AbstractFifo();
  76. //==============================================================================
  77. /** Returns the total size of the buffer being managed. */
  78. int getTotalSize() const noexcept;
  79. /** Returns the number of items that can currently be added to the buffer without it overflowing. */
  80. int getFreeSpace() const noexcept;
  81. /** Returns the number of items that can currently be read from the buffer. */
  82. int getNumReady() const noexcept;
  83. /** Clears the buffer positions, so that it appears empty. */
  84. void reset() noexcept;
  85. /** Changes the buffer's total size.
  86. Note that this isn't thread-safe, so don't call it if there's any danger that it
  87. might overlap with a call to any other method in this class!
  88. */
  89. void setTotalSize (int newSize) noexcept;
  90. //==============================================================================
  91. /** Returns the location within the buffer at which an incoming block of data should be written.
  92. Because the section of data that you want to add to the buffer may overlap the end
  93. and wrap around to the start, two blocks within your buffer are returned, and you
  94. should copy your data into the first one, with any remaining data spilling over into
  95. the second.
  96. If the number of items you ask for is too large to fit within the buffer's free space, then
  97. blockSize1 + blockSize2 may add up to a lower value than numToWrite. If this happens, you
  98. may decide to keep waiting and re-trying the method until there's enough space available.
  99. After calling this method, if you choose to write your data into the blocks returned, you
  100. must call finishedWrite() to tell the FIFO how much data you actually added.
  101. e.g.
  102. @code
  103. void addToFifo (const int* someData, int numItems)
  104. {
  105. int start1, size1, start2, size2;
  106. prepareToWrite (numItems, start1, size1, start2, size2);
  107. if (size1 > 0)
  108. copySomeData (myBuffer + start1, someData, size1);
  109. if (size2 > 0)
  110. copySomeData (myBuffer + start2, someData + size1, size2);
  111. finishedWrite (size1 + size2);
  112. }
  113. @endcode
  114. @param numToWrite indicates how many items you'd like to add to the buffer
  115. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  116. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  117. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  118. the first block should be written
  119. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  120. @see finishedWrite
  121. */
  122. void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  123. /** Called after writing from the FIFO, to indicate that this many items have been added.
  124. @see prepareToWrite
  125. */
  126. void finishedWrite (int numWritten) noexcept;
  127. /** Returns the location within the buffer from which the next block of data should be read.
  128. Because the section of data that you want to read from the buffer may overlap the end
  129. and wrap around to the start, two blocks within your buffer are returned, and you
  130. should read from both of them.
  131. If the number of items you ask for is greater than the amount of data available, then
  132. blockSize1 + blockSize2 may add up to a lower value than numWanted. If this happens, you
  133. may decide to keep waiting and re-trying the method until there's enough data available.
  134. After calling this method, if you choose to read the data, you must call finishedRead() to
  135. tell the FIFO how much data you have consumed.
  136. e.g.
  137. @code
  138. void readFromFifo (int* someData, int numItems)
  139. {
  140. int start1, size1, start2, size2;
  141. prepareToRead (numSamples, start1, size1, start2, size2);
  142. if (size1 > 0)
  143. copySomeData (someData, myBuffer + start1, size1);
  144. if (size2 > 0)
  145. copySomeData (someData + size1, myBuffer + start2, size2);
  146. finishedRead (size1 + size2);
  147. }
  148. @endcode
  149. @param numWanted indicates how many items you'd like to add to the buffer
  150. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  151. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  152. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  153. the first block should be written
  154. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  155. @see finishedRead
  156. */
  157. void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  158. /** Called after reading from the FIFO, to indicate that this many items have now been consumed.
  159. @see prepareToRead
  160. */
  161. void finishedRead (int numRead) noexcept;
  162. private:
  163. //==============================================================================
  164. int bufferSize;
  165. Atomic <int> validStart, validEnd;
  166. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
  167. };
  168. #endif // JUCE_ABSTRACTFIFO_H_INCLUDED