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.

123 lines
5.0KB

  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. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /**
  24. Performs a fast fourier transform.
  25. This is only a simple low-footprint implementation and isn't tuned for speed - it may
  26. be useful for simple applications where one of the more complex FFT libraries would be
  27. overkill. (But in the future it may end up becoming optimised of course...)
  28. The FFT class itself contains lookup tables, so there's some overhead in creating
  29. one, you should create and cache an FFT object for each size/direction of transform
  30. that you need, and re-use them to perform the actual operation.
  31. @tags{DSP}
  32. */
  33. class JUCE_API FFT
  34. {
  35. public:
  36. //==============================================================================
  37. /** Initialises an object for performing forward and inverse FFT with the given size.
  38. The number of points the FFT will operate on will be 2 ^ order.
  39. */
  40. FFT (int order);
  41. /** Destructor. */
  42. ~FFT();
  43. //==============================================================================
  44. /** Performs an out-of-place FFT, either forward or inverse.
  45. The arrays must contain at least getSize() elements.
  46. */
  47. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept;
  48. /** Performs an in-place forward transform on a block of real data.
  49. As the coefficients of the negative frequences (frequencies higher than
  50. N/2 or pi) are the complex conjugate of their positive counterparts,
  51. it may not be necessary to calculate them for your particular application.
  52. You can use dontCalculateNegativeFrequencies to let the FFT
  53. engine know that you do not plan on using them. Note that this is only a
  54. hint: some FFT engines (currently only the Fallback engine), will still
  55. calculate the negative frequencies even if dontCalculateNegativeFrequencies
  56. is true.
  57. The size of the array passed in must be 2 * getSize(), and the first half
  58. should contain your raw input sample data. On return, if
  59. dontCalculateNegativeFrequencies is false, the array will contain size
  60. complex real + imaginary parts data interleaved. If
  61. dontCalculateNegativeFrequencies is true, the array will contain at least
  62. (size / 2) + 1 complex numbers. Both outputs can be passed to
  63. performRealOnlyInverseTransform() in order to convert it back to reals.
  64. */
  65. void performRealOnlyForwardTransform (float* inputOutputData,
  66. bool dontCalculateNegativeFrequencies = false) const noexcept;
  67. /** Performs a reverse operation to data created in performRealOnlyForwardTransform().
  68. Although performRealOnlyInverseTransform will only use the first ((size / 2) + 1)
  69. complex numbers, the size of the array passed in must still be 2 * getSize(), as some
  70. FFT engines require the extra space for the calculation. On return, the first half of the
  71. array will contain the reconstituted samples.
  72. */
  73. void performRealOnlyInverseTransform (float* inputOutputData) const noexcept;
  74. /** Takes an array and simply transforms it to the magnitude frequency response
  75. spectrum. This may be handy for things like frequency displays or analysis.
  76. The size of the array passed in must be 2 * getSize().
  77. */
  78. void performFrequencyOnlyForwardTransform (float* inputOutputData) const noexcept;
  79. /** Returns the number of data points that this FFT was created to work with. */
  80. int getSize() const noexcept { return size; }
  81. //==============================================================================
  82. #ifndef DOXYGEN
  83. /* internal */
  84. struct Instance;
  85. template <typename> struct EngineImpl;
  86. #endif
  87. private:
  88. //==============================================================================
  89. struct Engine;
  90. std::unique_ptr<Instance> engine;
  91. int size;
  92. //==============================================================================
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFT)
  94. };
  95. } // namespace dsp
  96. } // namespace juce