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.

297 lines
8.5KB

  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. AudioTransportSource::AudioTransportSource()
  18. : source (nullptr),
  19. resamplerSource (nullptr),
  20. bufferingSource (nullptr),
  21. positionableSource (nullptr),
  22. masterSource (nullptr),
  23. gain (1.0f),
  24. lastGain (1.0f),
  25. playing (false),
  26. stopped (true),
  27. sampleRate (44100.0),
  28. sourceSampleRate (0.0),
  29. blockSize (128),
  30. readAheadBufferSize (0),
  31. isPrepared (false),
  32. inputStreamEOF (false)
  33. {
  34. }
  35. AudioTransportSource::~AudioTransportSource()
  36. {
  37. setSource (nullptr);
  38. releaseMasterResources();
  39. }
  40. void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
  41. int readAheadSize, TimeSliceThread* readAheadThread,
  42. double sourceSampleRateToCorrectFor, int maxNumChannels)
  43. {
  44. if (source == newSource)
  45. {
  46. if (source == nullptr)
  47. return;
  48. setSource (nullptr, 0, nullptr); // deselect and reselect to avoid releasing resources wrongly
  49. }
  50. readAheadBufferSize = readAheadSize;
  51. sourceSampleRate = sourceSampleRateToCorrectFor;
  52. ResamplingAudioSource* newResamplerSource = nullptr;
  53. BufferingAudioSource* newBufferingSource = nullptr;
  54. PositionableAudioSource* newPositionableSource = nullptr;
  55. AudioSource* newMasterSource = nullptr;
  56. ScopedPointer<ResamplingAudioSource> oldResamplerSource (resamplerSource);
  57. ScopedPointer<BufferingAudioSource> oldBufferingSource (bufferingSource);
  58. AudioSource* oldMasterSource = masterSource;
  59. if (newSource != nullptr)
  60. {
  61. newPositionableSource = newSource;
  62. if (readAheadSize > 0)
  63. {
  64. // If you want to use a read-ahead buffer, you must also provide a TimeSliceThread
  65. // for it to use!
  66. jassert (readAheadThread != nullptr);
  67. newPositionableSource = newBufferingSource
  68. = new BufferingAudioSource (newPositionableSource, *readAheadThread,
  69. false, readAheadSize, maxNumChannels);
  70. }
  71. newPositionableSource->setNextReadPosition (0);
  72. if (sourceSampleRateToCorrectFor > 0)
  73. newMasterSource = newResamplerSource
  74. = new ResamplingAudioSource (newPositionableSource, false, maxNumChannels);
  75. else
  76. newMasterSource = newPositionableSource;
  77. if (isPrepared)
  78. {
  79. if (newResamplerSource != nullptr && sourceSampleRate > 0 && sampleRate > 0)
  80. newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  81. newMasterSource->prepareToPlay (blockSize, sampleRate);
  82. }
  83. }
  84. {
  85. const ScopedLock sl (callbackLock);
  86. source = newSource;
  87. resamplerSource = newResamplerSource;
  88. bufferingSource = newBufferingSource;
  89. masterSource = newMasterSource;
  90. positionableSource = newPositionableSource;
  91. inputStreamEOF = false;
  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 (double) getNextReadPosition() / sampleRate;
  133. return 0.0;
  134. }
  135. double AudioTransportSource::getLengthInSeconds() const
  136. {
  137. if (sampleRate > 0.0)
  138. return (double) getTotalLength() / sampleRate;
  139. return 0.0;
  140. }
  141. void AudioTransportSource::setNextReadPosition (int64 newPosition)
  142. {
  143. if (positionableSource != nullptr)
  144. {
  145. if (sampleRate > 0 && sourceSampleRate > 0)
  146. newPosition = (int64) ((double) newPosition * sourceSampleRate / sampleRate);
  147. positionableSource->setNextReadPosition (newPosition);
  148. if (resamplerSource != nullptr)
  149. resamplerSource->flushBuffers();
  150. inputStreamEOF = false;
  151. }
  152. }
  153. int64 AudioTransportSource::getNextReadPosition() const
  154. {
  155. if (positionableSource != nullptr)
  156. {
  157. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  158. return (int64) ((double) positionableSource->getNextReadPosition() * ratio);
  159. }
  160. return 0;
  161. }
  162. int64 AudioTransportSource::getTotalLength() const
  163. {
  164. const ScopedLock sl (callbackLock);
  165. if (positionableSource != nullptr)
  166. {
  167. const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;
  168. return (int64) ((double) positionableSource->getTotalLength() * ratio);
  169. }
  170. return 0;
  171. }
  172. bool AudioTransportSource::isLooping() const
  173. {
  174. const ScopedLock sl (callbackLock);
  175. return positionableSource != nullptr && positionableSource->isLooping();
  176. }
  177. void AudioTransportSource::setGain (const float newGain) noexcept
  178. {
  179. gain = newGain;
  180. }
  181. void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
  182. {
  183. const ScopedLock sl (callbackLock);
  184. sampleRate = newSampleRate;
  185. blockSize = samplesPerBlockExpected;
  186. if (masterSource != nullptr)
  187. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  188. if (resamplerSource != nullptr && sourceSampleRate > 0)
  189. resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);
  190. inputStreamEOF = false;
  191. isPrepared = true;
  192. }
  193. void AudioTransportSource::releaseMasterResources()
  194. {
  195. const ScopedLock sl (callbackLock);
  196. if (masterSource != nullptr)
  197. masterSource->releaseResources();
  198. isPrepared = false;
  199. }
  200. void AudioTransportSource::releaseResources()
  201. {
  202. releaseMasterResources();
  203. }
  204. void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  205. {
  206. const ScopedLock sl (callbackLock);
  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. info.buffer->applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  228. }
  229. else
  230. {
  231. info.clearActiveBufferRegion();
  232. stopped = true;
  233. }
  234. lastGain = gain;
  235. }