Audio plugin host https://kx.studio/carla
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.

305 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. // MSVC2015 seems to need this if statement to not generate a warning during linking.
  75. // As source is set in the constructor, there is no way that source could
  76. // ever equal this, but it seems to make MSVC2015 happy.
  77. if (source != this)
  78. source->releaseResources();
  79. }
  80. void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  81. {
  82. const ScopedLock sl (bufferStartPosLock);
  83. const int validStart = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  84. const int validEnd = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  85. if (validStart == validEnd)
  86. {
  87. // total cache miss
  88. info.clearActiveBufferRegion();
  89. }
  90. else
  91. {
  92. if (validStart > 0)
  93. info.buffer->clear (info.startSample, validStart); // partial cache miss at start
  94. if (validEnd < info.numSamples)
  95. info.buffer->clear (info.startSample + validEnd,
  96. info.numSamples - validEnd); // partial cache miss at end
  97. if (validStart < validEnd)
  98. {
  99. for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
  100. {
  101. jassert (buffer.getNumSamples() > 0);
  102. const int startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
  103. const int endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
  104. if (startBufferIndex < endBufferIndex)
  105. {
  106. info.buffer->copyFrom (chan, info.startSample + validStart,
  107. buffer,
  108. chan, startBufferIndex,
  109. validEnd - validStart);
  110. }
  111. else
  112. {
  113. const int initialSize = buffer.getNumSamples() - startBufferIndex;
  114. info.buffer->copyFrom (chan, info.startSample + validStart,
  115. buffer,
  116. chan, startBufferIndex,
  117. initialSize);
  118. info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
  119. buffer,
  120. chan, 0,
  121. (validEnd - validStart) - initialSize);
  122. }
  123. }
  124. }
  125. nextPlayPos += info.numSamples;
  126. }
  127. }
  128. bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout)
  129. {
  130. if (!source || source->getTotalLength() <= 0)
  131. return false;
  132. if (nextPlayPos + info.numSamples < 0)
  133. return true;
  134. if (! isLooping() && nextPlayPos > getTotalLength())
  135. return true;
  136. const uint32 endTime = Time::getMillisecondCounter() + timeout;
  137. uint32 now = Time::getMillisecondCounter();
  138. while (now < endTime)
  139. {
  140. {
  141. const ScopedLock sl (bufferStartPosLock);
  142. const int validStart = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  143. const int validEnd = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  144. if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples)
  145. return true;
  146. }
  147. if (! bufferReadyEvent.wait (static_cast<int> (endTime - now)))
  148. return false;
  149. now = Time::getMillisecondCounter();
  150. }
  151. return false;
  152. }
  153. int64 BufferingAudioSource::getNextReadPosition() const
  154. {
  155. jassert (source->getTotalLength() > 0);
  156. return (source->isLooping() && nextPlayPos > 0)
  157. ? nextPlayPos % source->getTotalLength()
  158. : nextPlayPos;
  159. }
  160. void BufferingAudioSource::setNextReadPosition (int64 newPosition)
  161. {
  162. const ScopedLock sl (bufferStartPosLock);
  163. nextPlayPos = newPosition;
  164. backgroundThread.moveToFrontOfQueue (this);
  165. }
  166. bool BufferingAudioSource::readNextBufferChunk()
  167. {
  168. int64 newBVS, newBVE, sectionToReadStart, sectionToReadEnd;
  169. {
  170. const ScopedLock sl (bufferStartPosLock);
  171. if (wasSourceLooping != isLooping())
  172. {
  173. wasSourceLooping = isLooping();
  174. bufferValidStart = 0;
  175. bufferValidEnd = 0;
  176. }
  177. newBVS = jmax ((int64) 0, nextPlayPos);
  178. newBVE = newBVS + buffer.getNumSamples() - 4;
  179. sectionToReadStart = 0;
  180. sectionToReadEnd = 0;
  181. const int maxChunkSize = 2048;
  182. if (newBVS < bufferValidStart || newBVS >= bufferValidEnd)
  183. {
  184. newBVE = jmin (newBVE, newBVS + maxChunkSize);
  185. sectionToReadStart = newBVS;
  186. sectionToReadEnd = newBVE;
  187. bufferValidStart = 0;
  188. bufferValidEnd = 0;
  189. }
  190. else if (std::abs ((int) (newBVS - bufferValidStart)) > 512
  191. || std::abs ((int) (newBVE - bufferValidEnd)) > 512)
  192. {
  193. newBVE = jmin (newBVE, bufferValidEnd + maxChunkSize);
  194. sectionToReadStart = bufferValidEnd;
  195. sectionToReadEnd = newBVE;
  196. bufferValidStart = newBVS;
  197. bufferValidEnd = jmin (bufferValidEnd, newBVE);
  198. }
  199. }
  200. if (sectionToReadStart == sectionToReadEnd)
  201. return false;
  202. jassert (buffer.getNumSamples() > 0);
  203. const int bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
  204. const int bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());
  205. if (bufferIndexStart < bufferIndexEnd)
  206. {
  207. readBufferSection (sectionToReadStart,
  208. (int) (sectionToReadEnd - sectionToReadStart),
  209. bufferIndexStart);
  210. }
  211. else
  212. {
  213. const int initialSize = buffer.getNumSamples() - bufferIndexStart;
  214. readBufferSection (sectionToReadStart,
  215. initialSize,
  216. bufferIndexStart);
  217. readBufferSection (sectionToReadStart + initialSize,
  218. (int) (sectionToReadEnd - sectionToReadStart) - initialSize,
  219. 0);
  220. }
  221. {
  222. const ScopedLock sl2 (bufferStartPosLock);
  223. bufferValidStart = newBVS;
  224. bufferValidEnd = newBVE;
  225. }
  226. bufferReadyEvent.signal();
  227. return true;
  228. }
  229. void BufferingAudioSource::readBufferSection (const int64 start, const int length, const int bufferOffset)
  230. {
  231. if (source->getNextReadPosition() != start)
  232. source->setNextReadPosition (start);
  233. AudioSourceChannelInfo info (&buffer, bufferOffset, length);
  234. source->getNextAudioBlock (info);
  235. }
  236. int BufferingAudioSource::useTimeSlice()
  237. {
  238. return readNextBufferChunk() ? 1 : 100;
  239. }