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.

300 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* s,
  18. TimeSliceThread& thread,
  19. const bool deleteSourceWhenDeleted,
  20. const int bufferSizeSamples,
  21. const int numChannels,
  22. bool prefillBufferOnPrepareToPlay)
  23. : source (s, deleteSourceWhenDeleted),
  24. backgroundThread (thread),
  25. numberOfSamplesToBuffer (jmax (1024, bufferSizeSamples)),
  26. numberOfChannels (numChannels),
  27. bufferValidStart (0),
  28. bufferValidEnd (0),
  29. nextPlayPos (0),
  30. sampleRate (0),
  31. wasSourceLooping (false),
  32. isPrepared (false),
  33. prefillBuffer (prefillBufferOnPrepareToPlay)
  34. {
  35. jassert (source != nullptr);
  36. jassert (numberOfSamplesToBuffer > 1024); // not much point using this class if you're
  37. // not using a larger buffer..
  38. }
  39. BufferingAudioSource::~BufferingAudioSource()
  40. {
  41. releaseResources();
  42. }
  43. //==============================================================================
  44. void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  45. {
  46. const int bufferSizeNeeded = jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer);
  47. if (newSampleRate != sampleRate
  48. || bufferSizeNeeded != buffer.getNumSamples()
  49. || ! isPrepared)
  50. {
  51. backgroundThread.removeTimeSliceClient (this);
  52. isPrepared = true;
  53. sampleRate = newSampleRate;
  54. source->prepareToPlay (samplesPerBlockExpected, newSampleRate);
  55. buffer.setSize (numberOfChannels, bufferSizeNeeded);
  56. buffer.clear();
  57. bufferValidStart = 0;
  58. bufferValidEnd = 0;
  59. backgroundThread.addTimeSliceClient (this);
  60. do
  61. {
  62. backgroundThread.moveToFrontOfQueue (this);
  63. Thread::sleep (5);
  64. }
  65. while (prefillBuffer
  66. && (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, buffer.getNumSamples() / 2)));
  67. }
  68. }
  69. void BufferingAudioSource::releaseResources()
  70. {
  71. isPrepared = false;
  72. backgroundThread.removeTimeSliceClient (this);
  73. buffer.setSize (numberOfChannels, 0);
  74. source->releaseResources();
  75. }
  76. void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  77. {
  78. const ScopedLock sl (bufferStartPosLock);
  79. const int validStart = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  80. const int validEnd = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  81. if (validStart == validEnd)
  82. {
  83. // total cache miss
  84. info.clearActiveBufferRegion();
  85. }
  86. else
  87. {
  88. if (validStart > 0)
  89. info.buffer->clear (info.startSample, validStart); // partial cache miss at start
  90. if (validEnd < info.numSamples)
  91. info.buffer->clear (info.startSample + validEnd,
  92. info.numSamples - validEnd); // partial cache miss at end
  93. if (validStart < validEnd)
  94. {
  95. for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
  96. {
  97. jassert (buffer.getNumSamples() > 0);
  98. const int startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
  99. const int endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
  100. if (startBufferIndex < endBufferIndex)
  101. {
  102. info.buffer->copyFrom (chan, info.startSample + validStart,
  103. buffer,
  104. chan, startBufferIndex,
  105. validEnd - validStart);
  106. }
  107. else
  108. {
  109. const int initialSize = buffer.getNumSamples() - startBufferIndex;
  110. info.buffer->copyFrom (chan, info.startSample + validStart,
  111. buffer,
  112. chan, startBufferIndex,
  113. initialSize);
  114. info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
  115. buffer,
  116. chan, 0,
  117. (validEnd - validStart) - initialSize);
  118. }
  119. }
  120. }
  121. nextPlayPos += info.numSamples;
  122. }
  123. }
  124. bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout)
  125. {
  126. if (!source || source->getTotalLength() <= 0)
  127. return false;
  128. if (nextPlayPos + info.numSamples < 0)
  129. return true;
  130. if (! isLooping() && nextPlayPos > getTotalLength())
  131. return true;
  132. const uint32 endTime = Time::getMillisecondCounter () + timeout;
  133. uint32 now = Time::getMillisecondCounter();
  134. while (now < endTime)
  135. {
  136. {
  137. const ScopedLock sl (bufferStartPosLock);
  138. const int validStart = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  139. const int validEnd = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  140. if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples)
  141. return true;
  142. }
  143. if (! bufferReadyEvent.wait (static_cast<int> (endTime - now)))
  144. return false;
  145. now = Time::getMillisecondCounter();
  146. }
  147. return false;
  148. }
  149. int64 BufferingAudioSource::getNextReadPosition() const
  150. {
  151. jassert (source->getTotalLength() > 0);
  152. return (source->isLooping() && nextPlayPos > 0)
  153. ? nextPlayPos % source->getTotalLength()
  154. : nextPlayPos;
  155. }
  156. void BufferingAudioSource::setNextReadPosition (int64 newPosition)
  157. {
  158. const ScopedLock sl (bufferStartPosLock);
  159. nextPlayPos = newPosition;
  160. backgroundThread.moveToFrontOfQueue (this);
  161. }
  162. bool BufferingAudioSource::readNextBufferChunk()
  163. {
  164. int64 newBVS, newBVE, sectionToReadStart, sectionToReadEnd;
  165. {
  166. const ScopedLock sl (bufferStartPosLock);
  167. if (wasSourceLooping != isLooping())
  168. {
  169. wasSourceLooping = isLooping();
  170. bufferValidStart = 0;
  171. bufferValidEnd = 0;
  172. }
  173. newBVS = jmax ((int64) 0, nextPlayPos);
  174. newBVE = newBVS + buffer.getNumSamples() - 4;
  175. sectionToReadStart = 0;
  176. sectionToReadEnd = 0;
  177. const int maxChunkSize = 2048;
  178. if (newBVS < bufferValidStart || newBVS >= bufferValidEnd)
  179. {
  180. newBVE = jmin (newBVE, newBVS + maxChunkSize);
  181. sectionToReadStart = newBVS;
  182. sectionToReadEnd = newBVE;
  183. bufferValidStart = 0;
  184. bufferValidEnd = 0;
  185. }
  186. else if (std::abs ((int) (newBVS - bufferValidStart)) > 512
  187. || std::abs ((int) (newBVE - bufferValidEnd)) > 512)
  188. {
  189. newBVE = jmin (newBVE, bufferValidEnd + maxChunkSize);
  190. sectionToReadStart = bufferValidEnd;
  191. sectionToReadEnd = newBVE;
  192. bufferValidStart = newBVS;
  193. bufferValidEnd = jmin (bufferValidEnd, newBVE);
  194. }
  195. }
  196. if (sectionToReadStart == sectionToReadEnd)
  197. return false;
  198. jassert (buffer.getNumSamples() > 0);
  199. const int bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
  200. const int bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());
  201. if (bufferIndexStart < bufferIndexEnd)
  202. {
  203. readBufferSection (sectionToReadStart,
  204. (int) (sectionToReadEnd - sectionToReadStart),
  205. bufferIndexStart);
  206. }
  207. else
  208. {
  209. const int initialSize = buffer.getNumSamples() - bufferIndexStart;
  210. readBufferSection (sectionToReadStart,
  211. initialSize,
  212. bufferIndexStart);
  213. readBufferSection (sectionToReadStart + initialSize,
  214. (int) (sectionToReadEnd - sectionToReadStart) - initialSize,
  215. 0);
  216. }
  217. {
  218. const ScopedLock sl2 (bufferStartPosLock);
  219. bufferValidStart = newBVS;
  220. bufferValidEnd = newBVE;
  221. }
  222. bufferReadyEvent.signal();
  223. return true;
  224. }
  225. void BufferingAudioSource::readBufferSection (const int64 start, const int length, const int bufferOffset)
  226. {
  227. if (source->getNextReadPosition() != start)
  228. source->setNextReadPosition (start);
  229. AudioSourceChannelInfo info (&buffer, bufferOffset, length);
  230. source->getNextAudioBlock (info);
  231. }
  232. int BufferingAudioSource::useTimeSlice()
  233. {
  234. return readNextBufferChunk() ? 1 : 100;
  235. }