Loading...
Searching...
No Matches
juce_Interpolators.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class Interpolators
{
private:
struct WindowedSincTraits
{
static constexpr float algorithmicLatency = 100.0f;
static forcedinline float windowedSinc (float firstFrac, int index) noexcept
{
auto index2 = index + 1;
auto frac = firstFrac;
auto value1 = lookupTable[index];
auto value2 = lookupTable[index2];
return value1 + (frac * (value2 - value1));
}
static forcedinline float valueAtOffset (const float* const inputs, const float offset, int indexBuffer) noexcept
{
const int numCrossings = 100;
const float floatCrossings = (float) numCrossings;
float result = 0.0f;
auto samplePosition = indexBuffer;
float firstFrac = 0.0f;
float lastSincPosition = -1.0f;
int index = 0, sign = -1;
for (int i = -numCrossings; i <= numCrossings; ++i)
{
auto sincPosition = (1.0f - offset) + (float) i;
if (i == -numCrossings || (sincPosition >= 0 && lastSincPosition < 0))
{
auto indexFloat = (sincPosition >= 0.f ? sincPosition : -sincPosition) * 100.0f;
auto indexFloored = std::floor (indexFloat);
index = (int) indexFloored;
firstFrac = indexFloat - indexFloored;
sign = (sincPosition < 0 ? -1 : 1);
}
if (exactlyEqual (sincPosition, 0.0f))
result += inputs[samplePosition];
else if (sincPosition < floatCrossings && sincPosition > -floatCrossings)
result += inputs[samplePosition] * windowedSinc (firstFrac, index);
if (++samplePosition == numCrossings * 2)
samplePosition = 0;
lastSincPosition = sincPosition;
index += 100 * sign;
}
return result;
}
static const float lookupTable[10001];
};
struct LagrangeTraits
{
static constexpr float algorithmicLatency = 2.0f;
static float valueAtOffset (const float*, float, int) noexcept;
};
struct CatmullRomTraits
{
//==============================================================================
static constexpr float algorithmicLatency = 2.0f;
static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
{
auto y0 = inputs[index]; if (++index == 4) index = 0;
auto y1 = inputs[index]; if (++index == 4) index = 0;
auto y2 = inputs[index]; if (++index == 4) index = 0;
auto y3 = inputs[index];
auto halfY0 = 0.5f * y0;
auto halfY3 = 0.5f * y3;
return y1 + offset * ((0.5f * y2 - halfY0)
+ (offset * (((y0 + 2.0f * y2) - (halfY3 + 2.5f * y1))
+ (offset * ((halfY3 + 1.5f * y1) - (halfY0 + 1.5f * y2))))));
}
};
struct LinearTraits
{
static constexpr float algorithmicLatency = 1.0f;
static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
{
auto y0 = inputs[index];
auto y1 = inputs[index == 0 ? 1 : 0];
return y1 * offset + y0 * (1.0f - offset);
}
};
struct ZeroOrderHoldTraits
{
static constexpr float algorithmicLatency = 0.0f;
static forcedinline float valueAtOffset (const float* const inputs, const float, int) noexcept
{
return inputs[0];
}
};
public:
using WindowedSinc = GenericInterpolator<WindowedSincTraits, 200>;
using Lagrange = GenericInterpolator<LagrangeTraits, 5>;
using CatmullRom = GenericInterpolator<CatmullRomTraits, 4>;
using Linear = GenericInterpolator<LinearTraits, 2>;
using ZeroOrderHold = GenericInterpolator<ZeroOrderHoldTraits, 1>;
};
//==============================================================================
using WindowedSincInterpolator = Interpolators::WindowedSinc;
using LagrangeInterpolator = Interpolators::Lagrange;
using CatmullRomInterpolator = Interpolators::CatmullRom;
using LinearInterpolator = Interpolators::Linear;
using ZeroOrderHoldInterpolator = Interpolators::ZeroOrderHold;
} // namespace juce
juce::GenericInterpolator
Definition juce_GenericInterpolator.h:41
juce::Interpolators
Definition juce_Interpolators.h:35