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.

260 lines
7.9KB

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