forked from HiFi-LoFi/FFTConvolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioFFT.h
executable file
·290 lines (243 loc) · 8.31 KB
/
AudioFFT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// ==================================================================================
// Copyright (c) 2013 HiFi-LoFi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ==================================================================================
#ifndef _AUDIOFFT_H
#define _AUDIOFFT_H
/**
* AudioFFT provides real-to-complex/complex-to-real FFT routines.
*
* Features:
*
* - Real-complex FFT and complex-real inverse FFT for power-of-2-sized real data.
*
* - Uniform interface to different FFT implementations (currently Ooura, FFTW3 and Apple Accelerate).
*
* - Complex data is handled in "split-complex" format, i.e. there are separate
* arrays for the real and imaginary parts which can be useful for SIMD optimizations
* (split-complex arrays have to be of length (size/2+1) representing bins from DC
* to Nyquist frequency).
*
* - Output is "ready to use" (all scaling etc. is already handled internally).
*
* - No allocations/deallocations after the initialization which makes it usable
* for real-time audio applications (that's what I wrote it for and using it).
*
*
* How to use it in your project:
*
* - Add the .h and .cpp file to your project - that's all.
*
* - To get extra speed, you can link FFTW3 to your project and define
* AUDIOFFT_FFTW3 (however, please check whether your project suits the
* according license).
*
* - To get the best speed on Apple platforms, you can link the Apple
* Accelerate framework to your project and define
* AUDIOFFT_APPLE_ACCELERATE (however, please check whether your
* project suits the according license).
*
*
* Remarks:
*
* - AudioFFT is not intended to be the fastest FFT, but to be a fast-enough
* FFT suitable for most audio applications.
*
* - AudioFFT uses the quite liberal MIT license.
*
*
* Example usage:
* @code
* #include "AudioFFT.h"
*
* void Example()
* {
* const size_t fftSize = 1024; // Needs to be power of 2!
*
* std::vector<float> input(fftSize, 0.0f);
* std::vector<float> re(fftaudio::AudioFFT::ComplexSize(fftSize));
* std::vector<float> im(fftaudio::AudioFFT::ComplexSize(fftSize));
* std::vector<float> output(fftSize);
*
* audiofft::AudioFFT fft;
* fft.init(1024);
* fft.fft(input.data(), re.data(), im.data());
* fft.ifft(output.data(), re.data(), im.data());
* }
* @endcode
*/
#include <cstddef>
#if defined(AUDIOFFT_APPLE_ACCELERATE)
#define AUDIOFFT_APPLE_ACCELERATE_USED
#include <Accelerate/Accelerate.h>
#include <vector>
#elif defined (AUDIOFFT_FFTW3)
#define AUDIOFFT_FFTW3_USED
#include <fftw3.h>
#else
#ifndef AUDIOFFT_OOURA
#define AUDIOFFT_OOURA
#endif
#define AUDIOFFT_OOURA_USED
#include <vector>
#endif
namespace audiofft
{
/**
* @class AudioFFTBase
* @brief Interface class for FFT implementations
*/
class AudioFFTBase
{
public:
/**
* @brief Constructor
*/
AudioFFTBase()
{
}
/**
* @brief Destructor
*/
virtual ~AudioFFTBase()
{
}
/**
* @brief Initializes the FFT object
* @param size Size of the real input (must be power 2)
*/
virtual void init(size_t size) = 0;
/**
* @brief Performs the forward FFT
* @param data The real input data (has to be of the length as specified in init())
* @param re The real part of the complex output (has to be of length as returned by ComplexSize())
* @param im The imaginary part of the complex output (has to be of length as returned by ComplexSize())
*/
virtual void fft(const float* data, float* re, float* im) = 0;
/**
* @brief Performs the inverse FFT
* @param data The real output data (has to be of the length as specified in init())
* @param re The real part of the complex input (has to be of length as returned by ComplexSize())
* @param im The imaginary part of the complex input (has to be of length as returned by ComplexSize())
*/
virtual void ifft(float* data, const float* re, const float* im) = 0;
/**
* @brief Calculates the necessary size of the real/imaginary complex arrays
* @param size The size of the real data
* @return The size of the real/imaginary complex arrays
*/
static size_t ComplexSize(size_t size)
{
return (size / 2) + 1;
}
private:
AudioFFTBase(const AudioFFTBase&);
AudioFFTBase& operator=(const AudioFFTBase&);
};
// ================================================================
#ifdef AUDIOFFT_APPLE_ACCELERATE_USED
/**
* @class AppleAccelerateFFT
* @brief FFT implementation using the Apple Accelerate framework internally
*/
class AppleAccelerateFFT : public AudioFFTBase
{
public:
AppleAccelerateFFT();
virtual ~AppleAccelerateFFT();
virtual void init(size_t size);
virtual void fft(const float* data, float* re, float* im);
virtual void ifft(float* data, const float* re, const float* im);
private:
size_t _size;
size_t _powerOf2;
FFTSetup _fftSetup;
std::vector<float> _re;
std::vector<float> _im;
// Prevent uncontrolled usage
AppleAccelerateFFT(const AppleAccelerateFFT&);
AppleAccelerateFFT& operator=(const AppleAccelerateFFT&);
};
typedef AppleAccelerateFFT AudioFFT;
#endif // AUDIOFFT_APPLE_ACCELERATE_USED
// ================================================================
#ifdef AUDIOFFT_FFTW3_USED
/**
* @class FFTW3FFT
* @brief FFT implementation using FFTW3 internally (see fftw.org)
*/
class FFTW3FFT : public AudioFFTBase
{
public:
FFTW3FFT();
virtual ~FFTW3FFT();
virtual void init(size_t size);
virtual void fft(const float* data, float* re, float* im);
virtual void ifft(float* data, const float* re, const float* im);
private:
size_t _size;
size_t _complexSize;
fftwf_plan _planForward;
fftwf_plan _planBackward;
float* _data;
float* _re;
float* _im;
// Prevent uncontrolled usage
FFTW3FFT(const FFTW3FFT&);
FFTW3FFT& operator=(const FFTW3FFT&);
};
typedef FFTW3FFT AudioFFT;
#endif // AUDIOFFT_FFTW3_USED
// ================================================================
#ifdef AUDIOFFT_OOURA_USED
/**
* @class OouraFFT
* @brief FFT implementation based on the great radix-4 routines by Takuya Ooura
*/
class OouraFFT : public AudioFFTBase
{
public:
OouraFFT();
virtual void init(size_t size);
virtual void fft(const float* data, float* re, float* im);
virtual void ifft(float* data, const float* re, const float* im);
private:
size_t _size;
std::vector<int> _ip;
std::vector<double> _w;
std::vector<double> _buffer;
// The original FFT routines by Takuya Ooura (see http://momonga.t.u-tokyo.ac.jp/~ooura/fft.html)
void rdft(int n, int isgn, double *a, int *ip, double *w);
void makewt(int nw, int *ip, double *w);
void makect(int nc, int *ip, double *c);
void bitrv2(int n, int *ip, double *a);
void cftfsub(int n, double *a, double *w);
void cftbsub(int n, double *a, double *w);
void rftfsub(int n, double *a, int nc, double *c);
void rftbsub(int n, double *a, int nc, double *c);
void cft1st(int n, double *a, double *w);
void cftmdl(int n, int l, double *a, double *w);
// Prevent uncontrolled usage
OouraFFT(const OouraFFT&);
OouraFFT& operator=(const OouraFFT&);
};
typedef OouraFFT AudioFFT;
#endif // AUDIOFFT_OOURA_USED
} // End of namespace
#endif // Header guard