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.

220 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ABSTRACTFIFO_JUCEHEADER__
  19. #define __JUCE_ABSTRACTFIFO_JUCEHEADER__
  20. #include "../memory/juce_Atomic.h"
  21. //==============================================================================
  22. /**
  23. Encapsulates the logic required to implement a lock-free FIFO.
  24. This class handles the logic needed when building a single-reader, single-writer FIFO.
  25. It doesn't actually hold any data itself, but your FIFO class can use one of these to manage
  26. its position and status when reading or writing to it.
  27. To use it, you can call prepareToWrite() to determine the position within your own buffer that
  28. an incoming block of data should be stored, and prepareToRead() to find out when the next
  29. outgoing block should be read from.
  30. e.g.
  31. @code
  32. class MyFifo
  33. {
  34. public:
  35. MyFifo() : abstractFifo (1024)
  36. {
  37. }
  38. void addToFifo (const int* someData, int numItems)
  39. {
  40. int start1, size1, start2, size2;
  41. abstractFifo.prepareToWrite (numItems, start1, size1, start2, size2);
  42. if (size1 > 0)
  43. copySomeData (myBuffer + start1, someData, size1);
  44. if (size2 > 0)
  45. copySomeData (myBuffer + start2, someData + size1, size2);
  46. abstractFifo.finishedWrite (size1 + size2);
  47. }
  48. void readFromFifo (int* someData, int numItems)
  49. {
  50. int start1, size1, start2, size2;
  51. abstractFifo.prepareToRead (numSamples, start1, size1, start2, size2);
  52. if (size1 > 0)
  53. copySomeData (someData, myBuffer + start1, size1);
  54. if (size2 > 0)
  55. copySomeData (someData + size1, myBuffer + start2, size2);
  56. abstractFifo.finishedRead (size1 + size2);
  57. }
  58. private:
  59. AbstractFifo abstractFifo;
  60. int myBuffer [1024];
  61. };
  62. @endcode
  63. */
  64. class JUCE_API AbstractFifo
  65. {
  66. public:
  67. //==============================================================================
  68. /** Creates a FIFO to manage a buffer with the specified capacity. */
  69. AbstractFifo (int capacity) noexcept;
  70. /** Destructor */
  71. ~AbstractFifo();
  72. //==============================================================================
  73. /** Returns the total size of the buffer being managed. */
  74. int getTotalSize() const noexcept;
  75. /** Returns the number of items that can currently be added to the buffer without it overflowing. */
  76. int getFreeSpace() const noexcept;
  77. /** Returns the number of items that can currently be read from the buffer. */
  78. int getNumReady() const noexcept;
  79. /** Clears the buffer positions, so that it appears empty. */
  80. void reset() noexcept;
  81. /** Changes the buffer's total size.
  82. Note that this isn't thread-safe, so don't call it if there's any danger that it
  83. might overlap with a call to any other method in this class!
  84. */
  85. void setTotalSize (int newSize) noexcept;
  86. //==============================================================================
  87. /** Returns the location within the buffer at which an incoming block of data should be written.
  88. Because the section of data that you want to add to the buffer may overlap the end
  89. and wrap around to the start, two blocks within your buffer are returned, and you
  90. should copy your data into the first one, with any remaining data spilling over into
  91. the second.
  92. If the number of items you ask for is too large to fit within the buffer's free space, then
  93. blockSize1 + blockSize2 may add up to a lower value than numToWrite. If this happens, you
  94. may decide to keep waiting and re-trying the method until there's enough space available.
  95. After calling this method, if you choose to write your data into the blocks returned, you
  96. must call finishedWrite() to tell the FIFO how much data you actually added.
  97. e.g.
  98. @code
  99. void addToFifo (const int* someData, int numItems)
  100. {
  101. int start1, size1, start2, size2;
  102. prepareToWrite (numItems, start1, size1, start2, size2);
  103. if (size1 > 0)
  104. copySomeData (myBuffer + start1, someData, size1);
  105. if (size2 > 0)
  106. copySomeData (myBuffer + start2, someData + size1, size2);
  107. finishedWrite (size1 + size2);
  108. }
  109. @endcode
  110. @param numToWrite indicates how many items you'd like to add to the buffer
  111. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  112. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  113. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  114. the first block should be written
  115. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  116. @see finishedWrite
  117. */
  118. void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  119. /** Called after writing from the FIFO, to indicate that this many items have been added.
  120. @see prepareToWrite
  121. */
  122. void finishedWrite (int numWritten) noexcept;
  123. /** Returns the location within the buffer from which the next block of data should be read.
  124. Because the section of data that you want to read from the buffer may overlap the end
  125. and wrap around to the start, two blocks within your buffer are returned, and you
  126. should read from both of them.
  127. If the number of items you ask for is greater than the amount of data available, then
  128. blockSize1 + blockSize2 may add up to a lower value than numWanted. If this happens, you
  129. may decide to keep waiting and re-trying the method until there's enough data available.
  130. After calling this method, if you choose to read the data, you must call finishedRead() to
  131. tell the FIFO how much data you have consumed.
  132. e.g.
  133. @code
  134. void readFromFifo (int* someData, int numItems)
  135. {
  136. int start1, size1, start2, size2;
  137. prepareToRead (numSamples, start1, size1, start2, size2);
  138. if (size1 > 0)
  139. copySomeData (someData, myBuffer + start1, size1);
  140. if (size2 > 0)
  141. copySomeData (someData + size1, myBuffer + start2, size2);
  142. finishedRead (size1 + size2);
  143. }
  144. @endcode
  145. @param numWanted indicates how many items you'd like to add to the buffer
  146. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  147. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  148. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  149. the first block should be written
  150. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  151. @see finishedRead
  152. */
  153. void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  154. /** Called after reading from the FIFO, to indicate that this many items have now been consumed.
  155. @see prepareToRead
  156. */
  157. void finishedRead (int numRead) noexcept;
  158. private:
  159. //==============================================================================
  160. int bufferSize;
  161. Atomic <int> validStart, validEnd;
  162. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo);
  163. };
  164. #endif // __JUCE_ABSTRACTFIFO_JUCEHEADER__