pffft_wrapper.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_
  11. #define MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_
  12. #include <memory>
  13. #include "api/array_view.h"
  14. // Forward declaration.
  15. struct PFFFT_Setup;
  16. namespace webrtc {
  17. // Pretty-Fast Fast Fourier Transform (PFFFT) wrapper class.
  18. // Not thread safe.
  19. class Pffft {
  20. public:
  21. enum class FftType { kReal, kComplex };
  22. // 1D floating point buffer used as input/output data type for the FFT ops.
  23. // It must be constructed using Pffft::CreateBuffer().
  24. class FloatBuffer {
  25. public:
  26. FloatBuffer(const FloatBuffer&) = delete;
  27. FloatBuffer& operator=(const FloatBuffer&) = delete;
  28. ~FloatBuffer();
  29. rtc::ArrayView<const float> GetConstView() const;
  30. rtc::ArrayView<float> GetView();
  31. private:
  32. friend class Pffft;
  33. FloatBuffer(size_t fft_size, FftType fft_type);
  34. const float* const_data() const { return data_; }
  35. float* data() { return data_; }
  36. size_t size() const { return size_; }
  37. const size_t size_;
  38. float* const data_;
  39. };
  40. // TODO(https://crbug.com/webrtc/9577): Consider adding a factory and making
  41. // the ctor private.
  42. // static std::unique_ptr<Pffft> Create(size_t fft_size,
  43. // FftType fft_type); Ctor. |fft_size| must be a supported size (see
  44. // Pffft::IsValidFftSize()). If not supported, the code will crash.
  45. Pffft(size_t fft_size, FftType fft_type);
  46. Pffft(const Pffft&) = delete;
  47. Pffft& operator=(const Pffft&) = delete;
  48. ~Pffft();
  49. // Returns true if the FFT size is supported.
  50. static bool IsValidFftSize(size_t fft_size, FftType fft_type);
  51. // Returns true if SIMD code optimizations are being used.
  52. static bool IsSimdEnabled();
  53. // Creates a buffer of the right size.
  54. std::unique_ptr<FloatBuffer> CreateBuffer() const;
  55. // TODO(https://crbug.com/webrtc/9577): Overload with rtc::ArrayView args.
  56. // Computes the forward fast Fourier transform.
  57. void ForwardTransform(const FloatBuffer& in, FloatBuffer* out, bool ordered);
  58. // Computes the backward fast Fourier transform.
  59. void BackwardTransform(const FloatBuffer& in, FloatBuffer* out, bool ordered);
  60. // Multiplies the frequency components of |fft_x| and |fft_y| and accumulates
  61. // them into |out|. The arrays must have been obtained with
  62. // ForwardTransform(..., /*ordered=*/false) - i.e., |fft_x| and |fft_y| must
  63. // not be ordered.
  64. void FrequencyDomainConvolve(const FloatBuffer& fft_x,
  65. const FloatBuffer& fft_y,
  66. FloatBuffer* out,
  67. float scaling = 1.f);
  68. private:
  69. const size_t fft_size_;
  70. const FftType fft_type_;
  71. PFFFT_Setup* pffft_status_;
  72. float* const scratch_buffer_;
  73. };
  74. } // namespace webrtc
  75. #endif // MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_