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.

306 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. #include "../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_AudioTransportSource.h"
  21. #include "../../memory/juce_ScopedPointer.h"
  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 readAheadBufferSize_,
  48. double sourceSampleRateToCorrectFor,
  49. int maxNumChannels)
  50. {
  51. if (source == newSource)
  52. {
  53. if (source == nullptr)
  54. return;
  55. setSource (0, 0, 0); // deselect and reselect to avoid releasing resources wrongly
  56. }
  57. readAheadBufferSize = readAheadBufferSize_;
  58. sourceSampleRate = sourceSampleRateToCorrectFor;
  59. ResamplingAudioSource* newResamplerSource = nullptr;
  60. BufferingAudioSource* newBufferingSource = nullptr;
  61. PositionableAudioSource* newPositionableSource = nullptr;
  62. AudioSource* newMasterSource = nullptr;
  63. ScopedPointer <ResamplingAudioSource> oldResamplerSource (resamplerSource);
  64. ScopedPointer <BufferingAudioSource> oldBufferingSource (bufferingSource);
  65. AudioSource* oldMasterSource = masterSource;
  66. if (newSource != nullptr)
  67. {
  68. newPositionableSource = newSource;
  69. if (readAheadBufferSize_ > 0)
  70. newPositionableSource = newBufferingSource
  71. = new BufferingAudioSource (newPositionableSource, false, readAheadBufferSize_, maxNumChannels);
  72. newPositionableSource->setNextReadPosition (0);
  73. if (sourceSampleRateToCorrectFor > 0)
  74. newMasterSource = newResamplerSource
  75. = new ResamplingAudioSource (newPositionableSource, false, maxNumChannels);
  76. else
  77. newMasterSource = newPositionableSource;
  78. if (isPrepared)
  79. {
  80. if (newResamplerSource != nullptr && sourceSampleRate > 0 && sampleRate > 0)
  81. newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  82. newMasterSource->prepareToPlay (blockSize, sampleRate);
  83. }
  84. }
  85. {
  86. const ScopedLock sl (callbackLock);
  87. source = newSource;
  88. resamplerSource = newResamplerSource;
  89. bufferingSource = newBufferingSource;
  90. masterSource = newMasterSource;
  91. positionableSource = newPositionableSource;
  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. else
  134. return 0.0;
  135. }
  136. double AudioTransportSource::getLengthInSeconds() const
  137. {
  138. return getTotalLength() / sampleRate;
  139. }
  140. void AudioTransportSource::setNextReadPosition (int64 newPosition)
  141. {
  142. if (positionableSource != nullptr)
  143. {
  144. if (sampleRate > 0 && sourceSampleRate > 0)
  145. newPosition = (int64) (newPosition * sourceSampleRate / sampleRate);
  146. positionableSource->setNextReadPosition (newPosition);
  147. }
  148. }
  149. int64 AudioTransportSource::getNextReadPosition() const
  150. {
  151. if (positionableSource != nullptr)
  152. {
  153. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  154. return (int64) (positionableSource->getNextReadPosition() * ratio);
  155. }
  156. return 0;
  157. }
  158. int64 AudioTransportSource::getTotalLength() const
  159. {
  160. const ScopedLock sl (callbackLock);
  161. if (positionableSource != nullptr)
  162. {
  163. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  164. return (int64) (positionableSource->getTotalLength() * ratio);
  165. }
  166. return 0;
  167. }
  168. bool AudioTransportSource::isLooping() const
  169. {
  170. const ScopedLock sl (callbackLock);
  171. return positionableSource != nullptr
  172. && positionableSource->isLooping();
  173. }
  174. void AudioTransportSource::setGain (const float newGain) noexcept
  175. {
  176. gain = newGain;
  177. }
  178. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected,
  179. double sampleRate_)
  180. {
  181. const ScopedLock sl (callbackLock);
  182. sampleRate = sampleRate_;
  183. blockSize = samplesPerBlockExpected;
  184. if (masterSource != nullptr)
  185. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  186. if (resamplerSource != nullptr && sourceSampleRate > 0)
  187. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  188. isPrepared = true;
  189. }
  190. void AudioTransportSource::releaseMasterResources()
  191. {
  192. const ScopedLock sl (callbackLock);
  193. if (masterSource != nullptr)
  194. masterSource->releaseResources();
  195. isPrepared = false;
  196. }
  197. void AudioTransportSource::releaseResources()
  198. {
  199. releaseMasterResources();
  200. }
  201. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  202. {
  203. const ScopedLock sl (callbackLock);
  204. inputStreamEOF = false;
  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. {
  226. info.buffer->applyGainRamp (i, info.startSample, info.numSamples,
  227. lastGain, gain);
  228. }
  229. }
  230. else
  231. {
  232. info.clearActiveBufferRegion();
  233. stopped = true;
  234. }
  235. lastGain = gain;
  236. }
  237. END_JUCE_NAMESPACE