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.1KB

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