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.

133 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. /**
  23. Performs a fast fourier transform.
  24. This is only a simple low-footprint implementation and isn't tuned for speed - it may
  25. be useful for simple applications where one of the more complex FFT libraries would be
  26. overkill. (But in the future it may end up becoming optimised of course...)
  27. The FFT class itself contains lookup tables, so there's some overhead in creating
  28. one, you should create and cache an FFT object for each size/direction of transform
  29. that you need, and re-use them to perform the actual operation.
  30. @tags{DSP}
  31. */
  32. class JUCE_API FFT
  33. {
  34. public:
  35. //==============================================================================
  36. /** Initialises an object for performing forward and inverse FFT with the given size.
  37. The number of points the FFT will operate on will be 2 ^ order.
  38. */
  39. FFT (int order);
  40. /** Move constructor. */
  41. FFT (FFT&&) noexcept;
  42. /** Move assignment operator. */
  43. FFT& operator= (FFT&&) noexcept;
  44. /** Destructor. */
  45. ~FFT();
  46. //==============================================================================
  47. /** Performs an out-of-place FFT, either forward or inverse.
  48. The arrays must contain at least getSize() elements.
  49. */
  50. void perform (const Complex<float>* input, Complex<float>* output, bool inverse) const noexcept;
  51. /** Performs an in-place forward transform on a block of real data.
  52. As the coefficients of the negative frequencies (frequencies higher than
  53. N/2 or pi) are the complex conjugate of their positive counterparts,
  54. it may not be necessary to calculate them for your particular application.
  55. You can use onlyCalculateNonNegativeFrequencies to let the FFT
  56. engine know that you do not plan on using them. Note that this is only a
  57. hint: some FFT engines (currently only the Fallback engine), will still
  58. calculate the negative frequencies even if onlyCalculateNonNegativeFrequencies
  59. is true.
  60. The size of the array passed in must be 2 * getSize(), and the first half
  61. should contain your raw input sample data. On return, if
  62. onlyCalculateNonNegativeFrequencies is false, the array will contain size
  63. complex real + imaginary parts data interleaved. If
  64. onlyCalculateNonNegativeFrequencies is true, the array will contain at least
  65. (size / 2) + 1 complex numbers. Both outputs can be passed to
  66. performRealOnlyInverseTransform() in order to convert it back to reals.
  67. */
  68. void performRealOnlyForwardTransform (float* inputOutputData,
  69. bool onlyCalculateNonNegativeFrequencies = false) const noexcept;
  70. /** Performs a reverse operation to data created in performRealOnlyForwardTransform().
  71. Although performRealOnlyInverseTransform will only use the first ((size / 2) + 1)
  72. complex numbers, the size of the array passed in must still be 2 * getSize(), as some
  73. FFT engines require the extra space for the calculation. On return, the first half of the
  74. array will contain the reconstituted samples.
  75. */
  76. void performRealOnlyInverseTransform (float* inputOutputData) const noexcept;
  77. /** Takes an array and simply transforms it to the magnitude frequency response
  78. spectrum. This may be handy for things like frequency displays or analysis.
  79. The size of the array passed in must be 2 * getSize().
  80. On return, if onlyCalculateNonNegativeFrequencies is false, the array will contain size
  81. magnitude values. If onlyCalculateNonNegativeFrequencies is true, the array will contain
  82. at least size / 2 + 1 magnitude values.
  83. */
  84. void performFrequencyOnlyForwardTransform (float* inputOutputData,
  85. bool onlyCalculateNonNegativeFrequencies = false) const noexcept;
  86. /** Returns the number of data points that this FFT was created to work with. */
  87. int getSize() const noexcept { return size; }
  88. //==============================================================================
  89. #ifndef DOXYGEN
  90. /* internal */
  91. struct Instance;
  92. template <typename> struct EngineImpl;
  93. #endif
  94. private:
  95. //==============================================================================
  96. struct Engine;
  97. std::unique_ptr<Instance> engine;
  98. int size;
  99. //==============================================================================
  100. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFT)
  101. };
  102. } // namespace dsp
  103. } // namespace juce