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.

238 lines
7.0KB

  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. AbstractFifo::AbstractFifo (const int capacity) noexcept
  24. : bufferSize (capacity)
  25. {
  26. jassert (bufferSize > 0);
  27. }
  28. AbstractFifo::~AbstractFifo() {}
  29. int AbstractFifo::getTotalSize() const noexcept { return bufferSize; }
  30. int AbstractFifo::getFreeSpace() const noexcept { return bufferSize - getNumReady() - 1; }
  31. int AbstractFifo::getNumReady() const noexcept
  32. {
  33. const int vs = validStart.get();
  34. const int ve = validEnd.get();
  35. return ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  36. }
  37. void AbstractFifo::reset() noexcept
  38. {
  39. validEnd = 0;
  40. validStart = 0;
  41. }
  42. void AbstractFifo::setTotalSize (int newSize) noexcept
  43. {
  44. jassert (newSize > 0);
  45. reset();
  46. bufferSize = newSize;
  47. }
  48. //==============================================================================
  49. void AbstractFifo::prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  50. {
  51. const int vs = validStart.get();
  52. const int ve = validEnd.value;
  53. const int freeSpace = ve >= vs ? (bufferSize - (ve - vs)) : (vs - ve);
  54. numToWrite = jmin (numToWrite, freeSpace - 1);
  55. if (numToWrite <= 0)
  56. {
  57. startIndex1 = 0;
  58. startIndex2 = 0;
  59. blockSize1 = 0;
  60. blockSize2 = 0;
  61. }
  62. else
  63. {
  64. startIndex1 = ve;
  65. startIndex2 = 0;
  66. blockSize1 = jmin (bufferSize - ve, numToWrite);
  67. numToWrite -= blockSize1;
  68. blockSize2 = numToWrite <= 0 ? 0 : jmin (numToWrite, vs);
  69. }
  70. }
  71. void AbstractFifo::finishedWrite (int numWritten) noexcept
  72. {
  73. jassert (numWritten >= 0 && numWritten < bufferSize);
  74. int newEnd = validEnd.value + numWritten;
  75. if (newEnd >= bufferSize)
  76. newEnd -= bufferSize;
  77. validEnd = newEnd;
  78. }
  79. void AbstractFifo::prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  80. {
  81. const int vs = validStart.value;
  82. const int ve = validEnd.get();
  83. const int numReady = ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  84. numWanted = jmin (numWanted, numReady);
  85. if (numWanted <= 0)
  86. {
  87. startIndex1 = 0;
  88. startIndex2 = 0;
  89. blockSize1 = 0;
  90. blockSize2 = 0;
  91. }
  92. else
  93. {
  94. startIndex1 = vs;
  95. startIndex2 = 0;
  96. blockSize1 = jmin (bufferSize - vs, numWanted);
  97. numWanted -= blockSize1;
  98. blockSize2 = numWanted <= 0 ? 0 : jmin (numWanted, ve);
  99. }
  100. }
  101. void AbstractFifo::finishedRead (int numRead) noexcept
  102. {
  103. jassert (numRead >= 0 && numRead <= bufferSize);
  104. int newStart = validStart.value + numRead;
  105. if (newStart >= bufferSize)
  106. newStart -= bufferSize;
  107. validStart = newStart;
  108. }
  109. //==============================================================================
  110. //==============================================================================
  111. #if JUCE_UNIT_TESTS
  112. class AbstractFifoTests : public UnitTest
  113. {
  114. public:
  115. AbstractFifoTests() : UnitTest ("Abstract Fifo") {}
  116. class WriteThread : public Thread
  117. {
  118. public:
  119. WriteThread (AbstractFifo& f, int* b, Random rng)
  120. : Thread ("fifo writer"), fifo (f), buffer (b), random (rng)
  121. {
  122. startThread();
  123. }
  124. ~WriteThread()
  125. {
  126. stopThread (5000);
  127. }
  128. void run()
  129. {
  130. int n = 0;
  131. while (! threadShouldExit())
  132. {
  133. int num = random.nextInt (2000) + 1;
  134. int start1, size1, start2, size2;
  135. fifo.prepareToWrite (num, start1, size1, start2, size2);
  136. jassert (size1 >= 0 && size2 >= 0);
  137. jassert (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()));
  138. jassert (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize()));
  139. for (int i = 0; i < size1; ++i)
  140. buffer [start1 + i] = n++;
  141. for (int i = 0; i < size2; ++i)
  142. buffer [start2 + i] = n++;
  143. fifo.finishedWrite (size1 + size2);
  144. }
  145. }
  146. private:
  147. AbstractFifo& fifo;
  148. int* buffer;
  149. Random random;
  150. };
  151. void runTest() override
  152. {
  153. beginTest ("AbstractFifo");
  154. int buffer [5000];
  155. AbstractFifo fifo (numElementsInArray (buffer));
  156. WriteThread writer (fifo, buffer, getRandom());
  157. int n = 0;
  158. Random r = getRandom();
  159. r.combineSeed (12345);
  160. for (int count = 100000; --count >= 0;)
  161. {
  162. int num = r.nextInt (6000) + 1;
  163. int start1, size1, start2, size2;
  164. fifo.prepareToRead (num, start1, size1, start2, size2);
  165. if (! (size1 >= 0 && size2 >= 0)
  166. && (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()))
  167. && (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize())))
  168. {
  169. expect (false, "prepareToRead returned -ve values");
  170. break;
  171. }
  172. bool failed = false;
  173. for (int i = 0; i < size1; ++i)
  174. failed = (buffer [start1 + i] != n++) || failed;
  175. for (int i = 0; i < size2; ++i)
  176. failed = (buffer [start2 + i] != n++) || failed;
  177. if (failed)
  178. {
  179. expect (false, "read values were incorrect");
  180. break;
  181. }
  182. fifo.finishedRead (size1 + size2);
  183. }
  184. }
  185. };
  186. static AbstractFifoTests fifoUnitTests;
  187. #endif