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_ResamplingAudioSource.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. ResamplingAudioSource::ResamplingAudioSource (AudioSource* const inputSource,
  20. const bool deleteInputWhenDeleted,
  21. const int channels)
  22. : input (inputSource, deleteInputWhenDeleted),
  23. numChannels (channels)
  24. {
  25. jassert (input != nullptr);
  26. zeromem (coefficients, sizeof (coefficients));
  27. }
  28. ResamplingAudioSource::~ResamplingAudioSource() {}
  29. void ResamplingAudioSource::setResamplingRatio (const double samplesInPerOutputSample)
  30. {
  31. jassert (samplesInPerOutputSample > 0);
  32. const SpinLock::ScopedLockType sl (ratioLock);
  33. ratio = jmax (0.0, samplesInPerOutputSample);
  34. }
  35. void ResamplingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  36. {
  37. const SpinLock::ScopedLockType sl (ratioLock);
  38. auto scaledBlockSize = roundToInt (samplesPerBlockExpected * ratio);
  39. input->prepareToPlay (scaledBlockSize, sampleRate * ratio);
  40. buffer.setSize (numChannels, scaledBlockSize + 32);
  41. filterStates.calloc (numChannels);
  42. srcBuffers.calloc (numChannels);
  43. destBuffers.calloc (numChannels);
  44. createLowPass (ratio);
  45. flushBuffers();
  46. }
  47. void ResamplingAudioSource::flushBuffers()
  48. {
  49. const ScopedLock sl (callbackLock);
  50. buffer.clear();
  51. bufferPos = 0;
  52. sampsInBuffer = 0;
  53. subSampleOffset = 0.0;
  54. resetFilters();
  55. }
  56. void ResamplingAudioSource::releaseResources()
  57. {
  58. input->releaseResources();
  59. buffer.setSize (numChannels, 0);
  60. }
  61. void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  62. {
  63. const ScopedLock sl (callbackLock);
  64. double localRatio;
  65. {
  66. const SpinLock::ScopedLockType ratioSl (ratioLock);
  67. localRatio = ratio;
  68. }
  69. if (lastRatio != localRatio)
  70. {
  71. createLowPass (localRatio);
  72. lastRatio = localRatio;
  73. }
  74. const int sampsNeeded = roundToInt (info.numSamples * localRatio) + 3;
  75. int bufferSize = buffer.getNumSamples();
  76. if (bufferSize < sampsNeeded + 8)
  77. {
  78. bufferPos %= bufferSize;
  79. bufferSize = sampsNeeded + 32;
  80. buffer.setSize (buffer.getNumChannels(), bufferSize, true, true);
  81. }
  82. bufferPos %= bufferSize;
  83. int endOfBufferPos = bufferPos + sampsInBuffer;
  84. const int channelsToProcess = jmin (numChannels, info.buffer->getNumChannels());
  85. while (sampsNeeded > sampsInBuffer)
  86. {
  87. endOfBufferPos %= bufferSize;
  88. int numToDo = jmin (sampsNeeded - sampsInBuffer,
  89. bufferSize - endOfBufferPos);
  90. AudioSourceChannelInfo readInfo (&buffer, endOfBufferPos, numToDo);
  91. input->getNextAudioBlock (readInfo);
  92. if (localRatio > 1.0001)
  93. {
  94. // for down-sampling, pre-apply the filter..
  95. for (int i = channelsToProcess; --i >= 0;)
  96. applyFilter (buffer.getWritePointer (i, endOfBufferPos), numToDo, filterStates[i]);
  97. }
  98. sampsInBuffer += numToDo;
  99. endOfBufferPos += numToDo;
  100. }
  101. for (int channel = 0; channel < channelsToProcess; ++channel)
  102. {
  103. destBuffers[channel] = info.buffer->getWritePointer (channel, info.startSample);
  104. srcBuffers[channel] = buffer.getReadPointer (channel);
  105. }
  106. int nextPos = (bufferPos + 1) % bufferSize;
  107. for (int m = info.numSamples; --m >= 0;)
  108. {
  109. jassert (sampsInBuffer > 0 && nextPos != endOfBufferPos);
  110. const float alpha = (float) subSampleOffset;
  111. for (int channel = 0; channel < channelsToProcess; ++channel)
  112. *destBuffers[channel]++ = srcBuffers[channel][bufferPos]
  113. + alpha * (srcBuffers[channel][nextPos] - srcBuffers[channel][bufferPos]);
  114. subSampleOffset += localRatio;
  115. while (subSampleOffset >= 1.0)
  116. {
  117. if (++bufferPos >= bufferSize)
  118. bufferPos = 0;
  119. --sampsInBuffer;
  120. nextPos = (bufferPos + 1) % bufferSize;
  121. subSampleOffset -= 1.0;
  122. }
  123. }
  124. if (localRatio < 0.9999)
  125. {
  126. // for up-sampling, apply the filter after transposing..
  127. for (int i = channelsToProcess; --i >= 0;)
  128. applyFilter (info.buffer->getWritePointer (i, info.startSample), info.numSamples, filterStates[i]);
  129. }
  130. else if (localRatio <= 1.0001 && info.numSamples > 0)
  131. {
  132. // if the filter's not currently being applied, keep it stoked with the last couple of samples to avoid discontinuities
  133. for (int i = channelsToProcess; --i >= 0;)
  134. {
  135. const float* const endOfBuffer = info.buffer->getReadPointer (i, info.startSample + info.numSamples - 1);
  136. FilterState& fs = filterStates[i];
  137. if (info.numSamples > 1)
  138. {
  139. fs.y2 = fs.x2 = *(endOfBuffer - 1);
  140. }
  141. else
  142. {
  143. fs.y2 = fs.y1;
  144. fs.x2 = fs.x1;
  145. }
  146. fs.y1 = fs.x1 = *endOfBuffer;
  147. }
  148. }
  149. jassert (sampsInBuffer >= 0);
  150. }
  151. void ResamplingAudioSource::createLowPass (const double frequencyRatio)
  152. {
  153. const double proportionalRate = (frequencyRatio > 1.0) ? 0.5 / frequencyRatio
  154. : 0.5 * frequencyRatio;
  155. const double n = 1.0 / std::tan (MathConstants<double>::pi * jmax (0.001, proportionalRate));
  156. const double nSquared = n * n;
  157. const double c1 = 1.0 / (1.0 + MathConstants<double>::sqrt2 * n + nSquared);
  158. setFilterCoefficients (c1,
  159. c1 * 2.0f,
  160. c1,
  161. 1.0,
  162. c1 * 2.0 * (1.0 - nSquared),
  163. c1 * (1.0 - MathConstants<double>::sqrt2 * n + nSquared));
  164. }
  165. void ResamplingAudioSource::setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6)
  166. {
  167. const double a = 1.0 / c4;
  168. c1 *= a;
  169. c2 *= a;
  170. c3 *= a;
  171. c5 *= a;
  172. c6 *= a;
  173. coefficients[0] = c1;
  174. coefficients[1] = c2;
  175. coefficients[2] = c3;
  176. coefficients[3] = c4;
  177. coefficients[4] = c5;
  178. coefficients[5] = c6;
  179. }
  180. void ResamplingAudioSource::resetFilters()
  181. {
  182. if (filterStates != nullptr)
  183. filterStates.clear ((size_t) numChannels);
  184. }
  185. void ResamplingAudioSource::applyFilter (float* samples, int num, FilterState& fs)
  186. {
  187. while (--num >= 0)
  188. {
  189. const double in = *samples;
  190. double out = coefficients[0] * in
  191. + coefficients[1] * fs.x1
  192. + coefficients[2] * fs.x2
  193. - coefficients[4] * fs.y1
  194. - coefficients[5] * fs.y2;
  195. #if JUCE_INTEL
  196. if (! (out < -1.0e-8 || out > 1.0e-8))
  197. out = 0;
  198. #endif
  199. fs.x2 = fs.x1;
  200. fs.x1 = in;
  201. fs.y2 = fs.y1;
  202. fs.y1 = out;
  203. *samples++ = (float) out;
  204. }
  205. }
  206. } // namespace juce