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.

312 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. // (For the moment, we'll implement a few local operators for this complex class - one
  24. // day we'll probably either have a juce complex class, or use the C++11 one)
  25. static FFT::Complex operator+ (FFT::Complex a, FFT::Complex b) noexcept { FFT::Complex c = { a.r + b.r, a.i + b.i }; return c; }
  26. static FFT::Complex operator- (FFT::Complex a, FFT::Complex b) noexcept { FFT::Complex c = { a.r - b.r, a.i - b.i }; return c; }
  27. static FFT::Complex operator* (FFT::Complex a, FFT::Complex b) noexcept { FFT::Complex c = { a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r }; return c; }
  28. static FFT::Complex& operator+= (FFT::Complex& a, FFT::Complex b) noexcept { a.r += b.r; a.i += b.i; return a; }
  29. //==============================================================================
  30. struct FFT::FFTConfig
  31. {
  32. FFTConfig (int sizeOfFFT, bool isInverse)
  33. : fftSize (sizeOfFFT), inverse (isInverse), twiddleTable ((size_t) sizeOfFFT)
  34. {
  35. for (int i = 0; i < fftSize; ++i)
  36. {
  37. const double phase = (isInverse ? 2.0 : -2.0) * double_Pi * i / fftSize;
  38. twiddleTable[i].r = (float) cos (phase);
  39. twiddleTable[i].i = (float) sin (phase);
  40. }
  41. const int root = (int) std::sqrt ((double) fftSize);
  42. int divisor = 4, n = fftSize;
  43. for (int i = 0; i < numElementsInArray (factors); ++i)
  44. {
  45. while ((n % divisor) != 0)
  46. {
  47. if (divisor == 2) divisor = 3;
  48. else if (divisor == 4) divisor = 2;
  49. else divisor += 2;
  50. if (divisor > root)
  51. divisor = n;
  52. }
  53. n /= divisor;
  54. jassert (divisor == 1 || divisor == 2 || divisor == 4);
  55. factors[i].radix = divisor;
  56. factors[i].length = n;
  57. }
  58. }
  59. void perform (const Complex* input, Complex* output) const noexcept
  60. {
  61. perform (input, output, 1, 1, factors);
  62. }
  63. const int fftSize;
  64. const bool inverse;
  65. struct Factor { int radix, length; };
  66. Factor factors[32];
  67. HeapBlock<Complex> twiddleTable;
  68. void perform (const Complex* input, Complex* output, const int stride, const int strideIn, const Factor* facs) const noexcept
  69. {
  70. const Factor factor (*facs++);
  71. Complex* const originalOutput = output;
  72. const Complex* const outputEnd = output + factor.radix * factor.length;
  73. if (stride == 1 && factor.radix <= 5)
  74. {
  75. for (int i = 0; i < factor.radix; ++i)
  76. perform (input + stride * strideIn * i, output + i * factor.length, stride * factor.radix, strideIn, facs);
  77. butterfly (factor, output, stride);
  78. return;
  79. }
  80. if (factor.length == 1)
  81. {
  82. do
  83. {
  84. *output++ = *input;
  85. input += stride * strideIn;
  86. }
  87. while (output < outputEnd);
  88. }
  89. else
  90. {
  91. do
  92. {
  93. perform (input, output, stride * factor.radix, strideIn, facs);
  94. input += stride * strideIn;
  95. output += factor.length;
  96. }
  97. while (output < outputEnd);
  98. }
  99. butterfly (factor, originalOutput, stride);
  100. }
  101. void butterfly (const Factor factor, Complex* data, const int stride) const noexcept
  102. {
  103. switch (factor.radix)
  104. {
  105. case 1: break;
  106. case 2: butterfly2 (data, stride, factor.length); return;
  107. case 4: butterfly4 (data, stride, factor.length); return;
  108. default: jassertfalse; break;
  109. }
  110. Complex* scratch = static_cast<Complex*> (alloca (sizeof (Complex) * (size_t) factor.radix));
  111. for (int i = 0; i < factor.length; ++i)
  112. {
  113. for (int k = i, q1 = 0; q1 < factor.radix; ++q1)
  114. {
  115. scratch[q1] = data[k];
  116. k += factor.length;
  117. }
  118. for (int k = i, q1 = 0; q1 < factor.radix; ++q1)
  119. {
  120. int twiddleIndex = 0;
  121. data[k] = scratch[0];
  122. for (int q = 1; q < factor.radix; ++q)
  123. {
  124. twiddleIndex += stride * k;
  125. if (twiddleIndex >= fftSize)
  126. twiddleIndex -= fftSize;
  127. data[k] += scratch[q] * twiddleTable[twiddleIndex];
  128. }
  129. k += factor.length;
  130. }
  131. }
  132. }
  133. void butterfly2 (Complex* data, const int stride, const int length) const noexcept
  134. {
  135. Complex* dataEnd = data + length;
  136. const Complex* tw = twiddleTable;
  137. for (int i = length; --i >= 0;)
  138. {
  139. const Complex s (*dataEnd * *tw);
  140. tw += stride;
  141. *dataEnd++ = *data - s;
  142. *data++ += s;
  143. }
  144. }
  145. void butterfly4 (Complex* data, const int stride, const int length) const noexcept
  146. {
  147. const int lengthX2 = length * 2;
  148. const int lengthX3 = length * 3;
  149. const Complex* twiddle1 = twiddleTable;
  150. const Complex* twiddle2 = twiddle1;
  151. const Complex* twiddle3 = twiddle1;
  152. for (int i = length; --i >= 0;)
  153. {
  154. const Complex s0 = data[length] * *twiddle1;
  155. const Complex s1 = data[lengthX2] * *twiddle2;
  156. const Complex s2 = data[lengthX3] * *twiddle3;
  157. const Complex s3 = s0 + s2;
  158. const Complex s4 = s0 - s2;
  159. const Complex s5 = *data - s1;
  160. *data += s1;
  161. data[lengthX2] = *data - s3;
  162. twiddle1 += stride;
  163. twiddle2 += stride * 2;
  164. twiddle3 += stride * 3;
  165. *data += s3;
  166. if (inverse)
  167. {
  168. data[length].r = s5.r - s4.i;
  169. data[length].i = s5.i + s4.r;
  170. data[lengthX3].r = s5.r + s4.i;
  171. data[lengthX3].i = s5.i - s4.r;
  172. }
  173. else
  174. {
  175. data[length].r = s5.r + s4.i;
  176. data[length].i = s5.i - s4.r;
  177. data[lengthX3].r = s5.r - s4.i;
  178. data[lengthX3].i = s5.i + s4.r;
  179. }
  180. ++data;
  181. }
  182. }
  183. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFTConfig)
  184. };
  185. //==============================================================================
  186. FFT::FFT (int order, bool inverse) : config (new FFTConfig (1 << order, inverse)), size (1 << order) {}
  187. FFT::~FFT() {}
  188. void FFT::perform (const Complex* const input, Complex* const output) const noexcept
  189. {
  190. config->perform (input, output);
  191. }
  192. const size_t maxFFTScratchSpaceToAlloca = 256 * 1024;
  193. void FFT::performRealOnlyForwardTransform (float* d) const noexcept
  194. {
  195. const size_t scratchSize = 16 + sizeof (FFT::Complex) * (size_t) size;
  196. if (scratchSize < maxFFTScratchSpaceToAlloca)
  197. {
  198. performRealOnlyForwardTransform (static_cast<Complex*> (alloca (scratchSize)), d);
  199. }
  200. else
  201. {
  202. HeapBlock<char> heapSpace (scratchSize);
  203. performRealOnlyForwardTransform (reinterpret_cast<Complex*> (heapSpace.getData()), d);
  204. }
  205. }
  206. void FFT::performRealOnlyInverseTransform (float* d) const noexcept
  207. {
  208. const size_t scratchSize = 16 + sizeof (FFT::Complex) * (size_t) size;
  209. if (scratchSize < maxFFTScratchSpaceToAlloca)
  210. {
  211. performRealOnlyInverseTransform (static_cast<Complex*> (alloca (scratchSize)), d);
  212. }
  213. else
  214. {
  215. HeapBlock<char> heapSpace (scratchSize);
  216. performRealOnlyInverseTransform (reinterpret_cast<Complex*> (heapSpace.getData()), d);
  217. }
  218. }
  219. void FFT::performRealOnlyForwardTransform (Complex* scratch, float* d) const noexcept
  220. {
  221. // This can only be called on an FFT object that was created to do forward transforms.
  222. jassert (! config->inverse);
  223. for (int i = 0; i < size; ++i)
  224. {
  225. scratch[i].r = d[i];
  226. scratch[i].i = 0;
  227. }
  228. perform (scratch, reinterpret_cast<Complex*> (d));
  229. }
  230. void FFT::performRealOnlyInverseTransform (Complex* scratch, float* d) const noexcept
  231. {
  232. // This can only be called on an FFT object that was created to do inverse transforms.
  233. jassert (config->inverse);
  234. perform (reinterpret_cast<const Complex*> (d), scratch);
  235. const float scaleFactor = 1.0f / size;
  236. for (int i = 0; i < size; ++i)
  237. {
  238. d[i] = scratch[i].r * scaleFactor;
  239. d[i + size] = scratch[i].i * scaleFactor;
  240. }
  241. }
  242. void FFT::performFrequencyOnlyForwardTransform (float* d) const noexcept
  243. {
  244. performRealOnlyForwardTransform (d);
  245. const int twiceSize = size * 2;
  246. for (int i = 0; i < twiceSize; i += 2)
  247. {
  248. d[i / 2] = juce_hypot (d[i], d[i + 1]);
  249. if (i >= size)
  250. {
  251. d[i] = 0;
  252. d[i + 1] = 0;
  253. }
  254. }
  255. }