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.

324 lines
10KB

  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. bool deleteSourceWhenDeleted,
  21. int bufferSizeSamples,
  22. int numChannels,
  23. bool prefillBufferOnPrepareToPlay)
  24. : source (s, deleteSourceWhenDeleted),
  25. backgroundThread (thread),
  26. numberOfSamplesToBuffer (jmax (1024, bufferSizeSamples)),
  27. numberOfChannels (numChannels),
  28. prefillBuffer (prefillBufferOnPrepareToPlay)
  29. {
  30. jassert (source != nullptr);
  31. jassert (numberOfSamplesToBuffer > 1024); // not much point using this class if you're
  32. // not using a larger buffer..
  33. }
  34. MyBufferingAudioSource::~MyBufferingAudioSource()
  35. {
  36. releaseResources();
  37. }
  38. //==============================================================================
  39. void MyBufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  40. {
  41. auto bufferSizeNeeded = jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer);
  42. if (newSampleRate != sampleRate
  43. || bufferSizeNeeded != buffer.getNumSamples()
  44. || ! isPrepared)
  45. {
  46. backgroundThread.removeTimeSliceClient (this);
  47. isPrepared = true;
  48. sampleRate = newSampleRate;
  49. source->prepareToPlay (samplesPerBlockExpected, newSampleRate);
  50. buffer.setSize (numberOfChannels, bufferSizeNeeded);
  51. buffer.clear();
  52. bufferValidStart = 0;
  53. bufferValidEnd = 0;
  54. backgroundThread.addTimeSliceClient (this);
  55. do
  56. {
  57. backgroundThread.moveToFrontOfQueue (this);
  58. Thread::sleep (5);
  59. }
  60. while (prefillBuffer
  61. && (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, buffer.getNumSamples() / 2)));
  62. }
  63. }
  64. void MyBufferingAudioSource::releaseResources()
  65. {
  66. isPrepared = false;
  67. backgroundThread.removeTimeSliceClient (this);
  68. buffer.setSize (numberOfChannels, 0);
  69. // MSVC2015 seems to need this if statement to not generate a warning during linking.
  70. // As source is set in the constructor, there is no way that source could
  71. // ever equal this, but it seems to make MSVC2015 happy.
  72. if (source != this)
  73. source->releaseResources();
  74. }
  75. void MyBufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  76. {
  77. const ScopedLock sl (bufferStartPosLock);
  78. auto start = bufferValidStart.load();
  79. auto end = bufferValidEnd.load();
  80. auto pos = nextPlayPos.load();
  81. auto validStart = (int) (jlimit (start, end, pos) - pos);
  82. auto validEnd = (int) (jlimit (start, end, pos + info.numSamples) - pos);
  83. if (validStart == validEnd)
  84. {
  85. // total cache miss
  86. info.clearActiveBufferRegion();
  87. }
  88. else
  89. {
  90. if (validStart > 0)
  91. info.buffer->clear (info.startSample, validStart); // partial cache miss at start
  92. if (validEnd < info.numSamples)
  93. info.buffer->clear (info.startSample + validEnd,
  94. info.numSamples - validEnd); // partial cache miss at end
  95. if (validStart < validEnd)
  96. {
  97. for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
  98. {
  99. jassert (buffer.getNumSamples() > 0);
  100. auto startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
  101. auto endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
  102. if (startBufferIndex < endBufferIndex)
  103. {
  104. info.buffer->copyFrom (chan, info.startSample + validStart,
  105. buffer,
  106. chan, startBufferIndex,
  107. validEnd - validStart);
  108. }
  109. else
  110. {
  111. auto initialSize = buffer.getNumSamples() - startBufferIndex;
  112. info.buffer->copyFrom (chan, info.startSample + validStart,
  113. buffer,
  114. chan, startBufferIndex,
  115. initialSize);
  116. info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
  117. buffer,
  118. chan, 0,
  119. (validEnd - validStart) - initialSize);
  120. }
  121. }
  122. }
  123. nextPlayPos += info.numSamples;
  124. }
  125. }
  126. bool MyBufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, uint32 timeout)
  127. {
  128. if (!source || source->getTotalLength() <= 0)
  129. return false;
  130. if (nextPlayPos + info.numSamples < 0)
  131. return true;
  132. if (! isLooping() && nextPlayPos > getTotalLength())
  133. return true;
  134. auto now = Time::getMillisecondCounter();
  135. auto startTime = now;
  136. auto elapsed = (now >= startTime ? now - startTime
  137. : (std::numeric_limits<uint32>::max() - startTime) + now);
  138. while (elapsed <= timeout)
  139. {
  140. {
  141. const ScopedLock sl (bufferStartPosLock);
  142. auto start = bufferValidStart.load();
  143. auto end = bufferValidEnd.load();
  144. auto pos = nextPlayPos.load();
  145. auto validStart = static_cast<int> (jlimit (start, end, pos) - pos);
  146. auto validEnd = static_cast<int> (jlimit (start, end, pos + info.numSamples) - pos);
  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. int64 MyBufferingAudioSource::getNextReadPosition() const
  159. {
  160. jassert (source->getTotalLength() > 0);
  161. auto pos = nextPlayPos.load();
  162. return (source->isLooping() && nextPlayPos > 0)
  163. ? pos % source->getTotalLength()
  164. : pos;
  165. }
  166. void MyBufferingAudioSource::setNextReadPosition (int64 newPosition)
  167. {
  168. const ScopedLock sl (bufferStartPosLock);
  169. nextPlayPos = newPosition;
  170. backgroundThread.moveToFrontOfQueue (this);
  171. }
  172. bool MyBufferingAudioSource::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.load());
  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.load(), newBVE);
  204. }
  205. }
  206. if (sectionToReadStart == sectionToReadEnd)
  207. return false;
  208. jassert (buffer.getNumSamples() > 0);
  209. auto bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
  210. auto bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());
  211. if (bufferIndexStart < bufferIndexEnd)
  212. {
  213. readBufferSection (sectionToReadStart,
  214. (int) (sectionToReadEnd - sectionToReadStart),
  215. bufferIndexStart);
  216. }
  217. else
  218. {
  219. auto 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 MyBufferingAudioSource::readBufferSection (int64 start, int length, 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 MyBufferingAudioSource::useTimeSlice()
  243. {
  244. return readNextBufferChunk() ? 1 : 100;
  245. }
  246. double MyBufferingAudioSource::getPercentReady()
  247. {
  248. if (bufferValidEnd == bufferValidStart)
  249. return 0.0;
  250. if (numberOfSamplesToBuffer == 0)
  251. return 0.0;
  252. return 1.0 / numberOfSamplesToBuffer * (bufferValidEnd - bufferValidStart);
  253. }