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

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