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.

301 lines
8.5KB

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