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.

305 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. AudioTransportSource::AudioTransportSource()
  24. : source (nullptr),
  25. resamplerSource (nullptr),
  26. bufferingSource (nullptr),
  27. positionableSource (nullptr),
  28. masterSource (nullptr),
  29. gain (1.0f),
  30. lastGain (1.0f),
  31. playing (false),
  32. stopped (true),
  33. sampleRate (44100.0),
  34. sourceSampleRate (0.0),
  35. blockSize (128),
  36. readAheadBufferSize (0),
  37. isPrepared (false),
  38. inputStreamEOF (false)
  39. {
  40. }
  41. AudioTransportSource::~AudioTransportSource()
  42. {
  43. setSource (nullptr);
  44. releaseMasterResources();
  45. }
  46. void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
  47. int readAheadSize, TimeSliceThread* readAheadThread,
  48. double sourceSampleRateToCorrectFor, int maxNumChannels)
  49. {
  50. if (source == newSource)
  51. {
  52. if (source == nullptr)
  53. return;
  54. setSource (nullptr, 0, nullptr); // deselect and reselect to avoid releasing resources wrongly
  55. }
  56. readAheadBufferSize = readAheadSize;
  57. sourceSampleRate = sourceSampleRateToCorrectFor;
  58. ResamplingAudioSource* newResamplerSource = nullptr;
  59. BufferingAudioSource* newBufferingSource = nullptr;
  60. PositionableAudioSource* newPositionableSource = nullptr;
  61. AudioSource* newMasterSource = nullptr;
  62. ScopedPointer<ResamplingAudioSource> oldResamplerSource (resamplerSource);
  63. ScopedPointer<BufferingAudioSource> oldBufferingSource (bufferingSource);
  64. AudioSource* oldMasterSource = masterSource;
  65. if (newSource != nullptr)
  66. {
  67. newPositionableSource = newSource;
  68. if (readAheadSize > 0)
  69. {
  70. // If you want to use a read-ahead buffer, you must also provide a TimeSliceThread
  71. // for it to use!
  72. jassert (readAheadThread != nullptr);
  73. newPositionableSource = newBufferingSource
  74. = new BufferingAudioSource (newPositionableSource, *readAheadThread,
  75. false, readAheadSize, maxNumChannels);
  76. }
  77. newPositionableSource->setNextReadPosition (0);
  78. if (sourceSampleRateToCorrectFor > 0)
  79. newMasterSource = newResamplerSource
  80. = new ResamplingAudioSource (newPositionableSource, false, maxNumChannels);
  81. else
  82. newMasterSource = newPositionableSource;
  83. if (isPrepared)
  84. {
  85. if (newResamplerSource != nullptr && sourceSampleRate > 0 && sampleRate > 0)
  86. newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  87. newMasterSource->prepareToPlay (blockSize, sampleRate);
  88. }
  89. }
  90. {
  91. const ScopedLock sl (callbackLock);
  92. source = newSource;
  93. resamplerSource = newResamplerSource;
  94. bufferingSource = newBufferingSource;
  95. masterSource = newMasterSource;
  96. positionableSource = newPositionableSource;
  97. inputStreamEOF = false;
  98. playing = false;
  99. }
  100. if (oldMasterSource != nullptr)
  101. oldMasterSource->releaseResources();
  102. }
  103. void AudioTransportSource::start()
  104. {
  105. if ((! playing) && masterSource != nullptr)
  106. {
  107. {
  108. const ScopedLock sl (callbackLock);
  109. playing = true;
  110. stopped = false;
  111. inputStreamEOF = false;
  112. }
  113. sendChangeMessage();
  114. }
  115. }
  116. void AudioTransportSource::stop()
  117. {
  118. if (playing)
  119. {
  120. {
  121. const ScopedLock sl (callbackLock);
  122. playing = false;
  123. }
  124. int n = 500;
  125. while (--n >= 0 && ! stopped)
  126. Thread::sleep (2);
  127. sendChangeMessage();
  128. }
  129. }
  130. void AudioTransportSource::setPosition (double newPosition)
  131. {
  132. if (sampleRate > 0.0)
  133. setNextReadPosition ((int64) (newPosition * sampleRate));
  134. }
  135. double AudioTransportSource::getCurrentPosition() const
  136. {
  137. if (sampleRate > 0.0)
  138. return getNextReadPosition() / sampleRate;
  139. return 0.0;
  140. }
  141. double AudioTransportSource::getLengthInSeconds() const
  142. {
  143. if (sampleRate > 0.0)
  144. return getTotalLength() / sampleRate;
  145. return 0.0;
  146. }
  147. void AudioTransportSource::setNextReadPosition (int64 newPosition)
  148. {
  149. if (positionableSource != nullptr)
  150. {
  151. if (sampleRate > 0 && sourceSampleRate > 0)
  152. newPosition = (int64) (newPosition * sourceSampleRate / sampleRate);
  153. positionableSource->setNextReadPosition (newPosition);
  154. if (resamplerSource != nullptr)
  155. resamplerSource->flushBuffers();
  156. inputStreamEOF = false;
  157. }
  158. }
  159. int64 AudioTransportSource::getNextReadPosition() const
  160. {
  161. if (positionableSource != nullptr)
  162. {
  163. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  164. return (int64) (positionableSource->getNextReadPosition() * ratio);
  165. }
  166. return 0;
  167. }
  168. int64 AudioTransportSource::getTotalLength() const
  169. {
  170. const ScopedLock sl (callbackLock);
  171. if (positionableSource != nullptr)
  172. {
  173. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  174. return (int64) (positionableSource->getTotalLength() * ratio);
  175. }
  176. return 0;
  177. }
  178. bool AudioTransportSource::isLooping() const
  179. {
  180. const ScopedLock sl (callbackLock);
  181. return positionableSource != nullptr && positionableSource->isLooping();
  182. }
  183. void AudioTransportSource::setGain (const float newGain) noexcept
  184. {
  185. gain = newGain;
  186. }
  187. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  188. {
  189. const ScopedLock sl (callbackLock);
  190. sampleRate = newSampleRate;
  191. blockSize = samplesPerBlockExpected;
  192. if (masterSource != nullptr)
  193. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  194. if (resamplerSource != nullptr && sourceSampleRate > 0)
  195. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  196. inputStreamEOF = false;
  197. isPrepared = true;
  198. }
  199. void AudioTransportSource::releaseMasterResources()
  200. {
  201. const ScopedLock sl (callbackLock);
  202. if (masterSource != nullptr)
  203. masterSource->releaseResources();
  204. isPrepared = false;
  205. }
  206. void AudioTransportSource::releaseResources()
  207. {
  208. releaseMasterResources();
  209. }
  210. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  211. {
  212. const ScopedLock sl (callbackLock);
  213. if (masterSource != nullptr && ! stopped)
  214. {
  215. masterSource->getNextAudioBlock (info);
  216. if (! playing)
  217. {
  218. // just stopped playing, so fade out the last block..
  219. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  220. info.buffer->applyGainRamp (i, info.startSample, jmin (256, info.numSamples), 1.0f, 0.0f);
  221. if (info.numSamples > 256)
  222. info.buffer->clear (info.startSample + 256, info.numSamples - 256);
  223. }
  224. if (positionableSource->getNextReadPosition() > positionableSource->getTotalLength() + 1
  225. && ! positionableSource->isLooping())
  226. {
  227. playing = false;
  228. inputStreamEOF = true;
  229. sendChangeMessage();
  230. }
  231. stopped = ! playing;
  232. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  233. info.buffer->applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  234. }
  235. else
  236. {
  237. info.clearActiveBufferRegion();
  238. stopped = true;
  239. }
  240. lastGain = gain;
  241. }