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.

284 lines
8.1KB

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