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.

264 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ResamplingAudioSource::ResamplingAudioSource (AudioSource* const inputSource,
  18. const bool deleteInputWhenDeleted,
  19. const int channels)
  20. : input (inputSource, deleteInputWhenDeleted),
  21. ratio (1.0),
  22. lastRatio (1.0),
  23. bufferPos (0),
  24. sampsInBuffer (0),
  25. subSampleOffset (0),
  26. numChannels (channels)
  27. {
  28. jassert (input != nullptr);
  29. zeromem (coefficients, sizeof (coefficients));
  30. }
  31. ResamplingAudioSource::~ResamplingAudioSource() {}
  32. void ResamplingAudioSource::setResamplingRatio (const double samplesInPerOutputSample)
  33. {
  34. jassert (samplesInPerOutputSample > 0);
  35. const SpinLock::ScopedLockType sl (ratioLock);
  36. ratio = jmax (0.0, samplesInPerOutputSample);
  37. }
  38. void ResamplingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  39. {
  40. const SpinLock::ScopedLockType sl (ratioLock);
  41. const int scaledBlockSize = roundToInt (samplesPerBlockExpected * ratio);
  42. input->prepareToPlay (scaledBlockSize, sampleRate * ratio);
  43. buffer.setSize (numChannels, scaledBlockSize + 32);
  44. filterStates.calloc ((size_t) numChannels);
  45. srcBuffers.calloc ((size_t) numChannels);
  46. destBuffers.calloc ((size_t) numChannels);
  47. createLowPass (ratio);
  48. flushBuffers();
  49. }
  50. void ResamplingAudioSource::flushBuffers()
  51. {
  52. buffer.clear();
  53. bufferPos = 0;
  54. sampsInBuffer = 0;
  55. subSampleOffset = 0.0;
  56. resetFilters();
  57. }
  58. void ResamplingAudioSource::releaseResources()
  59. {
  60. input->releaseResources();
  61. buffer.setSize (numChannels, 0);
  62. }
  63. void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  64. {
  65. double localRatio;
  66. {
  67. const SpinLock::ScopedLockType sl (ratioLock);
  68. localRatio = ratio;
  69. }
  70. if (lastRatio != localRatio)
  71. {
  72. createLowPass (localRatio);
  73. lastRatio = localRatio;
  74. }
  75. const int sampsNeeded = roundToInt (info.numSamples * localRatio) + 3;
  76. int bufferSize = buffer.getNumSamples();
  77. if (bufferSize < sampsNeeded + 8)
  78. {
  79. bufferPos %= bufferSize;
  80. bufferSize = sampsNeeded + 32;
  81. buffer.setSize (buffer.getNumChannels(), bufferSize, true, true);
  82. }
  83. bufferPos %= bufferSize;
  84. int endOfBufferPos = bufferPos + sampsInBuffer;
  85. const int channelsToProcess = jmin (numChannels, info.buffer->getNumChannels());
  86. while (sampsNeeded > sampsInBuffer)
  87. {
  88. endOfBufferPos %= bufferSize;
  89. int numToDo = jmin (sampsNeeded - sampsInBuffer,
  90. bufferSize - endOfBufferPos);
  91. AudioSourceChannelInfo readInfo (&buffer, endOfBufferPos, numToDo);
  92. input->getNextAudioBlock (readInfo);
  93. if (localRatio > 1.0001)
  94. {
  95. // for down-sampling, pre-apply the filter..
  96. for (int i = channelsToProcess; --i >= 0;)
  97. applyFilter (buffer.getWritePointer (i, endOfBufferPos), numToDo, filterStates[i]);
  98. }
  99. sampsInBuffer += numToDo;
  100. endOfBufferPos += numToDo;
  101. }
  102. for (int channel = 0; channel < channelsToProcess; ++channel)
  103. {
  104. destBuffers[channel] = info.buffer->getWritePointer (channel, info.startSample);
  105. srcBuffers[channel] = buffer.getReadPointer (channel);
  106. }
  107. int nextPos = (bufferPos + 1) % bufferSize;
  108. for (int m = info.numSamples; --m >= 0;)
  109. {
  110. jassert (sampsInBuffer > 0 && nextPos != endOfBufferPos);
  111. const float alpha = (float) subSampleOffset;
  112. for (int channel = 0; channel < channelsToProcess; ++channel)
  113. *destBuffers[channel]++ = srcBuffers[channel][bufferPos]
  114. + alpha * (srcBuffers[channel][nextPos] - srcBuffers[channel][bufferPos]);
  115. subSampleOffset += localRatio;
  116. while (subSampleOffset >= 1.0)
  117. {
  118. if (++bufferPos >= bufferSize)
  119. bufferPos = 0;
  120. --sampsInBuffer;
  121. nextPos = (bufferPos + 1) % bufferSize;
  122. subSampleOffset -= 1.0;
  123. }
  124. }
  125. if (localRatio < 0.9999)
  126. {
  127. // for up-sampling, apply the filter after transposing..
  128. for (int i = channelsToProcess; --i >= 0;)
  129. applyFilter (info.buffer->getWritePointer (i, info.startSample), info.numSamples, filterStates[i]);
  130. }
  131. else if (localRatio <= 1.0001 && info.numSamples > 0)
  132. {
  133. // if the filter's not currently being applied, keep it stoked with the last couple of samples to avoid discontinuities
  134. for (int i = channelsToProcess; --i >= 0;)
  135. {
  136. const float* const endOfBuffer = info.buffer->getReadPointer (i, info.startSample + info.numSamples - 1);
  137. FilterState& fs = filterStates[i];
  138. if (info.numSamples > 1)
  139. {
  140. fs.y2 = fs.x2 = *(endOfBuffer - 1);
  141. }
  142. else
  143. {
  144. fs.y2 = fs.y1;
  145. fs.x2 = fs.x1;
  146. }
  147. fs.y1 = fs.x1 = *endOfBuffer;
  148. }
  149. }
  150. jassert (sampsInBuffer >= 0);
  151. }
  152. void ResamplingAudioSource::createLowPass (const double frequencyRatio)
  153. {
  154. const double proportionalRate = (frequencyRatio > 1.0) ? 0.5 / frequencyRatio
  155. : 0.5 * frequencyRatio;
  156. const double n = 1.0 / std::tan (double_Pi * jmax (0.001, proportionalRate));
  157. const double nSquared = n * n;
  158. const double c1 = 1.0 / (1.0 + std::sqrt (2.0) * n + nSquared);
  159. setFilterCoefficients (c1,
  160. c1 * 2.0f,
  161. c1,
  162. 1.0,
  163. c1 * 2.0 * (1.0 - nSquared),
  164. c1 * (1.0 - std::sqrt (2.0) * n + nSquared));
  165. }
  166. void ResamplingAudioSource::setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6)
  167. {
  168. const double a = 1.0 / c4;
  169. c1 *= a;
  170. c2 *= a;
  171. c3 *= a;
  172. c5 *= a;
  173. c6 *= a;
  174. coefficients[0] = c1;
  175. coefficients[1] = c2;
  176. coefficients[2] = c3;
  177. coefficients[3] = c4;
  178. coefficients[4] = c5;
  179. coefficients[5] = c6;
  180. }
  181. void ResamplingAudioSource::resetFilters()
  182. {
  183. if (filterStates != nullptr)
  184. filterStates.clear ((size_t) numChannels);
  185. }
  186. void ResamplingAudioSource::applyFilter (float* samples, int num, FilterState& fs)
  187. {
  188. while (--num >= 0)
  189. {
  190. const double in = *samples;
  191. double out = coefficients[0] * in
  192. + coefficients[1] * fs.x1
  193. + coefficients[2] * fs.x2
  194. - coefficients[4] * fs.y1
  195. - coefficients[5] * fs.y2;
  196. #if JUCE_INTEL
  197. if (! (out < -1.0e-8 || out > 1.0e-8))
  198. out = 0;
  199. #endif
  200. fs.x2 = fs.x1;
  201. fs.x1 = in;
  202. fs.y2 = fs.y1;
  203. fs.y1 = out;
  204. *samples++ = (float) out;
  205. }
  206. }