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.

295 lines
8.4KB

  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. inputStreamEOF = false;
  149. }
  150. }
  151. int64 AudioTransportSource::getNextReadPosition() const
  152. {
  153. if (positionableSource != nullptr)
  154. {
  155. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  156. return (int64) (positionableSource->getNextReadPosition() * ratio);
  157. }
  158. return 0;
  159. }
  160. int64 AudioTransportSource::getTotalLength() const
  161. {
  162. const ScopedLock sl (callbackLock);
  163. if (positionableSource != nullptr)
  164. {
  165. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  166. return (int64) (positionableSource->getTotalLength() * ratio);
  167. }
  168. return 0;
  169. }
  170. bool AudioTransportSource::isLooping() const
  171. {
  172. const ScopedLock sl (callbackLock);
  173. return positionableSource != nullptr && positionableSource->isLooping();
  174. }
  175. void AudioTransportSource::setGain (const float newGain) noexcept
  176. {
  177. gain = newGain;
  178. }
  179. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  180. {
  181. const ScopedLock sl (callbackLock);
  182. sampleRate = newSampleRate;
  183. blockSize = samplesPerBlockExpected;
  184. if (masterSource != nullptr)
  185. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  186. if (resamplerSource != nullptr && sourceSampleRate > 0)
  187. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  188. inputStreamEOF = false;
  189. isPrepared = true;
  190. }
  191. void AudioTransportSource::releaseMasterResources()
  192. {
  193. const ScopedLock sl (callbackLock);
  194. if (masterSource != nullptr)
  195. masterSource->releaseResources();
  196. isPrepared = false;
  197. }
  198. void AudioTransportSource::releaseResources()
  199. {
  200. releaseMasterResources();
  201. }
  202. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  203. {
  204. const ScopedLock sl (callbackLock);
  205. if (masterSource != nullptr && ! stopped)
  206. {
  207. masterSource->getNextAudioBlock (info);
  208. if (! playing)
  209. {
  210. // just stopped playing, so fade out the last block..
  211. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  212. info.buffer->applyGainRamp (i, info.startSample, jmin (256, info.numSamples), 1.0f, 0.0f);
  213. if (info.numSamples > 256)
  214. info.buffer->clear (info.startSample + 256, info.numSamples - 256);
  215. }
  216. if (positionableSource->getNextReadPosition() > positionableSource->getTotalLength() + 1
  217. && ! positionableSource->isLooping())
  218. {
  219. playing = false;
  220. inputStreamEOF = true;
  221. sendChangeMessage();
  222. }
  223. stopped = ! playing;
  224. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  225. info.buffer->applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  226. }
  227. else
  228. {
  229. info.clearActiveBufferRegion();
  230. stopped = true;
  231. }
  232. lastGain = gain;
  233. }