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.

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