LMMS
Loading...
Searching...
No Matches
juce_FIRFilter.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
34namespace FIR
35{
36 template <typename NumericType>
37 struct Coefficients;
38
39 //==============================================================================
53 template <typename SampleType>
54 class Filter
55 {
56 public:
61
64
65 //==============================================================================
68
70 Filter (CoefficientsPtr coefficientsToUse) : coefficients (std::move (coefficientsToUse)) { reset(); }
71
72 Filter (const Filter&) = default;
73 Filter (Filter&&) = default;
74 Filter& operator= (const Filter&) = default;
75 Filter& operator= (Filter&&) = default;
76
77 //==============================================================================
79 inline void prepare (const ProcessSpec& spec) noexcept
80 {
81 // This class can only process mono signals. Use the ProcessorDuplicator class
82 // to apply this filter on a multi-channel audio stream.
83 jassertquiet (spec.numChannels == 1);
84 reset();
85 }
86
92 void reset()
93 {
94 if (coefficients != nullptr)
95 {
96 auto newSize = coefficients->getFilterOrder() + 1;
97
98 if (newSize != size)
99 {
100 memory.malloc (1 + jmax (newSize, size, static_cast<size_t> (128)));
101
102 fifo = snapPointerToAlignment (memory.getData(), sizeof (SampleType));
103 size = newSize;
104 }
105
106 for (size_t i = 0; i < size; ++i)
107 fifo[i] = SampleType {0};
108 }
109 }
110
111 //==============================================================================
119
120 //==============================================================================
122 template <typename ProcessContext>
123 void process (const ProcessContext& context) noexcept
124 {
125 static_assert (std::is_same<typename ProcessContext::SampleType, SampleType>::value,
126 "The sample-type of the FIR filter must match the sample-type supplied to this process callback");
127 check();
128
129 auto&& inputBlock = context.getInputBlock();
130 auto&& outputBlock = context.getOutputBlock();
131
132 // This class can only process mono signals. Use the ProcessorDuplicator class
133 // to apply this filter on a multi-channel audio stream.
134 jassert (inputBlock.getNumChannels() == 1);
135 jassert (outputBlock.getNumChannels() == 1);
136
137 auto numSamples = inputBlock.getNumSamples();
138 auto* src = inputBlock .getChannelPointer (0);
139 auto* dst = outputBlock.getChannelPointer (0);
140
141 auto* fir = coefficients->getRawCoefficients();
142 size_t p = pos;
143
144 if (context.isBypassed)
145 {
146 for (size_t i = 0; i < numSamples; ++i)
147 {
148 fifo[p] = dst[i] = src[i];
149 p = (p == 0 ? size - 1 : p - 1);
150 }
151 }
152 else
153 {
154 for (size_t i = 0; i < numSamples; ++i)
155 dst[i] = processSingleSample (src[i], fifo, fir, size, p);
156 }
157
158 pos = p;
159 }
160
161
165 SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept
166 {
167 check();
168 return processSingleSample (sample, fifo, coefficients->getRawCoefficients(), size, pos);
169 }
170
171 private:
172 //==============================================================================
174 SampleType* fifo = nullptr;
175 size_t pos = 0, size = 0;
176
177 //==============================================================================
178 void check()
179 {
180 jassert (coefficients != nullptr);
181
182 if (size != (coefficients->getFilterOrder() + 1))
183 reset();
184 }
185
186 static SampleType JUCE_VECTOR_CALLTYPE processSingleSample (SampleType sample, SampleType* buf,
187 const NumericType* fir, size_t m, size_t& p) noexcept
188 {
189 SampleType out (0);
190
191 buf[p] = sample;
192
193 size_t k;
194 for (k = 0; k < m - p; ++k)
195 out += buf[(p + k)] * fir[k];
196
197 for (size_t j = 0; j < p; ++j)
198 out += buf[j] * fir[j + k];
199
200 p = (p == 0 ? m - 1 : p - 1);
201
202 return out;
203 }
204
205
207 };
208
209 //==============================================================================
217 template <typename NumericType>
219 {
220 //==============================================================================
222 Coefficients() : coefficients ({ NumericType() }) {}
223
225 Coefficients (size_t size) { coefficients.resize ((int) size); }
226
228 Coefficients (const NumericType* samples, size_t numSamples) : coefficients (samples, (int) numSamples) {}
229
230 Coefficients (const Coefficients&) = default;
232 Coefficients& operator= (const Coefficients&) = default;
233 Coefficients& operator= (Coefficients&&) = default;
234
239
240 //==============================================================================
242 size_t getFilterOrder() const noexcept { return static_cast<size_t> (coefficients.size()) - 1; }
243
247 double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
248
252 void getMagnitudeForFrequencyArray (double* frequencies, double* magnitudes,
253 size_t numSamples, double sampleRate) const noexcept;
254
258 double getPhaseForFrequency (double frequency, double sampleRate) const noexcept;
259
263 void getPhaseForFrequencyArray (double* frequencies, double* phases,
264 size_t numSamples, double sampleRate) const noexcept;
265
267 NumericType* getRawCoefficients() noexcept { return coefficients.getRawDataPointer(); }
268
270 const NumericType* getRawCoefficients() const noexcept { return coefficients.begin(); }
271
272 //==============================================================================
275
276 //==============================================================================
280 Array<NumericType> coefficients;
281 };
282}
283
284} // namespace dsp
285} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
#define check(expr)
Definition blargg_source.h:32
Definition juce_Array.h:56
Definition juce_HeapBlock.h:87
Definition juce_ReferenceCountedObject.h:247
Definition juce_FIRFilter.h:55
Coefficients< NumericType >::Ptr coefficients
Definition juce_FIRFilter.h:118
HeapBlock< SampleType > memory
Definition juce_FIRFilter.h:173
typename Coefficients< NumericType >::Ptr CoefficientsPtr
Definition juce_FIRFilter.h:63
void process(const ProcessContext &context) noexcept
Definition juce_FIRFilter.h:123
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_FIRFilter.h:60
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition juce_FIRFilter.h:165
SampleType * fifo
Definition juce_FIRFilter.h:174
Filter(const Filter &)=default
size_t size
Definition juce_FIRFilter.h:175
size_t pos
Definition juce_FIRFilter.h:175
Filter(CoefficientsPtr coefficientsToUse)
Definition juce_FIRFilter.h:70
void check()
Definition juce_FIRFilter.h:178
static SampleType JUCE_VECTOR_CALLTYPE processSingleSample(SampleType sample, SampleType *buf, const NumericType *fir, size_t m, size_t &p) noexcept
Definition juce_FIRFilter.h:186
Filter(Filter &&)=default
void prepare(const ProcessSpec &spec) noexcept
Definition juce_FIRFilter.h:79
Filter()
Definition juce_FIRFilter.h:67
void reset()
Definition juce_FIRFilter.h:92
unsigned * m
Definition inflate.c:1559
register unsigned k
Definition inflate.c:946
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition juce_LeakedObjectDetector.h:138
#define jassert(expression)
#define jassertquiet(expression)
#define JUCE_VECTOR_CALLTYPE
Definition juce_dsp.h:100
float out
Definition lilv_test.c:1461
Definition juce_FIRFilter.h:35
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
Type * snapPointerToAlignment(Type *basePointer, IntegerType alignmentBytes) noexcept
Definition juce_Memory.h:45
Definition juce_Uuid.h:141
Definition juce_FIRFilter.h:219
void getMagnitudeForFrequencyArray(double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
size_t getFilterOrder() const noexcept
Definition juce_FIRFilter.h:242
Coefficients()
Definition juce_FIRFilter.h:222
Coefficients(const NumericType *samples, size_t numSamples)
Definition juce_FIRFilter.h:228
Coefficients(size_t size)
Definition juce_FIRFilter.h:225
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
ReferenceCountedObjectPtr< Coefficients > Ptr
Definition juce_FIRFilter.h:238
Array< NumericType > coefficients
Definition juce_FIRFilter.h:280
Coefficients(Coefficients &&)=default
Coefficients(const Coefficients &)=default
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
NumericType * getRawCoefficients() noexcept
Definition juce_FIRFilter.h:267
const NumericType * getRawCoefficients() const noexcept
Definition juce_FIRFilter.h:270
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
Definition juce_ProcessContext.h:38
Definition juce_ProcessContext.h:67
T Type
Definition juce_AudioBlock.h:37
signed int sample
Definition tap_dynamics_m.c:41
uch * p
Definition crypt.c:594
ulg size
Definition extract.c:2350
typedef int(UZ_EXP MsgFn)()
#define const
Definition zconf.h:137