pffft_wrapper.h (3203B)
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 11 #ifndef MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_ 12 #define MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_ 13 14 #include <cstddef> 15 #include <memory> 16 17 #include "api/array_view.h" 18 19 // Forward declaration. 20 struct PFFFT_Setup; 21 22 namespace webrtc { 23 24 // Pretty-Fast Fast Fourier Transform (PFFFT) wrapper class. 25 // Not thread safe. 26 class Pffft { 27 public: 28 enum class FftType { kReal, kComplex }; 29 30 // 1D floating point buffer used as input/output data type for the FFT ops. 31 // It must be constructed using Pffft::CreateBuffer(). 32 class FloatBuffer { 33 public: 34 FloatBuffer(const FloatBuffer&) = delete; 35 FloatBuffer& operator=(const FloatBuffer&) = delete; 36 ~FloatBuffer(); 37 38 ArrayView<const float> GetConstView() const; 39 ArrayView<float> GetView(); 40 41 private: 42 friend class Pffft; 43 FloatBuffer(size_t fft_size, FftType fft_type); 44 const float* const_data() const { return data_; } 45 float* data() { return data_; } 46 size_t size() const { return size_; } 47 48 const size_t size_; 49 float* const data_; 50 }; 51 52 // TODO(https://crbug.com/webrtc/9577): Consider adding a factory and making 53 // the ctor private. 54 // static std::unique_ptr<Pffft> Create(size_t fft_size, 55 // FftType fft_type); Ctor. `fft_size` must be a supported size (see 56 // Pffft::IsValidFftSize()). If not supported, the code will crash. 57 Pffft(size_t fft_size, FftType fft_type); 58 Pffft(const Pffft&) = delete; 59 Pffft& operator=(const Pffft&) = delete; 60 ~Pffft(); 61 62 // Returns true if the FFT size is supported. 63 static bool IsValidFftSize(size_t fft_size, FftType fft_type); 64 65 // Returns true if SIMD code optimizations are being used. 66 static bool IsSimdEnabled(); 67 68 // Creates a buffer of the right size. 69 std::unique_ptr<FloatBuffer> CreateBuffer() const; 70 71 // TODO(https://crbug.com/webrtc/9577): Overload with ArrayView args. 72 // Computes the forward fast Fourier transform. 73 void ForwardTransform(const FloatBuffer& in, FloatBuffer* out, bool ordered); 74 // Computes the backward fast Fourier transform. 75 void BackwardTransform(const FloatBuffer& in, FloatBuffer* out, bool ordered); 76 77 // Multiplies the frequency components of `fft_x` and `fft_y` and accumulates 78 // them into `out`. The arrays must have been obtained with 79 // ForwardTransform(..., /*ordered=*/false) - i.e., `fft_x` and `fft_y` must 80 // not be ordered. 81 void FrequencyDomainConvolve(const FloatBuffer& fft_x, 82 const FloatBuffer& fft_y, 83 FloatBuffer* out, 84 float scaling = 1.f); 85 86 private: 87 const size_t fft_size_; 88 const FftType fft_type_; 89 PFFFT_Setup* pffft_status_; 90 float* const scratch_buffer_; 91 }; 92 93 } // namespace webrtc 94 95 #endif // MODULES_AUDIO_PROCESSING_UTILITY_PFFFT_WRAPPER_H_