sfFDN
Loading...
Searching...
No Matches
filter.h
1// Copyright (C) 2025 Alexandre St-Onge
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "sffdn/audio_processor.h"
6#include "sffdn/delay.h"
7#include "sffdn/types.h"
8
9#include <array>
10#include <cassert>
11#include <cstddef>
12#include <cstdint>
13#include <iostream>
14#include <span>
15#include <vector>
16
17namespace sfFDN
18{
19
24{
25 public:
27 OnePoleFilter(float b0 = 1.f, float a1 = 0.f);
28
33 void SetPole(float pole);
34
39 void SetCoefficients(float b0, float a1);
40
47 void SetDecayFilter(float decay_db, float time_ms, float sample_rate);
48
53 void SetLowpass(float cutoff);
54
60 float Tick(float in);
61
67 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept override;
68
73 uint32_t InputChannelCount() const override;
74
79 uint32_t OutputChannelCount() const override;
80
84 void Clear() override;
85
89 std::unique_ptr<AudioProcessor> Clone() const override;
90
91 private:
92 float b0_, a1_;
93 std::array<float, 2> state_;
94};
95
100{
101 public:
104
108 void SetCoefficients(float coeff)
109 {
110 coeff_ = coeff;
111 }
112
118 float Tick(float in);
119
125 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept override;
126
131 uint32_t InputChannelCount() const override;
132
137 uint32_t OutputChannelCount() const override;
138
142 void Clear() override;
143
147 std::unique_ptr<AudioProcessor> Clone() const override;
148
149 private:
150 float coeff_;
151 float last_in_;
152 float last_out_;
153};
154
159{
160 public:
163 ~CascadedBiquads() = default;
164
168 void SetCoefficients(std::span<const FilterCoefficients> coeffs);
169
174 float Tick(float in);
175
181 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept override;
182
187 uint32_t InputChannelCount() const override;
188
193 uint32_t OutputChannelCount() const override;
194
198 void Clear() override;
199
203 std::unique_ptr<AudioProcessor> Clone() const override;
204
207 struct IIRState
208 {
209 float s0, s1;
210 };
211
212 private:
213 uint32_t stage_;
214 std::vector<IIRState> states_;
215 std::vector<FilterCoefficients> coeffs_;
216};
217
221class Fir : public AudioProcessor
222{
223 public:
225 Fir(const FirOptions& config = {});
226 ~Fir();
227
230 Fir(const Fir&);
231
235 Fir& operator=(const Fir&);
236
239 Fir(Fir&&) noexcept;
240
244 Fir& operator=(Fir&&) noexcept;
245
249 void SetCoefficients(std::span<const float> coeffs);
250
256 float Tick(float in);
257
263 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept override;
264
269 uint32_t InputChannelCount() const override;
270
275 uint32_t OutputChannelCount() const override;
276
280 void Clear() override;
281
285 std::unique_ptr<AudioProcessor> Clone() const override;
286
287 private:
288 class FirImpl;
289 std::unique_ptr<FirImpl> impl_;
290};
291
296{
297 public:
299 SparseFir(const SparseFirOptions& config = {});
300 ~SparseFir();
301
305 void SetCoefficients(const SparseFirOptions& config = {});
306
312 float Tick(float in);
313
319 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept override;
320
325 uint32_t InputChannelCount() const override;
326
331 uint32_t OutputChannelCount() const override;
332
336 void Clear() override;
337
341 std::unique_ptr<AudioProcessor> Clone() const override;
342
343 private:
344 class SparseFirImpl;
345 std::unique_ptr<SparseFirImpl> impl_;
346
347 SparseFirOptions config_;
348};
349
357std::unique_ptr<AudioProcessor> MakeFirFilter(const FirOptions& config, float sparse_threshold = 0.25f);
358
359} // namespace sfFDN
Implements a simple allpass filter with differential equation .
Definition filter.h:100
uint32_t InputChannelCount() const override
Returns the number of input channels supported by this processor.
uint32_t OutputChannelCount() const override
Returns the number of output channels produced by this processor.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the filter.
void SetCoefficients(float coeff)
Sets the allpass coefficient.
Definition filter.h:108
float Tick(float in)
Input a sample in the filter and return the next output.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept override
Processes a block of input samples through the filter.
AllpassFilter(const AllpassFilterOptions &config={})
Constructs an allpass filter.
void Clear() override
Clears the internal state of the processor.
A class representing an audio buffer with multiple channels of non-interleaved audio data.
Definition audio_buffer.h:18
Base class for audio processors.
Definition audio_processor.h:23
Implements a cascade of biquad IIR filters.
Definition filter.h:159
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept override
Processes a block of input samples through the filter.
float Tick(float in)
Processes a single input sample through the filter.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the filter.
CascadedBiquads(const CascadedBiquadsOptions &config={})
Constructs a cascaded biquad filter.
void Clear() override
Clears the internal state of the processor.
uint32_t InputChannelCount() const override
Returns the number of input channels supported by this processor.
uint32_t OutputChannelCount() const override
Returns the number of output channels produced by this processor.
void SetCoefficients(std::span< const FilterCoefficients > coeffs)
Sets the biquad coefficients for each stage.
Represents the internal state of the IIR filter.
Definition filter.h:208
Implements an FIR filter with arbitrary coefficients.
Definition filter.h:222
Fir(const FirOptions &config={})
Constructs a FIR filter.
Fir & operator=(const Fir &)
Copy assignment operator for the FIR filter.
void Clear() override
Clears the internal state of the processor.
float Tick(float in)
Input a sample in the filter and return the next output.
void SetCoefficients(std::span< const float > coeffs)
Sets the FIR coefficients.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept override
Processes a block of input samples through the filter.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the filter.
Fir(const Fir &)
Copy constructor for the FIR filter.
uint32_t InputChannelCount() const override
Returns the number of input channels supported by this processor.
uint32_t OutputChannelCount() const override
Returns the number of output channels produced by this processor.
Fir(Fir &&) noexcept
Move constructor for the FIR filter.
Implements a simple one pole filter with differential equation .
Definition filter.h:24
OnePoleFilter(float b0=1.f, float a1=0.f)
Constructs a one pole filter.
void SetPole(float pole)
Set the pole of the filter.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept override
Processes a block of input samples through the filter.
float Tick(float in)
Input a sample in the filter and return the next output.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the filter.
void SetDecayFilter(float decay_db, float time_ms, float sample_rate)
Set the pole of the filter to obtain an exponential decay filter.
uint32_t InputChannelCount() const override
Returns the number of input channels supported by this processor.
void Clear() override
Clears the internal state of the processor.
void SetCoefficients(float b0, float a1)
Set the coefficients of the filter.
uint32_t OutputChannelCount() const override
Returns the number of output channels produced by this processor.
void SetLowpass(float cutoff)
Set the pole of the filter to obtain a lowpass filter with a 3dB cutoff frequency.
Implements a sparse FIR filter.
Definition filter.h:296
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept override
Processes a block of input samples through the filter.
void SetCoefficients(const SparseFirOptions &config={})
Sets the FIR coefficients.
void Clear() override
Clears the internal state of the processor.
uint32_t OutputChannelCount() const override
Returns the number of output channels produced by this processor.
uint32_t InputChannelCount() const override
Returns the number of input channels supported by this processor.
float Tick(float in)
Input a sample in the filter and return the next output.
SparseFir(const SparseFirOptions &config={})
Constructs a sparse FIR filter.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the filter.
Options for configuring an allpass filter.
Definition types.h:250
Options for configuring cascaded biquad filters.
Definition types.h:263
Options for configuring a FIR filter.
Definition types.h:269
Options for configuring a sparse FIR filter.
Definition types.h:257