forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFTConvolver.h
executable file
·100 lines (87 loc) · 3.09 KB
/
FFTConvolver.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
// ==================================================================================
// Copyright (c) 2012 HiFi-LoFi
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ==================================================================================
#ifndef _FFTCONVOLVER_FFTCONVOLVER_H
#define _FFTCONVOLVER_FFTCONVOLVER_H
#include "AudioFFT.h"
#include "Utilities.h"
#include <vector>
namespace fftconvolver
{
/**
* @class FFTConvolver
* @brief Implementation of a partitioned FFT convolution algorithm with uniform block size
*
* Some notes on how to use it:
*
* - After initialization with an impulse response, subsequent data portions of
* arbitrary length can be convolved. The convolver internally can handle
* this by using appropriate buffering.
*
* - The convolver works without "latency" (except for the required
* processing time, of course), i.e. the output always is the convolved
* input for each processing call.
*
* - The convolver is suitable for real-time processing which means that no
* "unpredictable" operations like allocations, locking, API calls, etc. are
* performed during processing (all necessary allocations and preparations take
* place during initialization).
*/
class FFTConvolver
{
public:
FFTConvolver();
virtual ~FFTConvolver();
/**
* @brief Initializes the convolver
* @param blockSize Block size internally used by the convolver (partition size)
* @param ir The impulse response
* @param irLen Length of the impulse response
* @return true: Success - false: Failed
*/
bool init(size_t blockSize, const Sample* ir, size_t irLen);
/**
* @brief Convolves the the given input samples and immediately outputs the result
* @param input The input samples
* @param output The convolution result
* @param len Number of input/output samples
*/
void process(const Sample* input, Sample* output, size_t len);
/**
* @brief Resets the convolver and discards the set impulse response
*/
void reset();
private:
size_t _blockSize;
size_t _segSize;
size_t _segCount;
size_t _fftComplexSize;
std::vector<SplitComplex*> _segments;
std::vector<SplitComplex*> _segmentsIR;
SampleBuffer _fftBuffer;
audiofft::AudioFFT _fft;
SplitComplex _preMultiplied;
SplitComplex _conv;
SampleBuffer _overlap;
size_t _current;
SampleBuffer _inputBuffer;
size_t _inputBufferFill;
// Prevent uncontrolled usage
FFTConvolver(const FFTConvolver&);
FFTConvolver& operator=(const FFTConvolver&);
};
} // End of namespace fftconvolver
#endif // Header guard