49#include "AudioBuffer.h"
62#ifndef DSPARK_RESTRICT
63 #if defined(__clang__) || defined(__GNUC__)
64 #define DSPARK_RESTRICT __restrict__
65 #elif defined(_MSC_VER)
66 #define DSPARK_RESTRICT __restrict
68 #define DSPARK_RESTRICT
81template <
typename T,
int MaxChannels = 16>
103 : dryBuffer_(std::move(other.dryBuffer_)),
104 capturedSamples_(other.capturedSamples_),
105 delayHist_(std::move(other.delayHist_)),
106 latencySamples_(other.latencySamples_),
107 histPos_(other.histPos_),
108 mixRule_(other.mixRule_.load(std::memory_order_relaxed)),
109 currentMix_(other.currentMix_),
110 maxMixStep_(other.maxMixStep_)
115 if (
this == &other)
return *
this;
116 dryBuffer_ = std::move(other.dryBuffer_);
117 capturedSamples_ = other.capturedSamples_;
118 delayHist_ = std::move(other.delayHist_);
119 latencySamples_ = other.latencySamples_;
120 histPos_ = other.histPos_;
121 mixRule_.store(other.mixRule_.load(std::memory_order_relaxed),
122 std::memory_order_relaxed);
123 currentMix_ = other.currentMix_;
124 maxMixStep_ = other.maxMixStep_;
143 ?
static_cast<T
>(1.0 / std::max(1.0, spec.
sampleRate * kMinRampSeconds))
149 if (latencySamples_ > 0)
164 capturedSamples_ = 0;
178 const int r =
static_cast<int>(rule);
179 mixRule_.store(r >= 0 && r <= 2 ? rule :
MixRule::Linear, std::memory_order_relaxed);
183 [[nodiscard]]
MixRule getMixRule() const noexcept {
return mixRule_.load(std::memory_order_relaxed); }
199 samples = std::max(0, samples);
200 if (samples == latencySamples_)
return;
201 latencySamples_ = samples;
223 const int chCount = std::min(input.getNumChannels(), dryBuffer_.
getNumChannels());
224 const int nSamples = std::min(input.getNumSamples(), dryBuffer_.
getNumSamples());
226 if (latencySamples_ <= 0)
228 for (
int ch = 0; ch < chCount; ++ch)
230 const T* DSPARK_RESTRICT src = input.getChannel(ch);
231 T* DSPARK_RESTRICT dst = dryBuffer_.
getChannel(ch);
232 std::copy_n(src, nSamples, dst);
240 const int D = latencySamples_;
241 for (
int ch = 0; ch < chCount; ++ch)
243 const T* DSPARK_RESTRICT src = input.getChannel(ch);
244 T* DSPARK_RESTRICT dst = dryBuffer_.
getChannel(ch);
245 T* DSPARK_RESTRICT hist = delayHist_.
getChannel(ch);
247 for (
int i = 0; i < nSamples; ++i)
251 if (++pos == D) pos = 0;
254 histPos_ = (histPos_ + nSamples) % D;
257 capturedSamples_ = nSamples;
281 if (std::isnan(targetMix)) targetMix = T(0);
282 targetMix = std::clamp(targetMix, T(0), T(1));
284 const int chCount = std::min(wetBuffer.getNumChannels(), dryBuffer_.
getNumChannels());
285 const int nSamples = std::min(wetBuffer.getNumSamples(), capturedSamples_);
287 if (nSamples == 0 || chCount == 0)
return;
290 if (currentMix_ < T(0)) currentMix_ = targetMix;
292 const bool needsSmoothing = std::abs(currentMix_ - targetMix) > T(1e-5);
294 bool rateLimited =
false;
297 mixStep = (targetMix - currentMix_) / T(nSamples);
298 if (std::abs(mixStep) > maxMixStep_)
300 mixStep = mixStep > T(0) ? maxMixStep_ : -maxMixStep_;
304 const MixRule rule = mixRule_.load(std::memory_order_relaxed);
313 const T mix0 = needsSmoothing ? currentMix_ : targetMix;
315 for (
int ch = 0; ch < chCount; ++ch)
317 T* DSPARK_RESTRICT wetData = wetBuffer.getChannel(ch);
318 const T* DSPARK_RESTRICT dryData = dryBuffer_.
getChannel(ch);
326 const T w = std::sqrt(mix0);
327 const T d = std::sqrt(T(1) - mix0);
328 for (
int i = 0; i < nSamples; ++i)
329 wetData[i] = dryData[i] * d + wetData[i] * w;
333 for (
int i = 0; i < nSamples; ++i)
339 const T m = mix0 + mixStep * T(i);
340 const T w = std::sqrt(std::max(T(0), m));
341 const T d = std::sqrt(std::max(T(0), T(1) - m));
342 wetData[i] = dryData[i] * d + wetData[i] * w;
349 for (
int i = 0; i < nSamples; ++i)
351 const T m = needsSmoothing ? mix0 + mixStep * T(i) : mix0;
352 const T w = std::min(T(1), T(2) * m);
353 const T d = std::min(T(1), T(2) * (T(1) - m));
354 wetData[i] = dryData[i] * d + wetData[i] * w;
362 const T d = T(1) - w;
363 for (
int i = 0; i < nSamples; ++i)
364 wetData[i] = dryData[i] * d + wetData[i] * w;
368 for (
int i = 0; i < nSamples; ++i)
370 const T w = mix0 + mixStep * T(i);
371 const T d = T(1) - w;
372 wetData[i] = dryData[i] * d + wetData[i] * w;
380 currentMix_ = rateLimited
381 ? std::clamp(mix0 + mixStep * T(nSamples), T(0), T(1))
404 int capturedSamples_ = 0;
408 int latencySamples_ = 0;
412 T currentMix_ = T(-1);
413 T maxMixStep_ = T(1);
415 static constexpr double kMinRampSeconds = 0.02;
451 sampleRate_ = sampleRate;
452 step_ = 1.0 / std::max(1.0, sampleRate * 0.02);
453 state_.assign(
static_cast<std::size_t
>(std::max(channels, 0)), {});
458 void reset() noexcept { std::fill(state_.begin(), state_.end(), std::array<double, 2> {}); }
466 if (hz == corner_)
return;
468 const double t = std::tan(std::numbers::pi * std::clamp(hz / sampleRate_, 0.0, 0.45));
469 pole_ = (1.0 - t) / (1.0 + t);
476 void aim(
bool on)
noexcept { target_ = on ? 1.0 : 0.0; }
479 void land() noexcept { amount_ = target_; }
484 if (amount_ != target_)
485 amount_ = (amount_ < target_) ? std::min(target_, amount_ + step_) : std::max(target_, amount_ - step_);
495 template <
typename T>
498 if (channel < 0 ||
static_cast<std::size_t
>(channel) >= state_.size())
return x;
499 auto& s = state_[
static_cast<std::size_t
>(channel)];
500 const double in =
static_cast<double>(x);
501 double y = pole_ * (in + s[1]) - s[0];
502 if (!std::isfinite(y))
507 if (std::abs(y) < 1e-30) y = 0.0;
510 if (amount == 0.0)
return x;
511 return static_cast<T
>(amount == 1.0 ? y : in + amount * (y - in));
518 template <
typename T>
521 const int channels = std::min(buffer.getNumChannels(),
static_cast<int>(state_.size()));
522 const int samples = buffer.getNumSamples();
523 for (
int i = 0; i < samples; ++i)
525 const double amount =
advance();
526 for (
int ch = 0; ch < channels; ++ch)
528 T* data = buffer.getChannel(ch);
535 std::vector<std::array<double, 2>> state_;
536 double sampleRate_ = 48000.0, step_ = 1.0 / 960.0;
537 double corner_ = -1.0, pole_ = 0.0;
538 double amount_ = 0.0, target_ = 0.0;
Non-owning view over audio channel data.
Owning audio buffer with contiguous, 32-byte aligned storage.
int getNumChannels() const noexcept
Returns the number of active channels.
T * getChannel(int ch) noexcept
Returns a pointer to the sample data.
int getNumSamples() const noexcept
Returns the number of samples per channel.
void resize(int numChannels, int numSamples)
Allocates the buffer for the given dimensions.
void clear() noexcept
Clears all channels, including SIMD alignment padding. Ensures any SIMD over-read will consume pure z...
Pre-allocated, SIMD-friendly dry/wet blender for real-time audio.
void setLatencyCompensation(int samples)
Delays the captured dry signal to compensate for an effect's internal latency (e.g....
int getLatencyCompensation() const noexcept
Returns the configured dry-path latency compensation in samples.
MixRule
Defines the mathematical curve used for mixing.
int getDryCapturedSamples() const noexcept
Returns the number of samples valid from the last pushDry() call.
void mixWet(AudioBufferView< T > wetBuffer, T targetMix) noexcept
Blends the stored dry signal with the current (wet) buffer in-place.
void reset() noexcept
Resets the internal buffer and smoothing states to zero.
int getDryNumChannels() const noexcept
Returns the internal capacity of channels in the dry buffer.
DryWetMixer() noexcept=default
MixRule getMixRule() const noexcept
DryWetMixer & operator=(DryWetMixer &&other) noexcept
void pushDry(const AudioBufferView< const T > &input) noexcept
Captures a snapshot of the dry (unprocessed) signal.
void setMixRule(MixRule rule) noexcept
Sets the mathematical curve to use during the mix phase.
void prepare(const AudioSpec &spec)
Allocates the internal dry buffer for the given audio spec.
DryWetMixer & operator=(const DryWetMixer &)=delete
const T * getDryChannel(int ch) const noexcept
Retrieves a read-only pointer to the captured dry channel data.
DryWetMixer(const DryWetMixer &)=delete
Gives the dry side of a mix the phase that a high-pass leaves in the wet side, and none of its magnit...
void reset() noexcept
Clears the memory (the amount stays).
T processSample(int channel, T x, double amount) noexcept
A sample of a channel, with the amount that advance() returned.
void process(AudioBufferView< T > buffer) noexcept
A stretch of a buffer, in place.
void prepare(double sampleRate, int channels)
Sizes the state (allocates).
void aim(bool on) noexcept
Where the amount goes.
double advance() noexcept
Moves the amount by a frame and returns it.
void land() noexcept
Puts the amount where it goes, without the fade.
void setCorner(double hz) noexcept
Moves the corner (a tangent, when it changes).
static constexpr double kButterworthShare
Main namespace for the DSPark framework.
Describes the audio environment for a DSP processor.
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
int maxBlockSize
Maximum number of samples per processing block.
double sampleRate
Sample rate in Hz.