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.

juce_AudioTransportSource.cpp 8.5KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. AudioTransportSource::AudioTransportSource()
  18. : source (nullptr),
  19. resamplerSource (nullptr),
  20. bufferingSource (nullptr),
  21. positionableSource (nullptr),
  22. masterSource (nullptr),
  23. gain (1.0f),
  24. lastGain (1.0f),
  25. playing (false),
  26. stopped (true),
  27. sampleRate (44100.0),
  28. sourceSampleRate (0.0),
  29. blockSize (128),
  30. readAheadBufferSize (0),
  31. isPrepared (false),
  32. inputStreamEOF (false)
  33. {
  34. }
  35. AudioTransportSource::~AudioTransportSource()
  36. {
  37. setSource (nullptr);
  38. releaseMasterResources();
  39. }
  40. void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
  41. int readAheadSize, TimeSliceThread* readAheadThread,
  42. double sourceSampleRateToCorrectFor, int maxNumChannels)
  43. {
  44. if (source == newSource)
  45. {
  46. if (source == nullptr)
  47. return;
  48. setSource (nullptr, 0, nullptr); // deselect and reselect to avoid releasing resources wrongly
  49. }
  50. readAheadBufferSize = readAheadSize;
  51. sourceSampleRate = sourceSampleRateToCorrectFor;
  52. ResamplingAudioSource* newResamplerSource = nullptr;
  53. BufferingAudioSource* newBufferingSource = nullptr;
  54. PositionableAudioSource* newPositionableSource = nullptr;
  55. AudioSource* newMasterSource = nullptr;
  56. ScopedPointer<ResamplingAudioSource> oldResamplerSource (resamplerSource);
  57. ScopedPointer<BufferingAudioSource> oldBufferingSource (bufferingSource);
  58. AudioSource* oldMasterSource = masterSource;
  59. if (newSource != nullptr)
  60. {
  61. newPositionableSource = newSource;
  62. if (readAheadSize > 0)
  63. {
  64. // If you want to use a read-ahead buffer, you must also provide a TimeSliceThread
  65. // for it to use!
  66. jassert (readAheadThread != nullptr);
  67. newPositionableSource = newBufferingSource
  68. = new BufferingAudioSource (newPositionableSource, *readAheadThread,
  69. false, readAheadSize, maxNumChannels);
  70. }
  71. newPositionableSource->setNextReadPosition (0);
  72. if (sourceSampleRateToCorrectFor > 0)
  73. newMasterSource = newResamplerSource
  74. = new ResamplingAudioSource (newPositionableSource, false, maxNumChannels);
  75. else
  76. newMasterSource = newPositionableSource;
  77. if (isPrepared)
  78. {
  79. if (newResamplerSource != nullptr && sourceSampleRate > 0 && sampleRate > 0)
  80. newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  81. newMasterSource->prepareToPlay (blockSize, sampleRate);
  82. }
  83. }
  84. {
  85. const ScopedLock sl (callbackLock);
  86. source = newSource;
  87. resamplerSource = newResamplerSource;
  88. bufferingSource = newBufferingSource;
  89. masterSource = newMasterSource;
  90. positionableSource = newPositionableSource;
  91. inputStreamEOF = false;
  92. playing = false;
  93. }
  94. if (oldMasterSource != nullptr)
  95. oldMasterSource->releaseResources();
  96. }
  97. void AudioTransportSource::start()
  98. {
  99. if ((! playing) && masterSource != nullptr)
  100. {
  101. {
  102. const ScopedLock sl (callbackLock);
  103. playing = true;
  104. stopped = false;
  105. inputStreamEOF = false;
  106. }
  107. sendChangeMessage();
  108. }
  109. }
  110. void AudioTransportSource::stop()
  111. {
  112. if (playing)
  113. {
  114. {
  115. const ScopedLock sl (callbackLock);
  116. playing = false;
  117. }
  118. int n = 500;
  119. while (--n >= 0 && ! stopped)
  120. Thread::sleep (2);
  121. sendChangeMessage();
  122. }
  123. }
  124. void AudioTransportSource::setPosition (double newPosition)
  125. {
  126. if (sampleRate > 0.0)
  127. setNextReadPosition ((int64) (newPosition * sampleRate));
  128. }
  129. double AudioTransportSource::getCurrentPosition() const
  130. {
  131. if (sampleRate > 0.0)
  132. return getNextReadPosition() / sampleRate;
  133. return 0.0;
  134. }
  135. double AudioTransportSource::getLengthInSeconds() const
  136. {
  137. if (sampleRate > 0.0)
  138. return getTotalLength() / sampleRate;
  139. return 0.0;
  140. }
  141. void AudioTransportSource::setNextReadPosition (int64 newPosition)
  142. {
  143. if (positionableSource != nullptr)
  144. {
  145. if (sampleRate > 0 && sourceSampleRate > 0)
  146. newPosition = (int64) (newPosition * sourceSampleRate / sampleRate);
  147. positionableSource->setNextReadPosition (newPosition);
  148. if (resamplerSource != nullptr)
  149. resamplerSource->flushBuffers();
  150. inputStreamEOF = false;
  151. }
  152. }
  153. int64 AudioTransportSource::getNextReadPosition() const
  154. {
  155. if (positionableSource != nullptr)
  156. {
  157. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  158. return (int64) (positionableSource->getNextReadPosition() * ratio);
  159. }
  160. return 0;
  161. }
  162. int64 AudioTransportSource::getTotalLength() const
  163. {
  164. const ScopedLock sl (callbackLock);
  165. if (positionableSource != nullptr)
  166. {
  167. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  168. return (int64) (positionableSource->getTotalLength() * ratio);
  169. }
  170. return 0;
  171. }
  172. bool AudioTransportSource::isLooping() const
  173. {
  174. const ScopedLock sl (callbackLock);
  175. return positionableSource != nullptr && positionableSource->isLooping();
  176. }
  177. void AudioTransportSource::setGain (const float newGain) noexcept
  178. {
  179. gain = newGain;
  180. }
  181. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  182. {
  183. const ScopedLock sl (callbackLock);
  184. sampleRate = newSampleRate;
  185. blockSize = samplesPerBlockExpected;
  186. if (masterSource != nullptr)
  187. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  188. if (resamplerSource != nullptr && sourceSampleRate > 0)
  189. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  190. inputStreamEOF = false;
  191. isPrepared = true;
  192. }
  193. void AudioTransportSource::releaseMasterResources()
  194. {
  195. const ScopedLock sl (callbackLock);
  196. if (masterSource != nullptr)
  197. masterSource->releaseResources();
  198. isPrepared = false;
  199. }
  200. void AudioTransportSource::releaseResources()
  201. {
  202. releaseMasterResources();
  203. }
  204. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  205. {
  206. const ScopedLock sl (callbackLock);
  207. if (masterSource != nullptr && ! stopped)
  208. {
  209. masterSource->getNextAudioBlock (info);
  210. if (! playing)
  211. {
  212. // just stopped playing, so fade out the last block..
  213. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  214. info.buffer->applyGainRamp (i, info.startSample, jmin (256, info.numSamples), 1.0f, 0.0f);
  215. if (info.numSamples > 256)
  216. info.buffer->clear (info.startSample + 256, info.numSamples - 256);
  217. }
  218. if (positionableSource->getNextReadPosition() > positionableSource->getTotalLength() + 1
  219. && ! positionableSource->isLooping())
  220. {
  221. playing = false;
  222. inputStreamEOF = true;
  223. sendChangeMessage();
  224. }
  225. stopped = ! playing;
  226. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  227. info.buffer->applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  228. }
  229. else
  230. {
  231. info.clearActiveBufferRegion();
  232. stopped = true;
  233. }
  234. lastGain = gain;
  235. }