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) noexcept SFFDN_NONBLOCKING;
61
67 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
68
73 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
74
79 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING 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) noexcept SFFDN_NONBLOCKING
109 {
110 coeff_ = coeff;
111 }
112
127 void WarpState(float last_in, float coeff) noexcept SFFDN_NONBLOCKING
128 {
129 last_in_ = last_in;
130 coeff_ = coeff;
131 }
132
138 float Tick(float in) noexcept SFFDN_NONBLOCKING;
139
145 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
146
151 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
152
157 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
158
162 void Clear() override;
163
167 std::unique_ptr<AudioProcessor> Clone() const override;
168
169 private:
170 float coeff_;
171 float last_in_;
172 float last_out_;
173};
174
179{
180 public:
183
187 void SetCoefficients(std::span<const FilterCoefficients> coeffs);
188
193 float Tick(float in) noexcept SFFDN_NONBLOCKING;
194
200 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
201
206 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
207
212 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
213
217 void Clear() override;
218
222 std::unique_ptr<AudioProcessor> Clone() const override;
223
226 struct IIRState
227 {
228 float s0, s1;
229 };
230
231 private:
232 uint32_t stage_;
233 std::vector<IIRState> states_;
234 std::vector<FilterCoefficients> coeffs_;
235};
236
240class Fir : public AudioProcessor
241{
242 public:
244 Fir(const FirOptions& config = {});
245 ~Fir() override;
246
249 Fir(const Fir& other);
250
254 Fir& operator=(const Fir& other);
255
258 Fir(Fir&& other) noexcept;
259
263 Fir& operator=(Fir&& other) noexcept;
264
268 void SetCoefficients(std::span<const float> coeffs);
269
275 float Tick(float in) noexcept SFFDN_NONBLOCKING;
276
282 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
283
288 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
289
294 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
295
299 void Clear() override;
300
304 std::unique_ptr<AudioProcessor> Clone() const override;
305
306 private:
307 class FirImpl;
308 std::unique_ptr<FirImpl> impl_;
309};
310
315{
316 public:
318 SparseFir(const SparseFirOptions& config = {});
319 ~SparseFir() override;
320
321 // impl_ is a unique_ptr to an incomplete type, so the destructor must be defined out of line,
322 // which suppresses the implicit move operations. Declare them explicitly so a SparseFir can be
323 // moved instead of silently failing to compile. The pimpl cannot be copied; use Clone().
324 SparseFir(const SparseFir&) = delete;
325 SparseFir& operator=(const SparseFir&) = delete;
326 SparseFir(SparseFir&& other) noexcept;
327 SparseFir& operator=(SparseFir&& other) noexcept;
328
332 void SetCoefficients(const SparseFirOptions& config = {});
333
339 float Tick(float in) noexcept SFFDN_NONBLOCKING;
340
346 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
347
352 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
353
358 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
359
363 void Clear() override;
364
368 std::unique_ptr<AudioProcessor> Clone() const override;
369
370 private:
371 class SparseFirImpl;
372 std::unique_ptr<SparseFirImpl> impl_;
373
374 SparseFirOptions config_;
375};
376
384std::unique_ptr<AudioProcessor> MakeFirFilter(const FirOptions& config, float sparse_threshold = 0.25f);
385
386} // namespace sfFDN
Implements a simple allpass filter with differential equation .
Definition filter.h:100
void WarpState(float last_in, float coeff) noexcept SFFDN_NONBLOCKING
Re-seeds the filter state and sets a new coefficient in one step.
Definition filter.h:127
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes a block of input samples through the filter.
float Tick(float in) noexcept SFFDN_NONBLOCKING
Input a sample in the filter and return the next output.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels supported by this processor.
AllpassFilter(const AllpassFilterOptions &config={})
Constructs an allpass filter.
void SetCoefficients(float coeff) noexcept SFFDN_NONBLOCKING
Sets the allpass coefficient.
Definition filter.h:108
A class representing an audio buffer with multiple channels of non-interleaved audio data.
Definition audio_buffer.h:20
Base class for audio processors.
Definition audio_processor.h:24
Implements a cascade of biquad IIR filters.
Definition filter.h:179
CascadedBiquads(const CascadedBiquadsOptions &config={})
Constructs a cascaded biquad filter.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels supported by this processor.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes a block of input samples through the filter.
float Tick(float in) noexcept SFFDN_NONBLOCKING
Processes a single input sample through the filter.
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:227
Implements an FIR filter with arbitrary coefficients.
Definition filter.h:241
Fir(const FirOptions &config={})
Constructs a FIR filter.
float Tick(float in) noexcept SFFDN_NONBLOCKING
Input a sample in the filter and return the next output.
Fir & operator=(Fir &&other) noexcept
Move assignment operator for the FIR filter.
Fir(const Fir &other)
Copy constructor for the FIR filter.
void SetCoefficients(std::span< const float > coeffs)
Sets the FIR coefficients.
Fir(Fir &&other) noexcept
Move constructor for the FIR filter.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels supported by this processor.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes a block of input samples through the filter.
Fir & operator=(const Fir &other)
Copy assignment operator for the FIR filter.
Implements a simple one pole filter with differential equation .
Definition filter.h:24
float Tick(float in) noexcept SFFDN_NONBLOCKING
Input a sample in the filter and return the next output.
OnePoleFilter(float b0=1.f, float a1=0.f)
Constructs a one pole filter.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels supported by this processor.
void SetPole(float pole)
Set the pole of the filter.
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 OutputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of output channels produced 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.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes a block of input samples through the filter.
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:315
void SetCoefficients(const SparseFirOptions &config={})
Sets the FIR coefficients.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes a block of input samples through the filter.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels supported by this processor.
SparseFir(const SparseFirOptions &config={})
Constructs a sparse FIR filter.
float Tick(float in) noexcept SFFDN_NONBLOCKING
Input a sample in the filter and return the next output.
Options for configuring an allpass filter.
Definition types.h:285
Options for configuring cascaded biquad filters.
Definition types.h:298
Options for configuring a FIR filter.
Definition types.h:304
Options for configuring a sparse FIR filter.
Definition types.h:292