OpenShot Library | OpenShotAudio 0.2.2
juce_Convolution.h
1
2/** @weakgroup juce_dsp-frequency
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16 27th April 2017).
17
18 End User License Agreement: www.juce.com/juce-5-licence
19 Privacy Policy: www.juce.com/juce-5-privacy-policy
20
21 Or: You may also use this code under the terms of the GPL v3 (see
22 www.gnu.org/licenses).
23
24 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26 DISCLAIMED.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33namespace dsp
34{
35
36/**
37 Performs stereo uniform-partitioned convolution of an input signal with an
38 impulse response in the frequency domain, using the juce FFT class.
39
40 It provides some thread-safe functions to load impulse responses as well,
41 from audio files or memory on the fly without any noticeable artefacts,
42 performing resampling and trimming if necessary.
43
44 The processing is equivalent to the time domain convolution done in the
45 class FIRFilter, with a FIRFilter::Coefficients object having as
46 coefficients the samples of the impulse response. However, it is more
47 efficient in general to do frequency domain convolution when the size of
48 the impulse response is higher than 64 samples.
49
50 @see FIRFilter, FIRFilter::Coefficients, FFT
51
52 @tags{DSP}
53*/
55{
56public:
57 //==============================================================================
58 /** Initialises an object for performing convolution in the frequency domain. */
60
61 /** Destructor. */
63
64 //==============================================================================
65 /** Must be called before loading any impulse response, to provide to the
66 convolution the maximumBufferSize to handle, and the sample rate useful for
67 optional resampling.
68 */
69 void prepare (const ProcessSpec&);
70
71 /** Resets the processing pipeline, ready to start a new stream of data. */
72 void reset() noexcept;
73
74 /** Performs the filter operation on the given set of samples, with optional
75 stereo processing.
76 */
77 template <typename ProcessContext>
78 void process (const ProcessContext& context) noexcept
79 {
80 static_assert (std::is_same<typename ProcessContext::SampleType, float>::value,
81 "Convolution engine only supports single precision floating point data");
82
83 processSamples (context.getInputBlock(), context.getOutputBlock(), context.isBypassed);
84 }
85
86 //==============================================================================
87 /** This function loads an impulse response audio file from memory, added in a
88 JUCE project with the Projucer as binary data. It can load any of the audio
89 formats registered in JUCE, and performs some resampling and pre-processing
90 as well if needed.
91
92 Note: Obviously, don't try to use this function on float samples, since the
93 data is supposed to be an audio file in its binary format, and be sure that
94 the original data is not going to move at all its memory location during the
95 process !!
96
97 @param sourceData the block of data to use as the stream's source
98 @param sourceDataSize the number of bytes in the source data block
99 @param wantsStereo requests to process both stereo channels or only one mono channel
100 @param wantsTrimming requests to trim the start and the end of the impulse response
101 @param size the expected size for the impulse response after loading, can be
102 set to 0 for requesting maximum original impulse response size
103 @param wantsNormalisation requests to normalise the impulse response amplitude
104 */
105 void loadImpulseResponse (const void* sourceData, size_t sourceDataSize,
106 bool wantsStereo, bool wantsTrimming, size_t size,
107 bool wantsNormalisation = true);
108
109 /** This function loads an impulse response from an audio file on any drive. It
110 can load any of the audio formats registered in JUCE, and performs some
111 resampling and pre-processing as well if needed.
112
113 @param fileImpulseResponse the location of the audio file
114 @param wantsStereo requests to process both stereo channels or only one mono channel
115 @param wantsTrimming requests to trim the start and the end of the impulse response
116 @param size the expected size for the impulse response after loading, can be
117 set to 0 for requesting maximum original impulse response size
118 @param wantsNormalisation requests to normalise the impulse response amplitude
119 */
120 void loadImpulseResponse (const File& fileImpulseResponse,
121 bool wantsStereo, bool wantsTrimming, size_t size,
122 bool wantsNormalisation = true);
123
124 /** This function loads an impulse response from an audio buffer, which is
125 copied before doing anything else. Performs some resampling and
126 pre-processing as well if needed.
127
128 @param buffer the AudioBuffer to use
129 @param bufferSampleRate the sampleRate of the data in the AudioBuffer
130 @param wantsStereo requests to process both stereo channels or only one mono channel
131 @param wantsTrimming requests to trim the start and the end of the impulse response
132 @param wantsNormalisation requests to normalise the impulse response amplitude
133 @param size the expected size for the impulse response after loading, can be
134 set to 0 for requesting maximum original impulse response size
135 */
136 void copyAndLoadImpulseResponseFromBuffer (AudioBuffer<float>& buffer, double bufferSampleRate,
137 bool wantsStereo, bool wantsTrimming, bool wantsNormalisation,
138 size_t size);
139
140 /** This function loads an impulse response from an audio block, which is
141 copied before doing anything else. Performs some resampling and
142 pre-processing as well if needed.
143
144 @param block the AudioBlock to use
145 @param bufferSampleRate the sampleRate of the data in the AudioBuffer
146 @param wantsStereo requests to process both stereo channels or only one channel
147 @param wantsTrimming requests to trim the start and the end of the impulse response
148 @param wantsNormalisation requests to normalise the impulse response amplitude
149 @param size the expected size for the impulse response after loading,
150 -1 for maximum length
151 */
152 void copyAndLoadImpulseResponseFromBlock (AudioBlock<float> block, double bufferSampleRate,
153 bool wantsStereo, bool wantsTrimming, bool wantsNormalisation,
154 size_t size);
155
156
157private:
158 //==============================================================================
159 struct Pimpl;
160 std::unique_ptr<Pimpl> pimpl;
161
162 //==============================================================================
163 void processSamples (const AudioBlock<const float>&, AudioBlock<float>&, bool isBypassed) noexcept;
164
165 //==============================================================================
166 double sampleRate;
167 bool currentIsBypassed = false;
168 bool isActive = false;
169 SmoothedValue<float> volumeDry[2], volumeWet[2];
170 AudioBlock<float> dryBuffer;
171 HeapBlock<char> dryBufferStorage;
172
173 //==============================================================================
174 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Convolution)
175};
176
177} // namespace dsp
178} // namespace juce
179
180/** @}*/
Represents a local file or directory.
Definition: juce_File.h:45
Performs stereo uniform-partitioned convolution of an input signal with an impulse response in the fr...
void process(const ProcessContext &context) noexcept
Performs the filter operation on the given set of samples, with optional stereo processing.
#define JUCE_API
This macro is added to all JUCE public class declarations.
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...