• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef COMMON_AUDIO_REAL_FOURIER_H_
12 #define COMMON_AUDIO_REAL_FOURIER_H_
13 
14 #include <stddef.h>
15 
16 #include <complex>
17 #include <memory>
18 
19 #include "rtc_base/memory/aligned_malloc.h"
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.
30   typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
31   typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
32       fft_cplx_scoper;
33 
34   // The alignment required for all input and output buffers, in bytes.
35   static const size_t kFftBufferAlignment;
36 
37   // Construct a wrapper instance for the given input order, which must be
38   // between 1 and kMaxFftOrder, inclusively.
39   static std::unique_ptr<RealFourier> Create(int fft_order);
~RealFourier()40   virtual ~RealFourier() {}
41 
42   // Helper to compute the smallest FFT order (a power of 2) which will contain
43   // the given input length.
44   static int FftOrder(size_t length);
45 
46   // Helper to compute the input length from the FFT order.
47   static size_t FftLength(int order);
48 
49   // Helper to compute the exact length, in complex floats, of the transform
50   // output (i.e. |2^order / 2 + 1|).
51   static size_t ComplexLength(int order);
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).
65   virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
66 
67   // Inverse transform. Same input format as output above, conjugate pairs
68   // not needed.
69   virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
70 
71   virtual int order() const = 0;
72 };
73 
74 }  // namespace webrtc
75 
76 #endif  // COMMON_AUDIO_REAL_FOURIER_H_
77