sfFDN
Loading...
Searching...
No Matches
nonlinear.h
1// Copyright (C) 2026 Alexandre St-Onge
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "sffdn/attributes.h"
6#include "sffdn/audio_buffer.h"
7#include "sffdn/audio_processor.h"
8#include "sffdn/filterbank.h"
9#include "sffdn/oscillator.h"
10#include "sffdn/types.h"
11
12#include <cstdint>
13#include <memory>
14
15namespace sfFDN
16{
17
18class DcBlocker;
19
72{
73 public:
81 static constexpr float kAntialiasingEpsilon = 1e-5f;
82
89
95
101 void SetAlpha(float alpha);
102
104 float GetAlpha() const noexcept SFFDN_NONBLOCKING;
105
107 float GetCompensationGain() const noexcept SFFDN_NONBLOCKING;
108
110 float Tick(float input) noexcept SFFDN_NONBLOCKING;
111
115 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
116
118 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
119
121 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
122
124 void Clear() override;
125
127 std::unique_ptr<AudioProcessor> Clone() const override;
128
129 private:
130 float Rectify(float input) const noexcept SFFDN_NONBLOCKING;
131
132 std::unique_ptr<DcBlocker> dc_blocker_;
133
134 float alpha_;
135 float compensation_gain_;
136 bool antialiasing_;
137
138 float prev_input_{0.f};
139};
140
169{
170 public:
176
181 void SetD(float d);
182
184 float GetD() const noexcept SFFDN_NONBLOCKING;
185
187 float Tick(float input) noexcept SFFDN_NONBLOCKING;
188
192 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
193
195 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
196
198 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
199
201 void Clear() override;
202
204 std::unique_ptr<AudioProcessor> Clone() const override;
205
206 private:
207 float d_;
208
209 float prev_positive_{0.f};
210 float prev_prev_positive_{0.f};
211 float prev_negative_{0.f};
212};
213
235{
236 public:
242 explicit RingModulator(const RingModulatorOptions& options = {});
243
245 float GetFrequency() const noexcept SFFDN_NONBLOCKING;
246
248 float GetAmplitude() const noexcept SFFDN_NONBLOCKING;
249
251 float Tick(float input) noexcept SFFDN_NONBLOCKING;
252
256 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
257
259 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
260
262 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
263
265 void Clear() override;
266
268 std::unique_ptr<AudioProcessor> Clone() const override;
269
270 private:
271 SineWave lfo_;
272};
273
281
292 float alpha, float sample_rate, uint32_t channel_count, uint32_t active_channel_count = 0);
293
300
308 float d, uint32_t channel_count, uint32_t active_channel_count = 0);
309
315
328 uint32_t channel_count,
329 uint32_t active_channel_count = 0);
330
333} // namespace sfFDN
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
A controllable full-wave rectifier, with optional antiderivative antialiasing.
Definition nonlinear.h:72
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes the audio buffer.
void SetAlpha(float alpha)
Sets the distortion amount.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels this processor expects.
float GetAlpha() const noexcept SFFDN_NONBLOCKING
Returns the distortion amount.
float GetCompensationGain() const noexcept SFFDN_NONBLOCKING
Returns the power compensation gain of the current distortion amount.
void Clear() override
Clears the antialiasing state and the dc blocker.
static constexpr float kAntialiasingEpsilon
Threshold below which the denominator of the antialiasing approximation is considered ill-conditioned...
Definition nonlinear.h:81
float Tick(float input) noexcept SFFDN_NONBLOCKING
Processes a single sample.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the processor.
uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of output channels this processor produces.
ControllableFullWaveRectifier(const ControllableFullWaveRectifierOptions &options={})
Constructs a controllable full-wave rectifier.
Implements a bank of filters.
Definition filterbank.h:19
A ring modulator.
Definition nonlinear.h:235
uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of output channels this processor produces.
float GetFrequency() const noexcept SFFDN_NONBLOCKING
Returns the modulation frequency, in cycles per sample.
RingModulator(const RingModulatorOptions &options={})
Constructs a ring modulator.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels this processor expects.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the processor.
void Clear() override
Restores the configured initial phase.
float GetAmplitude() const noexcept SFFDN_NONBLOCKING
Returns the linear gain applied to the modulating sinusoid.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes the audio buffer.
float Tick(float input) noexcept SFFDN_NONBLOCKING
Processes a single sample.
A signal-dependent fractional delay filter.
Definition nonlinear.h:169
float GetD() const noexcept SFFDN_NONBLOCKING
Returns the interpolation weight.
uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of output channels this processor produces.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes the audio buffer.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels this processor expects.
void SetD(float d)
Sets the interpolation weight.
void Clear() override
Clears the delay state.
std::unique_ptr< AudioProcessor > Clone() const override
Creates a copy of the processor.
SignalDependentFractionalDelay(const SignalDependentFractionalDelayOptions &options={})
Constructs a signal-dependent fractional delay.
float Tick(float input) noexcept SFFDN_NONBLOCKING
Processes a single sample.
A sine wave oscillator.
Definition oscillator.h:33
Options for configuring a controllable full-wave rectifier.
Definition types.h:401
Options for configuring a multichannel bank of controllable full-wave rectifiers.
Definition types.h:419
Options for configuring a multichannel bank of ring modulators.
Definition types.h:465
Options for configuring a multichannel bank of signal-dependent fractional delays.
Definition types.h:439
Options for configuring a ring modulator.
Definition types.h:450
Options for configuring a signal-dependent fractional delay.
Definition types.h:429
std::unique_ptr< FilterBank > MakeMultichannelControllableFullWaveRectifier(const MultichannelControllableFullWaveRectifierOptions &options)
Builds a bank of controllable full-wave rectifiers, one per channel.
std::unique_ptr< FilterBank > MakeMultichannelSignalDependentFractionalDelay(const MultichannelSignalDependentFractionalDelayOptions &options)
Builds a bank of signal-dependent fractional delays, one per channel.
MultichannelSignalDependentFractionalDelayOptions MakeMultichannelSignalDependentFractionalDelayOptions(float d, uint32_t channel_count, uint32_t active_channel_count=0)
Returns the options of a bank of signal-dependent fractional delays covering every channel.
MultichannelControllableFullWaveRectifierOptions MakeMultichannelControllableFullWaveRectifierOptions(float alpha, float sample_rate, uint32_t channel_count, uint32_t active_channel_count=0)
Returns the options of a bank of controllable full-wave rectifiers covering every channel.
MultichannelRingModulatorOptions MakeMultichannelRingModulatorOptions(float frequency, float amplitude, uint32_t channel_count, uint32_t active_channel_count=0)
Returns the options of a decorrelated bank of ring modulators.
std::unique_ptr< FilterBank > MakeMultichannelRingModulator(const MultichannelRingModulatorOptions &options)
Builds a bank of ring modulators, one per channel.