DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
ConvolutionRate.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
12#include "../AudioBuffer.h"
13#include "../Convolver.h"
14#include "../FIRFilter.h"
15#include <algorithm>
16#include <cassert>
17#include <cmath>
18#include <cstddef>
19#include <vector>
20
21namespace dspark::detail
22{
23class ConvolutionRate final
24{
25 Convolver<double> up_, referenceUp_, down_;
26 AudioBuffer<double> buffer_, reference_;
27 int factor_, latency_ = 0;
28
29 public:
30 struct Settings
31 {
32 int filterSpan = 16384;
33 int partitionFrames = 512;
34 double beta = 18.5;
35 double cutoff = 0.5;
36 };
37
38 [[nodiscard]] static bool valid(int factor, Settings settings) noexcept
39 {
40 return factor >= 1 && factor <= 16 && (factor & (factor - 1)) == 0 &&
41 settings.filterSpan >= 2 && settings.filterSpan <= 65536 &&
42 (settings.filterSpan & 1) == 0 && settings.partitionFrames >= 16 &&
43 settings.partitionFrames <= 4096 &&
44 (settings.partitionFrames & (settings.partitionFrames - 1)) == 0 &&
45 std::isfinite(settings.beta) && settings.beta >= 0 && settings.beta <= 30 &&
46 std::isfinite(settings.cutoff) && settings.cutoff > 0 && settings.cutoff <= 0.5;
47 }
48
49 // Cumulative setup bound, including temporary FIR designs, FFT plans, vector
50 // headers and allocation alignment/proxy overhead. Not a live-payload estimate.
51 [[nodiscard]] static std::size_t allocationBound(int factor, Settings settings,
52 bool withReference) noexcept
53 {
54 assert(valid(factor, settings));
55 const auto buffers = static_cast<std::size_t>(withReference ? 2 : 1);
56 const auto storage = buffers * 256 * static_cast<std::size_t>(factor) * sizeof(double);
57 if (factor == 1)
58 return storage;
59 const auto block = static_cast<std::size_t>(settings.partitionFrames * factor);
60 const auto length = static_cast<std::size_t>(settings.filterSpan * factor + 1);
61 return storage + 6 * (length * sizeof(double) + 64) +
62 (buffers + 1) * convolutionAllocationBound(block, length);
63 }
64
65 // Shared preflight for a fresh uniform Core Convolver: its partition
66 // spectra, delay line and work buffers ((4P + 9)B + 4P + 2 doubles), the
67 // FFT of size 2B as that FFT reports it, and an allowance for vector
68 // bookkeeping.
69 [[nodiscard]] static std::size_t convolutionAllocationBound(std::size_t block,
70 std::size_t length) noexcept
71 {
72 const auto partitions = (length + block - 1) / block;
73 return ((4 * partitions + 9) * block + 4 * partitions + 2) * sizeof(double) +
75 2 * partitions * sizeof(std::vector<double>) + sizeof(FFTReal<double>) + 2048 +
76 (2 * partitions + 12) * 64;
77 }
78
79 ConvolutionRate(int factor, Settings settings, bool withReference)
80 : factor_(factor)
81 {
82 assert(valid(factor, settings));
83 buffer_.resize(1, 256 * factor);
84 if (withReference)
85 reference_.resize(1, 256 * factor);
86 if (factor == 1)
87 return;
88 const int block = settings.partitionFrames * factor;
89 const int length = settings.filterSpan * factor + 1;
90 auto up = FIRDesign<double>::lowPass(1., settings.cutoff / factor, length, settings.beta);
91 auto down = FIRDesign<double>::lowPass(1., settings.cutoff / factor, length, settings.beta);
92 for (auto &value : up)
93 value *= factor;
94 up_.prepare(block, up.data(), length);
95 if (withReference)
96 referenceUp_.prepare(block, up.data(), length);
97 down_.prepare(block, down.data(), length);
98 latency_ = (length - 1 + 2 * block) / factor;
99 }
100
101 [[nodiscard]] int latency() const noexcept
102 {
103 return latency_;
104 }
105
106 void reset() noexcept
107 {
108 up_.reset();
109 referenceUp_.reset();
110 down_.reset();
111 }
112
113 double *upsampleReference(const double *input, int count) noexcept
114 {
115 assert(reference_.getNumChannels() == 1 && count >= 0 && count <= 256);
116 auto *out = reference_.getChannel(0);
117 std::fill_n(out, count * factor_, 0.);
118 for (int i = 0; i < count; ++i)
119 out[i * factor_] = input[i];
120 if (factor_ > 1)
121 referenceUp_.processInPlace(out, count * factor_);
122 return out;
123 }
124
126 {
127 const int count = input.getNumSamples();
128 assert(input.getNumChannels() == 1 && count <= 256);
129 auto *out = buffer_.getChannel(0);
130 std::fill_n(out, count * factor_, 0.);
131 for (int i = 0; i < count; ++i)
132 out[i * factor_] = input.getChannel(0)[i];
133 if (factor_ > 1)
134 up_.processInPlace(out, count * factor_);
135 return buffer_.toView().getSubView(0, count * factor_);
136 }
137
139 {
140 const int count = output.getNumSamples();
141 assert(output.getNumChannels() == 1 && count <= 256);
142 auto *data = buffer_.getChannel(0);
143 if (factor_ > 1)
144 down_.processInPlace(data, count * factor_);
145 for (int i = 0; i < count; ++i)
146 output.getChannel(0)[i] = data[i * factor_];
147 }
148};
149} // namespace dspark::detail
Non-owning view over audio channel data.
Definition AudioBuffer.h:53
Owning audio buffer with contiguous, 32-byte aligned storage.
AudioBufferView< T, MaxChannels > toView() noexcept
Returns a non-owning mutable view of this buffer. The view's channel capacity is propagated from MaxC...
int getNumChannels() const noexcept
Returns the number of active channels.
T * getChannel(int ch) noexcept
Returns a pointer to the sample data.
void resize(int numChannels, int numSamples)
Allocates the buffer for the given dimensions.
Real-time partitioned convolution using overlap-save with FFT.
Definition Convolver.h:78
FFT optimised for real-valued input signals (the common audio case).
Definition FFT.h:1197
static size_t allocationBytes(size_t size) noexcept
Upper bound of the heap bytes a constructor of this size requests, excluding the object itself,...
Definition FFT.h:1243
static std::vector< T > lowPass(double sampleRate, double cutoffHz, int numTaps, T beta=T(5))
Designs a low-pass FIR filter.
Definition FIRFilter.h:89
static std::size_t allocationBound(int factor, Settings settings, bool withReference) noexcept
AudioBufferView< double > upsample(AudioBufferView< double > input) noexcept
double * upsampleReference(const double *input, int count) noexcept
static bool valid(int factor, Settings settings) noexcept
static std::size_t convolutionAllocationBound(std::size_t block, std::size_t length) noexcept
ConvolutionRate(int factor, Settings settings, bool withReference)
void downsample(AudioBufferView< double > output) noexcept