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.

230 lines
6.5KB

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