andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef COMMON_AUDIO_REAL_FOURIER_H_ |
| 12 | #define COMMON_AUDIO_REAL_FOURIER_H_ |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 16 | #include <complex> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 17 | #include <memory> |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 18 | |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 19 | #include "rtc_base/memory/aligned_malloc.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 20 | |
| 21 | // Uniform interface class for the real DFT and its inverse, for power-of-2 |
| 22 | // input lengths. Also contains helper functions for buffer allocation, taking |
| 23 | // care of any memory alignment requirements the underlying library might have. |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | class RealFourier { |
| 28 | public: |
| 29 | // Shorthand typenames for the scopers used by the buffer allocation helpers. |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 30 | typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper; |
| 31 | typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter> |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 32 | fft_cplx_scoper; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 34 | // The alignment required for all input and output buffers, in bytes. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 35 | static const size_t kFftBufferAlignment; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 36 | |
| 37 | // Construct a wrapper instance for the given input order, which must be |
| 38 | // between 1 and kMaxFftOrder, inclusively. |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 39 | static std::unique_ptr<RealFourier> Create(int fft_order); |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 40 | virtual ~RealFourier() {} |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 41 | |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 42 | // Helper to compute the smallest FFT order (a power of 2) which will contain |
| 43 | // the given input length. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 44 | static int FftOrder(size_t length); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 45 | |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 46 | // Helper to compute the input length from the FFT order. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | static size_t FftLength(int order); |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 48 | |
| 49 | // Helper to compute the exact length, in complex floats, of the transform |
| 50 | // output (i.e. |2^order / 2 + 1|). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | static size_t ComplexLength(int order); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 52 | |
| 53 | // Buffer allocation helpers. The buffers are large enough to hold |count| |
| 54 | // floats/complexes and suitably aligned for use by the implementation. |
| 55 | // The returned scopers are set up with proper deleters; the caller owns |
| 56 | // the allocated memory. |
| 57 | static fft_real_scoper AllocRealBuffer(int count); |
| 58 | static fft_cplx_scoper AllocCplxBuffer(int count); |
| 59 | |
| 60 | // Main forward transform interface. The output array need only be big |
| 61 | // enough for |2^order / 2 + 1| elements - the conjugate pairs are not |
| 62 | // returned. Input and output must be properly aligned (e.g. through |
| 63 | // AllocRealBuffer and AllocCplxBuffer) and input length must be |
| 64 | // |2^order| (same as given at construction time). |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 65 | virtual void Forward(const float* src, std::complex<float>* dest) const = 0; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 66 | |
| 67 | // Inverse transform. Same input format as output above, conjugate pairs |
| 68 | // not needed. |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 69 | virtual void Inverse(const std::complex<float>* src, float* dest) const = 0; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 70 | |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 71 | virtual int order() const = 0; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace webrtc |
| 75 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 76 | #endif // COMMON_AUDIO_REAL_FOURIER_H_ |