OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_FIRFilter.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12 27th April 2017).
13
14 End User License Agreement: www.juce.com/juce-5-licence
15 Privacy Policy: www.juce.com/juce-5-privacy-policy
16
17 Or: You may also use this code under the terms of the GPL v3 (see
18 www.gnu.org/licenses).
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27 namespace juce
28{
29 namespace dsp
30{
31
35 namespace FIR
36{
37 template <typename NumericType>
38 struct Coefficients;
39
40 //==============================================================================
54 template <typename SampleType>
55 class Filter
56 {
57 public:
61 using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
62
64 using CoefficientsPtr = typename Coefficients<NumericType>::Ptr;
65
66 //==============================================================================
68 Filter() : coefficients (new Coefficients<NumericType>) { reset(); }
69
71 Filter (CoefficientsPtr coefficientsToUse) : coefficients (std::move (coefficientsToUse)) { reset(); }
72
73 Filter (const Filter&) = default;
74 Filter (Filter&&) = default;
75 Filter& operator= (const Filter&) = default;
76 Filter& operator= (Filter&&) = default;
77
78 //==============================================================================
80 inline void prepare (const ProcessSpec& spec) noexcept
81 {
82 // This class can only process mono signals. Use the ProcessorDuplicator class
83 // to apply this filter on a multi-channel audio stream.
84 jassert (spec.numChannels == 1);
85 ignoreUnused (spec);
86 reset();
87 }
88
94 void reset()
95 {
96 if (coefficients != nullptr)
97 {
98 auto newSize = coefficients->getFilterOrder() + 1;
99
100 if (newSize != size)
101 {
102 memory.malloc (1 + jmax (newSize, size, static_cast<size_t> (128)));
103
104 fifo = snapPointerToAlignment (memory.getData(), sizeof (SampleType));
105 size = newSize;
106 }
107
108 for (size_t i = 0; i < size; ++i)
109 fifo[i] = SampleType {0};
110 }
111 }
112
113 //==============================================================================
120 typename Coefficients<NumericType>::Ptr coefficients;
121
122 //==============================================================================
124 template <typename ProcessContext>
125 void process (const ProcessContext& context) noexcept
126 {
127 static_assert (std::is_same<typename ProcessContext::SampleType, SampleType>::value,
128 "The sample-type of the FIR filter must match the sample-type supplied to this process callback");
129 check();
130
131 auto&& inputBlock = context.getInputBlock();
132 auto&& outputBlock = context.getOutputBlock();
133
134 // This class can only process mono signals. Use the ProcessorDuplicator class
135 // to apply this filter on a multi-channel audio stream.
136 jassert (inputBlock.getNumChannels() == 1);
137 jassert (outputBlock.getNumChannels() == 1);
138
139 auto numSamples = inputBlock.getNumSamples();
140 auto* src = inputBlock .getChannelPointer (0);
141 auto* dst = outputBlock.getChannelPointer (0);
142
143 auto* fir = coefficients->getRawCoefficients();
144 size_t p = pos;
145
146 if (context.isBypassed)
147 {
148 for (size_t i = 0; i < numSamples; ++i)
149 {
150 fifo[p] = dst[i] = src[i];
151 p = (p == 0 ? size - 1 : p - 1);
152 }
153 }
154 else
155 {
156 for (size_t i = 0; i < numSamples; ++i)
157 dst[i] = processSingleSample (src[i], fifo, fir, size, p);
158 }
159
160 pos = p;
161 }
162
163
167 SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept
168 {
169 check();
170 return processSingleSample (sample, fifo, coefficients->getRawCoefficients(), size, pos);
171 }
172
173 private:
174 //==============================================================================
175 HeapBlock<SampleType> memory;
176 SampleType* fifo = nullptr;
177 size_t pos = 0, size = 0;
178
179 //==============================================================================
180 void check()
181 {
182 jassert (coefficients != nullptr);
183
184 if (size != (coefficients->getFilterOrder() + 1))
185 reset();
186 }
187
188 static SampleType JUCE_VECTOR_CALLTYPE processSingleSample (SampleType sample, SampleType* buf,
189 const NumericType* fir, size_t m, size_t& p) noexcept
190 {
191 SampleType out (0);
192
193 buf[p] = sample;
194
195 size_t k;
196 for (k = 0; k < m - p; ++k)
197 out += buf[(p + k)] * fir[k];
198
199 for (size_t j = 0; j < p; ++j)
200 out += buf[j] * fir[j + k];
201
202 p = (p == 0 ? m - 1 : p - 1);
203
204 return out;
205 }
206
207
208 JUCE_LEAK_DETECTOR (Filter)
209 };
210
211 //==============================================================================
219 template <typename NumericType>
220 struct Coefficients : public ProcessorState
221 {
222 //==============================================================================
224 Coefficients() : coefficients ({ NumericType() }) {}
225
227 Coefficients (size_t size) { coefficients.resize ((int) size); }
228
230 Coefficients (const NumericType* samples, size_t numSamples) : coefficients (samples, (int) numSamples) {}
231
232 Coefficients (const Coefficients&) = default;
233 Coefficients (Coefficients&&) = default;
234 Coefficients& operator= (const Coefficients&) = default;
235 Coefficients& operator= (Coefficients&&) = default;
236
240 using Ptr = ReferenceCountedObjectPtr<Coefficients>;
241
242 //==============================================================================
244 size_t getFilterOrder() const noexcept { return static_cast<size_t> (coefficients.size()) - 1; }
245
249 double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
250
254 void getMagnitudeForFrequencyArray (double* frequencies, double* magnitudes,
255 size_t numSamples, double sampleRate) const noexcept;
256
260 double getPhaseForFrequency (double frequency, double sampleRate) const noexcept;
261
265 void getPhaseForFrequencyArray (double* frequencies, double* phases,
266 size_t numSamples, double sampleRate) const noexcept;
267
269 NumericType* getRawCoefficients() noexcept { return coefficients.getRawDataPointer(); }
270
272 const NumericType* getRawCoefficients() const noexcept { return coefficients.begin(); }
273
274 //==============================================================================
276 void normalise() noexcept;
277
278 //==============================================================================
282 Array<NumericType> coefficients;
283 };
284}
285
286} // namespace dsp
287} // namespace juce
juce::Array
Definition: juce_Array.h:56
juce::Array::size
int size() const noexcept
Definition: juce_Array.h:215
juce::Array::begin
ElementType * begin() noexcept
Definition: juce_Array.h:328
juce::Array::getRawDataPointer
ElementType * getRawDataPointer() noexcept
Definition: juce_Array.h:310
juce::Array::resize
void resize(int targetNumItems)
Definition: juce_Array.h:670
juce::HeapBlock
Definition: juce_HeapBlock.h:87
juce::HeapBlock::malloc
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Definition: juce_HeapBlock.h:252
juce::HeapBlock::getData
ElementType * getData() const noexcept
Definition: juce_HeapBlock.h:194
juce::ReferenceCountedObjectPtr
Definition: juce_ReferenceCountedObject.h:247
juce::dsp::FIR::Filter
Definition: juce_FIRFilter.h:56
juce::dsp::FIR::Filter::coefficients
Coefficients< NumericType >::Ptr coefficients
Definition: juce_FIRFilter.h:120
juce::dsp::FIR::Filter::CoefficientsPtr
typename Coefficients< NumericType >::Ptr CoefficientsPtr
Definition: juce_FIRFilter.h:64
juce::dsp::FIR::Filter::process
void process(const ProcessContext &context) noexcept
Definition: juce_FIRFilter.h:125
juce::dsp::FIR::Filter::NumericType
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition: juce_FIRFilter.h:61
juce::dsp::FIR::Filter::processSample
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition: juce_FIRFilter.h:167
juce::dsp::FIR::Filter::Filter
Filter(CoefficientsPtr coefficientsToUse)
Definition: juce_FIRFilter.h:71
juce::dsp::FIR::Filter::prepare
void prepare(const ProcessSpec &spec) noexcept
Definition: juce_FIRFilter.h:80
juce::dsp::FIR::Filter::Filter
Filter()
Definition: juce_FIRFilter.h:68
juce::dsp::FIR::Filter::reset
void reset()
Definition: juce_FIRFilter.h:94
juce::dsp::FIR::Coefficients
Definition: juce_FIRFilter.h:221
juce::dsp::FIR::Coefficients::normalise
void normalise() noexcept
juce::dsp::FIR::Coefficients::getMagnitudeForFrequencyArray
void getMagnitudeForFrequencyArray(double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::getFilterOrder
size_t getFilterOrder() const noexcept
Definition: juce_FIRFilter.h:244
juce::dsp::FIR::Coefficients::Coefficients
Coefficients()
Definition: juce_FIRFilter.h:224
juce::dsp::FIR::Coefficients::Coefficients
Coefficients(const NumericType *samples, size_t numSamples)
Definition: juce_FIRFilter.h:230
juce::dsp::FIR::Coefficients::Coefficients
Coefficients(size_t size)
Definition: juce_FIRFilter.h:227
juce::dsp::FIR::Coefficients::getMagnitudeForFrequency
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::coefficients
Array< NumericType > coefficients
Definition: juce_FIRFilter.h:282
juce::dsp::FIR::Coefficients::getPhaseForFrequency
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::getRawCoefficients
NumericType * getRawCoefficients() noexcept
Definition: juce_FIRFilter.h:269
juce::dsp::FIR::Coefficients::getRawCoefficients
const NumericType * getRawCoefficients() const noexcept
Definition: juce_FIRFilter.h:272
juce::dsp::FIR::Coefficients::getPhaseForFrequencyArray
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
juce::dsp::ProcessSpec
Definition: juce_ProcessContext.h:39
juce::dsp::ProcessorState
Definition: juce_ProcessContext.h:59