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.

213 lines
8.7KB

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