37#include "../Core/AudioBuffer.h"
38#include "../Core/AudioSpec.h"
39#include "../Core/Biquad.h"
40#include "../Core/DspMath.h"
41#include "../Core/Smoothers.h"
42#include "../Core/StateBlob.h"
54template <
typename T =
float>
84 static_cast<float>(
pan_.load(std::memory_order_relaxed)));
93 delayL_.prepareMs(monoSpec,
static_cast<double>(maxMs) + 1.0);
94 delayR_.prepareMs(monoSpec,
static_cast<double>(maxMs) + 1.0);
116 algo =
static_cast<Algorithm>(std::clamp(
static_cast<int>(algo), 0,
118 algorithm_.store(algo, std::memory_order_relaxed);
128 if (!std::isfinite(position))
return;
132 pan_.store(std::clamp(position, T(-1), T(1)), std::memory_order_relaxed);
142 if (buffer.getNumChannels() < 2)
return;
150 const int gN = buffer.getNumSamples();
151 for (
int ch = 0; ch < buffer.getNumChannels(); ++ch)
153 T* d = buffer.getChannel(ch);
154 for (
int i = 0; i < gN; ++i)
155 if (!std::isfinite(d[i])) d[i] = T(0);
165 float pTarget =
static_cast<float>(
pan_.load(std::memory_order_relaxed));
168 switch (
algorithm_.load(std::memory_order_relaxed))
206 if (!std::isfinite(ms))
return;
211 if (!std::isfinite(ms))
return;
212 haasMaxDelay_.store(std::max(0.0f, ms), std::memory_order_relaxed);
216 if (!std::isfinite(hz))
return;
217 spectralFreq_.store(std::clamp(hz, 20.0f, 20000.0f), std::memory_order_relaxed);
221 if (!std::isfinite(dB))
return;
230 if (!std::isfinite(ms))
return;
231 smoothingTime_.store(std::max(0.0f, ms), std::memory_order_relaxed);
239 [[nodiscard]] std::vector<uint8_t>
getState()
const
243 w.
write(
"pan",
static_cast<float>(
pan_.load(std::memory_order_relaxed)));
244 w.
write(
"algorithm",
static_cast<int32_t
>(
algorithm_.load(std::memory_order_relaxed)));
271 T* L = buffer.getChannel(0);
272 T* R = buffer.getChannel(1);
273 const int n = buffer.getNumSamples();
274 constexpr T
halfPi = pi<T> / T(2);
280 const T gL = std::cos(angle);
281 const T gR = std::sin(angle);
282 for (
int i = 0; i < n; ++i)
290 for (
int i = 0; i < n; ++i)
293 T angle = (p * T(0.5) + T(0.5)) *
halfPi;
303 T* L = buffer.getChannel(0);
304 T* R = buffer.getChannel(1);
305 const int n = buffer.getNumSamples();
308 T targetP =
static_cast<T
>(panTarget);
316 delayL_.setDelayMs(baseMs + itdMax * std::max(T(0), targetP));
317 delayR_.setDelayMs(baseMs + itdMax * std::max(T(0), -targetP));
319 for (
int i = 0; i < n; ++i)
333 T absp = std::abs(p);
334 T farAtten = T(1) - absp * T(0.5);
335 T leakage = absp * T(0.3);
341 l_temp = L[i] * farAtten + R[i] * leakage;
342 r_temp = R[i] + L[i] * leakage * T(0.5);
347 l_temp = L[i] + R[i] * leakage * T(0.5);
348 r_temp = R[i] * farAtten + L[i] * leakage;
353 L[i] =
delayL_.processSample(0, l_temp);
354 R[i] =
delayR_.processSample(0, r_temp);
360 T* L = buffer.getChannel(0);
361 T* R = buffer.getChannel(1);
362 const int n = buffer.getNumSamples();
363 constexpr T
halfPi = pi<T> / T(2);
365 for (
int i = 0; i < n; ++i)
368 T mid = (L[i] + R[i]) * T(0.5);
369 T side = (L[i] - R[i]) * T(0.5);
376 T angle = (p * T(0.5) + T(0.5)) *
halfPi;
377 T gL =
fastCos(angle) * sqrt2<T>;
378 T gR =
fastSin(angle) * sqrt2<T>;
380 L[i] = mid * gL + side;
381 R[i] = mid * gR - side;
387 T* L = buffer.getChannel(0);
388 T* R = buffer.getChannel(1);
389 const int n = buffer.getNumSamples();
390 constexpr T
halfPi = pi<T> / T(2);
392 for (
int i = 0; i < n; ++i)
395 T mid = (L[i] + R[i]) * T(0.5);
396 T side = (L[i] - R[i]) * T(0.5);
398 T angle = (p * T(0.5) + T(0.5)) *
halfPi;
402 T sideGainL = std::clamp(
fastCos(angle) * sqrt2<T>, T(0), T(1));
403 T sideGainR = std::clamp(
fastSin(angle) * sqrt2<T>, T(0), T(1));
405 L[i] = mid + (side * sideGainL);
406 R[i] = mid - (side * sideGainR);
412 T haasMax = T(
haasMaxDelay_.load(std::memory_order_relaxed));
413 T pT =
static_cast<T
>(panTarget);
419 delayL_.setDelayMs(baseMs + haasMax * std::max(T(0), pT));
420 delayR_.setDelayMs(baseMs + haasMax * std::max(T(0), -pT));
422 T* L = buffer.getChannel(0);
423 T* R = buffer.getChannel(1);
424 const int n = buffer.getNumSamples();
426 for (
int i = 0; i < n; ++i)
434 L[i] =
delayL_.processSample(0, L[i]);
435 R[i] =
delayR_.processSample(0, R[i]);
444 T* L = buffer.getChannel(0);
445 T* R = buffer.getChannel(1);
446 const int n = buffer.getNumSamples();
447 constexpr int kSubBlock = 4;
449 for (
int start = 0; start < n; start += kSubBlock)
451 const int end = std::min(n, start + kSubBlock);
453 for (
int i = start; i < end; ++i)
457 for (
int i = start; i < end; ++i)
469 const float pan =
static_cast<float>(targetPan);
476 T gainLdB = -targetPan *
static_cast<T
>(sMaxGain);
477 T gainRdB = targetPan *
static_cast<T
>(sMaxGain);
482 sampleRate_,
static_cast<double>(sFreq),
static_cast<double>(gainLdB)));
484 sampleRate_,
static_cast<double>(sFreq),
static_cast<double>(gainRdB)));
Non-owning view over audio channel data.
Biquad filter using Transposed Direct Form II (TDF-II) with thread-safe updates.
void setCoeffsNow(const BiquadCoeffs &c) noexcept
Stream-owner direct set: makes c the active set immediately.
void reset() noexcept
Resets all per-channel filter states to zero to avoid ringing/clicks.
T processSample(T input, int channel) noexcept
Processes a single sample for a specific channel.
void setHaasMaxDelay(float ms) noexcept
void setAlgorithm(Algorithm algo) noexcept
Sets the active panning algorithm safely from any thread.
std::atomic< float > binauralMaxITD_
void prepare(const AudioSpec &spec)
Initializes internal delays, filters, and smoothers.
Biquad< T, 1 > spectralR_
std::atomic< Algorithm > algorithm_
void setSmoothingTime(float ms) noexcept
Sets the pan smoothing time: a change settles (to 96%) in this many ms along a critically damped curv...
std::atomic< float > spectralMaxGain_
int getLatency() const noexcept
Latency in samples: the 4-sample common base delay of the Binaural and Haas paths (centred image),...
std::vector< uint8_t > getState() const
Serializes the parameter state (setup/UI threads; allocates).
Biquad< T, 1 > spectralL_
void setPan(T position) noexcept
Sets the target pan position (automatable, smoothed).
void setBinauralMaxITD(float ms) noexcept
void applyEqualPower(AudioBufferView< T > buffer, float) noexcept
std::atomic< float > haasMaxDelay_
void setSpectralMaxGain(float dB) noexcept
static float panTimeConstantMs(float smoothingMs) noexcept
Smoothers::CriticallyDampedSmoother panSmoother_
void applySpectral(AudioBufferView< T > buffer, float) noexcept
void reset() noexcept
Clears delay lines and filter states to prevent ghost echoes.
void applyMidPan(AudioBufferView< T > buffer, float) noexcept
std::atomic< bool > smoothingDirty_
Pan smoother re-config pending.
std::atomic< float > smoothingTime_
void processBlock(AudioBufferView< T > buffer) noexcept
Processes an audio block in-place. Real-time safe.
void applyHaas(AudioBufferView< T > buffer, float panTarget) noexcept
void setSpectralFrequency(float hz) noexcept
void applyCombinedBinaural(AudioBufferView< T > buffer, float panTarget) noexcept
void updateSpectralFilters(T targetPan) noexcept
static constexpr int kBaseDelaySamples
Binaural/Haas common delay.
std::atomic< float > spectralFreq_
Algorithm
Available panning algorithms.
@ Binaural
Cross-feeding + ITD delay.
@ EqualPower
Standard -3 dB constant-power pan.
@ MidPan
Pans only the centre (mid) image.
@ SidePan
Pans only the stereo (side) image.
@ Haas
Precedence effect via inter-channel delay.
@ Spectral
Frequency-dependent panning via high-shelf.
bool setState(const uint8_t *data, size_t size)
Restores parameters from a blob (tolerant; rejects foreign ids).
void applySidePan(AudioBufferView< T > buffer, float) noexcept
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
bool isValid() const noexcept
uint32_t processorId() const noexcept
Serializes key/value parameters into a versioned blob.
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
void write(const char *key, float value)
Writes a float parameter.
Main namespace for the DSPark framework.
T fastSin(T x) noexcept
Fast sine approximation (degree-9 odd minimax polynomial).
T fastCos(T x) noexcept
Fast cosine approximation. See fastSin() for accuracy notes (the pi/2 offset costs float about one ul...
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
constexpr T halfPi
Pi / 2 (1.57079...). Quarter period; sin/cos phase offset.
Describes the audio environment for a DSP processor.
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
int maxBlockSize
Maximum number of samples per processing block.
double sampleRate
Sample rate in Hz.
static BiquadCoeffs makeHighShelf(double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
High-shelf filter.
Critically damped smoother (no overshoot, exact Q=0.5).
void reset(double sampleRate, float timeConstantMilliseconds, float initialValue=0.0f) noexcept
float getCurrentValue() const noexcept
void setTargetValue(float newTarget) noexcept
float getNextValue() noexcept
bool isSmoothing() const noexcept