The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

303 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. AudioTransportSource::AudioTransportSource()
  19. : source (nullptr),
  20. resamplerSource (nullptr),
  21. bufferingSource (nullptr),
  22. positionableSource (nullptr),
  23. masterSource (nullptr),
  24. gain (1.0f),
  25. lastGain (1.0f),
  26. playing (false),
  27. stopped (true),
  28. sampleRate (44100.0),
  29. sourceSampleRate (0.0),
  30. blockSize (128),
  31. readAheadBufferSize (0),
  32. isPrepared (false),
  33. inputStreamEOF (false)
  34. {
  35. }
  36. AudioTransportSource::~AudioTransportSource()
  37. {
  38. setSource (nullptr);
  39. releaseMasterResources();
  40. }
  41. void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
  42. int readAheadBufferSize_,
  43. TimeSliceThread* readAheadThread,
  44. double sourceSampleRateToCorrectFor,
  45. int maxNumChannels)
  46. {
  47. if (source == newSource)
  48. {
  49. if (source == nullptr)
  50. return;
  51. setSource (nullptr, 0, nullptr); // deselect and reselect to avoid releasing resources wrongly
  52. }
  53. readAheadBufferSize = readAheadBufferSize_;
  54. sourceSampleRate = sourceSampleRateToCorrectFor;
  55. ResamplingAudioSource* newResamplerSource = nullptr;
  56. BufferingAudioSource* newBufferingSource = nullptr;
  57. PositionableAudioSource* newPositionableSource = nullptr;
  58. AudioSource* newMasterSource = nullptr;
  59. ScopedPointer <ResamplingAudioSource> oldResamplerSource (resamplerSource);
  60. ScopedPointer <BufferingAudioSource> oldBufferingSource (bufferingSource);
  61. AudioSource* oldMasterSource = masterSource;
  62. if (newSource != nullptr)
  63. {
  64. newPositionableSource = newSource;
  65. if (readAheadBufferSize_ > 0)
  66. {
  67. // If you want to use a read-ahead buffer, you must also provide a TimeSliceThread
  68. // for it to use!
  69. jassert (readAheadThread != nullptr);
  70. newPositionableSource = newBufferingSource
  71. = new BufferingAudioSource (newPositionableSource, *readAheadThread,
  72. false, readAheadBufferSize_, maxNumChannels);
  73. }
  74. newPositionableSource->setNextReadPosition (0);
  75. if (sourceSampleRateToCorrectFor > 0)
  76. newMasterSource = newResamplerSource
  77. = new ResamplingAudioSource (newPositionableSource, false, maxNumChannels);
  78. else
  79. newMasterSource = newPositionableSource;
  80. if (isPrepared)
  81. {
  82. if (newResamplerSource != nullptr && sourceSampleRate > 0 && sampleRate > 0)
  83. newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  84. newMasterSource->prepareToPlay (blockSize, sampleRate);
  85. }
  86. }
  87. {
  88. const ScopedLock sl (callbackLock);
  89. source = newSource;
  90. resamplerSource = newResamplerSource;
  91. bufferingSource = newBufferingSource;
  92. masterSource = newMasterSource;
  93. positionableSource = newPositionableSource;
  94. playing = false;
  95. }
  96. if (oldMasterSource != nullptr)
  97. oldMasterSource->releaseResources();
  98. }
  99. void AudioTransportSource::start()
  100. {
  101. if ((! playing) && masterSource != nullptr)
  102. {
  103. {
  104. const ScopedLock sl (callbackLock);
  105. playing = true;
  106. stopped = false;
  107. inputStreamEOF = false;
  108. }
  109. sendChangeMessage();
  110. }
  111. }
  112. void AudioTransportSource::stop()
  113. {
  114. if (playing)
  115. {
  116. {
  117. const ScopedLock sl (callbackLock);
  118. playing = false;
  119. }
  120. int n = 500;
  121. while (--n >= 0 && ! stopped)
  122. Thread::sleep (2);
  123. sendChangeMessage();
  124. }
  125. }
  126. void AudioTransportSource::setPosition (double newPosition)
  127. {
  128. if (sampleRate > 0.0)
  129. setNextReadPosition ((int64) (newPosition * sampleRate));
  130. }
  131. double AudioTransportSource::getCurrentPosition() const
  132. {
  133. if (sampleRate > 0.0)
  134. return getNextReadPosition() / sampleRate;
  135. else
  136. return 0.0;
  137. }
  138. double AudioTransportSource::getLengthInSeconds() const
  139. {
  140. return getTotalLength() / sampleRate;
  141. }
  142. void AudioTransportSource::setNextReadPosition (int64 newPosition)
  143. {
  144. if (positionableSource != nullptr)
  145. {
  146. if (sampleRate > 0 && sourceSampleRate > 0)
  147. newPosition = (int64) (newPosition * sourceSampleRate / sampleRate);
  148. positionableSource->setNextReadPosition (newPosition);
  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
  174. && positionableSource->isLooping();
  175. }
  176. void AudioTransportSource::setGain (const float newGain) noexcept
  177. {
  178. gain = newGain;
  179. }
  180. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected,
  181. double sampleRate_)
  182. {
  183. const ScopedLock sl (callbackLock);
  184. sampleRate = sampleRate_;
  185. blockSize = samplesPerBlockExpected;
  186. if (masterSource != nullptr)
  187. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  188. if (resamplerSource != nullptr && sourceSampleRate > 0)
  189. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  190. isPrepared = true;
  191. }
  192. void AudioTransportSource::releaseMasterResources()
  193. {
  194. const ScopedLock sl (callbackLock);
  195. if (masterSource != nullptr)
  196. masterSource->releaseResources();
  197. isPrepared = false;
  198. }
  199. void AudioTransportSource::releaseResources()
  200. {
  201. releaseMasterResources();
  202. }
  203. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  204. {
  205. const ScopedLock sl (callbackLock);
  206. inputStreamEOF = false;
  207. if (masterSource != nullptr && ! stopped)
  208. {
  209. masterSource->getNextAudioBlock (info);
  210. if (! playing)
  211. {
  212. // just stopped playing, so fade out the last block..
  213. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  214. info.buffer->applyGainRamp (i, info.startSample, jmin (256, info.numSamples), 1.0f, 0.0f);
  215. if (info.numSamples > 256)
  216. info.buffer->clear (info.startSample + 256, info.numSamples - 256);
  217. }
  218. if (positionableSource->getNextReadPosition() > positionableSource->getTotalLength() + 1
  219. && ! positionableSource->isLooping())
  220. {
  221. playing = false;
  222. inputStreamEOF = true;
  223. sendChangeMessage();
  224. }
  225. stopped = ! playing;
  226. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  227. {
  228. info.buffer->applyGainRamp (i, info.startSample, info.numSamples,
  229. lastGain, gain);
  230. }
  231. }
  232. else
  233. {
  234. info.clearActiveBufferRegion();
  235. stopped = true;
  236. }
  237. lastGain = gain;
  238. }