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.

315 lines
11KB

  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. namespace juce
  18. {
  19. BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* s,
  20. TimeSliceThread& thread,
  21. const bool deleteSourceWhenDeleted,
  22. const int bufferSizeSamples,
  23. const int numChannels,
  24. bool prefillBufferOnPrepareToPlay)
  25. : source (s, deleteSourceWhenDeleted),
  26. backgroundThread (thread),
  27. numberOfSamplesToBuffer (jmax (1024, bufferSizeSamples)),
  28. numberOfChannels (numChannels),
  29. bufferValidStart (0),
  30. bufferValidEnd (0),
  31. nextPlayPos (0),
  32. sampleRate (0),
  33. wasSourceLooping (false),
  34. isPrepared (false),
  35. prefillBuffer (prefillBufferOnPrepareToPlay)
  36. {
  37. jassert (source != nullptr);
  38. jassert (numberOfSamplesToBuffer > 1024); // not much point using this class if you're
  39. // not using a larger buffer..
  40. }
  41. BufferingAudioSource::~BufferingAudioSource()
  42. {
  43. releaseResources();
  44. }
  45. //==============================================================================
  46. void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  47. {
  48. const int bufferSizeNeeded = jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer);
  49. if (newSampleRate != sampleRate
  50. || bufferSizeNeeded != buffer.getNumSamples()
  51. || ! isPrepared)
  52. {
  53. backgroundThread.removeTimeSliceClient (this);
  54. isPrepared = true;
  55. sampleRate = newSampleRate;
  56. source->prepareToPlay (samplesPerBlockExpected, newSampleRate);
  57. buffer.setSize (numberOfChannels, bufferSizeNeeded);
  58. buffer.clear();
  59. bufferValidStart = 0;
  60. bufferValidEnd = 0;
  61. backgroundThread.addTimeSliceClient (this);
  62. do
  63. {
  64. backgroundThread.moveToFrontOfQueue (this);
  65. Thread::sleep (5);
  66. }
  67. while (prefillBuffer
  68. && (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, buffer.getNumSamples() / 2)));
  69. }
  70. }
  71. void BufferingAudioSource::releaseResources()
  72. {
  73. isPrepared = false;
  74. backgroundThread.removeTimeSliceClient (this);
  75. buffer.setSize (numberOfChannels, 0);
  76. // MSVC2015 seems to need this if statement to not generate a warning during linking.
  77. // As source is set in the constructor, there is no way that source could
  78. // ever equal this, but it seems to make MSVC2015 happy.
  79. if (source != this)
  80. source->releaseResources();
  81. }
  82. void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  83. {
  84. const ScopedLock sl (bufferStartPosLock);
  85. const int validStart = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  86. const int validEnd = (int) (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  87. if (validStart == validEnd)
  88. {
  89. // total cache miss
  90. info.clearActiveBufferRegion();
  91. }
  92. else
  93. {
  94. if (validStart > 0)
  95. info.buffer->clear (info.startSample, validStart); // partial cache miss at start
  96. if (validEnd < info.numSamples)
  97. info.buffer->clear (info.startSample + validEnd,
  98. info.numSamples - validEnd); // partial cache miss at end
  99. if (validStart < validEnd)
  100. {
  101. for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
  102. {
  103. jassert (buffer.getNumSamples() > 0);
  104. const int startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
  105. const int endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
  106. if (startBufferIndex < endBufferIndex)
  107. {
  108. info.buffer->copyFrom (chan, info.startSample + validStart,
  109. buffer,
  110. chan, startBufferIndex,
  111. validEnd - validStart);
  112. }
  113. else
  114. {
  115. const int initialSize = buffer.getNumSamples() - startBufferIndex;
  116. info.buffer->copyFrom (chan, info.startSample + validStart,
  117. buffer,
  118. chan, startBufferIndex,
  119. initialSize);
  120. info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
  121. buffer,
  122. chan, 0,
  123. (validEnd - validStart) - initialSize);
  124. }
  125. }
  126. }
  127. nextPlayPos += info.numSamples;
  128. }
  129. }
  130. bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout)
  131. {
  132. if (!source || source->getTotalLength() <= 0)
  133. return false;
  134. if (nextPlayPos + info.numSamples < 0)
  135. return true;
  136. if (! isLooping() && nextPlayPos > getTotalLength())
  137. return true;
  138. uint32 now = Time::getMillisecondCounter();
  139. const uint32 startTime = now;
  140. uint32 elapsed = (now >= startTime ? now - startTime
  141. : (std::numeric_limits<uint32>::max() - startTime) + now);
  142. while (elapsed <= timeout)
  143. {
  144. {
  145. const ScopedLock sl (bufferStartPosLock);
  146. const int validStart = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
  147. const int validEnd = static_cast<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos);
  148. if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples)
  149. return true;
  150. }
  151. if (elapsed < timeout && (! bufferReadyEvent.wait (static_cast<int> (timeout - elapsed))))
  152. return false;
  153. now = Time::getMillisecondCounter();
  154. elapsed = (now >= startTime ? now - startTime
  155. : (std::numeric_limits<uint32>::max() - startTime) + now);
  156. }
  157. return false;
  158. }
  159. int64 BufferingAudioSource::getNextReadPosition() const
  160. {
  161. jassert (source->getTotalLength() > 0);
  162. return (source->isLooping() && nextPlayPos > 0)
  163. ? nextPlayPos % source->getTotalLength()
  164. : nextPlayPos;
  165. }
  166. void BufferingAudioSource::setNextReadPosition (int64 newPosition)
  167. {
  168. const ScopedLock sl (bufferStartPosLock);
  169. nextPlayPos = newPosition;
  170. backgroundThread.moveToFrontOfQueue (this);
  171. }
  172. bool BufferingAudioSource::readNextBufferChunk()
  173. {
  174. int64 newBVS, newBVE, sectionToReadStart, sectionToReadEnd;
  175. {
  176. const ScopedLock sl (bufferStartPosLock);
  177. if (wasSourceLooping != isLooping())
  178. {
  179. wasSourceLooping = isLooping();
  180. bufferValidStart = 0;
  181. bufferValidEnd = 0;
  182. }
  183. newBVS = jmax ((int64) 0, nextPlayPos);
  184. newBVE = newBVS + buffer.getNumSamples() - 4;
  185. sectionToReadStart = 0;
  186. sectionToReadEnd = 0;
  187. const int maxChunkSize = 2048;
  188. if (newBVS < bufferValidStart || newBVS >= bufferValidEnd)
  189. {
  190. newBVE = jmin (newBVE, newBVS + maxChunkSize);
  191. sectionToReadStart = newBVS;
  192. sectionToReadEnd = newBVE;
  193. bufferValidStart = 0;
  194. bufferValidEnd = 0;
  195. }
  196. else if (std::abs ((int) (newBVS - bufferValidStart)) > 512
  197. || std::abs ((int) (newBVE - bufferValidEnd)) > 512)
  198. {
  199. newBVE = jmin (newBVE, bufferValidEnd + maxChunkSize);
  200. sectionToReadStart = bufferValidEnd;
  201. sectionToReadEnd = newBVE;
  202. bufferValidStart = newBVS;
  203. bufferValidEnd = jmin (bufferValidEnd, newBVE);
  204. }
  205. }
  206. if (sectionToReadStart == sectionToReadEnd)
  207. return false;
  208. jassert (buffer.getNumSamples() > 0);
  209. const int bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
  210. const int bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());
  211. if (bufferIndexStart < bufferIndexEnd)
  212. {
  213. readBufferSection (sectionToReadStart,
  214. (int) (sectionToReadEnd - sectionToReadStart),
  215. bufferIndexStart);
  216. }
  217. else
  218. {
  219. const int initialSize = buffer.getNumSamples() - bufferIndexStart;
  220. readBufferSection (sectionToReadStart,
  221. initialSize,
  222. bufferIndexStart);
  223. readBufferSection (sectionToReadStart + initialSize,
  224. (int) (sectionToReadEnd - sectionToReadStart) - initialSize,
  225. 0);
  226. }
  227. {
  228. const ScopedLock sl2 (bufferStartPosLock);
  229. bufferValidStart = newBVS;
  230. bufferValidEnd = newBVE;
  231. }
  232. bufferReadyEvent.signal();
  233. return true;
  234. }
  235. void BufferingAudioSource::readBufferSection (const int64 start, const int length, const int bufferOffset)
  236. {
  237. if (source->getNextReadPosition() != start)
  238. source->setNextReadPosition (start);
  239. AudioSourceChannelInfo info (&buffer, bufferOffset, length);
  240. source->getNextAudioBlock (info);
  241. }
  242. int BufferingAudioSource::useTimeSlice()
  243. {
  244. return readNextBufferChunk() ? 1 : 100;
  245. }
  246. } // namespace juce