sfFDN
Loading...
Searching...
No Matches
types.h
1#pragma once
2
3#include <nlohmann/json.hpp>
4
5#include <array>
6#include <cstdint>
7#include <numbers>
8#include <optional>
9#include <tuple>
10#include <variant>
11#include <vector>
12
13namespace sfFDN
14{
15
16// helper type for the visitor #4
17template <class... Ts>
18struct overloaded : Ts...
19{
20 using Ts::operator()...;
21};
22// explicit deduction guide (not needed as of C++20)
23template <class... Ts>
24overloaded(Ts...) -> overloaded<Ts...>;
25
26constexpr uint32_t kDefaultSampleRate = 48000;
27constexpr uint32_t kDefaultBlockSize = 128;
28
37// ENUMS
38
49enum class ScalarMatrixType : uint8_t
50{
51 Identity = 0,
52 Random = 1,
53 Householder = 2,
55 Hadamard = 4,
56 Circulant = 5,
57 Allpass = 6,
58 NestedAllpass = 7,
60 Count = 9
61};
62
64enum class DelayInterpolationType : uint8_t
65{
67 None = 0,
68
70 Linear = 1,
71
73 Allpass = 2,
74
75 // Lagrange interpolation.
76 Lagrange = 3,
77 Count = 4,
78};
79
83enum class DelayLengthType : uint8_t
84{
86 Random = 0,
87
89 Gaussian = 1,
90
92 Primes = 2,
93
95 Uniform = 3,
96
99 PrimePower = 4,
100
102 SteamAudio = 5,
103
104 Count = 6,
105};
106
108enum class ParallelGainsMode : uint8_t
109{
111 Split,
112
114 Merge,
115
118};
119
120// STRUCTS
121
127{
129 uint32_t matrix_size;
130
133
136 std::optional<std::vector<float>> custom_matrix{std::nullopt};
137
139 uint32_t rng_seed{0};
140
142 // the diffusion parameter.
143 std::optional<float> arg{std::nullopt};
144};
145
163
166{
167 float frequency{0.f}; /*< Frequency of the modulation, normalized to [0, 1] by the sampling rate. */
168 float amplitude{0.f}; /*< Amplitude of the modulation. */
169 float initial_phase{0.f}; /*< Initial phase of the modulation, normalized to [0, 1]. */
170};
171
174{
176 std::vector<float>
178 std::vector<ModulationOptions>
181};
182
185{
186 float delay{256.f}; /*< Delay in samples. This can be a fractional value if interpolation is used. */
187 uint32_t max_delay{512}; /*< Maximum delay in samples. This is used to determine the size of the delay buffer and
188 must be greater than or equal to `delay`. */
190 sfFDN::DelayInterpolationType::None}; /*< Interpolation type for fractional delays. */
191 std::optional<sfFDN::ModulationOptions> lfo_config{
192 std::nullopt}; /*< Optional LFO configuration for time-varying delay modulation. If provided, the delay will be
193 modulated according to the specified parameters. */
194};
195
198{
199 std::vector<float>
200 delays; /*< Delay values for each channel in samples. These can be fractional values if interpolation is used.
201 The size of the vector determines the number of channels in the delay bank. */
202 uint32_t block_size{kDefaultBlockSize}; /*< Block size for processing audio. This is used to determine the size of
203 internal buffers and can affect performance. */
204 DelayInterpolationType interpolation_type{
205 DelayInterpolationType::None}; /*< Interpolation type for fractional delays. */
206};
207
210{
211 std::vector<float> delays; /*< Initial delay values for each channel in samples. These can be fractional values if
212 interpolation is used. The size of the vector determines the number of channels in the delay bank. */
213 uint32_t max_delay; /*< Maximum delay in samples. This is used to determine the size of the delay buffer and
214 must be greater than or equal to the initial delays. */
215 DelayInterpolationType interpolation_type; /*< Interpolation type for fractional delays. */
216 std::vector<ModulationOptions> time_varying_config; /*< Time-varying modulation configuration for each channel. The
217 size of the vector must match the size of `delays`. */
218};
219
222{
224 float b0;
225
227 float b1;
228
230 float b2;
231
233 float a0;
234
236 float a1;
237
239 float a2;
240
243 {
244 return {b0 / a0, b1 / a0, b2 / a0, 1.0f, a1 / a0, a2 / a0};
245 }
246};
247
250{
252 float coeff{0.f};
253};
254
257{
258 std::vector<std::pair<uint32_t, float>> coeffs; // pair of (index, coefficient)
259};
260
263{
264 std::vector<FilterCoefficients> coeffs;
265};
266
269{
270 std::vector<float> coeffs{1.f};
271};
272
276{
277 std::vector<float> delays; /*< Initial delay values for each Schroeder allpass in samples. */
278 std::vector<float> gains; /*< Feedback gain values for each Schroeder allpass. The size of this vector must match
279 the size of `delays`. */
280 bool parallel{false}; /*< If true, the allpass filters in the section are connected in parallel. If false, they are
281 connected in series. */
282};
283
287{
288 std::vector<SchroederAllpassSectionOptions> sections;
289};
290
294{
295 float t60 = 1.f; /*< Target T60 value for the filter. */
296 float delay; /*< Delay in samples for the delay line preceding the filter. If set to <= 0, it will be updated
297 automatically when accessed from `CreateFDNFromConfig()`*/
298 float sample_rate = kDefaultSampleRate; /*< Sample rate in Hz. This is used to calculate the filter coefficients
299 based on the specified T60 values. */
300};
301
309{
310 std::array<float, 2> t60s{1.f, 0.5f};
311 float delay; /*< Delay in samples for the delay line preceding the filter. If set to <= 0, it will be updated
312 automatically when accessed from `CreateFDNFromConfig()`*/
313 float sample_rate = kDefaultSampleRate; /*< Sample rate in Hz. This is used to calculate the filter coefficients
314 based on the specified T60 values. */
315};
316
321{
322 std::array<float, 3> t60s{1.f, 0.5f, 0.25f}; /*< Target T60 values for the low, mid and high bands. */
323 float delay; /*< Delay in samples for the delay line preceding the filter. If set to <= 0, it will be updated
324 automatically when accessed from `CreateFDNFromConfig()`*/
325 std::array<float, 2> freqs{800.f, 8000.f}; /*< Frequency values for the low and high shelves. */
326 float q = 1.f / std::numbers::sqrt2_v<float>; /*< Q-factor for the shelf filters. Q values higher than 0.707 may
327 cause instability if placed in a feedback loop. */
328 float sample_rate = kDefaultSampleRate; /*< Sample rate in Hz. This is used to calculate the filter coefficients
329 based on the specified T60 values. */
330};
331
340{
342 std::array<float, 10> t60s = {1.f, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f, 0.1f};
343
346 float delay;
347
349 float sample_rate = kDefaultSampleRate;
350
352 float shelf_cutoff = 8000.f;
353};
354
357 std::variant<HomogenousFilterOptions, TwoBandFilterOptions, ThreeBandFilterOptions, TenBandFilterOptions>;
358
361{
363 std::vector<attenuation_filter_variant_t> filter_configs;
364};
365
368{
369
371 std::array<float, 10> gains_db;
372
374 std::array<float, 10> freqs;
375
377 float sample_rate = kDefaultSampleRate;
378};
379
381using feedback_matrix_variant_t = std::variant<CascadedFeedbackMatrixOptions, ScalarFeedbackMatrixOptions>;
382
387
393
396NLOHMANN_JSON_SERIALIZE_ENUM(ScalarMatrixType, {{ScalarMatrixType::Identity, "Identity"},
397 {ScalarMatrixType::Random, "Random"},
398 {ScalarMatrixType::Householder, "Householder"},
399 {ScalarMatrixType::RandomHouseholder, "RandomHouseholder"},
400 {ScalarMatrixType::Hadamard, "Hadamard"},
401 {ScalarMatrixType::Circulant, "Circulant"},
402 {ScalarMatrixType::Allpass, "Allpass"},
403 {ScalarMatrixType::NestedAllpass, "NestedAllpass"},
404 {ScalarMatrixType::VariableDiffusion, "VariableDiffusion"},
405 {ScalarMatrixType::Count, "Count"}});
406
407NLOHMANN_JSON_SERIALIZE_ENUM(DelayInterpolationType, {{DelayInterpolationType::None, "None"},
410 {DelayInterpolationType::Lagrange, "Lagrange"}});
411
412NLOHMANN_JSON_SERIALIZE_ENUM(DelayLengthType, {{DelayLengthType::Random, "Random"},
413 {DelayLengthType::Gaussian, "Gaussian"},
414 {DelayLengthType::Primes, "Primes"},
415 {DelayLengthType::Uniform, "Uniform"},
416 {DelayLengthType::PrimePower, "PrimePower"},
417 {DelayLengthType::SteamAudio, "SteamAudio"}});
418
419NLOHMANN_JSON_SERIALIZE_ENUM(ParallelGainsMode, {{ParallelGainsMode::Split, "Split"},
420 {ParallelGainsMode::Merge, "Merge"},
421 {ParallelGainsMode::Parallel, "Parallel"}});
422
423void to_json(nlohmann::json& j, const ScalarFeedbackMatrixOptions& config);
424void from_json(const nlohmann::json& j, ScalarFeedbackMatrixOptions& config);
425NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CascadedFeedbackMatrixOptions, matrix_size, stage_count, sparsity, type,
426 gain_per_samples);
427NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ModulationOptions, frequency, amplitude, initial_phase);
428NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ParallelGainsOptions, mode, gains, time_varying_config);
429void to_json(nlohmann::json& j, const DelayOptions& config);
430void from_json(const nlohmann::json& j, DelayOptions& config);
431NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DelayBankOptions, delays, block_size, interpolation_type);
432NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DelayBankTimeVaryingOptions, delays, max_delay, interpolation_type,
433 time_varying_config);
434NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FilterCoefficients, b0, b1, b2, a0, a1, a2);
435NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AllpassFilterOptions, coeff);
436NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SparseFirOptions, coeffs);
437NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CascadedBiquadsOptions, coeffs);
438NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FirOptions, coeffs);
439NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SchroederAllpassSectionOptions, delays, gains, parallel);
440NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MultichannelSchroederAllpassSectionOptions, sections);
441NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(HomogenousFilterOptions, t60, delay, sample_rate);
442NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TwoBandFilterOptions, t60s, delay, sample_rate);
443NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ThreeBandFilterOptions, t60s, delay, freqs, q, sample_rate);
444NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TenBandFilterOptions, t60s, delay, sample_rate, shelf_cutoff);
445NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GraphicEQOptions, gains_db, freqs, sample_rate);
446
447void to_json(nlohmann::json& j, const AttenuationFilterBankOptions& config);
448void from_json(const nlohmann::json& j, AttenuationFilterBankOptions& config);
449
450} // namespace sfFDN
std::optional< float > arg
Optional argument for certain matrix types. For example, for the VariableDiffusion type,...
Definition types.h:143
float coeff
The coefficient for the allpass filter.
Definition types.h:252
ParallelGainsMode mode
Mode of parallel gain processing.
Definition types.h:175
float shelf_cutoff
Cutoff frequency for the shelf filters.
Definition types.h:352
std::vector< ModulationOptions > time_varying_config
Optional time-varying modulation configuration for each channel.
Definition types.h:179
ScalarMatrixType type
Type of the feedback matrix.
Definition types.h:132
uint32_t matrix_size
Size of the feedback matrix.
Definition types.h:129
std::array< float, 10 > freqs
Frequency values for the ten bands in Hz.
Definition types.h:374
ScalarMatrixType type
Type of the feedback matrix.
Definition types.h:159
std::vector< attenuation_filter_variant_t > filter_configs
Vector of attenuation filter configurations.
Definition types.h:363
std::array< float, 2 > t60s
Target T60 values for the low and high bands.
Definition types.h:310
float delay
Delay in samples for the delay line preceding the filter. If set to <= 0, it will be updated automati...
Definition types.h:346
uint32_t rng_seed
Optional. Seed for random number generation when type is Random or RandomHouseholder.
Definition types.h:139
float sample_rate
Sample rate in Hz. This is used to calculate the filter coefficients based on the specified T60 value...
Definition types.h:349
uint32_t stage_count
Number of stages.
Definition types.h:156
std::optional< std::vector< float > > custom_matrix
Optional custom matrix values in col-major order. The size of the vector must be equal to matrix_size...
Definition types.h:136
std::array< float, 10 > gains_db
Target gains for the ten bands in dB.
Definition types.h:371
std::vector< float > gains
Gain values for each channel.
Definition types.h:177
uint32_t matrix_size
Size of the feedback matrix.
Definition types.h:155
float sample_rate
Sample rate in Hz.
Definition types.h:377
float sparsity
Sparsity level (>= 1).
Definition types.h:157
std::array< float, 10 > t60s
Target T60 values for the ten bands.
Definition types.h:342
float gain_per_samples
Gain per sample.
Definition types.h:161
std::variant< ParallelGainsOptions, MultichannelSchroederAllpassSectionOptions, AttenuationFilterBankOptions, DelayBankOptions, DelayBankTimeVaryingOptions, CascadedFeedbackMatrixOptions, ScalarFeedbackMatrixOptions > multi_channel_processor_variant_t
Variant type for holding different multi-channel processor options.
Definition types.h:389
std::variant< CascadedFeedbackMatrixOptions, ScalarFeedbackMatrixOptions > feedback_matrix_variant_t
Variant type for holding different feedback matrix options.
Definition types.h:381
std::variant< SchroederAllpassSectionOptions, AllpassFilterOptions, CascadedBiquadsOptions, FirOptions, DelayOptions, GraphicEQOptions > single_channel_processor_variant_t
Variant type for holding different single-channel processor options.
Definition types.h:384
ParallelGainsMode
Enumeration for parallel gain processing modes.
Definition types.h:109
DelayInterpolationType
Types of interpolation for fractional delay lengths.
Definition types.h:65
ScalarMatrixType
Represents the type of a scalar matrix.
Definition types.h:50
DelayLengthType
Types of delay length distributions.
Definition types.h:84
std::variant< HomogenousFilterOptions, TwoBandFilterOptions, ThreeBandFilterOptions, TenBandFilterOptions > attenuation_filter_variant_t
Variant type for holding different attenuation filter options.
Definition types.h:356
@ Merge
Process each input channel separately and output to one channel.
@ Split
Process input as a single channel and output to multiple channels.
@ Parallel
Process each input channel separately and output to the same number of channels.
@ Linear
Linear interpolation.
@ None
No interpolation. The delay length will be rounded to the nearest integer value.
@ Allpass
Allpass interpolation.
@ NestedAllpass
Nested Allpass matrix.
@ Householder
Householder matrix.
@ Random
Random orthogonal matrix.
@ VariableDiffusion
Variable diffusion matrix as described in [3].
@ Circulant
Circulant matrix as described in [1].
@ RandomHouseholder
Random Householder matrix.
@ Hadamard
Hadamard matrix.
@ Identity
Identity matrix.
@ Allpass
Allpass matrix.
@ Random
Delay lengths are generated randomly within the specified range based on a uniform distribution.
@ SteamAudio
Delay lengths are generated using the algorithm from the SteamAudio library.
@ PrimePower
Delay lengths are generated as powers of prime numbers within the specified range....
@ Uniform
Delay lengths are uniformly distributed within the specified range.
@ Primes
Delay lengths are selected randomly from a list of prime numbers within the specified range.
@ Gaussian
Delay lengths are generated based on a Gaussian distribution within the specified range.
Options for configuring an allpass filter.
Definition types.h:250
Options for configuring an attenuation filter bank.
Definition types.h:361
Options for configuring cascaded biquad filters.
Definition types.h:263
Information structure for constructing a cascaded feedback matrix (also known as a filter feedback ma...
Definition types.h:154
Options for configuring a delay bank.
Definition types.h:198
Options for configuring a time-varying delay bank.
Definition types.h:210
Options for configuring delays.
Definition types.h:185
Options for configuring a FIR filter.
Definition types.h:269
Options for configuring a graphic equalizer.
Definition types.h:368
Options for configuring a homogenous filter.
Definition types.h:294
Options for configuring signal modulation.
Definition types.h:166
Options for configuring a multichannel bank of Schroeder allpass sections.
Definition types.h:287
Options for configuring parallel gain processing.
Definition types.h:174
Options for configuring a scalar feedback matrix.
Definition types.h:127
Options for configuring a Schroeder allpass section consisting of N Schroeder allpass in series or in...
Definition types.h:276
Options for configuring a sparse FIR filter.
Definition types.h:257
Options for configuring a ten-band filter.
Definition types.h:340
Options for configuring a three-band filter.
Definition types.h:321
Options for configuring a two-band filter.
Definition types.h:309
Coefficients for a digital IIR filter.
Definition types.h:222
float b1
Feedforward coefficients.
Definition types.h:227
float a1
Feedback coefficient.
Definition types.h:236
float a2
Feedback coefficient.
Definition types.h:239
float b2
Feedforward coefficients.
Definition types.h:230
float b0
Feedforward coefficients.
Definition types.h:224
FilterCoefficients Normalize() const
Returns the filter coefficients normalized so that a0 is equal to 1.
Definition types.h:242
float a0
Feedback coefficient.
Definition types.h:233
Definition types.h:19