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.

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