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.

830 lines
28KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. struct FFT::Instance
  20. {
  21. virtual ~Instance() {}
  22. virtual void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept = 0;
  23. virtual void performRealOnlyForwardTransform (float*) const noexcept = 0;
  24. virtual void performRealOnlyInverseTransform (float*) const noexcept = 0;
  25. };
  26. struct FFT::Engine
  27. {
  28. Engine (int priorityToUse) : enginePriority (priorityToUse)
  29. {
  30. EnginePriorityComparator comparator;
  31. getEngines().addSorted (comparator, this);
  32. }
  33. virtual ~Engine() {}
  34. virtual FFT::Instance* create (int order) const = 0;
  35. //==============================================================================
  36. static FFT::Instance* createBestEngineForPlatform (int order)
  37. {
  38. for (auto* engine : getEngines())
  39. if (auto* instance = engine->create (order))
  40. return instance;
  41. jassertfalse; // This should never happen as the fallback engine should always work!
  42. return nullptr;
  43. }
  44. private:
  45. struct EnginePriorityComparator
  46. {
  47. static int compareElements (Engine* first, Engine* second) noexcept
  48. {
  49. // sort in reverse order
  50. return DefaultElementComparator<int>::compareElements (second->enginePriority, first->enginePriority);
  51. }
  52. };
  53. static Array<Engine*>& getEngines()
  54. {
  55. static Array<Engine*> engines;
  56. return engines;
  57. }
  58. int enginePriority; // used so that faster engines have priority over slower ones
  59. };
  60. template <typename InstanceToUse>
  61. struct FFT::EngineImpl : public FFT::Engine
  62. {
  63. EngineImpl() : FFT::Engine (InstanceToUse::priority) {}
  64. FFT::Instance* create (int order) const override { return InstanceToUse::create (order); }
  65. };
  66. //==============================================================================
  67. //==============================================================================
  68. struct FFTFallback : public FFT::Instance
  69. {
  70. // this should have the least priority of all engines
  71. static constexpr int priority = -1;
  72. static FFTFallback* create (int order)
  73. {
  74. return new FFTFallback (order);
  75. }
  76. FFTFallback (int order)
  77. {
  78. configForward = new FFTConfig (1 << order, false);
  79. configInverse = new FFTConfig (1 << order, true);
  80. size = 1 << order;
  81. }
  82. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept override
  83. {
  84. if (size == 1)
  85. {
  86. *output = *input;
  87. return;
  88. }
  89. const SpinLock::ScopedLockType sl(processLock);
  90. jassert (configForward != nullptr);
  91. if (inverse)
  92. {
  93. configInverse->perform (input, output);
  94. const float scaleFactor = 1.0f / size;
  95. for (int i = 0; i < size; ++i)
  96. output[i] *= scaleFactor;
  97. }
  98. else
  99. {
  100. configForward->perform (input, output);
  101. }
  102. }
  103. const size_t maxFFTScratchSpaceToAlloca = 256 * 1024;
  104. void performRealOnlyForwardTransform (float* d) const noexcept override
  105. {
  106. if (size == 1)
  107. return;
  108. const size_t scratchSize = 16 + sizeof (Complex<float>) * (size_t) size;
  109. if (scratchSize < maxFFTScratchSpaceToAlloca)
  110. {
  111. performRealOnlyForwardTransform (static_cast<Complex<float>*> (alloca (scratchSize)), d);
  112. }
  113. else
  114. {
  115. HeapBlock<char> heapSpace (scratchSize);
  116. performRealOnlyForwardTransform (reinterpret_cast<Complex<float>*> (heapSpace.getData()), d);
  117. }
  118. }
  119. void performRealOnlyInverseTransform (float* d) const noexcept override
  120. {
  121. if (size == 1)
  122. return;
  123. const size_t scratchSize = 16 + sizeof (Complex<float>) * (size_t) size;
  124. if (scratchSize < maxFFTScratchSpaceToAlloca)
  125. {
  126. performRealOnlyInverseTransform (static_cast<Complex<float>*> (alloca (scratchSize)), d);
  127. }
  128. else
  129. {
  130. HeapBlock<char> heapSpace (scratchSize);
  131. performRealOnlyInverseTransform (reinterpret_cast<Complex<float>*> (heapSpace.getData()), d);
  132. }
  133. }
  134. void performRealOnlyForwardTransform (Complex<float>* scratch, float* d) const noexcept
  135. {
  136. for (int i = 0; i < size; ++i)
  137. {
  138. scratch[i].real (d[i]);
  139. scratch[i].imag (0);
  140. }
  141. perform (scratch, reinterpret_cast<Complex<float>*> (d), false);
  142. }
  143. void performRealOnlyInverseTransform (Complex<float>* scratch, float* d) const noexcept
  144. {
  145. perform (reinterpret_cast<const Complex<float>*> (d), scratch, true);
  146. for (int i = 0; i < size; ++i)
  147. {
  148. d[i] = scratch[i].real();
  149. d[i + size] = scratch[i].imag();
  150. }
  151. }
  152. //==============================================================================
  153. struct FFTConfig
  154. {
  155. FFTConfig (int sizeOfFFT, bool isInverse)
  156. : fftSize (sizeOfFFT), inverse (isInverse), twiddleTable ((size_t) sizeOfFFT)
  157. {
  158. const double inverseFactor = (inverse ? 2.0 : -2.0) * double_Pi / (double) fftSize;
  159. if (fftSize <= 4)
  160. {
  161. for (int i = 0; i < fftSize; ++i)
  162. {
  163. const double phase = i * inverseFactor;
  164. twiddleTable[i].real ((float) std::cos (phase));
  165. twiddleTable[i].imag ((float) std::sin (phase));
  166. }
  167. }
  168. else
  169. {
  170. for (int i = 0; i < fftSize / 4; ++i)
  171. {
  172. const double phase = i * inverseFactor;
  173. twiddleTable[i].real ((float) std::cos (phase));
  174. twiddleTable[i].imag ((float) std::sin (phase));
  175. }
  176. for (int i = fftSize / 4; i < fftSize / 2; ++i)
  177. {
  178. const int index = i - fftSize / 4;
  179. twiddleTable[i].real (inverse ? -twiddleTable[index].imag() : twiddleTable[index].imag());
  180. twiddleTable[i].imag (inverse ? twiddleTable[index].real() : -twiddleTable[index].real());
  181. }
  182. twiddleTable[fftSize / 2].real (-1.0f);
  183. twiddleTable[fftSize / 2].imag (0.0f);
  184. for (int i = fftSize / 2; i < fftSize; ++i)
  185. {
  186. const int index = fftSize / 2 - (i - fftSize / 2);
  187. twiddleTable[i] = conj(twiddleTable[index]);
  188. }
  189. }
  190. const int root = (int) std::sqrt ((double) fftSize);
  191. int divisor = 4, n = fftSize;
  192. for (int i = 0; i < numElementsInArray (factors); ++i)
  193. {
  194. while ((n % divisor) != 0)
  195. {
  196. if (divisor == 2) divisor = 3;
  197. else if (divisor == 4) divisor = 2;
  198. else divisor += 2;
  199. if (divisor > root)
  200. divisor = n;
  201. }
  202. n /= divisor;
  203. jassert (divisor == 1 || divisor == 2 || divisor == 4);
  204. factors[i].radix = divisor;
  205. factors[i].length = n;
  206. }
  207. }
  208. void perform (const Complex<float>* input, Complex<float>* output) const noexcept
  209. {
  210. perform (input, output, 1, 1, factors);
  211. }
  212. const int fftSize;
  213. const bool inverse;
  214. struct Factor { int radix, length; };
  215. Factor factors[32];
  216. HeapBlock<Complex<float>> twiddleTable;
  217. void perform (const Complex<float>* input, Complex<float>* output, int stride, int strideIn, const Factor* facs) const noexcept
  218. {
  219. auto factor = *facs++;
  220. auto* originalOutput = output;
  221. auto* outputEnd = output + factor.radix * factor.length;
  222. if (stride == 1 && factor.radix <= 5)
  223. {
  224. for (int i = 0; i < factor.radix; ++i)
  225. perform (input + stride * strideIn * i, output + i * factor.length, stride * factor.radix, strideIn, facs);
  226. butterfly (factor, output, stride);
  227. return;
  228. }
  229. if (factor.length == 1)
  230. {
  231. do
  232. {
  233. *output++ = *input;
  234. input += stride * strideIn;
  235. }
  236. while (output < outputEnd);
  237. }
  238. else
  239. {
  240. do
  241. {
  242. perform (input, output, stride * factor.radix, strideIn, facs);
  243. input += stride * strideIn;
  244. output += factor.length;
  245. }
  246. while (output < outputEnd);
  247. }
  248. butterfly (factor, originalOutput, stride);
  249. }
  250. void butterfly (const Factor factor, Complex<float>* data, int stride) const noexcept
  251. {
  252. switch (factor.radix)
  253. {
  254. case 1: break;
  255. case 2: butterfly2 (data, stride, factor.length); return;
  256. case 4: butterfly4 (data, stride, factor.length); return;
  257. default: jassertfalse; break;
  258. }
  259. auto* scratch = static_cast<Complex<float>*> (alloca (sizeof (Complex<float>) * (size_t) factor.radix));
  260. for (int i = 0; i < factor.length; ++i)
  261. {
  262. for (int k = i, q1 = 0; q1 < factor.radix; ++q1)
  263. {
  264. scratch[q1] = data[k];
  265. k += factor.length;
  266. }
  267. for (int k = i, q1 = 0; q1 < factor.radix; ++q1)
  268. {
  269. int twiddleIndex = 0;
  270. data[k] = scratch[0];
  271. for (int q = 1; q < factor.radix; ++q)
  272. {
  273. twiddleIndex += stride * k;
  274. if (twiddleIndex >= fftSize)
  275. twiddleIndex -= fftSize;
  276. data[k] += scratch[q] * twiddleTable[twiddleIndex];
  277. }
  278. k += factor.length;
  279. }
  280. }
  281. }
  282. void butterfly2 (Complex<float>* data, const int stride, const int length) const noexcept
  283. {
  284. auto* dataEnd = data + length;
  285. auto* tw = twiddleTable.getData();
  286. for (int i = length; --i >= 0;)
  287. {
  288. auto s = *dataEnd;
  289. s *= (*tw);
  290. tw += stride;
  291. *dataEnd++ = *data - s;
  292. *data++ += s;
  293. }
  294. }
  295. void butterfly4 (Complex<float>* data, const int stride, const int length) const noexcept
  296. {
  297. auto lengthX2 = length * 2;
  298. auto lengthX3 = length * 3;
  299. auto strideX2 = stride * 2;
  300. auto strideX3 = stride * 3;
  301. auto* twiddle1 = twiddleTable.getData();
  302. auto* twiddle2 = twiddle1;
  303. auto* twiddle3 = twiddle1;
  304. for (int i = length; --i >= 0;)
  305. {
  306. auto s0 = data[length] * *twiddle1;
  307. auto s1 = data[lengthX2] * *twiddle2;
  308. auto s2 = data[lengthX3] * *twiddle3;
  309. auto s3 = s0; s3 += s2;
  310. auto s4 = s0; s4 -= s2;
  311. auto s5 = *data; s5 -= s1;
  312. *data += s1;
  313. data[lengthX2] = *data;
  314. data[lengthX2] -= s3;
  315. twiddle1 += stride;
  316. twiddle2 += strideX2;
  317. twiddle3 += strideX3;
  318. *data += s3;
  319. if (inverse)
  320. {
  321. data[length].real (s5.real() - s4.imag());
  322. data[length].imag (s5.imag() + s4.real());
  323. data[lengthX3].real (s5.real() + s4.imag());
  324. data[lengthX3].imag (s5.imag() - s4.real());
  325. }
  326. else
  327. {
  328. data[length].real (s5.real() + s4.imag());
  329. data[length].imag (s5.imag() - s4.real());
  330. data[lengthX3].real (s5.real() - s4.imag());
  331. data[lengthX3].imag (s5.imag() + s4.real());
  332. }
  333. ++data;
  334. }
  335. }
  336. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFTConfig)
  337. };
  338. //==============================================================================
  339. SpinLock processLock;
  340. ScopedPointer<FFTConfig> configForward, configInverse;
  341. int size;
  342. };
  343. FFT::EngineImpl<FFTFallback> fftFallback;
  344. //==============================================================================
  345. //==============================================================================
  346. #if (JUCE_MAC || JUCE_IOS) && JUCE_USE_VDSP_FRAMEWORK
  347. struct AppleFFT : public FFT::Instance
  348. {
  349. static constexpr int priority = 5;
  350. static AppleFFT* create (int order)
  351. {
  352. return new AppleFFT (order);
  353. }
  354. AppleFFT (int orderToUse)
  355. : order (static_cast<vDSP_Length> (orderToUse)),
  356. fftSetup (vDSP_create_fftsetup (order, 2)),
  357. forwardNormalisation (.5f),
  358. inverseNormalisation (1.0f / static_cast<float> (1 << order))
  359. {}
  360. ~AppleFFT() override
  361. {
  362. if (fftSetup != nullptr)
  363. {
  364. vDSP_destroy_fftsetup (fftSetup);
  365. fftSetup = nullptr;
  366. }
  367. }
  368. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept override
  369. {
  370. auto size = (1 << order);
  371. DSPSplitComplex splitInput (toSplitComplex (const_cast<Complex<float>*> (input)));
  372. DSPSplitComplex splitOutput (toSplitComplex (output));
  373. vDSP_fft_zop (fftSetup, &splitInput, 2, &splitOutput, 2,
  374. order, inverse ? kFFTDirection_Inverse : kFFTDirection_Forward);
  375. float factor = (inverse ? inverseNormalisation : forwardNormalisation * 2.0f);
  376. vDSP_vsmul ((float*) output, 1, &factor, (float*) output, 1, static_cast<size_t> (size << 1));
  377. }
  378. void performRealOnlyForwardTransform (float* inoutData) const noexcept override
  379. {
  380. auto size = (1 << order);
  381. auto* inout = reinterpret_cast<Complex<float>*> (inoutData);
  382. auto splitInOut (toSplitComplex (inout));
  383. inoutData[size] = 0.0f;
  384. vDSP_fft_zrip (fftSetup, &splitInOut, 2, order, kFFTDirection_Forward);
  385. vDSP_vsmul (inoutData, 1, &forwardNormalisation, inoutData, 1, static_cast<size_t> (size << 1));
  386. mirrorResult (inout);
  387. }
  388. void performRealOnlyInverseTransform (float* inoutData) const noexcept override
  389. {
  390. auto* inout = reinterpret_cast<Complex<float>*> (inoutData);
  391. auto size = (1 << order);
  392. auto splitInOut (toSplitComplex (inout));
  393. // Imaginary part of nyquist and DC frequencies are always zero
  394. // so Apple uses the imaginary part of the DC frequency to store
  395. // the real part of the nyquist frequency
  396. if (size != 1)
  397. inout[0] = Complex<float> (inout[0].real(), inout[size >> 1].real());
  398. vDSP_fft_zrip (fftSetup, &splitInOut, 2, order, kFFTDirection_Inverse);
  399. vDSP_vsmul (inoutData, 1, &inverseNormalisation, inoutData, 1, static_cast<size_t> (size << 1));
  400. vDSP_vclr (inoutData + size, 1, static_cast<size_t> (size));
  401. }
  402. private:
  403. //==============================================================================
  404. void mirrorResult (Complex<float>* out) const noexcept
  405. {
  406. auto size = (1 << order);
  407. auto i = size >> 1;
  408. // Imaginary part of nyquist and DC frequencies are always zero
  409. // so Apple uses the imaginary part of the DC frequency to store
  410. // the real part of the nyquist frequency
  411. out[i++] = { out[0].imag(), 0.0 };
  412. out[0] = { out[0].real(), 0.0 };
  413. for (; i < size; ++i)
  414. out[i] = std::conj (out[size - i]);
  415. }
  416. static DSPSplitComplex toSplitComplex (Complex<float>* data) noexcept
  417. {
  418. // this assumes that Complex interleaves real and imaginary parts
  419. // and is tightly packed.
  420. return { reinterpret_cast<float*> (data),
  421. reinterpret_cast<float*> (data) + 1};
  422. }
  423. //==============================================================================
  424. vDSP_Length order;
  425. FFTSetup fftSetup;
  426. float forwardNormalisation, inverseNormalisation;
  427. };
  428. FFT::EngineImpl<AppleFFT> appleFFT;
  429. #endif
  430. //==============================================================================
  431. //==============================================================================
  432. #if JUCE_DSP_USE_SHARED_FFTW || JUCE_DSP_USE_STATIC_FFTW
  433. #if JUCE_DSP_USE_STATIC_FFTW
  434. extern "C"
  435. {
  436. void* fftwf_plan_dft_1d (int, void*, void*, int, int);
  437. void* fftwf_plan_dft_r2c_1d (int, void*, void*, int);
  438. void* fftwf_plan_dft_c2r_1d (int, void*, void*, int);
  439. void fftwf_destroy_plan (void*);
  440. void fftwf_execute_dft (void*, void*, void*);
  441. void fftwf_execute_dft_r2c (void*, void*, void*);
  442. void fftwf_execute_dft_c2r (void*, void*, void*);
  443. }
  444. #endif
  445. struct FFTWImpl : public FFT::Instance
  446. {
  447. #if JUCE_DSP_USE_STATIC_FFTW
  448. // if the JUCE developer has gone through the hassle of statically
  449. // linking in fftw, they probably want to use it
  450. static constexpr int priority = 10;
  451. #else
  452. static constexpr int priority = 3;
  453. #endif
  454. struct FFTWPlan;
  455. using FFTWPlanRef = FFTWPlan*;
  456. enum
  457. {
  458. measure = 0,
  459. unaligned = (1 << 1),
  460. estimate = (1 << 6)
  461. };
  462. struct Symbols
  463. {
  464. FFTWPlanRef (*plan_dft_fftw) (unsigned, Complex<float>*, Complex<float>*, int, unsigned);
  465. FFTWPlanRef (*plan_r2c_fftw) (unsigned, float*, Complex<float>*, unsigned);
  466. FFTWPlanRef (*plan_c2r_fftw) (unsigned, Complex<float>*, float*, unsigned);
  467. void (*destroy_fftw) (FFTWPlanRef);
  468. void (*execute_dft_fftw) (FFTWPlanRef, const Complex<float>*, Complex<float>*);
  469. void (*execute_r2c_fftw) (FFTWPlanRef, float*, Complex<float>*);
  470. void (*execute_c2r_fftw) (FFTWPlanRef, Complex<float>*, float*);
  471. #if JUCE_DSP_USE_STATIC_FFTW
  472. template <typename FuncPtr, typename ActualSymbolType>
  473. static bool symbol (FuncPtr& dst, ActualSymbolType sym)
  474. {
  475. dst = reinterpret_cast<FuncPtr> (sym);
  476. return true;
  477. }
  478. #else
  479. template <typename FuncPtr>
  480. static bool symbol (DynamicLibrary& lib, FuncPtr& dst, const char* name)
  481. {
  482. dst = reinterpret_cast<FuncPtr> (lib.getFunction (name));
  483. return (dst != nullptr);
  484. }
  485. #endif
  486. };
  487. static FFTWImpl* create (int order)
  488. {
  489. DynamicLibrary lib;
  490. #if ! JUCE_DSP_USE_STATIC_FFTW
  491. #if JUCE_MAC
  492. const char* libsuffix = "dylib";
  493. #elif JUCE_WINDOWS
  494. const char* libsuffix = "dll";
  495. #else
  496. const char* libsuffix = "so";
  497. #endif
  498. if (lib.open (String ("libfftw3f.") + libsuffix))
  499. #endif
  500. {
  501. Symbols symbols;
  502. #if JUCE_DSP_USE_STATIC_FFTW
  503. if (! Symbols::symbol (symbols.plan_dft_fftw, fftwf_plan_dft_1d)) return nullptr;
  504. if (! Symbols::symbol (symbols.plan_r2c_fftw, fftwf_plan_dft_r2c_1d)) return nullptr;
  505. if (! Symbols::symbol (symbols.plan_c2r_fftw, fftwf_plan_dft_c2r_1d)) return nullptr;
  506. if (! Symbols::symbol (symbols.destroy_fftw, fftwf_destroy_plan)) return nullptr;
  507. if (! Symbols::symbol (symbols.execute_dft_fftw, fftwf_execute_dft)) return nullptr;
  508. if (! Symbols::symbol (symbols.execute_r2c_fftw, fftwf_execute_dft_r2c)) return nullptr;
  509. if (! Symbols::symbol (symbols.execute_c2r_fftw, fftwf_execute_dft_c2r)) return nullptr;
  510. #else
  511. if (! Symbols::symbol (lib, symbols.plan_dft_fftw, "fftwf_plan_dft_1d")) return nullptr;
  512. if (! Symbols::symbol (lib, symbols.plan_r2c_fftw, "fftwf_plan_dft_r2c_1d")) return nullptr;
  513. if (! Symbols::symbol (lib, symbols.plan_c2r_fftw, "fftwf_plan_dft_c2r_1d")) return nullptr;
  514. if (! Symbols::symbol (lib, symbols.destroy_fftw, "fftwf_destroy_plan")) return nullptr;
  515. if (! Symbols::symbol (lib, symbols.execute_dft_fftw, "fftwf_execute_dft")) return nullptr;
  516. if (! Symbols::symbol (lib, symbols.execute_r2c_fftw, "fftwf_execute_dft_r2c")) return nullptr;
  517. if (! Symbols::symbol (lib, symbols.execute_c2r_fftw, "fftwf_execute_dft_c2r")) return nullptr;
  518. #endif
  519. return new FFTWImpl (static_cast<size_t> (order), static_cast<DynamicLibrary&&> (lib), symbols);
  520. }
  521. return nullptr;
  522. }
  523. FFTWImpl (size_t orderToUse, DynamicLibrary&& libraryToUse, const Symbols& symbols)
  524. : fftwLibrary (std::move (libraryToUse)), fftw (symbols), order (static_cast<size_t> (orderToUse))
  525. {
  526. auto n = (1u << order);
  527. HeapBlock<Complex<float>> in (n), out (n);
  528. c2cForward = fftw.plan_dft_fftw (n, in.getData(), out.getData(), -1, unaligned | estimate);
  529. c2cInverse = fftw.plan_dft_fftw (n, in.getData(), out.getData(), +1, unaligned | estimate);
  530. r2c = fftw.plan_r2c_fftw (n, (float*) in.getData(), in.getData(), unaligned | estimate);
  531. c2r = fftw.plan_c2r_fftw (n, in.getData(), (float*) in.getData(), unaligned | estimate);
  532. }
  533. ~FFTWImpl() override
  534. {
  535. fftw.destroy_fftw (c2cForward);
  536. fftw.destroy_fftw (c2cInverse);
  537. fftw.destroy_fftw (r2c);
  538. fftw.destroy_fftw (c2r);
  539. }
  540. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept override
  541. {
  542. if (inverse)
  543. {
  544. auto n = (1u << order);
  545. fftw.execute_dft_fftw (c2cInverse, input, output);
  546. FloatVectorOperations::multiply ((float*) output, 1.0f / static_cast<float> (n), (int) n << 1);
  547. }
  548. else
  549. {
  550. fftw.execute_dft_fftw (c2cForward, input, output);
  551. }
  552. }
  553. void performRealOnlyForwardTransform (float* inputOutputData) const noexcept override
  554. {
  555. if (order == 0)
  556. return;
  557. auto* out = reinterpret_cast<Complex<float>*> (inputOutputData);
  558. fftw.execute_r2c_fftw (r2c, inputOutputData, out);
  559. auto size = (1 << order);
  560. for (auto i = size >> 1; i < size; ++i)
  561. out[i] = std::conj (out[size - i]);
  562. }
  563. void performRealOnlyInverseTransform (float* inputOutputData) const noexcept override
  564. {
  565. auto n = (1u << order);
  566. fftw.execute_c2r_fftw (c2r, (Complex<float>*) inputOutputData, inputOutputData);
  567. FloatVectorOperations::multiply ((float*) inputOutputData, 1.0f / static_cast<float> (n), (int) n);
  568. }
  569. //==============================================================================
  570. DynamicLibrary fftwLibrary;
  571. Symbols fftw;
  572. size_t order;
  573. FFTWPlanRef c2cForward, c2cInverse, r2c, c2r;
  574. };
  575. FFT::EngineImpl<FFTWImpl> fftwEngine;
  576. #endif
  577. //==============================================================================
  578. //==============================================================================
  579. #if JUCE_DSP_USE_INTEL_MKL
  580. struct IntelFFT : public FFT::Instance
  581. {
  582. static constexpr int priority = 8;
  583. static bool succeeded (MKL_LONG status) noexcept { return status == 0; }
  584. static IntelFFT* create (int orderToUse)
  585. {
  586. DFTI_DESCRIPTOR_HANDLE mklc2c, mklc2r;
  587. if (DftiCreateDescriptor (&mklc2c, DFTI_SINGLE, DFTI_COMPLEX, 1, 1 << orderToUse) == 0)
  588. {
  589. if (succeeded (DftiSetValue (mklc2c, DFTI_PLACEMENT, DFTI_NOT_INPLACE))
  590. && succeeded (DftiSetValue (mklc2c, DFTI_BACKWARD_SCALE, 1.0f / static_cast<float> (1 << orderToUse)))
  591. && succeeded (DftiCommitDescriptor (mklc2c)))
  592. {
  593. if (succeeded (DftiCreateDescriptor (&mklc2r, DFTI_SINGLE, DFTI_REAL, 1, 1 << orderToUse)))
  594. {
  595. if (succeeded (DftiSetValue (mklc2r, DFTI_PLACEMENT, DFTI_INPLACE))
  596. && succeeded (DftiSetValue (mklc2r, DFTI_BACKWARD_SCALE, 1.0f / static_cast<float> (1 << orderToUse)))
  597. && succeeded (DftiCommitDescriptor (mklc2r)))
  598. {
  599. return new IntelFFT (static_cast<size_t> (orderToUse), mklc2c, mklc2r);
  600. }
  601. DftiFreeDescriptor (&mklc2r);
  602. }
  603. }
  604. DftiFreeDescriptor (&mklc2c);
  605. }
  606. return {};
  607. }
  608. IntelFFT (size_t orderToUse, DFTI_DESCRIPTOR_HANDLE c2cToUse, DFTI_DESCRIPTOR_HANDLE cr2ToUse)
  609. : order (orderToUse), c2c (c2cToUse), c2r (cr2ToUse)
  610. {}
  611. ~IntelFFT()
  612. {
  613. DftiFreeDescriptor (&c2c);
  614. DftiFreeDescriptor (&c2r);
  615. }
  616. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept override
  617. {
  618. if (inverse)
  619. DftiComputeBackward (c2c, (void*) input, output);
  620. else
  621. DftiComputeForward (c2c, (void*) input, output);
  622. }
  623. void performRealOnlyForwardTransform (float* inputOutputData) const noexcept override
  624. {
  625. if (order == 0)
  626. return;
  627. DftiComputeForward (c2r, inputOutputData);
  628. auto* out = reinterpret_cast<Complex<float>*> (inputOutputData);
  629. auto size = (1 << order);
  630. for (auto i = size >> 1; i < size; ++i)
  631. out[i] = std::conj (out[size - i]);
  632. }
  633. void performRealOnlyInverseTransform (float* inputOutputData) const noexcept override
  634. {
  635. DftiComputeBackward (c2r, inputOutputData);
  636. }
  637. size_t order;
  638. DFTI_DESCRIPTOR_HANDLE c2c, c2r;
  639. };
  640. FFT::EngineImpl<IntelFFT> fftwEngine;
  641. #endif
  642. //==============================================================================
  643. //==============================================================================
  644. FFT::FFT (int order)
  645. : engine (FFT::Engine::createBestEngineForPlatform (order)),
  646. size (1 << order)
  647. {}
  648. FFT::~FFT() {}
  649. void FFT::perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept
  650. {
  651. if (engine != nullptr)
  652. engine->perform (input, output, inverse);
  653. }
  654. void FFT::performRealOnlyForwardTransform (float* inputOutputData) const noexcept
  655. {
  656. if (engine != nullptr)
  657. engine->performRealOnlyForwardTransform (inputOutputData);
  658. }
  659. void FFT::performRealOnlyInverseTransform (float* inputOutputData) const noexcept
  660. {
  661. if (engine != nullptr)
  662. engine->performRealOnlyInverseTransform (inputOutputData);
  663. }
  664. void FFT::performFrequencyOnlyForwardTransform (float* inputOutputData) const noexcept
  665. {
  666. if (size == 1)
  667. return;
  668. performRealOnlyForwardTransform (inputOutputData);
  669. auto* out = reinterpret_cast<Complex<float>*> (inputOutputData);
  670. for (auto i = 0; i < size; ++i)
  671. inputOutputData[i] = std::abs (out[i]);
  672. zeromem (&inputOutputData[size], sizeof (float) * static_cast<size_t> (size));
  673. }