43#include "AudioBuffer.h"
44#include "Oversampling.h"
45#include "Interpolation.h"
91 assert(tableSize >= 4);
98 tableSize = std::clamp(tableSize, 4, 1 << 20);
99 if (!(xMax > T(0))) xMax = T(8);
101 tableSize_ = tableSize;
102 xMax_ = std::max(xMax, T(0.001));
103 invRange_ = T(1) / (T(2) * xMax_);
107 table_.resize(
static_cast<size_t>(tableSize) + 3);
109 for (
int i = 0; i < tableSize; ++i)
111 T x = -xMax_ + T(2) * xMax_ *
static_cast<T
>(i) /
static_cast<T
>(tableSize - 1);
112 table_[
static_cast<size_t>(i + 1)] = func(x);
116 table_[0] = table_[1];
117 table_[
static_cast<size_t>(tableSize + 1)] = table_[
static_cast<size_t>(tableSize)];
118 table_[
static_cast<size_t>(tableSize + 2)] = table_[
static_cast<size_t>(tableSize)];
123 step_ = 2.0 *
static_cast<double>(xMax_) /
static_cast<double>(tableSize - 1);
124 integral_.assign(
static_cast<size_t>(tableSize), 0.0);
125 for (
int i = 0; i + 1 < tableSize; ++i)
126 integral_[
static_cast<size_t>(i + 1)] = integral_[
static_cast<size_t>(i)]
127 + step_ * segmentIntegral(i + 1, 1.0);
149 return std::clamp(x, -threshold, threshold);
160 if (x > T(1))
return T(2.0 / 3.0);
161 if (x < T(-1))
return T(-2.0 / 3.0);
162 return x - (x * x * x) / T(3);
173 return x >= T(0) ? std::tanh(x * T(1.2)) : std::tanh(x * T(0.8));
185 [[nodiscard]]
inline T
process(T input, T preGain = T(1), T postGain = T(1)) const noexcept
192 T driven = std::max(-xMax_, std::min(xMax_, input * preGain));
195 T pos = (driven + xMax_) * invRange_ *
static_cast<T
>(tableSize_ - 1);
197 int idx =
static_cast<int>(pos);
198 T frac = pos -
static_cast<T
>(idx);
201 const T* t = table_.data() + idx + 1;
218 void process(T* __restrict data,
int numSamples, T preGain = T(1), T postGain = T(1)) const noexcept
220 for (
int i = 0; i < numSamples; ++i)
221 data[i] =
process(data[i], preGain, postGain);
233 if (oversamplingAutomatic_)
235 else if (oversampler_)
236 oversampler_->prepare(spec);
246 assert(factor >= 1 && (factor & (factor - 1)) == 0);
247 oversamplingAutomatic_ =
false;
248 applyOversampling(factor);
266 if (on == oversamplingAutomatic_)
return;
267 oversamplingAutomatic_ = on;
276 void applyOversampling(
int factor)
280 oversampler_ = std::make_unique<Oversampling<T>>(factor);
284 oversamplingFactor_ = oversampler_->getFactor();
286 oversampler_->prepare(spec_);
290 oversamplingFactor_ = 1;
291 oversampler_.reset();
306 const bool adaa = antialias_;
308 auto shape = [&](T* data,
int n,
int ch) {
309 if (adaa && ch < kAntialiasChannels)
310 processAntialiased(data, n, preGain, postGain, ch);
312 process(data, n, preGain, postGain);
315 if (oversamplingFactor_ > 1 && oversampler_)
317 auto upView = oversampler_->upsample(buffer);
318 for (
int ch = 0; ch < upView.getNumChannels(); ++ch)
319 shape(upView.getChannel(ch), upView.getNumSamples(), ch);
320 oversampler_->downsample(buffer);
348 if (enabled && !antialias_) resetAntialiasing();
349 antialias_ = enabled;
357 if (oversampler_) oversampler_->reset();
362 [[nodiscard]]
bool isReady() const noexcept {
return tableSize_ > 0; }
374 return (oversampler_ && oversamplingFactor_ > 1) ? oversampler_->getLatency() : 0;
378 static constexpr int kAntialiasChannels = 16;
382 [[nodiscard]]
double segmentIntegral(
int i,
double u)
const noexcept
384 const double y0 = table_[
static_cast<size_t>(i - 1)];
385 const double y1 = table_[
static_cast<size_t>(i)];
386 const double y2 = table_[
static_cast<size_t>(i + 1)];
387 const double y3 = table_[
static_cast<size_t>(i + 2)];
389 const double c = (y2 - y0) * 0.5;
390 const double v = y1 - y2;
391 const double w = c + v;
392 const double a = w + v + (y3 - y1) * 0.5;
393 const double b = w + a;
394 return (((a * 0.25 * u - b / 3.0) * u + c * 0.5) * u + y1) * u;
399 [[nodiscard]]
double antiderivative(
double x)
const noexcept
401 const double xMax =
static_cast<double>(xMax_);
402 const int last = tableSize_ - 1;
404 return integral_[
static_cast<size_t>(last)]
405 +
static_cast<double>(table_[
static_cast<size_t>(last + 1)]) * (x - xMax);
407 return static_cast<double>(table_[1]) * (x + xMax);
408 const double pos = (x + xMax) / step_;
409 const int idx = std::min(
static_cast<int>(pos), last - 1);
410 return integral_[
static_cast<size_t>(idx)] + step_ * segmentIntegral(idx + 1, pos - idx);
413 void processAntialiased(T* data,
int numSamples, T preGain, T postGain,
int ch)
noexcept
417 const double limit = 1.0e3 *
static_cast<double>(xMax_);
418 const auto c =
static_cast<size_t>(ch);
419 double x0 = adaaX_[c];
420 double f0 = adaaF_[c];
421 const double post =
static_cast<double>(postGain);
422 for (
int i = 0; i < numSamples; ++i)
424 const double x = std::max(-limit, std::min(limit,
static_cast<double>(data[i] * preGain)));
425 const double f = antiderivative(x);
430 adaaPrimed_[c] =
true;
432 const double dx = x - x0;
434 if (std::abs(dx) > 1.0e-6)
437 y =
static_cast<double>(
process(
static_cast<T
>(0.5 * (x + x0))));
438 data[i] =
static_cast<T
>(y * post);
446 void resetAntialiasing() noexcept { adaaPrimed_.fill(
false); }
448 std::vector<T> table_;
449 std::vector<double> integral_;
451 bool antialias_ =
false;
452 std::array<double, kAntialiasChannels> adaaX_ {};
453 std::array<double, kAntialiasChannels> adaaF_ {};
454 std::array<bool, kAntialiasChannels> adaaPrimed_ {};
457 T invRange_ = T(1) / T(16);
460 std::unique_ptr<Oversampling<T>> oversampler_;
461 int oversamplingFactor_ = 1;
462 bool oversamplingAutomatic_ =
false;
463 static constexpr double kAutomaticInternalRate = 88200.0;
Non-owning view over audio channel data.
int getNumSamples() const noexcept
Returns the number of samples per channel.
int getNumChannels() const noexcept
Returns the number of channels in this view.
T * getChannel(int ch) const noexcept
Returns a pointer to the sample data for the given channel.
Power-of-two oversampling processor with polyphase anti-aliasing.
Zero-latency lookup-table waveshaper with RT-safe gain modulation.
void buildTanh(int tableSize=4096)
Builds a normalized tanh (soft clip) table.
bool isAntialiasingEnabled() const noexcept
True when processBlock() applies ADAA.
int getLatency() const noexcept
Reports the processing latency in samples.
void buildSoftClip(int tableSize=4096)
Builds a cubic soft-clip table.
int getTableSize() const noexcept
T process(T input, T preGain=T(1), T postGain=T(1)) const noexcept
Processes a single sample through the waveshaper with Hermite interpolation.
void buildAsymmetric(int tableSize=4096)
Builds an asymmetric clipping table for even-harmonic generation.
void prepare(const AudioSpec &spec)
Prepares the waveshaper for oversampled block processing.
void setAutomaticOversampling(bool on)
Lets the host rate choose the factor (allocates: not from the audio thread).
void buildHardClip(T threshold=T(0.8), int tableSize=4096)
Builds a hard-clip table.
bool isOversamplingAutomatic() const noexcept
Whether the factor follows the host rate (setAutomaticOversampling()).
WaveshapeTable()
Constructor. Initializes a safe passthrough table to prevent RT crashes.
bool isReady() const noexcept
void buildFromFunction(std::function< T(T)> func, int tableSize=4096, T xMax=T(8))
Builds the lookup table from an arbitrary transfer function.
void setAntialiasing(bool enabled) noexcept
Enables first-order antiderivative anti-aliasing (ADAA) in processBlock().
void setOversampling(int factor)
Enables oversampling.
void processBlock(AudioBufferView< T > buffer, T preGain=T(1), T postGain=T(1)) noexcept
Processes a buffer view with optional oversampling.
void process(T *__restrict data, int numSamples, T preGain=T(1), T postGain=T(1)) const noexcept
Processes a buffer in-place.
T getInputRange() const noexcept
Returns the half-range of the table's input domain.
int getOversamplingFactor() const noexcept
Main namespace for the DSPark framework.
T interpolateHermite(T y0, T y1, T y2, T y3, T frac) noexcept
4-point, 3rd-order Hermite interpolation (optimized x-form).
Describes the audio environment for a DSP processor.
double sampleRate
Sample rate in Hz.